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 "magic squares".

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:

@PlanningSolution MagicSquareSolution

//facts
List<Column> columnList
List<Row> rowList
List<Number> numberList

//entity
List<Box> boxList


@valueRangeProvider (id="numberRange")
getNumberList()

@PlanningEntityCollectionProperty
getBoxList

@PlanningEntity Box

Column column
Row row
Number number // planningVariable

@PlanningVariable(valueRangeProviderRefs="{numberRange}")
getNumber

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,

Était-ce utile?

La solution

Add a score constraint:

when
     Box($n : number, $id : id)
     Box(number == $n, $id > id)
then
     // -1 hard
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top