0

I'm trying to port a minizinc model in choco. I know how to define variables and other basic stuff but despite having read the tutorial and some code examples I've some trouble defining some non trivial constraints. Could someone give me some advice how to translate the following code (just z) in a choco solver style?

array[1..n,1..n] of int: c;
array[1..n] of var 0..10: next;

var 0..sum(c): z = sum(i in 1..n)(c[i,next[i]]);

Thanks!

Ali Hashemi
  • 3,158
  • 3
  • 34
  • 48
acco93
  • 128
  • 2
  • 11

1 Answers1

1

I believe you know how to post a sum constraint so the non trivial part lies in the c[i,next[i]] which retrieves the integer in matrix c at row i and column next[i]. The problem is that next[i] is a variable so you cannot use it directly to access a (Java) array.

You need to use the element constraint (that is also in minizinc):

/**
 * Creates an element constraint: value = table[index]
 *
 * @param value an integer variable taking its value in table
 * @param table an array of integer values
 * @param index an integer variable representing the value of value in table
 */
default Constraint element(IntVar value, int[] table, IntVar index)

As you work with a matrix, you need to do that for each row and then post a sum on them.

Note also that in Java, array cells are accessed from 0 to n-1 (in minizinc it is from 1 to n), so you may need to update the model accordingly or use an offset.

Hope this helps

https://www.cosling.com/