Constraint ordering for dynamic array

Hi,

I’ve been studying constraints for the job search, and I had a somewhat basic question about the exact ordering for constraint solving. I’ve been running the following code:


class my_item;
  
  rand int m_array[];

  // Randomization constraints.
  constraint c {    
    m_array.size() inside {[1:5]};
    foreach (m_array[i]){
      m_array[i] inside {[1:10]};
    }
    m_array.sum >= 15;
  }

and have been seeing randomization failures where size is randomized to 1, which would cause a failure to meet the sum condition. It was resolved when the minimum size condition was set to 2. My understanding was that all constraints get solved at the same time, meaning that randomization would see the sum, size, and element constraints and solve for all three simultaneously avoiding the size = 1 situation (i.e. size would always be greater than 1 to meet the sum condition). Clearly that’s wrong, so what exactly is happening here in terms of constraint ordering?

Thanks,
kibbles

In reply to kibbles:

As you mentioned, all the constraints mentioned under “constraint c” solved at the same time to make sure it’s satisfies all rules specified…
i think it might be issue with tool, which tool it is?

In reply to kibbles:

You are constraining each array element to be from 1 to 10. If you constraint the array size to have only one array element, it is impossible to have a sum of greater than or equal to 15, so you will get a randomization failure.

The following will randomize 50 times with no errors:


class my_item;
 
  rand int m_array[];
 
  // Randomization constraints.
  constraint c {    
    m_array.size() inside {[1:5]};
    foreach (m_array[i]){
      m_array[i] inside {[1:10]};
    }
    m_array.sum >= 15;
  }
  
  function new();
  endfunction
  
  function void print();
    $display("Array size is: %0d  Array is %p", m_array.size(), m_array);
  endfunction
endclass

module testbench();
  initial begin
    my_item m_;
    m_ = new();
    repeat(50) begin
      if (!m_.randomize())
        $display("Randomization failure!");
      else m_.print();
    end
  end
  
endmodule

I modified an example on EDA playground with the code I originally attached. The thing is I didn’t actively constrain the size to 1, just within 1:5.Code and randomization failure pictured here.

In reply to kibbles:

This is a tool issue. If a set of constraints doesn’t work, another set of constraints should be randomly chosen which meets the requirements. It is only when you can’t generate a set of constraints that are correct should the randomize() call fail.

I see, thanks… will mark as fixed.

I don’t think it is a tool issue since there is an implicit ordering between size and value. This is what LRM says about implicit order when randomizing dynamic arrays:

If an array is constrained by both size constraints and iterative constraints, the size constraints are solved first and the iterative constraints next. As a result of this implicit ordering between size constraints and iterative constraints, the size method shall be treated as a state variable within the foreach block of the corresponding array. For example, the expression A.size is treated as a random variable in constraint c1 and as a state variable in constraint c2. This implicit ordering can cause the solver to fail in some situations.