Search code examples
optaplanner

Conditionally running Optaplanner phases based on problem input


I'm using Optaplanner to solver a TWVRP problem, with the "delay until last" pattern to enable multiple workers performing some tasks. The cycle detection needed for that scales horribly with the problem size, so I would like to run the local search phase only if the problem size is below some threshhold. How can I accomplish that? For instance,

if(problemSize < X){
    solveWithCHandLS(problem)
}else{
    solveWithCHonly(problem)
}

Solution

  • OptaPlanner configuration can optionally be coded. So instead of giving OptaPlanner the XML or the properties in Quarkus, you can code your own solver config:

    SolverConfig solverConfig = new SolverConfig();
    

    From there, use your IDE code completion to discover the API. It is more or less a 1-to-1 mapping to the solver config XML. When you are done building your solver config, you need to build your solver from it:

    SolverFactory<...> solverFactory = SolverFactory.create(solverConfig);
    Solver<...> solver = solverFactory.buildSolver();
    

    For each of your problem sizes, create a different config and build a solver from the config that is appropriate in the moment.