Query on generating rand 2D array with sum of set bits equal to specific value and bits must be connected to each other

Hello all,

Tried with this approach but the generated array doesn’t have the bits connected to each other.

Can someone help with how we can enforce the constraint of the set bits to be connected to each other in either adjacent row, col or diagonal elements.

Also, not sure if the additional constraints for the row=0,col=0 row= N-1 and col= N-1 cases are needed or can be further optimized.

class abc#(int N=9);

 rand bit arr\[N\]\[N\];
 rand int row_sum\[N\];

 constraint sum_arr_c {
  //constraints for total number of set bits
  foreach(arr[i]) {
   arr[i].sum() with (int'(item)) == row_sum[i];
  }
         
  row_sum.sum() with (int'(item)) == 9;    
  
  //constraints for the set bits to be adjacent col,row,or diagonal  
  foreach(arr[i,j]) {       
    	if(arr[i][j]) {
       if((i>0) & (j>0) & (i< N-2) & (j<N-2)) {
     	 (arr[i+1][j] ==1) ||(arr[i+1][j+1] ==1) ||(arr[i+1][j-1] ==1) ||
      	(arr[i-1][j] ==1) ||(arr[i-1][j+1] ==1) ||(arr[i-1][j-1] ==1) ||
       (arr[i][j+1] ==1) ||(arr[i][j-1] ==1);
   	}
    }
  }
    /*if(i==0) {
       if(arr[i][j]) {
         (arr[i+1][j] ==1) ||(arr[i+1][j+1] ==1) ||(arr[i+1][j-1] ==1) || (arr[i][j+1] ==1) ||(arr[i][j-1] ==1);
       }
    }
    if(i==N-1) {
      if(arr[i][j]) {
        (arr[i-1][j] ==1) ||(arr[i-1][j+1] ==1) ||(arr[i-1][j-1] ==1) || (arr[i][j+1] ==1) ||(arr[i][j-1] ==1);
       }
    }
    if(j==0) {
       if(arr[i][j]) {
         (arr[i+1][j] ==1) ||(arr[i+1][j+1] ==1)  ||
      	(arr[i-1][j] ==1) ||(arr[i-1][j+1] ==1) ||
         (arr[i][j+1] ==1) ;
       }
    }
    if(j==N-1) {
      if(arr[i][j]) {
        (arr[i+1][j] ==1) ||(arr[i+1][j-1] ==1)  ||
        (arr[i-1][j] ==1) ||(arr[i-1][j-1] ==1) ||
        (arr[i][j-1] ==1)  ;          
       }
    }*/           
 

 }

 endclass

module tb();
 abc abc_i;
 initial begin
  abc_i = new();
  abc_i.randomize();
 foreach(abc_i.arr\[i\])
 $display(“%p”,abc_i.arr\[i\]);

 end
endmodule

Can you clarify what do you mean by” bits must be conncted to each other” ?

There should be no position in the array which has a set bit which is isolated i.e bit 1 surrounded by 0’s. All the positions which have 1 need to be connected. For eg in 4x4 array if we need to have total 4 bits set valid combinations are shown below

Valid:

1 1 0 0

1 1 0 0

0 0 0 0

Invalid:

1 0 0 0

0 0 1 0

1 0 0 0

0 0 1 0