How to constraint a 2d matrix such that no two adjacent elements are the same Using sum() ? No two adjacent and diagonal elements should be the same

Any efficient way to code below?

module abc;
  class aa;
    rand bit [3:0] a1[7][7], val3;
     ///Working
    constraint c1{   
      foreach(a1[i])
      {
        foreach(a1[j]){
         if(i>0 &&i<7 &&j>0 && j<7)
         {
           a1[i][j] != a1[i+1][j];
           a1[i][j] != a1[i-1][j];
           a1[i][j] != a1[i][j+1];
           a1[i][j] != a1[i][j-1];
           a1[i][j] != a1[i+1][j+1];
           a1[i][j] != a1[i-1][j-1];
           a1[i][j] != a1[i-1][j+1];
           a1[i][j] != a1[i+1][j-1];
          }
         
           else if(i==0)
           {
             a1[i][j] != a1[i][j+1];
             a1[i][j] != a1[i+1][j];
           }
           else if(i==7)
           {
             a1[i][j] != a1[i][j+1];
             a1[i][j] != a1[i-1][j+1];
           }

           else if((j==7) || (j==0))
           {
            a1[i][j] != a1[i+1][j];
           }
        }
      }
         
  //Doesnt work

   constraint c2{   
      foreach(a1[i])
        foreach(a[j])
           a1[i].sum() with int'(item.index inside {[i+1:j-1]}) == 1;
             // foreach(a1[,j]) a1.sum() with (int'(a1[item.index][j] != 2));
        
        } 
       
   
  function void display();
    foreach (a1[i]) begin
      foreach (a1[i][j]) begin
        $write(" %d ",a1[i][j]);
      end
      $display("\n");
    end
  endfunction
                      
  endclass
 
  aa a2 = new();
 
  initial begin
    a2.randomize();
    a2.display();
    //$display("%p", a2.a1);
  end
endmodule

In reply to Quest4Knowledge:

I suppose there is an answer using the sum method, but you would have to offer a prize for the time spent on doing it that way.

Hi Dave
Can you please reply on this thread

How much is it worth to you?

Every piece of information from this forum is lot of worth especially from you Dave
I tried something myself though not working tool is giving item2 not found

class array_2d_unique;
 rand bit [4:0] a2d_arr[][];
 rand bit [4:0] val[];

 constraint sz_c {
         a2d_arr.size inside {[4:8]};
         foreach(a2d_arr[i]) a2d_arr[i].size() == a2d_arr.size();//Square Array
         val.size() == a2d_arr.size();
 }
constraint unq_array_c {
        unique{val};
        foreach(a2d_arr[i,j]){
                a2d_arr[i].sum() with (item == val)==1; //Unique row
                a2d_arr[j].sum() with (item == val)==1; //Unique col
                a2d_arr.sum(item1) with (item1.sum(item2) with ((item1.index == item2.index) && a2d_arr[item1.index][item2.index] == val)) == 1; //Unique col
                a2d_arr.sum(item1) with (item1.sum(item2) with (((item1.index + item2.index) == a2d_arr.size()-1) && (a2d_arr[item1.index][item2.index] == val))) == 1; //Unique col

        }
}
function void print();
        int size;
        size = a2d_arr.size();
 foreach(a2d_arr[i,j])begin
  if(j < size)
   $display("%d  ",a2d_arr[i][j%size]);
  else
   $display("\n");
 end
endfunction
endclass

OK, I took a moment to examine this. You could easily replace the sum() reduction method with or() reduction and get the same results. That would be more aligned with the intended meaning of the constraint expression.

class C;
  rand bit [2:0] arr[][];
 constraint sz_c {
         arr.size inside {[4:8]};
         foreach(arr[i]) arr[i].size() == arr.size();//Square Array
 }
constraint unq_array_c {
   arr.sum(item1) with (item1.sum(item2) with (
     item1.index ==0 ? 0 : 
     arr[item1.index][item2.index] == arr[item1.index-1][item2.index]  ) // adjacent row
                       ) == 0;
   arr.sum(item1) with (item1.sum(item2) with (
     item2.index ==0 ? 0 : 
     arr[item1.index][item2.index] == arr[item1.index][item2.index-1] )  // adjacent column
   )== 0;

  arr.sum(item1) with (item1.sum(item2) with (
         item1.index ==0 || item2.index ==0 ? 0 : 

     arr[item1.index][item2.index] == arr[item1.index-1][item2.index-1] || // diaganal \
     arr[item1.index-1][item2.index] == arr[item1.index][item2.index-1]  )  // diaganal /
                      ) == 0;
}
function void print();
  $display("\narray size = %0d",arr.size);;

  foreach(arr[i,j]) begin
       $write("%d  ",arr[i][j]);
    if(j == arr.size-1)
      $display("\n");
  end
endfunction
endclass

module top;
  C c = new;
  initial repeat(5) begin
    assert(c.randomize);
    c.print();
  end
endmodule
1 Like