This is the 15th day of my participation in Gwen Challenge
Contents summary
In the last chapter we looked at ConstraintStream, and today we’ll look at The Drools approach, which is perfectly suited to the OptaPlanner approach. OptaPlanner has some learning costs, but the benefits are huge.
Score calculations implemented using the Drools rules engine. Each score constraint is written as one or more score rules.
advantages
- Incremental score calculation is supported
- Because most DRL syntax uses forward chaining, incremental calculations can be made without any additional code.
- The score constraint rules are separated from the code as separate rules
- Easy to add or modify rules
- You can flexibly configure rules in the following ways
- Use decision tables
- Excel (XLS) spreadsheet
- KIE Workbench WebUI
- Store and publish in KIE Workbench repository
- Use decision tables
- Superior performance
- With each release, the Drools rules engine gets faster
disadvantages
- Drools come with a learning cost
- For some companies’ technical teams, they don’t want to learn an extra tool or language.
Drools file configuration
There are several ways to define the location of Drools files:
scoreDrl
Easy way. The score rules exist in a DRL file that is provided as a CLASspath resource. Simply add the scoring rule DRL file as an element in the solver configuration.
<scoreDirectorFactory>
<scoreDrl>org/optaplanner/examples/nqueens/solver/nQueensConstraints.drl</scoreDrl>
</scoreDirectorFactory>
Copy the code
In a typical project (following the Maven directory structure), The DRL file in $PROJECT_DIR/SRC/main/resources/org/optaplanner/examples/nqueens/solver/nQueensConstraints DRL.
If the constraint rule is split into multiple DRL files, add multiple elements.
scoreDrlFile
To use files in the local file system instead of classpath resources, add constraint rule DRL files as elements in the solver configuration.
<scoreDirectorFactory>
<scoreDrlFile>/home/ge0ffrey/tmp/nQueensConstraints.drl</scoreDrlFile>
</scoreDirectorFactory>
Copy the code
If the constraint rules are split into multiple DRL files, add multiple elements.
Implementing simple rules
Here is an example of implementing a score constraint with a score rule in a DRL file:
rule "Horizontal conflict"
when
Queen($id : id, row ! =null, $i : rowIndex)
Queen(id > $id, rowIndex == $i)
then
scoreHolder.addConstraintMatch(kcontext, -1);
end
Copy the code
This rule will deduct points for every two queens with the same number of peers. (id>$ID) This condition is necessary to ensure that for both queens A and B, it matches only against (A, B) and not against (B, A), (A, A), or (B, B). Let’s take a closer look at the scoring rules for the four Queens solution:
In this solution, the level conflict scoring rule will be matched for six queen couples, (A, B), (A, C), (A, D), (B, C), (B, D) and (C, D). Since none of the queens are on the same vertical or diagonal, this solution has a score of -6.
An instance of ScoreHolder is put into KieSession as a global variable named ScoreHolder. The score rule requires (directly or indirectly) updating this instance to change the score.
Score weight rule
If you have configured a ConstraintConfig, the score level and score weight for each constraint are decoupled from the implementation of the constraint, so they can be changed more easily by business users.
In this case, the ScoreHolder reward() and penalize() methods are used.
packageorg.optaplanner.examples.nqueens.solver; . global SimpleScoreHolder scoreHolder; rule"Horizontal conflict"
when
Queen($id : id, row ! =null, $i : rowIndex)
Queen(id > $id, rowIndex == $i)
then
scoreHolder.penalize(kcontext);
end
// Vertical conflict is impossible due the model
rule "Ascending diagonal conflict"
when
Queen($id : id, row ! =null, $i : ascendingDiagonalIndex)
Queen(id > $id, ascendingDiagonalIndex == $i)
then
scoreHolder.penalize(kcontext);
end
rule "Descending diagonal conflict"
when
Queen($id : id, row ! =null, $i : descendingDiagonalIndex)
Queen(id > $id, descendingDiagonalIndex == $i)
then
scoreHolder.penalize(kcontext);
end
Copy the code
They automatically affect the score matched for each constraint based on the score weight defined in the constraint configuration.
The DRL file must define a package (otherwise Drools default to DefaulTPkg) and must match constraintPackage of the constraint configuration.
(For specific methods, please refer to our article 2.10 Dynamic Adjustment of constraint weights.
conclusion
The syntax of Drools rules in this chapter is difficult to understand at first glance, but it will be very simple once we learn the syntax of Drools rules. You can watch this video, but it’s really easy once you’ve learned the basic grammar. www.bilibili.com/video/BV1Pa…
conclusion
In the next chapter we will learn about a special Variable, Shadow Variable, which is very important in some scenarios.
Creation is not easy, unauthorized reprint is prohibited. If my article is helpful to you, please like/favorites/follow it to encourage and support 💕💕💕💕 college