Docs.jboss.org Chapternbsp5nbspScore Calculation
-
Upload
user44448605 -
Category
Documents
-
view
214 -
download
1
Transcript of Docs.jboss.org Chapternbsp5nbspScore Calculation
do cs.jbo ss.o rg http://docs.jboss.org/droo ls/release/6.1.0.Beta1/optaplanner-docs/html/scoreCalculation.html
Chapter 5. Score calculation
5.3.2. Simple Java score calculat ion
A simple way to implement your score calculation in Java.
Just implement one method of the interf ace SimpleScoreCalculator:
public interface SimpleScoreCalculator<Sol extends Solut ion> {
Score calculateScore(Sol solut ion);
}
For example in n queens:
public class NQueensSimpleScoreCalculator implements SimpleScoreCalculator<NQueens> {
public SimpleScore calculateScore(NQueens nQueens) {
int n = nQueens.getN();
List<Queen> queenList = nQueens.getQueenList();
int score = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
Queen leftQueen = queenList.get(i);
Queen rightQueen = queenList.get(j);
if (leftQueen.getRow() != null && rightQueen.getRow() != null) {
if (leftQueen.getRowIndex() == rightQueen.getRowIndex()) {
score--;
}
if (leftQueen.getAscendingDiagonalIndex() == rightQueen.getAscendingDiagonalIndex()) {
score--;
}
if (leftQueen.getDescendingDiagonalIndex() == rightQueen.getDescendingDiagonalIndex()) {
score--;
}
}
}
}
return SimpleScore.valueOf(score);
}
}
Conf igure it in your solver conf iguration:
<scoreDirectorFactory> <scoreDefinit ionType>...</scoreDefinit ionType> <simpleScoreCalculatorClass>org.optaplanner.examples.nqueens.solver.score.NQueensSimpleScoreCalculator</simpleScoreCalculatorClass> </scoreDirectorFactory>
Alternatively, build a SimpleScoreCalculator instance at runtime and set it with the programmatic API:
solverFactory.getSolverConfig().getScoreDirectorFactoryConfig.setSimpleScoreCalculator(simpleScoreCalculator);
5.3.5. Invalid score detect ion
Put the environmentMode in FULL_ASSERT (or FAST_ASSERT) to detect corruption in the incrementalscore calculation. For more inf ormation, see the section about environmentMode. However, that will notverif y that your score calculator implements your score constraints as your business actually desires.
A piece of incremental score calculator code can be dif f icult to write and to review. Assert its correctness byusing a dif f erent implementation (f or example a SimpleScoreCalculator) to do the assertions triggered bythe environmentMode. Just conf igure the dif f erent implementation as a assert ionScoreDirectorFactory:
<environmentMode>FAST_ASSERT</environmentMode> ... <scoreDirectorFactory> <scoreDefinit ionType>...</scoreDefinit ionType> <scoreDrl>/org/optaplanner/examples/nqueens/solver/nQueensScoreRules.drl</scoreDrl> <assert ionScoreDirectorFactory> <simpleScoreCalculatorClass>org.optaplanner.examples.nqueens.solver.score.NQueensSimpleScoreCalculator</simpleScoreCalculatorClass> </assert ionScoreDirectorFactory> </scoreDirectorFactory>
This way, the scoreDrl will be validated by the SimpleScoreCalculator.