optaplanner: how to enforce planning variables values to be used only once

坚强是说给别人听的谎言 提交于 2019-12-24 15:40:28

问题


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,


回答1:


Add a score constraint:

when
     Box($n : number, $id : id)
     Box(number == $n, $id > id)
then
     // -1 hard


来源:https://stackoverflow.com/questions/22335179/optaplanner-how-to-enforce-planning-variables-values-to-be-used-only-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!