Initializing help system before first use

Definition of a network


Type: Programming
Rating: 3 (intermediate)
Description:
  • definition of a data structure to represent arcs in a network with source, sink, and other properties
  • accessing the different record fields
  • initialization of records with data from a text file
  • selection of record fields in initializations (arcs2.mos)
  • separate initialization of record fields (arcs3.mos)
  • sets and lists of records, using type constructors (arcs4.mos)
  • constant record definition, sets of constant records (arcs5.mos)
File(s): arcs.mos, arcs2.mos, arcs3.mos, arcs4.mos, arcs5.mos
Data file(s): arcs.dat


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

   file arcs.mos
   `````````````
   Working with records: definition of a network.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Nov. 2006
*******************************************************!)

model "Arcs"

 declarations
  NODES: set of string                  ! Set of nodes
  ARC: array(ARCSET:range) of record    ! Arcs:
   Source,Sink: string                  !   Source and sink of arc
   Cost: real                           !   Cost coefficient
  end-record 
 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) )

end-model 

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

   file arcs2.mos
   ``````````````
   Working with records: definition of a network.
   - Selecting the record fields in initializations -
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Sep. 2007
*******************************************************!)

model "Arcs"

 declarations
  NODES: set of string                  ! Set of nodes
  ARC: array(ARCSET:range) of record    ! Arcs:
   flow: mpvar                          !   Flow quantity
   Source,Sink: string                  !   Source and sink of arc
   Cost: real                           !   Cost coefficient
  end-record 
 end-declarations

 initializations from 'arcs.dat'
  ARC(Source,Sink,Cost)
 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) )

end-model 

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

   file arcs3.mos
   ``````````````
   Working with records: definition of a network.
   - Reading the record fields from different sources -
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Sep. 2007
*******************************************************!)

model "Arcs"

 declarations
  NODES: set of string                  ! Set of nodes
  ARC: array(ARCSET:range) of record    ! Arcs:
   flow: mpvar                          !   Flow quantity
   Source,Sink: string                  !   Source and sink of arc
   Cost: real                           !   Cost coefficient
  end-record 
 end-declarations

 initializations from 'arcs.dat'
  ARC(Source,Sink) as "ARCDEF" 
  ARC(Cost) as "COST"
 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) )

end-model 

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

   file arcs4.mos
   ``````````````
   Working with records.
   - Sets and lists of records -
   - Using type constructors -
   
   (c) 2011 Fair Isaac Corporation
       author: S. Heipcke, May 2011, rev. Sep. 2018
*******************************************************!)

model "Arcs"

 public declarations
  NODES: list of string                 ! List of nodes
  arc = public record                   ! Arcs:
   Source,Sink: string                  !   Source and sink of arc
   Cost: real                           !   Cost coefficient
  end-record 
  ARCS: set of arc  
  ARCLIST: list of arc  
 end-declarations

 function newarc(s,t:string, c:real): arc
  returned.Source:=s
  returned.Sink:=t
  returned.Cost:=c
 end-function

 NODES:=['A','B','C','D','E','F']

 forall(j in NODES) do
  ARCS += {newarc("","",0)}   ! Creates several (distinct) set elements of the same contents
! Same as:
!  ARCS += {arc(.Cost:=0)}
 end-do 
 
 writeln(ARCS)

 i:='S'; ct:=0
 forall(j in NODES, ct as counter) do
  ARCLIST+= [arc(.Source:=i,.Sink:=j,.Cost:=10*ct)]
! Same as:
! ARCLIST += {newarc(i,j,10*ct)}
  i:=j
 end-do 
 
 writeln(ARCLIST)

 ! Initializing selected record fields
 writeln(arc(.Source:="S"))
 writeln(arc(.Sink:="Z"))
 writeln(arc(.Cost:=100,.Source:="XYZ"))

end-model 

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

   file arcs5.mos
   ``````````````
   Working with records.
   - Sets of constant records -
   
   (c) 2020 Fair Isaac Corporation
       author: S. Heipcke, Apr. 2020
*******************************************************!)

model "Arcs constants"

 public declarations
  NODES: list of string                 ! List of nodes
  arc = public record                   ! Arcs:
   Source,Sink: string                  !   Source and sink of arc
   Cost: real                           !   Cost coefficient
  end-record 
  ARCS: set of arc  
!!! Only records with scalar fields of basic types can be declared as constants
  ARCSC: set of constant arc  
  myarc=arc(.Source:='S',.Sink:='A',.Cost:=10)    ! A constant record  
 end-declarations

 function newarc(s,t:string, c:real): arc
  returned.Source:=s
  returned.Sink:=t
  returned.Cost:=c
 end-function

 NODES:=['A','B','C','D','E','F']

 forall(j in NODES) do
  ARCS += {newarc("","",0)}      ! Add several elements with the same contents
  ARCSC += {newarc("","",0)}     ! Add repeatedly the same set element
! Same as:
  ARCSC += {arc(.Cost:=0)}
 end-do 
 
 writeln("ARCS =", ARCS, " size=", ARCS.size)
 writeln("ARCSC=", ARCSC, " size=", ARCSC.size)

 i:='S'; ct:=0
 forall(j in NODES, ct as counter) do
  ARCS+= {arc(.Source:=i,.Sink:=j,.Cost:=10*ct)}
  ARCSC+= {arc(.Source:=i,.Sink:=j,.Cost:=10*ct)}
! Same as:
  ARCSC += {newarc(i,j,10*ct)}
  i:=j
 end-do 
 
 writeln("myarc=", myarc)
 writeln("ARCS =", ARCS, " size=", ARCS.size)
 writeln("myarc is in ARCS: ", myarc in ARCS)

 writeln("ARCSC=", ARCSC, " size=", ARCSC.size)
 writeln("myarc is in ARCS: ", myarc in ARCSC)

end-model