Skip to content

Commit

Permalink
Migrate RockTour to @ConstraintWeight
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Oct 23, 2018
1 parent 1151d76 commit a5b62ce
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 401 deletions.
Binary file modified optaplanner-examples/data/rocktour/unsolved/47shows.xlsx
Binary file not shown.
Expand Up @@ -44,14 +44,21 @@
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.optaplanner.core.api.score.Score;
import org.optaplanner.core.api.score.constraint.ConstraintMatch;
import org.optaplanner.core.api.score.constraint.ConstraintMatchTotal; import org.optaplanner.core.api.score.constraint.ConstraintMatchTotal;
import org.optaplanner.core.api.score.constraint.Indictment; import org.optaplanner.core.api.score.constraint.Indictment;
import org.optaplanner.core.api.solver.SolverFactory; import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.impl.score.definition.ScoreDefinition;
import org.optaplanner.core.impl.score.director.InnerScoreDirectorFactory;
import org.optaplanner.core.impl.score.director.ScoreDirector; import org.optaplanner.core.impl.score.director.ScoreDirector;
import org.optaplanner.core.impl.score.director.ScoreDirectorFactory; import org.optaplanner.core.impl.score.director.ScoreDirectorFactory;
import org.optaplanner.examples.conferencescheduling.domain.Talk;
import org.optaplanner.persistence.common.api.domain.solution.SolutionFileIO; import org.optaplanner.persistence.common.api.domain.solution.SolutionFileIO;
import org.optaplanner.swing.impl.TangoColorFactory; import org.optaplanner.swing.impl.TangoColorFactory;


import static java.util.stream.Collectors.*;

public abstract class AbstractXlsxSolutionFileIO<Solution_> implements SolutionFileIO<Solution_> { public abstract class AbstractXlsxSolutionFileIO<Solution_> implements SolutionFileIO<Solution_> {


protected static final Pattern VALID_TAG_PATTERN = Pattern.compile("(?U)^[\\w&\\-\\.\\/\\(\\)\\'][\\w&\\-\\.\\/\\(\\)\\' ]*[\\w&\\-\\.\\/\\(\\)\\']?$"); protected static final Pattern VALID_TAG_PATTERN = Pattern.compile("(?U)^[\\w&\\-\\.\\/\\(\\)\\'][\\w&\\-\\.\\/\\(\\)\\' ]*[\\w&\\-\\.\\/\\(\\)\\']?$");
Expand Down Expand Up @@ -83,6 +90,7 @@ public String getInputFileExtension() {
public static abstract class AbstractXlsxReader<Solution_> { public static abstract class AbstractXlsxReader<Solution_> {


protected final XSSFWorkbook workbook; protected final XSSFWorkbook workbook;
protected final ScoreDefinition scoreDefinition;


protected Solution_ solution; protected Solution_ solution;


Expand All @@ -92,13 +100,17 @@ public static abstract class AbstractXlsxReader<Solution_> {
protected int currentRowNumber; protected int currentRowNumber;
protected int currentColumnNumber; protected int currentColumnNumber;


public AbstractXlsxReader(XSSFWorkbook workbook) { public AbstractXlsxReader(XSSFWorkbook workbook, String solverConfigResource) {
this.workbook = workbook; this.workbook = workbook;
ScoreDirectorFactory<Solution_> scoreDirectorFactory
= SolverFactory.<Solution_>createFromXmlResource(solverConfigResource)
.buildSolver().getScoreDirectorFactory();
scoreDefinition = ((InnerScoreDirectorFactory) scoreDirectorFactory).getScoreDefinition();
} }


public abstract Solution_ read(); public abstract Solution_ read();


protected void readIntConstraintLine(String name, Consumer<Integer> consumer, String constraintDescription) { protected void readIntConstraintParameterLine(String name, Consumer<Integer> consumer, String constraintDescription) {
nextRow(); nextRow();
readHeaderCell(name); readHeaderCell(name);
XSSFCell weightCell = nextCell(); XSSFCell weightCell = nextCell();
Expand All @@ -125,7 +137,7 @@ protected void readIntConstraintLine(String name, Consumer<Integer> consumer, St
readHeaderCell(constraintDescription); readHeaderCell(constraintDescription);
} }


protected void readLongConstraintLine(String name, Consumer<Long> consumer, String constraintDescription) { protected void readLongConstraintParameterLine(String name, Consumer<Long> consumer, String constraintDescription) {
nextRow(); nextRow();
readHeaderCell(name); readHeaderCell(name);
XSSFCell weightCell = nextCell(); XSSFCell weightCell = nextCell();
Expand Down Expand Up @@ -316,6 +328,8 @@ protected XSSFColor extractColor(XSSFCell cell, XSSFColor... acceptableColors) {
public static abstract class AbstractXlsxWriter<Solution_> { public static abstract class AbstractXlsxWriter<Solution_> {


protected final Solution_ solution; protected final Solution_ solution;
protected final Score score;
protected final ScoreDefinition scoreDefinition;
protected final List<ConstraintMatchTotal> constraintMatchTotalList; protected final List<ConstraintMatchTotal> constraintMatchTotalList;
protected final Map<Object, Indictment> indictmentMap; protected final Map<Object, Indictment> indictmentMap;


Expand Down Expand Up @@ -344,9 +358,10 @@ public AbstractXlsxWriter(Solution_ solution, String solverConfigResource) {
ScoreDirectorFactory<Solution_> scoreDirectorFactory ScoreDirectorFactory<Solution_> scoreDirectorFactory
= SolverFactory.<Solution_>createFromXmlResource(solverConfigResource) = SolverFactory.<Solution_>createFromXmlResource(solverConfigResource)
.buildSolver().getScoreDirectorFactory(); .buildSolver().getScoreDirectorFactory();
scoreDefinition = ((InnerScoreDirectorFactory) scoreDirectorFactory).getScoreDefinition();
try (ScoreDirector<Solution_> scoreDirector = scoreDirectorFactory.buildScoreDirector()) { try (ScoreDirector<Solution_> scoreDirector = scoreDirectorFactory.buildScoreDirector()) {
scoreDirector.setWorkingSolution(solution); scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore(); score = scoreDirector.calculateScore();
constraintMatchTotalList = new ArrayList<>(scoreDirector.getConstraintMatchTotals()); constraintMatchTotalList = new ArrayList<>(scoreDirector.getConstraintMatchTotals());
constraintMatchTotalList.sort(Comparator.comparing(ConstraintMatchTotal::getScore)); constraintMatchTotalList.sort(Comparator.comparing(ConstraintMatchTotal::getScore));
indictmentMap = scoreDirector.getIndictmentMap(); indictmentMap = scoreDirector.getIndictmentMap();
Expand Down Expand Up @@ -387,15 +402,15 @@ protected XSSFCellStyle createStyle(XSSFColor color) {
return style; return style;
} }


protected void writeIntConstraintLine(String name, int value, String constraintDescription) { protected void writeIntConstraintParameterLine(String name, int value, String constraintDescription) {
nextRow(); nextRow();
nextHeaderCell(name); nextHeaderCell(name);
XSSFCell weightCell = nextCell(); XSSFCell weightCell = nextCell();
weightCell.setCellValue(value); weightCell.setCellValue(value);
nextHeaderCell(constraintDescription); nextHeaderCell(constraintDescription);
} }


protected void writeIntConstraintLine(String name, Supplier<Integer> supplier, String constraintDescription) { protected void writeIntConstraintParameterLine(String name, Supplier<Integer> supplier, String constraintDescription) {
nextRow(); nextRow();
nextHeaderCell(name); nextHeaderCell(name);
XSSFCell weightCell = nextCell(); XSSFCell weightCell = nextCell();
Expand All @@ -407,7 +422,7 @@ protected void writeIntConstraintLine(String name, Supplier<Integer> supplier, S
nextHeaderCell(constraintDescription); nextHeaderCell(constraintDescription);
} }


protected void writeLongConstraintLine(String name, Supplier<Long> supplier, String constraintDescription) { protected void writeLongConstraintParameterLine(String name, Supplier<Long> supplier, String constraintDescription) {
nextRow(); nextRow();
nextHeaderCell(name); nextHeaderCell(name);
XSSFCell weightCell = nextCell(); XSSFCell weightCell = nextCell();
Expand All @@ -419,6 +434,57 @@ protected void writeLongConstraintLine(String name, Supplier<Long> supplier, Str
nextHeaderCell(constraintDescription); nextHeaderCell(constraintDescription);
} }


protected void writeScoreConstraintHeaders() {
nextRow();
nextHeaderCell("Constraint");
nextHeaderCell("Score weight");
nextHeaderCell("Description");
}

protected <Score_ extends Score<Score_>> void writeScoreConstraintLine(
String constraintName, Score_ constraintScore, String constraintDescription) {
nextRow();
nextHeaderCell(constraintName);
nextCell().setCellValue(constraintScore.toString());
nextHeaderCell(constraintDescription);
}

protected void writeScoreView() {
nextSheet("Score view", 1, 3, true);
nextRow();
nextHeaderCell("Score");
nextCell().setCellValue(score == null ? "Not yet solved" : score.toShortString());
nextRow();
nextRow();
nextHeaderCell("Constraint match");
nextHeaderCell("Match score");
nextHeaderCell("Total score");
if (!score.isSolutionInitialized()) {
nextRow();
nextHeaderCell("Unassigned variables");
nextCell();
nextCell().setCellValue(score.getInitScore());
}
for (ConstraintMatchTotal constraintMatchTotal : constraintMatchTotalList) {
nextRow();
nextHeaderCell(constraintMatchTotal.getConstraintName());
nextCell();
nextCell().setCellValue(constraintMatchTotal.getScore().toShortString());
List<ConstraintMatch> constraintMatchList = new ArrayList<>(constraintMatchTotal.getConstraintMatchSet());
constraintMatchList.sort(Comparator.comparing(ConstraintMatch::getScore));
for (ConstraintMatch constraintMatch : constraintMatchList) {
nextRow();
nextCell().setCellValue(" " + constraintMatch.getJustificationList().stream()
.filter(o -> o instanceof Talk).map(o -> ((Talk) o).getCode())
.collect(joining(", ")));
nextCell().setCellValue(constraintMatch.getScore().toShortString());
nextCell();
nextCell();
}
}
autoSizeColumnsWithHeader();
}

protected void nextSheet(String sheetName, int colSplit, int rowSplit, boolean view) { protected void nextSheet(String sheetName, int colSplit, int rowSplit, boolean view) {
currentSheet = workbook.createSheet(sheetName); currentSheet = workbook.createSheet(sheetName);
currentDrawing = currentSheet.createDrawingPatriarch(); currentDrawing = currentSheet.createDrawingPatriarch();
Expand Down

0 comments on commit a5b62ce

Please sign in to comment.