Let’s say i have 3x3 matrix. I want to constraint rows and columns in such a way that at any given point either there can be 2 1’s in a given row or 2 1’s in a column not more than that. The below constraint will ensure only 2 1’s but there is no restriction on rows and columns. If i use sum constraint it constraints each row and each column. Appreciate the help.
class test;
rand bit arr[3][3];
constraint same_row {
arr.sum(item1) with (int'(item1.sum(item2))) == 2;
}
// Possible solution
// 0 0 1
// 0 0 1
// 0 0 0
// Another solution
// 0 1 1
// 0 0 0
// 0 0 0
endclass
I can give you a hint for an approach to take.
You can add a constraint that says there’s at least one row or one column whose sum is 2. That together with your original constraint will make sure it’s exclusive.
I have a solution but thats not straight forward. Is that possible with sum method? If i unroll foreach it with sum that is applicable for all rows or columns, that is the confusion
class matrix;
rand bit a [3][3];
rand int i,j;
rand bit b;
**constraint c2 { i inside {[0:2]};j inside {[0:2]};}**
constraint c3 { if(b==1)
a.sum with (int'(a[i][item.index]==1))==2;
else
a.sum with (int'(a[item.index][j]==1))==2;}
constraint c1 { a.sum(item1) with (item1.sum(item2) with (int'(item2==1))) == 2;}
endclass