0% found this document useful (0 votes)
28 views2 pages

Chapternbsp5nbspScore Calculation

The document discusses score calculation in OptaPlanner. It describes implementing a SimpleScoreCalculator interface to calculate scores in a simple way for problems like N-Queens. It also discusses detecting invalid score changes using environment modes and validating an incremental score calculator using an alternative simple score calculator implementation.

Uploaded by

user44448605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Chapternbsp5nbspScore Calculation

The document discusses score calculation in OptaPlanner. It describes implementing a SimpleScoreCalculator interface to calculate scores in a simple way for problems like N-Queens. It also discusses detecting invalid score changes using environment modes and validating an incremental score calculator using an alternative simple score calculator implementation.

Uploaded by

user44448605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

do cs.jbo ss.

o rg

http://do cs.jbo ss.o rg/dro o ls/release/6.1.0.Beta1/o ptaplanner-do cs/html/sco reCalculatio n.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 SimpleScoreCalculat or: public int erface SimpleScoreCalculat or<Sol ext ends Solut ion> { Score calculat eScore(Sol solut ion);

} For example in n queens: public class NQueensSimpleScoreCalculat or implement s SimpleScoreCalculat or<NQueens> { public SimpleScore calculat eScore(NQueens nQueens) { int n = nQueens.get N(); List <Queen> queenList = nQueens.get QueenList ();

int score = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { Queen left Queen = queenList .get (i); Queen right Queen = queenList .get (j); if (left Queen.get Row() != null && right Queen.get Row() != null) { if (left 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> <scoreDenit ionType>...</scoreDenit ionType> <simpleScoreCalculat orClass>org.opt aplanner.examples.nqueens.solver.score.NQueensSimpleScoreCalculat or</simpleScoreCalculat orClass> </scoreDirect orFact ory> Alternatively, build a SimpleScoreCalculat or instance at runtime and set it with the programmatic API:

solverFact ory.get SolverCong().get ScoreDirect orFact oryCong.set SimpleScoreCalculat or(simpleScoreCalculat or);

5.3.5. Invalid score det ect ion


Put the environment Mode in FULL_ASSERT (or FAST_ASSERT ) to detect corruption in the incremental score calculation. For more inf ormation, see the section about environment Mode . However, that will not verif 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 by using a dif f erent implementation (f or example a SimpleScoreCalculat or) to do the assertions triggered by the environment Mode . Just conf igure the dif f erent implementation as a assert ionScoreDirect orFact ory:

<environment Mode>FAST_ASSERT</environment Mode> ... <scoreDirect orFact ory> <scoreDenit ionType>...</scoreDenit ionType> <scoreDrl>/org/opt aplanner/examples/nqueens/solver/nQueensScoreRules.drl</scoreDrl> <assert ionScoreDirect orFact ory> <simpleScoreCalculat orClass>org.opt aplanner.examples.nqueens.solver.score.NQueensSimpleScoreCalculat or</simpleScoreCalculat orClass> </assert ionScoreDirect orFact ory> </scoreDirect orFact ory> T his way, the scoreDrl will be validated by the SimpleScoreCalculat or.

You might also like