Assertions

Explains the use of assertions with regard to data consistency.

OPL provides assertions to verify the consistency of the model data. This functionality enables you to avoid wrong results due to incorrect input data. In their simplest form, assertions are simply Boolean expressions that must be true; otherwise, they raise an execution error. For instance, it is common in some transportation problems to require that the demand matches the supply. The declaration


int demand[Customers] = ...; 
int supply[Suppliers] = ...; 

assert sum(s in Suppliers) supply[s] == sum(c in Customers) demand[c];

makes sure that the total supply by the suppliers meets the total demand from the customers. This assertion can be generalized to the case of multiple products, as in


int demand[Customers] [Products] = ...; 
int supply[Suppliers] [Products] = ...; 
assert 
   forall(p in Products) 
       sum(s in Suppliers) supply[s][p] == sum(c in Customers) demand[c][p];

This assertion verifies that the total supply meets the total demand for each product. The use of assertions is highly recommended, since they make it possible to detect errors in the data input early, avoiding tedious inspection of the model data and results.

Assertions can be labeled. See Labeled assert statements.

Note:

In OPL, elements are initialized when they are first used. This is called 'lazy initialization' (see Data initialization).

However, the execution of an assert statement triggers an evaluation of the elements involved in the assertion. So, if you use an OPL element for the first time in a skipped assert expression, the lazy initialization will be triggered later. Skipping or removing assert statements may impact the order in which OPL elements are initialized and thus change the behavior of a program.