/** * Unused alternative. Abnormal way to create a {@link Solver}. * <p> * Not recommended! It is recommended to use {@link #createSolverByXml()} instead. * @return never null */ protected Solver<NQueens> createSolverByApi() { SolverFactory<NQueens> solverFactory = SolverFactory.createEmpty(); SolverConfig solverConfig = solverFactory.getSolverConfig(); solverConfig.setSolutionClass(NQueens.class); solverConfig.setEntityClassList(Collections.<Class<?>>singletonList(Queen.class)); ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig(); scoreDirectorFactoryConfig.setScoreDrlList( Arrays.asList("org/optaplanner/examples/nqueens/solver/nQueensScoreRules.drl")); solverConfig.setScoreDirectorFactoryConfig(scoreDirectorFactoryConfig); solverConfig.setTerminationConfig(new TerminationConfig().withBestScoreLimit("0")); List<PhaseConfig> phaseConfigList = new ArrayList<>(); ConstructionHeuristicPhaseConfig constructionHeuristicPhaseConfig = new ConstructionHeuristicPhaseConfig(); constructionHeuristicPhaseConfig.setConstructionHeuristicType( ConstructionHeuristicType.FIRST_FIT_DECREASING); phaseConfigList.add(constructionHeuristicPhaseConfig); LocalSearchPhaseConfig localSearchPhaseConfig = new LocalSearchPhaseConfig(); ChangeMoveSelectorConfig changeMoveSelectorConfig = new ChangeMoveSelectorConfig(); changeMoveSelectorConfig.setSelectionOrder(SelectionOrder.ORIGINAL); localSearchPhaseConfig.setMoveSelectorConfig(changeMoveSelectorConfig); localSearchPhaseConfig.setAcceptorConfig(new AcceptorConfig().withEntityTabuSize(5)); phaseConfigList.add(localSearchPhaseConfig); solverConfig.setPhaseConfigList(phaseConfigList); return solverFactory.buildSolver(); }
@Test public void uninitializedBenchmarkResult() { PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromXmlResource( "org/optaplanner/benchmark/api/testdataPlannerBenchmarkConfig.xml"); SolverBenchmarkConfig solverBenchmarkConfig = plannerBenchmarkFactory.getPlannerBenchmarkConfig().getSolverBenchmarkConfigList().get(0); CustomPhaseConfig phaseConfig = new CustomPhaseConfig(); phaseConfig.setCustomPhaseCommandClassList(Collections.singletonList(NoChangeCustomPhaseCommand.class)); solverBenchmarkConfig.getSolverConfig() .setPhaseConfigList(Collections.singletonList(phaseConfig)); PlannerBenchmark plannerBenchmark = plannerBenchmarkFactory.buildPlannerBenchmark(); assertNotNull(plannerBenchmark); plannerBenchmark.benchmark(); }
@Override protected SolverFactory<Solution_> buildSolverFactory() { SolverFactory<Solution_> solverFactory = SolverFactory.createFromXmlResource(commonApp.getSolverConfig()); SolverConfig solverConfig = solverFactory.getSolverConfig(); solverConfig.setTerminationConfig(new TerminationConfig()); ExhaustiveSearchPhaseConfig exhaustiveSearchPhaseConfig = new ExhaustiveSearchPhaseConfig(); exhaustiveSearchPhaseConfig.setExhaustiveSearchType(exhaustiveSearchType); solverConfig.setPhaseConfigList(Arrays.asList(exhaustiveSearchPhaseConfig)); return solverFactory; }
@Override protected SolverFactory<Solution_> buildSolverFactory() { SolverFactory<Solution_> solverFactory = SolverFactory.createFromXmlResource(commonApp.getSolverConfig()); SolverConfig solverConfig = solverFactory.getSolverConfig(); solverConfig.setTerminationConfig(new TerminationConfig()); ConstructionHeuristicPhaseConfig constructionHeuristicPhaseConfig = new ConstructionHeuristicPhaseConfig(); constructionHeuristicPhaseConfig.setConstructionHeuristicType(constructionHeuristicType); solverConfig.setPhaseConfigList(Arrays.asList(constructionHeuristicPhaseConfig)); return solverFactory; }
public static <Solution_> SolverFactory<Solution_> buildSolverFactory( Class<Solution_> solutionClass, Class<?>... entityClasses) { SolverFactory<Solution_> solverFactory = SolverFactory.createEmpty(); SolverConfig solverConfig = solverFactory.getSolverConfig(); solverConfig.setSolutionClass(solutionClass); solverConfig.setEntityClassList(Arrays.asList(entityClasses)); ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig(); scoreDirectorFactoryConfig.setEasyScoreCalculatorClass(DummySimpleScoreEasyScoreCalculator.class); solverConfig.setScoreDirectorFactoryConfig(scoreDirectorFactoryConfig); List<PhaseConfig> phaseConfigList = new ArrayList<>(2); phaseConfigList.add(new ConstructionHeuristicPhaseConfig()); LocalSearchPhaseConfig localSearchPhaseConfig = new LocalSearchPhaseConfig(); localSearchPhaseConfig.setTerminationConfig(new TerminationConfig().withStepCountLimit(TERMINATION_STEP_COUNT_LIMIT)); phaseConfigList.add(localSearchPhaseConfig); solverConfig.setPhaseConfigList(phaseConfigList); return solverFactory; }
private static SolverFactory<TestdataSolution> createSolverFactory(boolean infinite) { SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils .buildSolverFactory(TestdataSolution.class, TestdataEntity.class); SolverConfig solverConfig = solverFactory.getSolverConfig(); PartitionedSearchPhaseConfig partitionedSearchPhaseConfig = new PartitionedSearchPhaseConfig(); partitionedSearchPhaseConfig.setSolutionPartitionerClass(TestdataSolutionPartitioner.class); solverConfig.setPhaseConfigList(Arrays.asList(partitionedSearchPhaseConfig)); ConstructionHeuristicPhaseConfig constructionHeuristicPhaseConfig = new ConstructionHeuristicPhaseConfig(); LocalSearchPhaseConfig localSearchPhaseConfig = new LocalSearchPhaseConfig(); if (!infinite) { localSearchPhaseConfig.setTerminationConfig(new TerminationConfig().withStepCountLimit(1)); } partitionedSearchPhaseConfig.setPhaseConfigList( Arrays.asList(constructionHeuristicPhaseConfig, localSearchPhaseConfig)); return solverFactory; }
@Test(timeout = 600_000) public void solveThrowsExceptionWhenZeroEntity() { SolverFactory<TestdataChainedSolution> solverFactory = PlannerTestUtils.buildSolverFactory( TestdataChainedSolution.class, TestdataChainedEntity.class); LocalSearchPhaseConfig phaseConfig = new LocalSearchPhaseConfig(); phaseConfig.setTerminationConfig(new TerminationConfig().withStepCountLimit(1)); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList(phaseConfig)); Solver<TestdataChainedSolution> solver = solverFactory.buildSolver(); TestdataChainedSolution solution = new TestdataChainedSolution("1"); solution.setChainedEntityList(Collections.EMPTY_LIST); solution.setChainedAnchorList(Collections.singletonList(new TestdataChainedAnchor("4"))); try { solver.solve(solution); fail("There was no RuntimeException thrown."); } catch (RuntimeException exception) { assertEquals(true, exception instanceof IllegalStateException); assertEquals(true, exception.getMessage().contains("annotated member")); assertEquals(true, exception.getMessage().contains("must not return")); } } }
@Test public void solveWithEmptyEntityList() { 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(Collections.emptyList()); solution = solver.solve(solution); assertNotNull(solution); assertEquals(0, solution.getEntityList().size()); }
@Test public void solveWithEmptyEntityList() { 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(Collections.emptyList()); solution = solver.solve(solution); assertNotNull(solution); assertEquals(0, solution.getEntityList().size()); }
@Test public void solveWithEmptyEntityList() { SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory( TestdataSolution.class, TestdataEntity.class); LocalSearchPhaseConfig phaseConfig = new LocalSearchPhaseConfig(); phaseConfig.setTerminationConfig(new TerminationConfig().withScoreCalculationCountLimit(10L)); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList( phaseConfig)); 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(Collections.emptyList()); solution = solver.solve(solution); assertNotNull(solution); assertEquals(0, solution.getEntityList().size()); }
@Test public void solveTabuSearchWithEmptyEntityList() { SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory( TestdataSolution.class, TestdataEntity.class); LocalSearchPhaseConfig phaseConfig = new LocalSearchPhaseConfig(); phaseConfig.setLocalSearchType(LocalSearchType.TABU_SEARCH); phaseConfig.setTerminationConfig(new TerminationConfig().withScoreCalculationCountLimit(10L)); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList( phaseConfig)); 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(Collections.emptyList()); solution = solver.solve(solution); assertNotNull(solution); assertEquals(0, solution.getEntityList().size()); }
@Test public void solveStopsWhenUninitialized() { SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory( TestdataSolution.class, TestdataEntity.class); CustomPhaseConfig phaseConfig = new CustomPhaseConfig(); phaseConfig.setCustomPhaseCommandClassList(Collections.singletonList(NoChangeCustomPhaseCommand.class)); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList(phaseConfig)); Solver<TestdataSolution> solver = solverFactory.buildSolver(); TestdataSolution solution = new TestdataSolution("s1"); solution.setValueList(Arrays.asList(new TestdataValue("v1"), new TestdataValue("v2"))); solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2"), new TestdataEntity("e3"), new TestdataEntity("e4"), new TestdataEntity("e5"))); solution = solver.solve(solution); assertNotNull(solution); assertEquals(false, solution.getScore().isSolutionInitialized()); assertSame(solution, solver.getBestSolution()); }
@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, false), new TestdataImmovableEntity("e2", v2, true, false), new TestdataImmovableEntity("e3", null, false, 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()); }
@Test public void solve() { SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory( TestdataSolution.class, TestdataEntity.class); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList( new NoChangePhaseConfig())); 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); assertEquals(null, 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(-1, solution.getScore().getInitScore()); }
@Test public void solveWithInitializedEntities() { SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory( TestdataSolution.class, TestdataEntity.class); LocalSearchPhaseConfig phaseConfig = new LocalSearchPhaseConfig(); phaseConfig.setTerminationConfig(new TerminationConfig().withScoreCalculationCountLimit(10L)); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList( phaseConfig)); 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", v1), 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); assertNotNull(solvedE2.getValue()); TestdataEntity solvedE3 = solution.getEntityList().get(2); assertCode("e3", solvedE3); assertNotNull(solvedE3.getValue()); }
@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, false), new TestdataImmovableEntity("e2", v2, true, false), new TestdataImmovableEntity("e3", null, false, 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()); }
SolverFactory<TestdataReinitializeSolution> solverFactory = PlannerTestUtils.buildSolverFactory( TestdataReinitializeSolution.class, TestdataReinitializeEntity.class); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList( new ConstructionHeuristicPhaseConfig())); Solver<TestdataReinitializeSolution> solver = solverFactory.buildSolver();
@Test public void solveStopsWhenPartiallyInitialized() { SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory( TestdataSolution.class, TestdataEntity.class); ConstructionHeuristicPhaseConfig phaseConfig = new ConstructionHeuristicPhaseConfig(); // Run only 2 steps, although 5 are needed to initialize all entities phaseConfig.setTerminationConfig(new TerminationConfig().withStepCountLimit(2)); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList(phaseConfig)); Solver<TestdataSolution> solver = solverFactory.buildSolver(); TestdataSolution solution = new TestdataSolution("s1"); solution.setValueList(Arrays.asList(new TestdataValue("v1"), new TestdataValue("v2"))); solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2"), new TestdataEntity("e3"), new TestdataEntity("e4"), new TestdataEntity("e5"))); solution = solver.solve(solution); assertNotNull(solution); assertEquals(false, solution.getScore().isSolutionInitialized()); assertSame(solution, solver.getBestSolution()); }
@Test public void trackConstructionHeuristics() { SolverFactory<NQueens> solverFactory = SolverFactory.createFromXmlResource(NQueensApp.SOLVER_CONFIG); SolverConfig solverConfig = solverFactory.getSolverConfig(); ConstructionHeuristicPhaseConfig chConfig = new ConstructionHeuristicPhaseConfig(); chConfig.setValueSorterManner(valueSorterManner); chConfig.setEntitySorterManner(entitySorterManner); chConfig.setConstructionHeuristicType(constructionHeuristicType); solverConfig.setPhaseConfigList(Collections.<PhaseConfig>singletonList(chConfig)); NQueensGenerator generator = new NQueensGenerator(); NQueens problem = generator.createNQueens(8); NQueensStepTracker listener = new NQueensStepTracker(); DefaultSolver<NQueens> solver = (DefaultSolver<NQueens>) solverFactory.buildSolver(); solver.addPhaseLifecycleListener(listener); NQueens bestSolution = solver.solve(problem); assertNotNull(bestSolution); assertTrackingList(expectedCoordinates, listener.getTrackingList()); }