An Introduction To Reasoning In OptaPlanner

Posted By : Dhiraj Chauhan | 29-Jul-2022

Optaplanner

Loading...

Introduction

In this blog post, we shall learn "What is reasoning in OptaPlanner and how to implement the reasoning in any optaplanning project". But before jumping to the reasoning part let's overview the purpose of optaplanning first. "Optaplanner is a constraint solving algorithm which solves complex problem with the help of constraints. The constraint is just a way to tell Optaplanner which solution is better than another solutions".

So now if we come back to the reasoning part, the purpose behind reasoning in OptaPlanner is to know what all are the constraints that are getting broken while solving the constraints.

Example:

Considering the employee/shift rostering problem, suppose there is one hard rule written in the drools file. So at the end of plan if any of the shift is remain unplanned due to that one hard rule then we can see the exact reason of that unplanned shift by using the reasoning.

NOTE: Not only hard rule, we can also see the medium/soft rule in reasoning if its breaking.

Types of constraints:

  • Hard Constraint: Hard constraints is those constraint which is cannot be broken in any case. for Example:- A employee cannot present at two diffrent shift at the same time.
  • Medium constraint: Medium constraint is those constraint which can be broken, if a planning problem is not able to plan. For Example:- A employee cannot work more than 8 hours.
  • Soft constraint: Soft constraint is those constraint which is avoidable and it helps to improve the planning solution. For Example:- A employee doesn't want to work at Friday afternoon.

Below is the code snippet of reasoning:

SolverFactory<OptSolution> solverFactory = SolverFactory.createFromXmlResource("optaplanner/SolverConfig.xml");

org.optaplanner.core.api.solver.Solver<OptSolution> solver = solverFactory.buildSolver();

OptSolution optSolution1 = solver.solve(optSolution);

ScoreManager<OptSolution> manager= ScoreManager.create(solverFactory);

log.info(manager.explainScore(driverTripAssignationSolution));

ScoreDirector scoreDirector = solver.getScoreDirectorFactory().buildScoreDirector();

scoreDirector.setWorkingSolution(solver);

To get the entity wise reasoning use below code:

Map map = scoreDirector.getIndictmentMap();

scoreDirector.getConstraintMatchTotals();

OptSolution: OptSolution is the planning entity class.

ScoreManager: A stateless service which help to calculate Score, ConstraintMatchTotal, Indictment, etc.

ScoreDirector: The ScoreDirector holds the working solution and calculates the Score for it.

Conclusion

So in this blog, we have seen how to use reasoning in optaplanning to know what all the constraints are getting broken while solving the constraints.

At Oodles, we provide custom ERP development services with a focus on streamlining complex business workflows. To learn more about our custom ERP software development services, reach out at [email protected].