assert
OPL keyword to check some assumptions.
Purpose
OPL keyword to check some assumptions
context |
---|
Model files (.mod) |
Syntax
InModelDeclaration: . LocalVar ';' | . TypeDeclaration ';' | . Objective ';' | . "constraints" '{' Constraints_opt '}' | . ';' | . "assert" Expression ';' | . Scripting
Description
In OPL, assertions enable you to check some assumptions. You will ofen use them to check that some condition for the optim model to be valid is respected, but also to verify the consistency of the model data. The use of assertions is highly recommended as it can help you avoid tedious model debugging or wrong results due to incorrect input data.
These assertions are similar to the assert
instruction
in C language. In their simplest form, assertions are Boolean expressions
that must be true; they raise an execution error otherwise. For instance,
it is common in some transportation problems to require that demand
matches the supply.
Assert statements are processed in declaration order.
Assertions can be labeled. When
you label a constraint that is part of an assert
statement,
and if the assertion fails, the context of the failing assertions
appears in the Problems tab of the Output area.
Example
The following declaration makes sure that the total supply from the suppliers meets the total demand from the customers.
{string} Customers={ "Powerplant", "Factory" };
{string} Suppliers={ "BrandA" , "BrandB" };
int demand[Customers] = [ 50,20];
int supply[Suppliers]= [60, 10];
assert sum(s in Suppliers) supply[s] == sum(c in Customers) demand[c];
This assertion can be generalized to the case of multiple products. For example, the following code verifies that the total supply meets the total demand for each product.
{string} Products={ "Car", "Truck" };
{string} Customers={ "Powerplant", "Factory" };
{string} Suppliers={ "BrandA" , "BrandB" };
int demand[Customers][Products] = [[ 50,20 ] , [ 60,10]];
int supply[Suppliers][Products]= [[ 30, 25 ] , [ 80,5]];
assert
forall(p in Products)
sum(s in Suppliers) supply[s][p] == sum(c in Customers) demand[c][p];