Problem: I want to create a 2x2 matix with positive and negative numbers. the sum of positive numbers should be less than 7 and sum of negative numbers should be greater than -8.
But i only get 0’s when i print the matix, code is below please let me know if i am missing anything.
class mem;
rand int mat[2][2];
rand int p_cnt,n_cnt;
constraint c_size
{
foreach(mat[row,col])
{
mat[row][col] inside {[-8:7]};
if(mat[row][col][3])
{
n_cnt==n_cnt+mat[row][col];
}
else
p_cnt == p_cnt+mat[row][col];
}
p_cnt <7;
n_cnt > -8;
}
In reply to CRVAddict:
The way you wrote the constraint, 0’s are the only solution. I think you want
constraint c_size
{
foreach(mat[row,col])
mat[row][col] inside {[-8:7]};
mat.sum(D1) with (D1.sum(D2) with (D2 >=0 ? D2 : 0) ) < 7;
mat.sum(D1) with (D1.sum(D2) with (D2 < 0 ? D2 : 0) ) > -8;
}
endclass
BTW, the sum() method only works for one dimension, that why I nested the methods.
In reply to dave_59:
Hi Dave,
I am getting the following error, when i tried your code.
mat.sum(D1) with( D1.sum(D2) with (D2 >=0 ? D2:0) ) < 7;
Randomization constraint has this error, which will cause the randomize function to return 0 and no new rand values will be set:
Unsupported operator ‘sum over upper dimension of multi-dimensional array’ in this constraint expression.
mem_inst.randomize();
|
In reply to CRVAddict:
Unsupported means your tool recognizes the construct, but does has not implemented support for it.
You need to choose a different simulator, or convert your array to one dimension, and calculate the index from row/col manually.