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

Working with arrays

Description
  • arraydef.mos: Defining arrays
  • arrayinit.mos: Array initialization from a text file (requires arrayinit.dat)
  • autoarray.mos: Working with automatic arrays
  • chess2.mos: Indexing arrays by variables
Further explanation of this example: 'Mosel User Guide', Section 8.1 Arrays


Source Files

Data Files





arraydef.mos

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

   file arraydef.mos 
   `````````````````
   Defining arrays.
 
   (c) 2010 Fair Isaac Corporation
       author: S. Heipcke, Jul. 2010, rev. Sep. 2018
*******************************************************!)
model "array definition"

 declarations
  A1: array(1..3) of integer         ! Fixed size array
  F = {"a","b","c"}
  A2: array(F) of real               ! Fixed size array
  A3: array(R:range) of integer      ! Dense array with nknown index set
  A4: dynamic array(F) of real       ! Sparse array
  A5: hashmap array(F) of real       ! Sparse array
 end-declarations

 writeln("A1:", A1, " A2:", A2, " A3:", A3, " A4:", A4, " A5:", A5)

! Using the array initialization operator
 A1::[10,20,30]                      ! Range indices are known
 A2::(["a","b","c"])[1.1, 2.5, 3.9]  ! String indices must be stated
 A3::(1..3)[10,20,30]                ! Indices are not known upfront

! Redefine an entry
 A2("a"):=5.1

! This line leads to an 'index out of range' error
! A2("d"):=5.1
 
 setrandseed(3)
 forall(f in F) A4(f):= 10*random    ! Value assignment
 forall(f in F) A5(f):= A4(f)
 delcell(A4("a")); delcell(A5("b"))  ! Deleting entries from sparse arrays
 
 writeln("A1:", A1, " A2:", A2, " A3:", A3, " A4:", A4, " A5:", A5)

 delcell(A4); reset(A5)              ! Deleting the whole array contents
 writeln("A4:", A4, " A5:", A5)
end-model

Back to examples browserPrevious exampleNext example