FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Basic set operations

Example 'setops':
  • declaring constant and dynamic sets
  • assigning and adding elements to sets
  • using predefined set operations: union, intersection, subtraction
  • printing sets
Example 'setcomp':
  • comparison operators for sets
Further explanation of this example: 'Mosel User Guide', Section 8.2 Working with sets.

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.


  * Mosel Example Problems                              *
  * ======================                              *
  *                                                     *
  * file setops.mos                                     *
  * ```````````````                                     *
  * Example for the use of the Mosel language           *
  * (Performing set operations with Mosel)              *
  *                                                     *
  * (c) 2008 Fair Isaac Corporation                     *
  *     author: S. Heipcke, 2001                        *

model Setops

                                    ! Constant set definition: unchangeable set
 Cities={"rome", "bristol", "london", "paris", "liverpool"}
 Capitals, Ports: set of string     ! Dynamic set definition: elements
                                    ! added (or deleted) later; the set
                                    ! declaration is optional

                                    ! Assign elements to the (dynamic) sets
 Ports:={"plymouth", "bristol", "glasgow", "london", "calais", 
 Capitals:={"rome", "london", "paris", "madrid", "berlin"}
 writeln(" Ports: ", Ports, "\n Capitals: ", Capitals,"\n Cities: ", Cities)

 Places:= Cities+Ports+Capitals     ! Create the union of all 3 sets
 writeln("\n Union of all places: ", Places)

 All_three:= Cities*Ports*Capitals  ! Create the intersection of all 3 sets
 writeln(" Intersection of all three: ", All_three)

 Cities_not_cap:= Cities-Capitals   ! Create the set of all cities that are
                                    ! not capitals
 writeln(" Cities that are not capitals: ", Cities_not_cap)
                                    ! Create the set of all ports with names
                                    ! that do not start with "l"
 forall (i in Ports | substr(i,1,1)<>"l" ) Ports_no_l += {i}
 writeln(" Ports that do not start with 'l': ", Ports_no_l,
          " (", Ports_no_l.size ," out of ", Ports.size, ")")


Back to examples browserPrevious exampleNext example