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

Working with unions

Description
  • uniondef.mos: Defining unions, assignment of values, retrieving type information, compatible union types in subroutine arguments
  • unioninit.mos: Initializing unions from a text file (requires uniondata.dat), displaying type names
  • unionops.mos: 'reference to' operator, accessing unions of array type (exists, create, delcell, reset)
Further explanation of this example: 'Mosel User Guide', Section 8.8 Unions


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
uniondef.mos[download]
unioninit.mos[download]
unionops.mos[download]

Data Files





unioninit.mos

(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file unioninit.mos 
   ``````````````````
   Initializing unions from a text file.
 
   (c) 2021 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2021
*******************************************************!)
model "Initializing unions"
  uses "mmsystem"

  declarations
    L: list of any                  ! List of type 'any'
    S: set of any                   ! Set of type 'any'
    mytype = text or integer or real or boolean
    B: array(R:range) of mytype     ! Array of union type 'mytype'
    TNAME: array(integer) of text   ! Type names for display
  end-declarations

 ! Reading from text format file
  initializations from "uniondata.dat"
    L  S  B
  end-initializations

  TNAME(integer.id):="integer"; TNAME(real.id):="real"
  TNAME(string.id):="string"; TNAME(text.id):="text"
  TNAME(boolean.id):="boolean"

  writeln("L=", L)
  forall(i in L) writeln("  ", i, ":", i.typeid, " ", TNAME(i.typeid))
  writeln("S=", S)
  forall(i in S) writeln("  ", i, ":", i.typeid, " ", TNAME(i.typeid))
  writeln("B=", B)
  forall(i in R)
    writeln("  B", i, "=", B(i),":", B(i).typeid, " ", TNAME(B(i).typeid)) 

 ! Writing to text format file
  declarations
    x: mpvar                        ! This type does not support 'tostring'
  end-declarations
  L+= [any(x)]
  writeln(L)

  initializations to "unionout.dat"
    L  S  B
  end-initializations

end-model  

Back to examples browserPrevious exampleNext example