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

I didn’t fully understand the requirements.
Here’s the code I came up with.
please let me know if it meets your needs

class abc#(int N = 9);
  rand bit array[N][N];
  rand int row_sum[N];

  constraint c1{
    foreach(array[i]) {
     array[i].sum() with (int'(item)) == row_sum[i];
    }
    foreach (array[i,j]) {
      if (j==0) {
        (array[i][j] == 1) -> (array[i][j+1] == 1);
      } else if (j==N-1) {
        (array[i][j] == 1) -> (array[i][j-1] == 1);
      } else if (j>0) {
        (array[i][j] == 1) -> ((array[i][j-1]) ^ (array[i][j+1])) == 1;
      }
    }
  }
endclass

module top();
  abc abc_c;
  initial begin
    abc_c = new();
    repeat (1) begin
      abc_c.randomize();
      foreach (abc_c.array[i]) begin
        $write("%p", abc_c.array[i]);
        $display("  Sum = %0d",abc_c.row_sum[i]);
      end
      $display("\n");
    end
  end
endmodule


Output looks something like

{0, 0, 0, 1, 1, 0, 1, 1, 0}  Sum = 4
{0, 1, 1, 0, 0, 0, 0, 0, 0}  Sum = 2
{0, 0, 0, 0, 1, 1, 0, 1, 1}  Sum = 4
{0, 0, 1, 1, 0, 0, 0, 1, 1}  Sum = 4
{0, 0, 0, 0, 1, 1, 0, 1, 1}  Sum = 4
{0, 1, 1, 0, 1, 1, 0, 1, 1}  Sum = 6
{0, 0, 0, 0, 0, 0, 0, 0, 0}  Sum = 0
{1, 1, 0, 0, 0, 0, 0, 1, 1}  Sum = 4
{0, 1, 1, 0, 0, 1, 1, 0, 0}  Sum = 4

I believe they are referring to a single contiguous block of 1’s. This implies that there must be a continuous path of 1’s between any two bits that are set to 1. I think it would be incredibly difficult to solve as a single set of constraint equations, unless you restrict the block’s shape to a rectangle or some other quadrilateral-like shape.

Another approach would be to do this iteratively. Start at a random point, set it to one, and then spiral outwards, setting bits that are connected to the previous layer until you’ve reached the required number of set bits.