Docs.jboss.org Chapternbsp5nbspScore Calculation

2
docs.jboss.org http://docs.jboss.org/drools/release/6.1.0.Beta1/optaplanner-docs/html/scoreCalculation.html Chapter 5. Score calculation 5.3.2. Simple Java score calculation A simple way to implement your score calculation in Java. Just implement one method of the interf ace SimpleScoreCalculator: public int erface SimpleScoreCalculat or<Sol ext ends Solut ion> { Score calculateScore(Sol solution); } For example in n queens: public class NQueensSimpleScoreCalculat or implement s SimpleScoreCalculat or<NQueens> { public SimpleScore calculateScore(NQueens nQueens) { int n = nQueens.get N(); List <Queen> queenList = nQueens.get QueenList (); int score = 0; f or (int i = 0; i < n; i++) { f or (int j = i + 1; j < n; j++) { Queen left Queen = queenList .get (i); Queen right Queen = queenList .get (j); if (lef t Queen.get Row() != null && right Queen.get Row() != null) { if (lef t Queen.get RowIndex() == right Queen.get RowIndex()) { score--; } if (left Queen.get AscendingDiagonalIndex() == right Queen.get AscendingDiagonalIndex()) { score--; } if (left Queen.get DescendingDiagonalIndex() == right Queen.get DescendingDiagonalIndex()) { score--; } } } } ret urn SimpleScore.valueOf(score); } } Conf igure it in your solver conf iguration: <scoreDirect orFact ory> <scoreDefinit ionType>...</scoreDefinit ionType> <simpleScoreCalculat orClass>org.opt aplanner.examples.nqueens.solver.score.NQueensSimpleScoreCalculat or</simpleScoreCalculat orClass> </scoreDirectorFactory> Alternatively, build a SimpleScoreCalculator instance at runtime and set it with the programmatic API:

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.