Skip to content

Commit

Permalink
fix ConferenceSchedulingScoreRulesXlsxTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Nov 11, 2018
1 parent 289eac5 commit bdcefb1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 48 deletions.
Expand Up @@ -151,6 +151,13 @@ public Class<?> getConstraintConfigurationClass() {
return constraintConfigurationClass;
}

public ConstraintWeightDescriptor<Solution_> findConstraintWeightDescriptor(String constraintPackage, String constraintName) {
return constraintWeightDescriptorMap.values().stream().filter(constraintWeightDescriptor
-> constraintWeightDescriptor.getConstraintPackage().equals(constraintPackage)
&& constraintWeightDescriptor.getConstraintName().equals(constraintName))
.findFirst().orElse(null);
}

@Override
public String toString() {
return getClass().getSimpleName() + "(" + constraintConfigurationClass.getName() + ")";
Expand Down
Expand Up @@ -251,8 +251,8 @@ protected boolean currentRowIsEmpty() {
protected void readHeaderCell(String value) {
XSSFCell cell = currentRow == null ? null : nextStringCell();
if (cell == null || !cell.getStringCellValue().equals(value)) {
throw new IllegalStateException(currentPosition() + ": The cell does not contain the expected value ("
+ value + ").");
throw new IllegalStateException(currentPosition() + ": The cell (" + cell.getStringCellValue()
+ ") does not contain the expected value (" + value + ").");
}
}

Expand Down
Expand Up @@ -41,13 +41,13 @@ protected ConferenceSchedulingApp createCommonApp() {
@Test(timeout = 600000)
public void solveModel() {
File unsolvedDataFile = new File("data/conferencescheduling/unsolved/72talks-12timeslots-10rooms.xlsx");
runSpeedTest(unsolvedDataFile, "-1hard/0medium/-250soft");
runSpeedTest(unsolvedDataFile, "-2115hard/0medium/-1350915soft");
}

@Test(timeout = 600000)
public void solveModelFastAssert() {
File unsolvedDataFile = new File("data/conferencescheduling/unsolved/72talks-12timeslots-10rooms.xlsx");
runSpeedTest(unsolvedDataFile, "-1hard/0medium/-290soft", EnvironmentMode.FAST_ASSERT);
runSpeedTest(unsolvedDataFile, "-2700hard/0medium/-1379805soft", EnvironmentMode.FAST_ASSERT);
}

}
Expand Up @@ -40,9 +40,10 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.optaplanner.core.api.domain.solution.cloner.SolutionCloner;
import org.optaplanner.core.api.domain.constraintweight.ConstraintWeight;
import org.optaplanner.core.api.score.buildin.hardmediumsoft.HardMediumSoftScore;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.impl.domain.constraintweight.descriptor.ConstraintWeightDescriptor;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.examples.common.persistence.AbstractXlsxSolutionFileIO;
import org.optaplanner.examples.conferencescheduling.app.ConferenceSchedulingApp;
Expand All @@ -58,6 +59,26 @@
@RunWith(Parameterized.class)
public class ConferenceSchedulingScoreRulesXlsxTest {

@Parameterized.Parameters(name = "{4}")
public static Collection testSheetParameters() {
File testFile = new File(ConferenceSchedulingScoreRulesXlsxTest.class.getResource(testFileName).getFile());
try (InputStream in = new BufferedInputStream(new FileInputStream(testFile))) {
XSSFWorkbook workbook = new XSSFWorkbook(in);
ConferenceSolution initialSolution = new ConferenceSchedulingXlsxFileIO(false).read(testFile);
TestConferenceSchedulingScoreRulesReader reader = new TestConferenceSchedulingScoreRulesReader(workbook, initialSolution);

List<Object[]> parametersList = new ArrayList<>();
for (Object[] parameters = reader.nextTestSheetParameters();
parameters != null;
parameters = reader.nextTestSheetParameters()) {
parametersList.add(parameters);
}
return parametersList;
} catch (IOException | RuntimeException e) {
throw new IllegalStateException("Failed reading inputSolutionFile (" + testFile.getName() + ").", e);
}
}

private static final String testFileName = "testConferenceSchedulingScoreRules.xlsx";
private static final HardMediumSoftScore unassignedScore = HardMediumSoftScore.ZERO;

Expand All @@ -71,36 +92,14 @@ public class ConferenceSchedulingScoreRulesXlsxTest {
SolverFactory.createFromXmlResource(ConferenceSchedulingApp.SOLVER_CONFIG));

public ConferenceSchedulingScoreRulesXlsxTest(String constraintPackage, String constraintName,
HardMediumSoftScore expectedScore, ConferenceSolution solution, String testSheetName) {
HardMediumSoftScore expectedScore, ConferenceSolution solution, String testSheetName) {
this.constraintPackage = constraintPackage;
this.constraintName = constraintName;
this.expectedScore = expectedScore;
this.solution = solution;
this.testSheetName = testSheetName;
}

@Parameterized.Parameters(name = "{4}")
public static Collection testSheetParameters() {
List<Object[]> parametersList = new ArrayList<>();

File testFile = new File(ConferenceSchedulingScoreRulesXlsxTest.class.getResource(testFileName).getFile());
try (InputStream in = new BufferedInputStream(new FileInputStream(testFile))) {
XSSFWorkbook workbook = new XSSFWorkbook(in);
ConferenceSolution initialSolution = new ConferenceSchedulingXlsxFileIO(false).read(testFile);
TestConferenceSchedulingScoreRulesReader testFileReader = new TestConferenceSchedulingScoreRulesReader(workbook, initialSolution);

Object[] currentParameterList;
while ((currentParameterList = testFileReader.nextTestSheetParameterList()) != null) {
parametersList.add(currentParameterList);
}
} catch (IOException | RuntimeException e) {
throw new IllegalStateException("Failed reading inputSolutionFile ("
+ testFile.getName() + ").", e);
}

return parametersList;
}

@Test
public void scoreRules() {
scoreVerifier.assertHardWeight(constraintPackage, constraintName, expectedScore.getHardScore(), solution);
Expand All @@ -110,8 +109,9 @@ public void scoreRules() {

private static class TestConferenceSchedulingScoreRulesReader extends AbstractXlsxSolutionFileIO.AbstractXlsxReader<ConferenceSolution> {

private final SolutionCloner<ConferenceSolution> solutionCloner =
SolutionDescriptor.buildSolutionDescriptor(ConferenceSolution.class, Talk.class).getSolutionCloner();
// TODO Abstract out, mention ConferenceSchedulingApp.SOLVER_CONFIG once and get the solutionDescriptor from there
private final SolutionDescriptor<ConferenceSolution> solutionDescriptor
= SolutionDescriptor.buildSolutionDescriptor(ConferenceSolution.class, Talk.class);
private final ConferenceSolution initialSolution;

private int numberOfSheets, currentTestSheetIndex;
Expand Down Expand Up @@ -141,43 +141,65 @@ public ConferenceSolution read() {
return initialSolution;
}

private Object[] nextTestSheetParameterList() {
String constraintPackage;
String constraintName;
HardMediumSoftScore expectedScore;
ConferenceSolution nextSheetSolution;
String testSheetName;

private Object[] nextTestSheetParameters() {
if (currentTestSheetIndex >= numberOfSheets) {
return null;
}

nextSheet(workbook.getSheetName(currentTestSheetIndex++));
testSheetName = currentSheet.getSheetName();
String testSheetName = currentSheet.getSheetName();

nextRow(false);
readHeaderCell("Constraint package");
constraintPackage = nextStringCell().getStringCellValue();
String constraintPackage = nextStringCell().getStringCellValue();
nextRow(false);
readHeaderCell("Constraint name");
constraintName = nextStringCell().getStringCellValue();
String constraintName = nextStringCell().getStringCellValue();
ConstraintWeightDescriptor<ConferenceSolution> constraintWeightDescriptor
= solutionDescriptor.getConstraintConfigurationDescriptor()
.findConstraintWeightDescriptor(constraintPackage, constraintName);
if (constraintWeightDescriptor == null) {
throw new IllegalStateException(currentPosition() + ": There is no @"
+ ConstraintWeight.class.getSimpleName() + " for constraintPackage (" + constraintPackage
+ ") and constraintName (" + constraintName + ") in the constraintConfigurationClass ("
+ solutionDescriptor.getConstraintConfigurationDescriptor().getConstraintConfigurationClass()
+ ").");
}
nextRow(false);
nextRow(false);
readHeaderCell("Score");
expectedScore = HardMediumSoftScore.parseScore(nextStringCell().getStringCellValue());
readHeaderCell("Score weight multiplier");
double weightMultiplierDouble = nextNumericCell().getNumericCellValue();
if (weightMultiplierDouble != (double) (int) weightMultiplierDouble) {
throw new IllegalStateException(currentPosition() + ": The weightMultiplier (" + weightMultiplierDouble
+ ") must be an int.");
}
int weightMultiplier = (int) weightMultiplierDouble;

nextSheetSolution = solutionCloner.cloneSolution(initialSolution);
Map<String, Talk> talkMap = nextSheetSolution.getTalkList().stream().collect(
Collectors.toMap(Talk::getCode, Function.identity()));
ConferenceSolution solution = solutionDescriptor.getSolutionCloner().cloneSolution(initialSolution);
HardMediumSoftScore constraintScore = (HardMediumSoftScore) constraintWeightDescriptor.createExtractionFunction().apply(solution);
if (constraintScore.equals(HardMediumSoftScore.ZERO)) {
throw new IllegalStateException(currentPosition() + ": The constraintScore (" + constraintScore
+ ") of the @" + ConstraintWeight.class.getSimpleName()
+ " for constraintPackage (" + constraintPackage + ") and constraintName (" + constraintName
+ ") in the constraintConfigurationClass ("
+ solutionDescriptor.getConstraintConfigurationDescriptor().getConstraintConfigurationClass()
+ ") must not be zero.");
}
HardMediumSoftScore expectedScore = HardMediumSoftScore.of(
constraintScore.getHardScore() * weightMultiplier,
constraintScore.getMediumScore() * weightMultiplier,
constraintScore.getSoftScore() * weightMultiplier);

scoreVerifier.assertHardWeight(constraintPackage, constraintName, unassignedScore.getHardScore(), nextSheetSolution);
scoreVerifier.assertSoftWeight(constraintPackage, constraintName, unassignedScore.getSoftScore(), nextSheetSolution);
scoreVerifier.assertHardWeight(constraintPackage, constraintName, unassignedScore.getHardScore(), solution);
scoreVerifier.assertSoftWeight(constraintPackage, constraintName, unassignedScore.getSoftScore(), solution);

nextRow();
readTimeslotDays();
nextRow(false);
readHeaderCell("Room");
readTimeslotHours();
Map<String, Talk> talkMap = solution.getTalkList().stream().collect(
Collectors.toMap(Talk::getCode, Function.identity()));
while (nextRow()) {
String roomName = nextStringCell().getStringCellValue();
Room room = roomMap.get(roomName);
Expand Down Expand Up @@ -213,7 +235,7 @@ private Object[] nextTestSheetParameterList() {
}
}

return new Object[]{constraintPackage, constraintName, expectedScore, nextSheetSolution, testSheetName};
return new Object[]{constraintPackage, constraintName, expectedScore, solution, testSheetName};
}

private void readTimeslotDays() {
Expand Down Expand Up @@ -256,4 +278,5 @@ private void readTimeslotHours() {
});
}
}

}
Binary file not shown.

0 comments on commit bdcefb1

Please sign in to comment.