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


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:


@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.




Add a score constraint:

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


  • How to remove left and right margins from all wrapped rows in flexbox (without nth-child or js)
  • Application bar icon text length
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • How Get arguments value using inline assembly in C without Glibc?
  • Jquery Knockout: ko.computed() vs classic function?
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • DIV instruction jumping to random location?
  • Custom preprocessing in caret
  • Can I programmatically choose the Android layout folder?
  • Why use database factory in asp.net mvc?
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Does Mobilefirst provide a provision to access web services directly?
  • Diff between two dataframes in pandas
  • Moving Android View and preventing onDraw to be called over and over again
  • xtable package: Skipping some rows in the output
  • ADO and msqli connections very slow
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Marklogic : Query response time is very high
  • How to define and use opencv mat of user type
  • Email verification using google app script and google forms
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • What is Eclipse's Declaration View used for?
  • Azure Cloud Service Web Role web pages do not load
  • javaw.exe and eclipse startup problems
  • How to add date and time under each post in guestbook in google app engine
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Jquery - Jquery Wysiwyg return html as a string
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • SVN: Merging two branches together
  • python regex in pyparsing
  • Acquiring multiple attributes from .xml file in c#
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Append folder name and increment by 1 using batch script
  • java string with new operator and a literal