@Override public void inherit(ConstructionHeuristicPhaseConfig inheritedConfig) { super.inherit(inheritedConfig); constructionHeuristicType = ConfigUtils.inheritOverwritableProperty(constructionHeuristicType, inheritedConfig.getConstructionHeuristicType()); entitySorterManner = ConfigUtils.inheritOverwritableProperty(entitySorterManner, inheritedConfig.getEntitySorterManner()); valueSorterManner = ConfigUtils.inheritOverwritableProperty(valueSorterManner, inheritedConfig.getValueSorterManner()); setEntityPlacerConfig(ConfigUtils.inheritOverwritableProperty( getEntityPlacerConfig(), inheritedConfig.getEntityPlacerConfig())); moveSelectorConfigList = ConfigUtils.inheritMergeableListConfig( moveSelectorConfigList, inheritedConfig.getMoveSelectorConfigList()); foragerConfig = ConfigUtils.inheritConfig(foragerConfig, inheritedConfig.getForagerConfig()); }
private List<SolverBenchmarkConfig> buildEveryConstructionHeuristicType() { ConstructionHeuristicType[] chTypes = ConstructionHeuristicType.getBluePrintTypes(); List<SolverBenchmarkConfig> solverBenchmarkConfigList = new ArrayList<>(chTypes.length); for (ConstructionHeuristicType chType : chTypes) { solverBenchmarkConfigList.add(buildSolverBenchmarkConfig(chType, false, null)); } return solverBenchmarkConfigList; }
protected <Solution_> List<Phase<Solution_>> buildPhaseList(HeuristicConfigPolicy configPolicy, BestSolutionRecaller bestSolutionRecaller, Termination termination) { List<PhaseConfig> phaseConfigList_ = phaseConfigList; if (ConfigUtils.isEmptyCollection(phaseConfigList_)) { phaseConfigList_ = Arrays.asList( new ConstructionHeuristicPhaseConfig(), new LocalSearchPhaseConfig()); } List<Phase<Solution_>> phaseList = new ArrayList<>(phaseConfigList_.size()); int phaseIndex = 0; for (PhaseConfig phaseConfig : phaseConfigList_) { Phase<Solution_> phase = phaseConfig.buildPhase(phaseIndex, configPolicy, bestSolutionRecaller, termination); phaseList.add(phase); phaseIndex++; } return phaseList; }
@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; }
SolverBenchmarkConfig solverBenchmarkConfig = new SolverBenchmarkConfig(); String constructionHeuristicName = constructionHeuristicType == null ? "Construction Heuristic" : constructionHeuristicType.name(); String name; if (!localSearchEnabled) { SolverConfig solverConfig = new SolverConfig(); List<PhaseConfig> phaseConfigList = new ArrayList<>(2); ConstructionHeuristicPhaseConfig constructionHeuristicPhaseConfig = new ConstructionHeuristicPhaseConfig(); if (constructionHeuristicType != null) { constructionHeuristicPhaseConfig.setConstructionHeuristicType(constructionHeuristicType);
@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()); }
@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()); }
DefaultConstructionHeuristicPhase phase = new DefaultConstructionHeuristicPhase( phaseIndex, solverConfigPolicy.getLogIndentation(), bestSolutionRecaller, buildPhaseTermination(phaseConfigPolicy, solverTermination)); phase.setDecider(buildDecider(phaseConfigPolicy, phase.getTermination())); ConstructionHeuristicType constructionHeuristicType_ = defaultIfNull( constructionHeuristicType, ConstructionHeuristicType.ALLOCATE_ENTITY_FROM_QUEUE); phaseConfigPolicy.setEntitySorterManner(entitySorterManner != null ? entitySorterManner : constructionHeuristicType_.getDefaultEntitySorterManner()); phaseConfigPolicy.setValueSorterManner(valueSorterManner != null ? valueSorterManner : constructionHeuristicType_.getDefaultValueSorterManner()); EntityPlacerConfig entityPlacerConfig; if (ConfigUtils.isEmptyCollection(entityPlacerConfigList)) { entityPlacerConfig = buildUnfoldedEntityPlacerConfig(phaseConfigPolicy, constructionHeuristicType_); } else if (entityPlacerConfigList.size() == 1) { entityPlacerConfig = entityPlacerConfigList.get(0); + " elements to initialize multiple entity classes."); EntityPlacer entityPlacer = entityPlacerConfig.buildEntityPlacer(phaseConfigPolicy); phase.setEntityPlacer(entityPlacer); EnvironmentMode environmentMode = phaseConfigPolicy.getEnvironmentMode();
public void setModel(ConstructionHeuristicPhaseConfigModel model) { this.model = model; if (model.getConstructionHeuristicType() == null) { model.setConstructionHeuristicType(ConstructionHeuristicType.FIRST_FIT); } view.setSelectedConstructionHeuristicType(model.getConstructionHeuristicType().name()); if (model.getEntitySorterManner() == null) { model.setEntitySorterManner(EntitySorterManner.NONE); } view.setSelectedEntitySorterManner(model.getEntitySorterManner().name()); }
protected static <Solution_> Collection<Object[]> buildParameters(CommonApp<Solution_> commonApp, String... unsolvedFileNames) { if (commonApp instanceof CurriculumCourseApp) { /* * TODO Delete this temporary workaround to ignore ALLOCATE_TO_VALUE_FROM_QUEUE, * see https://issues.jboss.org/browse/PLANNER-486 */ List<ConstructionHeuristicType> typeList = new ArrayList<>(); for (ConstructionHeuristicType type : ConstructionHeuristicType.values()) { if (type != ConstructionHeuristicType.ALLOCATE_TO_VALUE_FROM_QUEUE) { typeList.add(type); } } return buildParameters(commonApp, typeList.toArray(new ConstructionHeuristicType[0]), unsolvedFileNames); } return buildParameters(commonApp, ConstructionHeuristicType.values(), unsolvedFileNames); }
public void onConstructionHeuristicTypeSelected(String constructionHeuristicType) { model.setConstructionHeuristicType(ConstructionHeuristicType.valueOf(constructionHeuristicType)); }
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; }
/** * 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(); }
private List<SolverBenchmarkConfig> buildEveryConstructionHeuristicTypeWithEveryLocalSearchType() { ConstructionHeuristicType[] chTypes = ConstructionHeuristicType.getBluePrintTypes(); LocalSearchType[] lsTypes = LocalSearchType.getBluePrintTypes(); List<SolverBenchmarkConfig> solverBenchmarkConfigList = new ArrayList<>( chTypes.length * lsTypes.length); for (ConstructionHeuristicType chType : chTypes) { for (LocalSearchType lsType : lsTypes) { solverBenchmarkConfigList.add(buildSolverBenchmarkConfig(chType, true, lsType)); } } return solverBenchmarkConfigList; }
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 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()); }
if (ConfigUtils.isEmptyCollection(phaseConfigList_)) { phaseConfigList_ = Arrays.asList( new ConstructionHeuristicPhaseConfig(), new LocalSearchPhaseConfig());
@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, 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()); }
TestdataReinitializeSolution.class, TestdataReinitializeEntity.class); solverFactory.getSolverConfig().setPhaseConfigList(Collections.singletonList( new ConstructionHeuristicPhaseConfig())); Solver<TestdataReinitializeSolution> solver = solverFactory.buildSolver();