Constraint solver issue while dealing with dynamic array size randomization

Hi,

I have a dynamic array of objects, and I want its size to be randomized, using another rand int variable:


    rand int n_objects;
    rand my_object object_arr[];
    rand bit fixed_size;

    constraint n_objects_c {
        n_objects> 0;
        n_objects<= 16;
        fixed_size == 1 -> n_objects== 5;
    };

    constraint fixed_size_c {
        fixed_size == 1;
    };

    constraint objects_mapping_c {
        object_arr.size() == n_objects;
    };
    
    function void pre_randomize();
        super.pre_randomize();
        object_arr= new[16];
        foreach (object_arr[i]) begin
            object_arr[i] = my_object::type_id::create($sformatf("object_%0d", i));
        end
    endfunction : pre_randomize    

I’m failing on constraint solver:


rand bit fixed_size; // rand_mode = ON 
rand int n_objects = 9; // rand_mode = OFF 

constraint fixed_size_c // (from this) (constraint_mode = ON) 
{
   (fixed_size == 1);
}
constraint n_profs_cfg    // (from this) (constraint_mode = ON) 
{
   (fixed_size == 1) -> (n_objects == 5);
}

I have no clue why the n_objects rand_mode is OFF. There is no place in my code where I’m disabling its randomization.

I really appreciate any help you can provide.
Ruben

In reply to rubendah:

Hi , I analyzed your code and I think you need to use solve before construct to make sure randomization of fixed_size happens before n_objects gets randomized. And another point I would like to mention is that size of the dynamic object array has been pre-randomized. So after randomization, we need to create objects which have not been done in this code.

And the last thing I want to know is what the following line suggests -

fixed_size == 1 → n_objects== 5;

Is it kind of an if-else condition?

In reply to rubendah:

At first I thought you were having an ordering problem due to the “size()” method which is solved first and the fact there was no ordering between fixed_size and n_objects (like solve fixed_size before n_objects), but I tried the following code in all 3 simulators and on EDA Playground’s Aldec simulator and I do not get the contradiction you are showing maybe you can share a complete executable code that shows the error


module test();
  class my_object;
    rand bit x;
    rand bit y;
  endclass
  
  class collection;
    rand int n_objects;
    rand my_object object_arr[];
    rand bit fixed_size;
 
    constraint n_objects_c {
        n_objects> 0;
        n_objects<= 16;
        (fixed_size == 1) -> (n_objects== 5);
    };
 
    constraint fixed_size_c {
        fixed_size == 1;
    };
 
    constraint objects_mapping_c {
        object_arr.size() == n_objects;
    };
 
    function void pre_randomize();
        object_arr= new[16];
        foreach (object_arr[i]) begin
          object_arr[i] = new();
        end
    endfunction : pre_randomize  
  endclass
  
  collection m_collection;
  
  initial begin
    m_collection = new();
    if(!m_collection.randomize()) $fatal(1, "Randomize Failed");
    $display("m_collection = %p", m_collection);
  end
  
endmodule
#VSIM> m_collection = '{n_objects:5, object_arr:'{@my_object@1, @my_object@2, @my_object@3, @my_object@4, @my_object@5}, fixed_size:1}
#xcelium> run
m_collection = '{n_objects:5, object_arr:'{test.my_object@4_1, test.my_object@5_1, test.my_object@6_1, test.my_object@7_1, test.my_object@8_1}, fixed_size:'h1}
#simv> m_collection = '{n_objects:5, object_arr:'{{ ref to class my_object}, { ref to class my_object}, { ref to class my_object}, { ref to class my_object}, { ref to class my_object}} , fixed_size:'h1}
# KERNEL: m_collection = '{n_objects:5, object_arr:'{'{x:1, y:0}, '{x:1, y:1}, '{x:1, y:1}, '{x:0, y:0}, '{x:0, y:1}}, fixed_size:1}

Hopefully someone can provide what I’m missing here as in my example I cannot find the error you showed.