forall

OPL keyword to specify constraints.

Purpose

OPL keyword to specify constraints

context
Model files (.mod)

Syntax

Constraint: Expression ';'
          | LocationExpression "=" Constraint
             | "if" '(' Expression ')' '{' Constraints '}' "else" '{' 
Constraints '}'
          | "if" '(' Expression ')' '{' Constraints '}'
          | "forall" '(' Qualifiers ')' Constraint
          | "forall" '(' Qualifiers ')' ArraySlotExpression "=" Constraint
          | BooleanBlock
          | ';'

Description

All constraints, and all arithmetic terms in constraints and in the objective function, are similar; they differ only in their indices.

Behavior on empty sets:

forall(empty set)=true

OPL has two features to factorize these commonalities:

  • aggregate operators

  • quantifiers

which are used in the following example.

Example 1

{string} Products = { "gas", "chloride" };
{string} Components = { "nitrogen", "hydrogen", "chlorine" };
 
float demand[Products][Components] = [ [1, 3, 0], [1, 4, 1] ];
float profit[Products] = [30, 40];
float stock[Components] = [50, 180, 40];
 
dvar float+ production[Products];
maximize
  sum (p in Products) profit[p] * production[p];
subject to {
  forall (c in Components)
    sum (p in Products) demand[p][c] * production[p] <= stock[c];
}

The objective function:

maximize
  sum (p in Products) profit[p] * production[p];

illustrates the use of the aggregate operator sum to take the summation of the individual profits. A variety of aggregate operators are available in OPL, including sum, prod, min, and max.

The instruction:

subject to {
  forall (c in Components)
    sum (p in Products) demand[p][c] * production[p] <= stock[c];

shows how you can use the universal quantifier forall to state closely related constraints. It generates one constraint for each chemical component, with each constraint stating that the total demand for the component cannot exceed its available stock.

Example 2

In this example:


assert forall(i,j in Components:i!=j) stock[i]!=stock[j];

:i!=j is a qualifier.