Search in sources :

Example 1 with LocalSearchPhaseConfig

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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SolverManagerConfig(org.optaplanner.core.config.solver.SolverManagerConfig) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) SolverConfig(org.optaplanner.core.config.solver.SolverConfig) ScoreDirectorFactoryConfig(org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig) PhaseConfig(org.optaplanner.core.config.phase.PhaseConfig) LocalSearchPhaseConfig(org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) BigDecimal(java.math.BigDecimal) TerminationConfig(org.optaplanner.core.config.solver.termination.TerminationConfig) LocalSearchPhaseConfig(org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig)

Example 2 with LocalSearchPhaseConfig

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();
}
Also used : TerminationConfig(org.optaplanner.core.config.solver.termination.TerminationConfig) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) LocalSearchPhaseConfig(org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig) ConstructionHeuristicPhaseConfig(org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig) SolverConfig(org.optaplanner.core.config.solver.SolverConfig)

Example 3 with LocalSearchPhaseConfig

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);
}
Also used : SubChainSwapMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.chained.SubChainSwapMoveSelectorConfig) SwapMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.SwapMoveSelectorConfig) SubChainChangeMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.chained.SubChainChangeMoveSelectorConfig) ChangeMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.ChangeMoveSelectorConfig) MoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.MoveSelectorConfig) UnionMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.composite.UnionMoveSelectorConfig) SubChainSwapMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.chained.SubChainSwapMoveSelectorConfig) SwapMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.SwapMoveSelectorConfig) LocalSearchForagerConfig(org.optaplanner.core.config.localsearch.decider.forager.LocalSearchForagerConfig) LocalSearchAcceptorConfig(org.optaplanner.core.config.localsearch.decider.acceptor.LocalSearchAcceptorConfig) SubChainChangeMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.chained.SubChainChangeMoveSelectorConfig) ChangeMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.ChangeMoveSelectorConfig) LocalSearchPhaseConfig(org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig) ConstructionHeuristicPhaseConfig(org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig) SubChainSwapMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.chained.SubChainSwapMoveSelectorConfig) SubChainChangeMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.generic.chained.SubChainChangeMoveSelectorConfig) UnionMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.composite.UnionMoveSelectorConfig)

Example 4 with 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();
}
Also used : LocalSearchForagerConfig(org.optaplanner.core.config.localsearch.decider.forager.LocalSearchForagerConfig) LocalSearchPhaseConfig(org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig) SolverConfig(org.optaplanner.core.config.solver.SolverConfig)

Example 5 with LocalSearchPhaseConfig

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();
}
Also used : LocalSearchForagerConfig(org.optaplanner.core.config.localsearch.decider.forager.LocalSearchForagerConfig) LocalSearchPhaseConfig(org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig) TspSolution(org.optaplanner.examples.tsp.domain.TspSolution) UnionMoveSelectorConfig(org.optaplanner.core.config.heuristic.selector.move.composite.UnionMoveSelectorConfig) SolverConfig(org.optaplanner.core.config.solver.SolverConfig)

Aggregations

LocalSearchPhaseConfig (org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig)13 SolverConfig (org.optaplanner.core.config.solver.SolverConfig)12 LocalSearchForagerConfig (org.optaplanner.core.config.localsearch.decider.forager.LocalSearchForagerConfig)8 UnionMoveSelectorConfig (org.optaplanner.core.config.heuristic.selector.move.composite.UnionMoveSelectorConfig)7 ConstructionHeuristicPhaseConfig (org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig)5 TerminationConfig (org.optaplanner.core.config.solver.termination.TerminationConfig)5 ScoreDirectorFactoryConfig (org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig)3 VehicleRoutingSolution (org.optaplanner.examples.vehiclerouting.domain.VehicleRoutingSolution)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1 ConferenceSchedulingTermination (org.jboss.qa.brms.performance.examples.conferencescheduling.termination.ConferenceSchedulingTermination)1 CloudBalance (org.kie.karaf.itest.planner.domain.CloudBalance)1 LimeConfig (org.kie.kogito.explainability.local.lime.LimeConfig)1 TaskAssigningSolution (org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)1 TaskAssigningSolution (org.kie.server.services.taskassigning.core.model.TaskAssigningSolution)1 MoveSelectorConfig (org.optaplanner.core.config.heuristic.selector.move.MoveSelectorConfig)1 ChangeMoveSelectorConfig (org.optaplanner.core.config.heuristic.selector.move.generic.ChangeMoveSelectorConfig)1 SwapMoveSelectorConfig (org.optaplanner.core.config.heuristic.selector.move.generic.SwapMoveSelectorConfig)1