Constraint for array

I have a 2d array array[3][3], how do i write a constraint such that there is only one Max number in the array and the other entries can have duplicates. Thanks

In reply to raja7052:

int Max=15;
rand int unsigned array[3][3];
constraint c_only_1 { array.sum(d1) with (d1.sum(d2) with (int'(d2 == Max))) ==1;}
constraint c_le_Max { foreach (array[i,j]) array[i][j] <= Max; }

In reply to dave_59:

I tried with these constraints but for Aldec its giving error -->> Internal error has occurred. Assertion failed at ‘./sources/dag/dagcachedsymbols.cpp (96): r->use.ruse!=NULL’.

could you give a solution to this?


In reply to dave_59:

Hi Dave,

Would it be possible for you to breakdown and explain the c_only_1 constraint in a bit more detail ?

I’m trying to run this on EDA Playground, but I keep getting “Identifier not declared error for d2 variable” - (1) - EDA Playground

Thank You.

In reply to mavcdn:
The tools on EDA Playground are fairly old. You will have to expand the sum() method out manually.

Can you explain what this line does
constraint c_only_1 { array.sum(d1) with (d1.sum(d2) with (int’(d2 == Max))) ==1;}


In reply to raja7052:
it’s equivalent to

 constraint c_only_1 { (
   ( int'(array[0][0]==Max) + int'(array[0][1]==Max) + int'(array[0][2]==Max) ) +
   ( int'(array[1][0]==Max) + int'(array[1][1]==Max) + int'(array[1][2]==Max) ) +
   ( int'(array[2][0]==Max) + int'(array[2][1]==Max) + int'(array[2][2]==Max) ) 
   ) == 1 ; }

It means only one of the equalities can be true

Can we just pick two random numbers in [0:2],
and in post randomized method set

constraint c_limit {
  num1 inside [0:2];
  num2 inside [0:2];
  foreach (arrary[i, j]) arrary[i][j] < MAX;

function void post_randomzise();
  array[num1][num2] = MAX;

Not arguing, but debating pros and cons of this solution.

In reply to ankitks79:

If it’s not too much work, you should always try to express all your constraints without have to resort to post_randomize(). That makes it easer to add other constraints to be solved simultaneously. For example, if you needed the sum of all the elements to be a constraint as well.