Skip to content

Commit

Permalink
PLANNER-405 test CH/EH immovable entities and initialized entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Jun 3, 2016
1 parent efefc30 commit 238ddc4
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 20 deletions.
@@ -0,0 +1,107 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.optaplanner.core.impl.constructionheuristic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig;
import org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig;
import org.optaplanner.core.config.phase.PhaseConfig;
import org.optaplanner.core.config.solver.termination.TerminationConfig;
import org.optaplanner.core.impl.testdata.domain.TestdataEntity;
import org.optaplanner.core.impl.testdata.domain.TestdataSolution;
import org.optaplanner.core.impl.testdata.domain.TestdataValue;
import org.optaplanner.core.impl.testdata.domain.immovable.TestdataImmovableEntity;
import org.optaplanner.core.impl.testdata.domain.immovable.TestdataImmovableSolution;
import org.optaplanner.core.impl.testdata.util.PlannerTestUtils;

import static org.junit.Assert.*;
import static org.optaplanner.core.impl.testdata.util.PlannerAssert.assertCode;

public class DefaultConstructionHeuristicPhaseTest {

@Test
public void solveWithInitializedEntities() {
SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory(
TestdataSolution.class, TestdataEntity.class);
solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList(
new ConstructionHeuristicPhaseConfig()));
Solver<TestdataSolution> solver = solverFactory.buildSolver();

TestdataSolution solution = new TestdataSolution("s1");
TestdataValue v1 = new TestdataValue("v1");
TestdataValue v2 = new TestdataValue("v2");
TestdataValue v3 = new TestdataValue("v3");
solution.setValueList(Arrays.asList(v1, v2, v3));
solution.setEntityList(Arrays.asList(
new TestdataEntity("e1", null),
new TestdataEntity("e2", v2),
new TestdataEntity("e3", v1)));

solution = solver.solve(solution);
assertNotNull(solution);
TestdataEntity solvedE1 = solution.getEntityList().get(0);
assertCode("e1", solvedE1);
assertNotNull(solvedE1.getValue());
TestdataEntity solvedE2 = solution.getEntityList().get(1);
assertCode("e2", solvedE2);
assertEquals(v2, solvedE2.getValue());
TestdataEntity solvedE3 = solution.getEntityList().get(2);
assertCode("e3", solvedE3);
assertEquals(v1, solvedE3.getValue());
assertEquals(0, solution.getScore().getInitScore());
}

@Test
public void solveWithImmovableEntities() {
SolverFactory<TestdataImmovableSolution> solverFactory = PlannerTestUtils.buildSolverFactory(
TestdataImmovableSolution.class, TestdataImmovableEntity.class);
solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList(
new ConstructionHeuristicPhaseConfig()));
Solver<TestdataImmovableSolution> solver = solverFactory.buildSolver();

TestdataImmovableSolution solution = new TestdataImmovableSolution("s1");
TestdataValue v1 = new TestdataValue("v1");
TestdataValue v2 = new TestdataValue("v2");
TestdataValue v3 = new TestdataValue("v3");
solution.setValueList(Arrays.asList(v1, v2, v3));
solution.setEntityList(Arrays.asList(
new TestdataImmovableEntity("e1", null, false),
new TestdataImmovableEntity("e2", v2, true),
new TestdataImmovableEntity("e3", null, true)));

solution = solver.solve(solution);
assertNotNull(solution);
TestdataImmovableEntity solvedE1 = solution.getEntityList().get(0);
assertCode("e1", solvedE1);
assertNotNull(solvedE1.getValue());
TestdataImmovableEntity solvedE2 = solution.getEntityList().get(1);
assertCode("e2", solvedE2);
assertEquals(v2, solvedE2.getValue());
TestdataImmovableEntity solvedE3 = solution.getEntityList().get(2);
assertCode("e3", solvedE3);
assertEquals(null, solvedE3.getValue());
assertEquals(-1, solution.getScore().getInitScore());
}

}
Expand Up @@ -16,9 +16,16 @@

package org.optaplanner.core.impl.exhaustivesearch;

import java.util.Arrays;
import java.util.Collections;

import org.junit.Test;
import org.optaplanner.core.api.score.Score;
import org.optaplanner.core.api.score.buildin.simple.SimpleScore;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig;
import org.optaplanner.core.config.exhaustivesearch.ExhaustiveSearchPhaseConfig;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.core.impl.exhaustivesearch.decider.ExhaustiveSearchDecider;
import org.optaplanner.core.impl.exhaustivesearch.node.ExhaustiveSearchLayer;
Expand All @@ -28,9 +35,16 @@
import org.optaplanner.core.impl.heuristic.move.Move;
import org.optaplanner.core.impl.heuristic.selector.entity.EntitySelector;
import org.optaplanner.core.impl.score.director.ScoreDirector;
import org.optaplanner.core.impl.testdata.domain.TestdataEntity;
import org.optaplanner.core.impl.testdata.domain.TestdataSolution;
import org.optaplanner.core.impl.testdata.domain.TestdataValue;
import org.optaplanner.core.impl.testdata.domain.immovable.TestdataImmovableEntity;
import org.optaplanner.core.impl.testdata.domain.immovable.TestdataImmovableSolution;
import org.optaplanner.core.impl.testdata.util.PlannerTestUtils;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.optaplanner.core.impl.testdata.util.PlannerAssert.assertCode;

public class DefaultExhaustiveSearchPhaseTest {

Expand Down Expand Up @@ -100,4 +114,68 @@ public void restoreWorkingSolution() {
// verify(workingSolution).setScore(newScore);
}

@Test
public void solveWithInitializedEntities() {
SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory(
TestdataSolution.class, TestdataEntity.class);
solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList(
new ExhaustiveSearchPhaseConfig()));
Solver<TestdataSolution> solver = solverFactory.buildSolver();

TestdataSolution solution = new TestdataSolution("s1");
TestdataValue v1 = new TestdataValue("v1");
TestdataValue v2 = new TestdataValue("v2");
TestdataValue v3 = new TestdataValue("v3");
solution.setValueList(Arrays.asList(v1, v2, v3));
solution.setEntityList(Arrays.asList(
new TestdataEntity("e1", null),
new TestdataEntity("e2", v2),
new TestdataEntity("e3", v1)));

solution = solver.solve(solution);
assertNotNull(solution);
TestdataEntity solvedE1 = solution.getEntityList().get(0);
assertCode("e1", solvedE1);
assertNotNull(solvedE1.getValue());
TestdataEntity solvedE2 = solution.getEntityList().get(1);
assertCode("e2", solvedE2);
assertEquals(v2, solvedE2.getValue());
TestdataEntity solvedE3 = solution.getEntityList().get(2);
assertCode("e3", solvedE3);
assertEquals(v1, solvedE3.getValue());
assertEquals(0, solution.getScore().getInitScore());
}

@Test
public void solveWithImmovableEntities() {
SolverFactory<TestdataImmovableSolution> solverFactory = PlannerTestUtils.buildSolverFactory(
TestdataImmovableSolution.class, TestdataImmovableEntity.class);
solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList(
new ExhaustiveSearchPhaseConfig()));
Solver<TestdataImmovableSolution> solver = solverFactory.buildSolver();

TestdataImmovableSolution solution = new TestdataImmovableSolution("s1");
TestdataValue v1 = new TestdataValue("v1");
TestdataValue v2 = new TestdataValue("v2");
TestdataValue v3 = new TestdataValue("v3");
solution.setValueList(Arrays.asList(v1, v2, v3));
solution.setEntityList(Arrays.asList(
new TestdataImmovableEntity("e1", null, false),
new TestdataImmovableEntity("e2", v2, true),
new TestdataImmovableEntity("e3", null, true)));

solution = solver.solve(solution);
assertNotNull(solution);
TestdataImmovableEntity solvedE1 = solution.getEntityList().get(0);
assertCode("e1", solvedE1);
assertNotNull(solvedE1.getValue());
TestdataImmovableEntity solvedE2 = solution.getEntityList().get(1);
assertCode("e2", solvedE2);
assertEquals(v2, solvedE2.getValue());
TestdataImmovableEntity solvedE3 = solution.getEntityList().get(2);
assertCode("e3", solvedE3);
assertEquals(null, solvedE3.getValue());
assertEquals(-1, solution.getScore().getInitScore());
}

}
Expand Up @@ -22,10 +22,10 @@
import org.junit.runners.Parameterized;
import org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicType;
import org.optaplanner.examples.cloudbalancing.persistence.CloudBalancingDao;
import org.optaplanner.examples.common.app.ConstructionHeuristicTest;
import org.optaplanner.examples.common.app.AbstractConstructionHeuristicTest;
import org.optaplanner.examples.common.persistence.SolutionDao;

public class CloudBalancingConstructionHeuristicTest extends ConstructionHeuristicTest {
public class CloudBalancingConstructionHeuristicTest extends AbstractConstructionHeuristicTest {

@Parameterized.Parameters(name = "{index}: {0} - {1}")
public static Collection<Object[]> getSolutionFilesAsParameters() {
Expand Down
Expand Up @@ -22,10 +22,10 @@
import org.junit.runners.Parameterized;
import org.optaplanner.core.config.exhaustivesearch.ExhaustiveSearchType;
import org.optaplanner.examples.cloudbalancing.persistence.CloudBalancingDao;
import org.optaplanner.examples.common.app.ExhaustiveSearchTest;
import org.optaplanner.examples.common.app.AbstractExhaustiveSearchTest;
import org.optaplanner.examples.common.persistence.SolutionDao;

public class CloudBalancingExhaustiveSearchTest extends ExhaustiveSearchTest {
public class CloudBalancingExhaustiveSearchTest extends AbstractExhaustiveSearchTest {

@Parameterized.Parameters(name = "{index}: {0} - {1}")
public static Collection<Object[]> getSolutionFilesAsParameters() {
Expand Down
Expand Up @@ -34,7 +34,7 @@
/**
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
*/
public abstract class ConstructionHeuristicTest<Solution_> extends PhaseTest<Solution_> {
public abstract class AbstractConstructionHeuristicTest<Solution_> extends AbstractPhaseTest<Solution_> {

protected static <Solution_> Collection<Object[]> buildParameters(SolutionDao<Solution_> solutionDao,
String... unsolvedFileNames) {
Expand All @@ -56,7 +56,7 @@ protected static <Solution_> Collection<Object[]> buildParameters(SolutionDao<So

protected ConstructionHeuristicType constructionHeuristicType;

protected ConstructionHeuristicTest(File dataFile,
protected AbstractConstructionHeuristicTest(File dataFile,
ConstructionHeuristicType constructionHeuristicType) {
super(dataFile);
this.constructionHeuristicType = constructionHeuristicType;
Expand Down
Expand Up @@ -34,7 +34,7 @@
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
*/
@RunWith(Parameterized.class)
public abstract class ExhaustiveSearchTest<Solution_> extends PhaseTest<Solution_> {
public abstract class AbstractExhaustiveSearchTest<Solution_> extends AbstractPhaseTest<Solution_> {

protected static <Solution_> Collection<Object[]> buildParameters(SolutionDao<Solution_> solutionDao,
String... unsolvedFileNames) {
Expand All @@ -44,7 +44,7 @@ protected static <Solution_> Collection<Object[]> buildParameters(SolutionDao<So

protected ExhaustiveSearchType exhaustiveSearchType;

protected ExhaustiveSearchTest(File dataFile,
protected AbstractExhaustiveSearchTest(File dataFile,
ExhaustiveSearchType exhaustiveSearchType) {
super(dataFile);
this.exhaustiveSearchType = exhaustiveSearchType;
Expand Down
Expand Up @@ -36,7 +36,7 @@
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
*/
@RunWith(Parameterized.class)
public abstract class PhaseTest<Solution_> extends LoggingTest {
public abstract class AbstractPhaseTest<Solution_> extends LoggingTest {

protected static <Solution_, Enum_ extends Enum> Collection<Object[]> buildParameters(
SolutionDao<Solution_> solutionDao, Enum_[] types, String... unsolvedFileNames) {
Expand All @@ -59,7 +59,7 @@ protected static <Solution_, Enum_ extends Enum> Collection<Object[]> buildParam
protected SolutionDao<Solution_> solutionDao;
protected File dataFile;

protected PhaseTest(File dataFile) {
protected AbstractPhaseTest(File dataFile) {
this.dataFile = dataFile;
}

Expand Down
Expand Up @@ -21,11 +21,11 @@

import org.junit.runners.Parameterized;
import org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicType;
import org.optaplanner.examples.common.app.ConstructionHeuristicTest;
import org.optaplanner.examples.common.app.AbstractConstructionHeuristicTest;
import org.optaplanner.examples.curriculumcourse.domain.CourseSchedule;
import org.optaplanner.examples.curriculumcourse.persistence.CurriculumCourseDao;

public class CurriculumCourseConstructionHeuristicTest extends ConstructionHeuristicTest<CourseSchedule> {
public class CurriculumCourseConstructionHeuristicTest extends AbstractConstructionHeuristicTest<CourseSchedule> {

@Parameterized.Parameters(name = "{index}: {0} - {1}")
public static Collection<Object[]> getSolutionFilesAsParameters() {
Expand Down
Expand Up @@ -21,11 +21,11 @@

import org.junit.runners.Parameterized;
import org.optaplanner.core.config.exhaustivesearch.ExhaustiveSearchType;
import org.optaplanner.examples.common.app.ExhaustiveSearchTest;
import org.optaplanner.examples.common.app.AbstractExhaustiveSearchTest;
import org.optaplanner.examples.common.persistence.SolutionDao;
import org.optaplanner.examples.curriculumcourse.persistence.CurriculumCourseDao;

public class CurriculumCourseExhaustiveSearchTest extends ExhaustiveSearchTest {
public class CurriculumCourseExhaustiveSearchTest extends AbstractExhaustiveSearchTest {

@Parameterized.Parameters(name = "{index}: {0} - {1}")
public static Collection<Object[]> getSolutionFilesAsParameters() {
Expand Down
Expand Up @@ -21,11 +21,11 @@

import org.junit.runners.Parameterized;
import org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicType;
import org.optaplanner.examples.common.app.ConstructionHeuristicTest;
import org.optaplanner.examples.common.app.AbstractConstructionHeuristicTest;
import org.optaplanner.examples.common.persistence.SolutionDao;
import org.optaplanner.examples.nqueens.persistence.NQueensDao;

public class NQueensConstructionHeuristicTest extends ConstructionHeuristicTest {
public class NQueensConstructionHeuristicTest extends AbstractConstructionHeuristicTest {

@Parameterized.Parameters(name = "{index}: {0} - {1}")
public static Collection<Object[]> getSolutionFilesAsParameters() {
Expand Down
Expand Up @@ -21,14 +21,14 @@

import org.junit.runners.Parameterized;
import org.optaplanner.core.config.exhaustivesearch.ExhaustiveSearchType;
import org.optaplanner.examples.common.app.ExhaustiveSearchTest;
import org.optaplanner.examples.common.app.AbstractExhaustiveSearchTest;
import org.optaplanner.examples.common.persistence.SolutionDao;
import org.optaplanner.examples.nqueens.domain.NQueens;
import org.optaplanner.examples.nqueens.persistence.NQueensDao;

import static org.junit.Assert.*;

public class NQueensExhaustiveSearchTest extends ExhaustiveSearchTest<NQueens> {
public class NQueensExhaustiveSearchTest extends AbstractExhaustiveSearchTest<NQueens> {

@Parameterized.Parameters(name = "{index}: {0} - {1}")
public static Collection<Object[]> getSolutionFilesAsParameters() {
Expand Down
Expand Up @@ -21,11 +21,11 @@

import org.junit.runners.Parameterized;
import org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicType;
import org.optaplanner.examples.common.app.ConstructionHeuristicTest;
import org.optaplanner.examples.common.app.AbstractConstructionHeuristicTest;
import org.optaplanner.examples.common.persistence.SolutionDao;
import org.optaplanner.examples.tsp.persistence.TspDao;

public class TspConstructionHeuristicTest extends ConstructionHeuristicTest {
public class TspConstructionHeuristicTest extends AbstractConstructionHeuristicTest {

@Parameterized.Parameters(name = "{index}: {0} - {1}")
public static Collection<Object[]> getSolutionFilesAsParameters() {
Expand Down

0 comments on commit 238ddc4

Please sign in to comment.