Using Two Constraint Files In a Single Service of OptaPlanner

Posted By : Pushkar Bisht | 31-Mar-2023

ERP Optaplanner

Loading...

In this blog post, we shall seehow we can use two constraint files in OptaPlanner 8 or OptaPlanner 9 beta version.

In OptaPlanner 8 version, we do not need to configure the XML file when we use the single constraint file. We can use the following steps to use auto config :-

1:- first you need to inject the following code in your service section to find the solution of your code.

@Autowired
private SolverManager<PlanningSolutionClassName, UUID> solverManager;

2:then you need to inject the following code in your service section to find the score of your solution.

@Autowired
private ScoreManager<OptForeCastingSolution, HardSoftScore> scoreManager;

2:- After these, two injections, you need to add the following code to solve the problem and find the score.

UUID problemId = UUID.randomUUID();


// Submit the problem to start solving


SolverJob<SolutionClassName, UUID> solverJob = solverManager.solve( problemId,SolutionClassName);
SolutionClassNamesolutionClassName;
try {
// Wait until the solving ends
solution = solverJob.getFinalBestSolution();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("Solving failed.", e);
}

// Calculate the score of the problem

ScoreExplanation<SolutionClassName, HardSoftScore> score = scoreManager.explainScore(solutionClassName);

However, when we use this code with two or more constraint files, it gives the exception. This is due to the API gap of OptaPlanner.

So, for using multiple constraint filesin a service, you need to create an XML file for each constraint file and need to define the following code in this file :-

For thefirst constraint file, you need to provide the name of the constraint file with the package address.
<scoreDirectorFactory>
<constraintProviderClass>com.oodles.FirstConstraintFileName >
</scoreDirectorFactory>

For thesecond constraint file, you need to provide the name of the constraint file with the package address.
<scoreDirectorFactory>
<constraintProviderClass>com.oodles.SecondConstraintFileName >
</scoreDirectorFactory>

and so on......

After adding this file, you need to write the following code in your service file:-

SolverFactory<OptForeCastingSolution> solverFactory = SolverFactory.createFromXmlResource("YourXmlFileName.xml");
org.optaplanner.core.api.solver.Solver<SolutionClassName> solver = solverFactory.buildSolver();
SolutionClassName ourSolution = solver.solve(SolutionClassName);
SolutionManager<SolutionClassName, HardMediumSoftLongScore> solutionManager = SolutionManager.create(solverFactory);
ScoreExplanation<SolutionClassName, HardMediumSoftLongScore> explain = solutionManager.explain(ourSolution);
log.info("score Explation is "+explain.getSummary());

In this manner, you can use two or more constraint files in OptaPlanner 8 and OptaPlanner 9 versions.\

We, at Oodles, provide end-to-end OptaPlanner application development services to help enterprises solve complex business problems. Our seasoned developers have years of experience in building scalable business applications from scratch. To learn more about our OptaPlanner development services, drop us a line at [email protected].