else

OPL keyword for conditional constraints.

Purpose

OPL keyword to state constraints conditionally

Also a keyword in IBM ILOG Script; see Loops in the Language Reference Manual.

context
Model files (.mod)

Syntax

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

Description

if-else statements make it possible to state constraints conditionally.

Example

dvar int x in 0..20;
dvar int y in 0..20;

int weWantToHaveAMaxConstraintOnTheSum=0;

maximize x+y;

subject to

{
 if  (weWantToHaveAMaxConstraintOnTheSum==1) x+y<=10;
 else x-y>=5;
}

Conditions in if-else statements must be ground, i.e., they must not contain decision variables. Implications of constraints can be used instead when conditions contain decision variables.

Conditionals can also be used in OPL to make different choices according to the truth value of a condition.