Achieving Unique Columns

Hi all ,

I am trying to obtain a 2D Random Array with Unique Rows and Columns .

Unique Rows I am able to achieve , but I am confused with Unique Column Constraint .



parameter N = 9 ;

class A;

rand int unsigned Arr  [N][N];


constraint UNIQ_ROW {
                      foreach(Arr[i])
                     unique { Arr[i] } ; // Will Give Unique Row
                    
		    }


constraint UNIQ_COL {
                      foreach(Arr[,j])
                     // unique { Arr[j] } ; // Won't Give Desired Result since it works on 1st Dimension . Need something else Here
                    
		    }




Also note that a Range doesn’t work in a Unique Constraint on the Simulators

Eg :

  

 constraint u { unique {b, a[2:3], excluded}; } // Range 2:3 doesn't work on Simulators 
 
 

Regards ,
AGIS

In reply to Etrx91:

Use a nested foreach:

   constraint c {
      foreach (arr[r1,c1]) foreach(arr[r2,c2]) {
          c1 == c2 && r1 != r2 -> arr[r1][c1] != arr[r2][c2]; // unique col
          r1 == r2 && c1 != c2 -> arr[r1][c1] != arr[r2][c2]; // unique row
   }}

In reply to dave_59:

Hi Dave ,

Thanks for your reply .

I am trying to achieve another Scenario

I have a 2D Random Array like Arr above

(1) Sum of all Row Elements is 1 ::


constraint SUM_ROW {  
                       foreach( Arr[i] )
                      Arr[i].sum(item) with int'(item) == 1'b1 ; // Achieved
                   }

Now I am stuck at the Other 2 Constraints



(2) Sum of all Column elements as 1 

(3) Sum of all Possible Diagonal Sum is 1 if any Element is 1 .


i.e for 5 X 5 Arr ::

If Arr[2][2] == 1 ; then all its Diagonal Elements Have to be 0

i.e Arr[0][0] == Arr[1][1] == Arr[3][3] == Arr[4][4] are All 0

Edit: Arr[0][4]==Arr[1][3]==Arr[3][1]==Arr[4][0]==0

Similarly if Arr[0][2] == 1 then all its Diagonal Elements ( Arr[1][1] ,Arr[2][0] , Arr[1][3] , Arr[2][4] ) are 0

Regards,
AGIS

works for column now I am working on diag


``` verilog

rand bit arr [N][N];

		constraint col
		{
			foreach (arr[r1,c1]) foreach(arr[r2,c2])
				{
					c1 == c2 && r1 != r2 && arr[r1][c1]==1 -> arr[r2][c2]==0;
				}
		}
		
		

		constraint sum
		{
			foreach(arr[i]) {arr[i].sum(item) with (int'(item))==1;}
		}


                
		

In reply to Etrx91:

constraint SUM_COL {
Arr.sum(item.index(2)) with int’(item) == 1’b1 ; // Will Work
}

In reply to nguruprasad01:

Hi nguruprasad01 ,

Thanks for your reply . Your Solution ( using sum with index ) was something I haven’t tried before but unfortunately it doesn’t work .

Regards,
AGIS

In reply to shimonc:

Hi Shimonc ,

Yes the column constraint works . I was actually trying to achieve it using sum() method but have’t still found a solution . Do let us know on getting the Diagonal Constraint .

Regards,
AGIS

I tried it with some seeds and it seems to work. Please give it a try.


``` verilog

module d2_arr_unique;                                                                                                                                                                            
                                                                                                                                                                                                 
	parameter N = 8 ;                                                                                                                                                                            
	class A;                                                                                                                                                                                     
                                                                                                                                                                                                 
		rand bit arr [N][N];                                                                                                                                                                     
		                                                                                                                                                                                         
		// Absolute value                                                                                                                                                                        
		function int abs(int val);                                                                                                                                                               
			return val >=0 ? val : -(val);                                                                                                                                                       
		endfunction                                                                                                                                                                              
		                                                                                                                                                                                         
		// column sum == 1                                                                                                                                                                       
		constraint col                                                                                                                                                                           
		{                                                                                                                                                                                        
			foreach (arr[r1,c1]) foreach(arr[r2,c2])                                                                                                                                             
				{                                                                                                                                                                                
					(c1 == c2) && (r1 != r2) && arr[r1][c1]==1 -> arr[r2][c2]==0;                                                                                                                
				}                                                                                                                                                                                
		}                                                                                                                                                                                        
                                                                                                                                                                                                 
		// row sum == 1                                                                                                                                                                          
		constraint row                                                                                                                                                                           
		{                                                                                                                                                                                        
			foreach(arr[i]) {arr[i].sum(item) with (int'(item))==1;}                                                                                                                             
		}                                                                                                                                                                                        
		                                                                                                                                                                                         
		// Take care that diagonals are zero                                                                                                                                                     
		constraint diag                                                                                                                                                                          
		{                                                                                                                                                                                        
			foreach (arr[r1,c1]) foreach (arr[r2,c2])                                                                                                                                            
					                                                                                                                                                                             
				{                                                                                                                                                                                
					(abs(r1-r2) == abs(c2-c1)) && r1!=r2 && c1!=c2 && arr[r1][c1]==1 -> arr[r2][c2]==0;                                                                                          
				}                                                                                                                                                                                
		}                                                                                                                                                                                        
                                                                                                                                                                                                 
		// Print array                                                                                                                                                                           
		function void print_arr();                                                                                                                                                               
			for(int i=0; i<N; i++) begin                                                                                                                                                         
				for (int j=0; j<N; j++) begin                                                                                                                                                    
					$write("[%0d]",arr[i][j]);                                                                                                                                                   
				end                                                                                                                                                                              
				$display("\n");                                                                                                                                                                  
			end                                                                                                                                                                                  
		endfunction                                                                                                                                                                              
                                                                                                                                                                                                 
	endclass                                                                                                                                                                                     
                                                                                                                                                                                                 
	A a= new;                                                                                                                                                                                    
                                                                                                                                                                                                 
	initial begin                                                                                                                                                                                
		void'(a.randomize());                                                                                                                                                                    
		a.print_arr();                                                                                                                                                                           
		$finish();                                                                                                                                                                               
	end                                                                                                                                                                                          
endmodule                                                                                                                                                                                        

In reply to shimonc:

Yes it works . Thank you for the time you put in solving the question