Initializing help system before first use

Working with arrays


Type: Programming
Rating: 2
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
File(s): arraydef.mos, arrayinit.mos, autoarray.mos, chess2.mos
Data file(s): arrayinit.dat


arraydef.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

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

 declarations
  A1: array(1..3) of integer
  F = {"a","b","c"}
  A2: array(F) of real
  A3: array(R:range) of integer
  A4: dynamic array(F) of real
 end-declarations

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

 A1::[10,20,30]
 A2::(["a","b","c"])[1.1, 2.5, 3.9]
 A3::(1..3)[10,20,30]

! 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
 delcell(A4("a"))
 
 writeln("A1:", A1, " A2:", A2, " A3:", A3, " A4:", A4)

end-model

arrayinit.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file arrayinit.mos 
   ``````````````````
   Array initialization from a text file.
 
   (c) 2010 Fair Isaac Corporation
       author: S. Heipcke, Jul. 2010
*******************************************************!)
model "Initializing arrays"

 declarations
  A: array(1..2,1..3) of real          ! Can use dense format
  B: array(R:range,T:range) of real    ! Requires sparse format
  D: dynamic array(set of string, range) of real   ! Requires sparse format
  S: set of string
  M: dynamic array(S) of integer       ! Requires sparse format
  N: dynamic array(S) of string        ! Requires sparse format
 end-declarations
 
 initializations from "arrayinit.dat"
  A  B           
  D as "SomeName"                      ! Data label different from model name
  D as "SomeName2"                     ! Add some data to 'D'
  [M,N] as "MNData"                    ! 2 arrays read from the same table
 end-initializations 
 
 writeln("A:", A, " B:", B, "\nD:", D, "\nM:", M, "\nN:", N)

end-model 

autoarray.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file autoarray.mos 
   ``````````````````
   Working with automatic arrays.
 
   (c) 2010 Fair Isaac Corporation
       author: S. Heipcke, Jul. 2010
*******************************************************!)
model "Automatic arrays"

 declarations
  B: array(S:set of string, I:set of real) of integer
 end-declarations
 
 B::(["a","b"], [3,1.5,7])[1,2,3,4,5,6]
 writeln("B: ", B)

! Row and colum arrays (1-dimensional)
 forall(s in S) writeln("Row ", s, ": ", array(i in I) B(s,i))
 forall(i in I) writeln("Column ", i, ": ", array(s in S) B(s,i))

! Filtered array entries (2-dimensional)
 writeln("B filtered: ", array(s in S,i in I | s<>"a" and i<5) B(s,i))

! Inversed indices (2-dimensional)
 writeln("Transpose: ", array(i in I, s in S) B(s,i)) 
 
 initializations to "arrayout.txt"
  B
  evaluation of array(i in I, s in S) B(s,i) as "B_T"
 end-initializations
end-model

chess2.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file chess2.mos
   ```````````````
   Indexing arrays by variables.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001
*******************************************************!)

model Chess2
 uses "mmxprs"
 
 declarations
  Allvars: set of mpvar
  DescrV: array(Allvars) of string
  small, large: mpvar
 end-declarations

 DescrV(small):= "Number of small chess sets"
 DescrV(large):= "Number of large chess sets"

 Profit:=  5*small + 20*large
 Lathe:=   3*small + 2*large <= 160
 Boxwood:=   small + 3*large <= 200

 maximize(Profit)

 writeln("Solution:\n Objective: ", getobjval)
 writeln(DescrV(small), ": ", getsol(small))
 writeln(DescrV(large), ": ", getsol(large))
end-model