use of org.optaplanner.core.config.solver.SolverManagerConfig 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);
}
}
}
Aggregations