use of org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig in project kogito-apps by kiegroup.
the class LimeConfigOptimizer method optimize.
public LimeConfig optimize(LimeConfig config, List<Prediction> predictions, PredictionProvider model) {
List<LimeConfigEntity> entities = new ArrayList<>();
if (samplingEntities) {
entities.addAll(LimeConfigEntityFactory.createSamplingEntities(config));
}
if (proximityEntities) {
entities.addAll(LimeConfigEntityFactory.createProximityEntities(config));
}
if (encodingEntities) {
entities.addAll(LimeConfigEntityFactory.createEncodingEntities(config));
}
if (weightingEntities) {
entities.addAll(LimeConfigEntityFactory.createWeightingEntities(config));
}
if (entities.isEmpty()) {
return config;
}
LimeConfigSolution initialSolution = new LimeConfigSolution(config, predictions, entities, model);
SolverConfig solverConfig = new SolverConfig().withEntityClasses(NumericLimeConfigEntity.class, BooleanLimeConfigEntity.class).withSolutionClass(LimeConfigSolution.class);
ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig();
scoreDirectorFactoryConfig.setEasyScoreCalculatorClass(scoreCalculator.getClass());
solverConfig.setScoreDirectorFactoryConfig(scoreDirectorFactoryConfig);
TerminationConfig terminationConfig = new TerminationConfig();
if (timeLimit > 0) {
terminationConfig.setSecondsSpentLimit(timeLimit);
}
solverConfig.setTerminationConfig(terminationConfig);
LocalSearchPhaseConfig localSearchPhaseConfig = new LocalSearchPhaseConfig();
if (deterministic) {
Optional<Long> seed = config.getPerturbationContext().getSeed();
seed.ifPresent(solverConfig::setRandomSeed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
} else {
logger.debug("non reproducible execution, set the seed inside initial LimeConfig's PerturbationContext and enable deterministic execution to fix this");
}
localSearchPhaseConfig.setLocalSearchType(LocalSearchType.LATE_ACCEPTANCE);
if (stepCountLimit > 0) {
localSearchPhaseConfig.setTerminationConfig(new TerminationConfig().withStepCountLimit(stepCountLimit).withBestScoreLimit("1.0"));
}
@SuppressWarnings("rawtypes") List<PhaseConfig> phaseConfigs = new ArrayList<>();
phaseConfigs.add(localSearchPhaseConfig);
solverConfig.setPhaseConfigList(phaseConfigs);
try (SolverManager<LimeConfigSolution, UUID> solverManager = SolverManager.create(solverConfig, new SolverManagerConfig())) {
UUID executionId = UUID.randomUUID();
SolverJob<LimeConfigSolution, UUID> solverJob = solverManager.solve(executionId, initialSolution);
try {
// Wait until the solving ends
LimeConfigSolution finalBestSolution = solverJob.getFinalBestSolution();
LimeConfig finalConfig = LimeConfigEntityFactory.toLimeConfig(finalBestSolution);
BigDecimal score = finalBestSolution.getScore().getScore();
logger.info("final best solution score {} with config {}", score, finalConfig);
return finalConfig;
} catch (ExecutionException e) {
logger.error("Solving failed: {}", e.getMessage());
throw new IllegalStateException("Prediction returned an error", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Solving failed (Thread interrupted)", e);
}
}
}
use of org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig in project kogito-apps by kiegroup.
the class AbstractTaskAssigningCoreTest method createNonDaemonSolver.
protected Solver<TaskAssigningSolution> createNonDaemonSolver(int stepCountLimit) {
SolverConfig config = createBaseConfig();
ConstructionHeuristicPhaseConfig constructionHeuristicPhaseConfig = new ConstructionHeuristicPhaseConfig();
constructionHeuristicPhaseConfig.setConstructionHeuristicType(ConstructionHeuristicType.FIRST_FIT);
LocalSearchPhaseConfig phaseConfig = new LocalSearchPhaseConfig();
phaseConfig.setTerminationConfig(new TerminationConfig().withStepCountLimit(stepCountLimit));
config.setPhaseConfigList(Arrays.asList(constructionHeuristicPhaseConfig, phaseConfig));
SolverFactory<TaskAssigningSolution> solverFactory = SolverFactory.create(config);
return solverFactory.buildSolver();
}
use of org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig in project kie-benchmarks by kiegroup.
the class VehicleRoutingBenchmark method getPhaseConfigList.
private List<PhaseConfig> getPhaseConfigList() {
ConstructionHeuristicPhaseConfig constructionHeuristicPhaseConfig = new ConstructionHeuristicPhaseConfig();
constructionHeuristicPhaseConfig.setConstructionHeuristicType(ConstructionHeuristicType.FIRST_FIT_DECREASING);
LocalSearchPhaseConfig localSearchPhaseConfig = new LocalSearchPhaseConfig();
SubChainChangeMoveSelectorConfig subChainChangeMoveSelectorConfig = new SubChainChangeMoveSelectorConfig();
subChainChangeMoveSelectorConfig.setSelectReversingMoveToo(true);
SubChainSwapMoveSelectorConfig subChainSwapMoveSelectorConfig = new SubChainSwapMoveSelectorConfig();
subChainSwapMoveSelectorConfig.setSelectReversingMoveToo(true);
List<MoveSelectorConfig> moveSelectorConfigList = Arrays.asList(new ChangeMoveSelectorConfig(), new SwapMoveSelectorConfig(), subChainChangeMoveSelectorConfig, subChainSwapMoveSelectorConfig);
UnionMoveSelectorConfig selectorConfig = new UnionMoveSelectorConfig(moveSelectorConfigList);
LocalSearchForagerConfig foragerConfig = new LocalSearchForagerConfig();
foragerConfig.setAcceptedCountLimit(FORAGER_CONFIG_ACCEPTED_COUNT_LIMIT);
localSearchPhaseConfig.setForagerConfig(foragerConfig);
localSearchPhaseConfig.setMoveSelectorConfig(selectorConfig);
localSearchPhaseConfig.setAcceptorConfig(new LocalSearchAcceptorConfig().withLateAcceptanceSize(ACCEPTOR_CONFIG_LATE_ACCEPTANCE_SIZE));
return Arrays.asList(constructionHeuristicPhaseConfig, localSearchPhaseConfig);
}
use of org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig in project kie-benchmarks by kiegroup.
the class AbstractMultithreadedSolvingScalabilityBenchmark method createSolver.
@Override
public Solver<Solution_> createSolver() {
SolverConfig solverConfig = getBaseSolverConfig();
solverConfig.setMoveThreadCount(moveThreadCount);
LocalSearchPhaseConfig localSearchPhaseConfig = new LocalSearchPhaseConfig();
localSearchPhaseConfig.setMoveSelectorConfig(getMoveSelectorConfig());
localSearchPhaseConfig.setAcceptorConfig(getAcceptorConfig());
localSearchPhaseConfig.setForagerConfig(new LocalSearchForagerConfig());
localSearchPhaseConfig.getForagerConfig().setAcceptedCountLimit(getAcceptedCountLimit());
localSearchPhaseConfig.setTerminationConfig(getTerminationConfig());
solverConfig.setPhaseConfigList(Collections.singletonList(localSearchPhaseConfig));
SolverFactory<Solution_> solverFactory = SolverFactory.create(solverConfig);
return solverFactory.buildSolver();
}
use of org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig in project kie-benchmarks by kiegroup.
the class AbstractTSPLocalSearchBenchmark method createSolver.
@Override
protected Solver<TspSolution> createSolver() {
LocalSearchPhaseConfig localSearchPhaseConfig = new LocalSearchPhaseConfig();
localSearchPhaseConfig.setMoveSelectorConfig(new UnionMoveSelectorConfig());
((UnionMoveSelectorConfig) localSearchPhaseConfig.getMoveSelectorConfig()).setMoveSelectorConfigList(createMoveSelectorConfigList());
localSearchPhaseConfig.setAcceptorConfig(createAcceptorConfig());
localSearchPhaseConfig.setForagerConfig(new LocalSearchForagerConfig());
localSearchPhaseConfig.getForagerConfig().setAcceptedCountLimit(getAcceptedCountLimit());
localSearchPhaseConfig.setTerminationConfig(getTerminationConfig());
SolverConfig solverConfig = Examples.TRAVELING_SALESMAN.getBaseSolverConfig();
solverConfig.setPhaseConfigList(Collections.singletonList(localSearchPhaseConfig));
SolverFactory<TspSolution> solverFactory = SolverFactory.create(solverConfig);
return solverFactory.buildSolver();
}
Aggregations