SV Constraint with Permutations and Combinations

Could someone help in understanding this problem-
Write SV constraint to distribute 20 Banana between 5 boys and 6 girls such that each person gets atleast one Banana.

This should help you get started

// Since each person gets 1 banana, min. banana a person would get is 1 & max. banana a person could get is 10
class  b_dist;
   rand bit [3:0] boys[5]; // 5 boys with Max value as 10  ( hence 4-bit )
   rand bit [3:0] girls[6] ; // 6 girls wit Max value as 10 ( hence 4-bit )
   constraint MIN_B { foreach( boys[i] ) boys[i] >= 1 ; 
                      foreach( girls[i] )  girls[i] >= 1 ; 
                    }
    // Your constraint homework comes here 
    //  Distribute 20 bananas amongst 5 boys & 6 girls via constraint
endclass
class container;
  rand int unsigned helper[11];
  rand int unsigned boys[5], girls[6];
  
  constraint bananas_c {
    helper.sum() with(int'(item)) == 20;
    
    foreach(helper[i]) {
      helper[i] <= 20; //to avoid overflow pass
       helper[i] > 0;
    if(i<$size(boys)) //you can move this to post_randomize. way more efficient
      boys[i]  == helper[i];
    else 
      girls[i- boys.size()] == helper[i];
    }
  }
  
      constraint order_c {solve helper before boys,girls;}
  function void post_randomize();

1 Like
class abc;
  rand bit[7:0] arr[];
  bit[7:0] boys = 2;// let boys are represented by number 2
  bit[7:0] girls = 3;// let girls are represented by number 3
  constraint arr_c{
    arr.size() == 20;
    foreach(arr[i]){
      arr.sum() with (int'(item == boys)) >= 5;
      arr.sum() with (int'(item == girls)) >=6;
      arr[i] inside {[2:3]}; 
    }
  }
  
    function void post_randomize();
      $display("arr == %p", arr);
    endfunction
  
  
endclass: abc
    
    
    module tb;
      abc abc_inst;
      
      initial begin 
        abc_inst = new();
        repeat(10)
        abc_inst.randomize();
      end
      
      
    endmodule

There’s no distinction between girls and boys in this problem; there are just 11 people.

rand int people[11]; // index 0-4 boys  5-10 girls
constraint c {
  people.sum() with (longint'(item))== 20; // 20 bananas, prevent overflow
  foreach(people[person]) people[person] >=1 ; // each person has at least 1 banana
}
1 Like