
Question:
I am trying to learn optaplanner. And as a learning project I am trying to implement a very basic and simple program which calculates "<a href="http://en.wikipedia.org/wiki/Magic_square" rel="nofollow">magic squares</a>".
Basically I am trying to assign "Number"s to the "Box"es defined in rows and columns.
Sorry for not copy/pasting directly from source code, my development machine can not connect to internet so I will try to write down important part of classes by hand.
My domain structure is as follows:
<blockquote>@PlanningSolution MagicSquareSolution
//facts
List<Column> columnList
List<Row> rowList
List<Number> numberList
//entity
List<Box> boxList
@valueRangeProvider (id="numberRange")
getNumberList()
@PlanningEntityCollectionProperty
getBoxList
</blockquote>
<hr /><blockquote>
@PlanningEntity Box
Column column
Row row
Number number // planningVariable
@PlanningVariable(valueRangeProviderRefs="{numberRange}")
getNumber
</blockquote>
I am using a SIMPLE Java score calculator class.
In my solver configuration xml I used FIRST_FIT and FIRST_NON_DETERIRATING_SCORE.
The problem is; in the solution I got, numbers are reused like
7 5 3
1 5 9
7 5 3
Here you can see although the sum of rows and columns are equal to 15; Numbers 7,5 and 3 are used multiple times. How can I enforce the all the values in the value range for the planning variable is used at least once and only once.
Thanks.
Akif,
Answer1:Add a score constraint:
when
Box($n : number, $id : id)
Box(number == $n, $id > id)
then
// -1 hard