Initializing help system before first use

Working with arrays


Type: Programming
Rating: 2 (easy-medium)
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, 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

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

© 2001-2022 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.