union

OPL keyword for unions

Purpose

OPL keyword to construct set expressions.

context
Model files (.mod)

Syntax

BinaryExpression: Expression "==" Expression
                | Expression "!=" Expression
                | Expression "<=" Expression
                | Expression "<" Expression
                | Expression ">=" Expression
                | Expression ">" Expression
                | Expression "+" Expression
                | Expression "-" Expression
                | Expression "*" Expression
                | Expression "/" Expression
                | Expression "div" Expression
                | Expression "%" Expression
                | Expression "mod" Expression
                | Expression "in" Expression
                | Expression "not in" Expression
                | Expression "inter" Expression
                | Expression "union" Expression
                | Expression "diff" Expression
                | Expression "symdiff" Expression
                | Expression "^" Expression
                | Expression "&&" Expression
                | Expression "||" Expression
                | Expression "=>" Expression


AggregateExpression: "sum" '(' Qualifiers ')' Expression
                   | "min" '(' Qualifiers ')' Expression
                   | "max" '(' Qualifiers ')' Expression
                   | "prod" '(' Qualifiers ')' Expression
                   | "or" '(' Qualifiers ')' Expression
                   | "and" '(' Qualifiers ')' Expression
                   | "union" '(' Qualifiers ')' Expression
                   | "inter" '(' Qualifiers ')' Expression
                   | AllExpression

Description

Set data can be initialized by set expressions. These expressions are constructed from previously defined sets and the set operations union, inter, diff, and symdiff.

Two forms:

  • In the BinaryExpression form, the keyword union keeps the first set, then goes through the second set and, for each element that is not already in the first set, keeps it and adds it to the resulting set.

  • In the AggregateExpression form, the keyword union allows you to express a union using an aggregate expression.

Examples

Binary

These code lines initialize i to {1}, u to {1,2,3,4,5}, d to {2,3}, and sd to {2,3,4,5}.


{int} s1 = {1,2,3}; 
{int} s2 = {1,4,5}; 
{int} i = s1 inter s2; 
{int} u = s1 union s2; 
{int} d = s1 diff s2; 
{int} sd = s1 symdiff s2;

Aggregate

These code lines initialize rUnion to {1, 2, 3}.

This is true if the result is an ordered set. If you apply the property sorted or reversed to the resulting set, the result is affected accordingly.


{int} i[x in 1..3] = {x};
{int} rUnion = union( x in 1..3 ) i[x];