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

Burglar - Data source access from Mosel models

Description
  • burglar.mos - data in the model, integer indices
  • burglari.mos - data in the model, string indices
  • burglar2.mos, burglar.dat - reading data from a text file
  • burglar2o.mos, burglar.mdb, burglar.sqlite - reading data with mmodbc.odbc
  • burglar2sql.mos, burglar.sqlite - reading data from SQLite, using SQL or ODBC
  • burglar2e.mos, burglar.xls - reading data with mmsheet.excel (Windows only)
  • burglar2dd.mos, burglardd.dat - reading data with mmetc.diskdata
  • burglar2ff.mos, burglarff.dat - reading data in free format
  • burglar2x.mos, burglar.xml - reading data in XML format
  • burglar2j.mos, burglar.json - reading data in JSON format
Further explanation of this example: Whitepaper 'Generalized file handling in Mosel', Sections 3 Example problem and 4 Data source access from Mosel models

burgdatamos.zip[download all files]

Source Files

Data Files





burglar2x.mos

(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2x.mos
   ``````````````````
   Data read from XML file.
   
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Apr. 2013, rev. Sep. 2014
*******************************************************!)

model "Burglar2 XML"
 uses "mmxprs", "mmxml"
 
 declarations
  WTMAX = 102                    ! Maximum weight allowed
  ITEMS: set of string           ! Index set for items
  VALUE: array(ITEMS) of real    ! Value of items
  WEIGHT: array(ITEMS) of real   ! Weight of items
  
  BurgData, ResData: xmldoc      ! XML document
  Root, Node: integer            ! XML nodes
  NodeList: list of integer
 end-declarations

! Reading data from an XML file
 load(BurgData, "burglar.xml")

! Retrieve all 'Item' nodes
 getnodes(BurgData, "BurgData/Item", NodeList) 
 
! Retrieve 'Value' and 'Weight' information
 forall(i in NodeList) do
   VALUE(getstrattr(BurgData,i,"name")):=
     getrealvalue(BurgData, getnode(BurgData, i, "Value") )
   WEIGHT(getstrattr(BurgData,i,"name")):=
     getrealvalue(BurgData, getnode(BurgData, i, "Weight") )
 end-do
 
 declarations
  take: array(ITEMS) of mpvar    ! 1 if we take item i; 0 otherwise
 end-declarations

! Objective: maximize total value
 MaxVal:= sum(i in ITEMS) VALUE(i)*take(i) 

! Weight restriction
 sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX

! All variables are 0/1
 forall(i in ITEMS) take(i) is_binary  

 maximize(MaxVal)                 ! Solve the MIP-problem

! Solution output
 forall(i in ITEMS) SOLTAKE(i):= getsol(take(i))

 writeln("Solution:\n Objective: ", getobjval)
 writeln(SOLTAKE)

! Create solution representation in XML format
 Root:=addnode(ResData, 0, XML_ELT, "SolTake")    ! Create root node "SolTake"
 setattr(ResData, Root, "objective", getobjval)   ! ... with attr. "objective"

 forall(i in ITEMS) do
   Node:=addnode(ResData, Root, XML_ELT, "Item") ! Add a node to "SolTake"
   setattr(ResData, Node, "name", i)             ! ... with attribute "name"
   setvalue(ResData, Node, SOLTAKE(i))           ! ... and solution value
 end-do

 save(ResData, "burglarout.xml")    ! Save solution to XML format file
 save(ResData, Root, "")            ! Display XML format solution on screen
 
end-model

Back to examples browserPrevious exampleNext example