Writing packages: definition of constant symbols, subroutines, types, and parameters
|  | |
| Type: | Programming | 
| Rating: | 3 (intermediate) | 
| Description: | Packages are libraries written in the Mosel language that extend the language with 
 | 
| File(s): | myconstants.mos (library, compile separately), myconst_test.mos, solarraypkg.mos (library, compile separately), solarr_test.mos, arcpkg.mos (library, compile separately), arc_test.mos, parpkg.mos (library, compile separately), params_test.mos | 
| Data file(s): | arcs.dat | 
|  | |
| myconstants.mos | 
| (!******************************************
  Mosel Examples
  ==============
  File myconstants.mos
  ````````````````````
  Example package defining 
    constants
  of different types.
  (c) 2008 Fair Isaac Corporation
      author: S. Heipcke, 2005
*******************************************!)
package myconstants
 public declarations
  MYCST_BIGM = 10000          ! A large integer value
  MYCST_TOL = 0.00001         ! A tolerance value
  MYCST_LINE =                ! String constant
     "----------------------------------------------------------------"
  MYCST_FLAG = true           ! Constant with value true
  MYCST_NOFLAG = false        ! Constant with value false
 end-declarations
end-package
 | 
| myconst_test.mos | 
| (!******************************************************
   Mosel NI Examples
   =================
   File myconst_test.mos
   `````````````````````
   Using module myconstants
   *** Compile myconstants.mos before running this model ****
   (c) 2008 Fair Isaac Corporation 
       author: S. Heipcke, 2002
*******************************************************!)
model "Test myconstants module"
 uses "myconstants"
 writeln(MYCST_LINE)
 writeln("BigM value: ", MYCST_BIGM, ", tolerance value: ", MYCST_TOL)
 writeln("Boolean flags: ", MYCST_FLAG, " ", MYCST_NOFLAG)
 writeln(MYCST_LINE)
 
end-model
 | 
| solarraypkg.mos | 
| (!******************************************
  Mosel Examples
  ==============
  File solarraypkg.mos
  ````````````````````
  Example package providing the procedure
    solarray(array of mpvar, array of real)
  for getting solutions into an array.
  (c) 2008 Fair Isaac Corporation
      author: S. Heipcke, May 2005
*******************************************!)
package solarraypkg 
! **** Integer indices (including ranges) ****
 public procedure solarray(x:array(R:set of integer) of mpvar,
                           s:array(set of integer) of real)
  forall(i in R) s(i):=getsol(x(i))
 end-procedure 
 public procedure solarray(x:array(R1:set of integer,
                                   R2:set of integer) of mpvar,
                           s:array(set of integer, set of integer) of real)
  forall(i in R1, j in R2) s(i,j):=getsol(x(i,j))
 end-procedure 
 
 public procedure solarray(x:array(R1:set of integer,
                                   R2:set of integer,
				   R3:set of integer) of mpvar,
                           s:array(set of integer,
			           set of integer,
				   set of integer) of real)
  forall(i in R1, j in R2, k in R3) s(i,j,k):=getsol(x(i,j,k))
 end-procedure 
 public procedure solarray(x:array(R1:set of integer,
                                   R2:set of integer,
                                   R3:set of integer,
				   R4:set of integer) of mpvar,
                           s:array(set of integer, set of integer,
			           set of integer, set of integer) of real)
  forall(i in R1, j in R2, k in R3, l in R4) s(i,j,k,l):=getsol(x(i,j,k,l))
 end-procedure 
! ****String indices ****
 public procedure solarray(x:array(R:set of string) of mpvar,
                           s:array(set of string) of real)
  forall(i in R) s(i):=getsol(x(i))
 end-procedure 
 public procedure solarray(x:array(R1:set of string,
                                   R2:set of string) of mpvar,
                           s:array(set of string, set of string) of real)
  forall(i in R1, j in R2) s(i,j):=getsol(x(i,j))
 end-procedure 
 
 public procedure solarray(x:array(R1:set of string,
                                   R2:set of string,
				   R3:set of string) of mpvar,
                           s:array(set of string,
			           set of string,
				   set of string) of real)
  forall(i in R1, j in R2, k in R3) s(i,j,k):=getsol(x(i,j,k))
 end-procedure 
 public procedure solarray(x:array(R1:set of string,
                                   R2:set of string,
                                   R3:set of string,
				   R4:set of string) of mpvar,
                           s:array(set of string, set of string,
			           set of string, set of string) of real)
  forall(i in R1, j in R2, k in R3, l in R4) s(i,j,k,l):=getsol(x(i,j,k,l))
 end-procedure   
end-package
 | 
| solarr_test.mos | 
| (!******************************************************
   Mosel NI Examples
   =================
   File solarr_test.mos
   ````````````````````
   Using module solarray
   *** Compile solarraypkg.mos before running this model ****
   (c) 2008 Fair Isaac Corporation 
       author: S. Heipcke, 2002
*******************************************************!)
model "Test solarray module"
 uses "solarraypkg", "mmxprs"
 declarations
  R1=1..2
  R2={6,7,9}
  R3={5,-1}
  x: array(R1,R2,R3) of mpvar
  sol: array(R1,R2,R3) of real
 end-declarations
! Define and solve a small problem  
 sum(i in R1, j in R2, k in R3) (i+j+2*k) * x(i,j,k) <= 20
 forall(i in R1, j in R2, k in R3) x(i,j,k)<=1
 maximize(sum(i in R1, j in R2, k in R3) (i+2*j+k) * x(i,j,k))
! Get the solution array  
 solarray(x,sol)
! Print the solution 
 forall(i in R1, j in R2, k in R3)
  writeln(" (", i, ",", j, ",", k, ") ", sol(i,j,k), " ", getsol(x(i,j,k)))
 writeln(sol)
end-model
 | 
| arcpkg.mos | 
| (!******************************************************
   Mosel User Guide Example Problems
   ================================= 
   file arc.mos
   ````````````
   Definition of the type 'arc'.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2007, rev. June 2018
*******************************************************!)
package arcpkg
 public declarations
  arc = public record                   ! Arcs:
   Source,Sink: string                  !   Source and sink of arc
   Cost: real                           !   Cost coefficient
  end-record 
 end-declarations
(! Alternatively:
 declarations
  public arc = record                   ! Arcs:
   public Source,Sink: string           !   Source and sink of arc
   public Cost: real                    !   Cost coefficient
  end-record 
 end-declarations
!)
 public function is_neighbor(n1,n2: string, A: array(Arcs:set of integer) of arc): boolean
  returned:=or(a in Arcs ) (( A(a).Source=n1 and A(a).Sink=n2) or
                            ( A(a).Source=n2 and A(a).Sink=n1))
 end-function  
end-package 
 | 
| arc_test.mos | 
| (!******************************************************
   Mosel Example Problems
   ======================
   file arc_test.mos
   `````````````````
   Using the package 'arcpkg'.
   *** Compile arcpkg.mos before running this model ****
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2007, rev. June 2018
*******************************************************!)
model "Arcs"
 uses "arcpkg"
 declarations
  NODES: set of string                  ! Set of nodes
  ARC: array(ARCSET:range) of arc       ! Arcs
 end-declarations
 initializations from 'arcs.dat'
  ARC
 end-initializations
! Calculate the set of nodes
 NODES:=union(a in ARCSET) {ARC(a).Source, ARC(a).Sink}
 writeln(NODES)
 writeln("Average arc cost: ", sum(a in ARCSET) ARC(a).Cost / getsize(ARCSET) )
 writeln("Neighbors A and F: ", is_neighbor("A","F",ARC))
 writeln("Neighbors A and B: ", is_neighbor("A","B",ARC))
end-model 
 | 
| parpkg.mos | 
| (!******************************************************
   Mosel User Guide Example Problems
   ================================= 
   file parpkg.mos
   ```````````````
   Definition of package parameters.
   
   (c) 2018 Fair Isaac Corporation
       author: Y. Colombani, May 2018
*******************************************************!)
package parpkg
 ! Specify parameter names and types
 parameters
  "p1":real
  "p2":integer
  "p3":string
  "p4":boolean
 end-parameters
 ! Entities for storing current parameter values
 declarations
  myp1: real
  myp2: integer
  myp3: string
  myp4: boolean
 end-declarations
 ! Get value of a real parameter
 public function parpkg~getrparam(p:string):real
  case p of
   "p1": returned:=myp1
  end-case
 end-function
 ! Get value of an integer parameter
 public function parpkg~getiparam(p:string):integer
  case p of
   "p2": returned:=myp2
  end-case
 end-function
 ! Get value of a string parameter
 public function parpkg~getsparam(p:string):string
  case p of
   "p3": returned:=myp3
  end-case
 end-function
 ! Get value of a boolean parameter
 public function parpkg~getbparam(p:string):boolean
  case p of
   "p4": returned:=myp4
  end-case
 end-function
 ! Set value for real parameters
 public procedure parpkg~setparam(p:string,v:real)
  case p of
   "p1": myp1:=v
  end-case
 end-procedure
 ! Set value for integer parameters
 public procedure parpkg~setparam(p:string,v:integer)
  case p of
   "p2": myp2:=v
  end-case
 end-procedure
 ! Set value for string parameters
 public procedure parpkg~setparam(p:string,v:string)
  case p of
   "p3": myp3:=v
  end-case
 end-procedure
 ! Set value procedure for boolean parameters
 public procedure parpkg~setparam(p:string,v:boolean)
  case p of
   "p4": myp4:=v
  end-case
 end-procedure
 ! Set default values for parameters
 myp1:=0.25
 myp2:=10
 myp3:="default"
 myp4:=true
end-package
 | 
| params_test.mos | 
| (!******************************************************
   Mosel User Guide Example Problems
   ================================= 
   file params_test.mos
   ````````````````````
   Working with package parameters.
   *** Compile parpkg.mos before running this model ****
   
   (c) 2018 Fair Isaac Corporation
       author: Y. Colombani, May 2018
*******************************************************!)
model "Packages with parameters"
 uses 'parpkg'
 ! Display default parameter values
 writeln("Default values:", 
   " p1=", getparam("parpkg.P1"), " p2=", getparam("P2"), 
   " p3=", getparam("parpkg.p3"), " p4=", getparam("p4"))
 ! Change values
 setparam("p1",133); setparam("parpkg.p2",-77)
 setparam("P3","tluafed"); setparam("parpkg.P4",not getparam("parpkg.P4"))
end-model
 | 
© 2001-2021 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.
 
