Skip to content

Random Objects

Andrew Dobis edited this page Jun 18, 2021 · 3 revisions

Random Objects are the core of the CRV tool. They are what allow us to define a problem that can solved by the CSP Solver in order to correctly generate our directed randomized values.

Random objects can be created by extending the RandObj trait.

trait RandObj {
  def randomize: Boolean
  def randomizeWith(constraints: Constraint*): Boolean
  def preRandomize(): Unit = {}
  def postRandomize(): Unit = {}
}

This trait offers 4 different methods, where two of them are optional:

  • randomize: Tries to solve the constraints defined within the object. If it succeeds, it randomizes all of the Random Variables in it. It then returns whether or not it was successful.
  • randomizeWith: The same idea as randomize but takes additional constraints, that can be temporarily added, as a parameter.
  • preRandomize: This defines a set of directives that will be run before each randomization.
  • postRandomize: This defines a set of directives that will be run after each randomization.

The RandObj trait then extended to link into the JaCoP solver. This extension is what is used in order to create your own custom random objects. I takes in a single optional parameter which is a Model.

class Frame extends RandObj(new Model)

A model corresponds to a database in which all the random variables and constraints declared inside the RandObj are stored. A model can be initialized with a seed, e.g. new Model(42), which allows the user to create reproducible tests.

Random Objects contain the user-defined CSPs, these are written using a combination of Rand fields and Constraints.

Once all of that is defined, the random object must then be instantiated and used as follows:

class Frame extends RandObj(new Model(seed)) { /* [...] Constraints [...] */ }  

val frame = new Frame  

//Use it
//Check that the constraints are solvable
assert(frame.randomize)  
  
//Use the values  
frame.randvar1.value()  
frame.randvar2.value()

Next topic: random variables or return home

Clone this wiki locally