Initializing help system before first use

Burglar - Data source access from Mosel models


Type: Knapsack Problem
Rating: 2 (easy-medium)
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
File(s): burglar.mos, burglari.mos, burglar2.mos, burglar2o.mos, burglar2sql.mos, burglar2e.mos, burglar2dd.mos, burglar2ff.mos, burglar2x.mos, burglar2j.mos
Data file(s): burglar.dat, burglardd.dat, burglarff.dat, burglar.xml, burglar.json, burglar.mdb, burglar.xls, burglar.sqlite


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

   file burglar.mos
   ````````````````
   Small MIP problem.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2001
*******************************************************!)

model Burglar 
 uses "mmxprs"
  
 declarations
  WTMAX = 102                    ! Maximum weight allowed
  ITEMS = 1..8                   ! Index range for items
  
  VALUE: array(ITEMS) of real    ! Value of items
  WEIGHT: array(ITEMS) of real   ! Weight of items
  
  take: array(ITEMS) of mpvar    ! 1 if we take item i; 0 otherwise
 end-declarations

! Item:      1   2   3   4   5   6   7   8
  VALUE :: [15, 100, 90, 60, 40, 15, 10,  1]
  WEIGHT:: [ 2,  20, 20, 30, 40, 30, 60, 10]

! 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

! Print out the solution
 writeln("Solution:\n Objective: ", getobjval)
 forall(i in ITEMS)  writeln(" take(", i, "): ", getsol(take(i)))
end-model

burglari.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglari.mos
   `````````````````
   Use of index sets.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. 2006
*******************************************************!)

model Burglari
 uses "mmxprs"
 
 declarations
  WTMAX = 102                    ! Maximum weight allowed
  ITEMS = {"camera", "necklace", "vase", "picture", "tv", "video", 
           "chest", "brick"}     ! Index set for items
  
  VALUE: array(ITEMS) of real    ! Value of items
  WEIGHT: array(ITEMS) of real   ! Weight of items
  
  take: array(ITEMS) of mpvar    ! 1 if we take item i; 0 otherwise
 end-declarations

 VALUE::(["camera", "necklace", "vase", "picture", "tv", "video", 
          "chest", "brick"])[15,100,90,60,40,15,10,1]
 WEIGHT::(["camera", "necklace", "vase", "picture", "tv", "video", 
           "chest", "brick"])[2,20,20,30,40,30,60,10]

! 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

! Print out the solution
 writeln("Solution:\n Objective: ", getobjval)
 forall(i in ITEMS)  writeln(" take(", i, "): ", getsol(take(i)))

! Deliberately introduced error to test redirection of error stream
 writeln(getsol(take("t")))
end-model

burglar2.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2.mos
   `````````````````
   Data read from file.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2001
*******************************************************!)

model Burglar2
 uses "mmxprs"
 
 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
 end-declarations

 initializations from "burglar.dat"
  [VALUE,WEIGHT] as "BurgData"
 end-initializations

 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)

 initializations to "burglarout.txt"
  SOLTAKE
 end-initializations

end-model

burglar2o.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2o.mos
   ``````````````````
   Use of ODBC driver for data handling,
   or alternatively, use of SQL statements.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2004, rev. July 2017
*******************************************************!)

model "Burglar2 (ODBC)"
 uses "mmxprs", "mmodbc"
 
 parameters
                          ! Windows only: Use Access database `burglar.mdb'
!  CNCT = 'DSN=MS Access Database;DBQ=burglar.mdb'
!  CNCTIO = "mmodbc.odbc:debug;burglar.mdb"
                          ! Use mysql database `burglar' (not provided)
!  CNCT = 'DSN=mysql;DB=burglar'
!  CNCTIO = "mmodbc.odbc:debug;DSN=mysql;DB=burglar"
                          ! Use SQLite database `burglar.sqlite'
  CNCT = 'burglar.sqlite'
  CNCTIO = "mmodbc.odbc:debug;burglar.sqlite"
 end-parameters
 
 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
  SOLTAKE: array(ITEMS) of real  ! Solution values
 end-declarations

 initializations from CNCTIO
  [VALUE,WEIGHT] as "BurgData"
 end-initializations

(! Alternatively:
 SQLconnect(CNCT)
 SQLexecute("select ITEM,VALUE,WEIGHT from BurgData", [VALUE,WEIGHT])
 SQLdisconnect
!)

 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 problem

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

! Insert solutions into database/spreadsheet: results from previous runs
! must be removed previously; otherwise the new results will either be 
! appended to the existing ones or, if "ITEM" has been defined as key field 
! in a database, the insertion will fail.
 initializations to CNCTIO
  SOLTAKE as "SolTake"
 end-initializations

(! Alternatively:
 SQLconnect(CNCT)
 SQLexecute("delete from SolTake")    ! Cleaning up previous results: works
                                      ! only for databases, cannot be used
                                      ! with spreadsheets (instead, delete
                                      ! previous solutions directly in the
                                      ! spreadsheet file)
 SQLexecute("insert into SolTake (ITEM,TAKE) values (?,?)" , SOLTAKE) 
 SQLdisconnect
!) 

end-model

burglar2sql.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2sql.mos
   ````````````````````
   Data read from SQLite DB.
   
   (c) 2014 Fair Isaac Corporation
       author: L. Varghese, Oct. 2014, rev. Aug. 2023
*******************************************************!)

model Burglar2sql
 uses "mmxprs"
 uses "mmodbc"
 
 parameters
 ! SQLite database:
  CNCT="burglar.sqlite"
 end-parameters
 
 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
 end-declarations

! Reading data from SQLite DB
 SQLconnect(CNCT)
 if not getparam("SQLsuccess"): setioerr("Database connection failed")
 SQLexecute("select * from BurgData", [VALUE, WEIGHT])
 SQLdisconnect	

(! Alternatively:
 initializations from "mmodbc.odbc:" + CNCT
  [VALUE,WEIGHT] as "BurgData"
 end-initializations
!)

 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)

 SQLconnect(CNCT)
 SQLexecute("delete from SolTake")
 SQLexecute("insert into SolTake values (?,?)", SOLTAKE)
 SQLdisconnect

(! Alternatively:
 initializations to "mmodbc.odbc:" + CNCT
  SOLTAKE as "SolTake"
 end-initializations
!)

end-model

burglar2e.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2e.mos
   ``````````````````
   Use of spreadsheet I/O drivers for data handling.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Feb. 2007, rev. Dec. 2012
*******************************************************!)

model "Burglar2 (Excel)"
 uses "mmxprs", "mmsheet"
 
 parameters
!  CNCTEXC = "mmsheet.excel:burglar.xls"      ! Windows only
  CNCTEXC = "mmsheet.xls:burglar.xls"        ! Windows and Linux
 end-parameters
 
 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
  SOLTAKE: array(ITEMS) of real  ! Solution values
 end-declarations

! Spreadsheet range includes header line -> use option 'skiph'
 initializations from CNCTEXC
  [VALUE,WEIGHT] as "skiph;BurgData"
 end-initializations
!)
(! Alternatively:
 initializations from CNCTEXC
  [VALUE,WEIGHT] as "[burglar$B4:D11]"
 end-initializations
!)

 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 problem

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

! Insert solutions into spreadsheet: results from previous runs
! are overwritten by new output
! (only first line of output range is specified -> use option 'grow')
 initializations to CNCTEXC
  SOLTAKE as "skiph;grow;SolTake"
 end-initializations
!)
(! Alternatively:
 initializations to CNCTEXC
  SOLTAKE as "grow;[burglar$F4:G4]"
 end-initializations
!)

(! Or:
 initializations to CNCTEXC
  SOLTAKE as "[burglar$F4:G11]"
 end-initializations
!)
end-model

burglar2dd.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2dd.mos
   ```````````````````
   Use of "diskdata" driver for data handling.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2004
*******************************************************!)

model Burglar2dd
 uses "mmxprs", "mmetc"
 
 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
  SOLTAKE: array(ITEMS) of real  ! Solution values
 end-declarations

 initializations from "mmetc.diskdata:sparse;noq"
  [VALUE,WEIGHT] as "burglardd.dat"
 end-initializations

! Alternatively:
! diskdata(ETC_SPARSE+ETC_NOQ, "burglardd.dat", [VALUE,WEIGHT])

 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 problem

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

 initializations to "mmetc.diskdata:append,sparse"
  SOLTAKE as "burglarout.txt"
 end-initializations

! Alternatively:
! diskdata(ETC_OUT+ETC_APPEND+ETC_SPARSE, "burglarout.txt", SOLTAKE)

end-model

burglar2ff.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2ff.mos
   ```````````````````
   Data read from free format file.
   Duplicate output to free format file + on screen.
   
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2013
*******************************************************!)

model Burglar2ff
 uses "mmxprs"
 
 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
  j: string
 end-declarations

 fopen("burglarff.dat", F_INPUT) ! Open file for reading
 while (not iseof) do            ! Read up to end-of-file
   fskipline("!")                ! Skip lines starting with '!'
   readln(j, " [v=", VALUE(j), ", w=", WEIGHT(j), "]")
   if getparam("nbread") < 6 then
     writeln("Error reading data for index '", j, "'")
   end-if  
 end-do
 fclose(F_INPUT)                 ! Close the input file

 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

! Print out the solution and redirect it to a file
 fopen("tee:burglarsol.txt&", F_OUTPUT+F_APPEND)
 writeln("Solution:\n Objective: ", getobjval)
 forall(i in ITEMS)  writeln(" take(", i, "): ", getsol(take(i)))
 fclose(F_OUTPUT)
 
end-model

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

burglar2j.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2j.mos
   ``````````````````
   Data read from JSON file.
   
   (c) 2014 Fair Isaac Corporation
       author: S. Heipcke, Sep. 2014
*******************************************************!)

model "Burglar2 JSON"
 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 a JSON file
 jsonload(BurgData, "burglar.json")

! Retrieve all 'Item' nodes 
 getnodes(BurgData, "jsv/Item/jsv", NodeList) 
 
! Retrieve 'Value' and 'Weight' information 
 forall(i in NodeList) do
   VALUE(getstrvalue(BurgData, getnode(BurgData,i,"Name"))):=
     getrealvalue(BurgData, getnode(BurgData, i, "Value") )
   WEIGHT(getstrvalue(BurgData, getnode(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 JSON format
 Root:=addnode(ResData, 0, XML_ELT, "jsv")         ! Create root node "jsv"
 Node:=addnode(ResData, Root, "Objective", getobjval) ! Add a node to root
 Node:=addnode(ResData, Root, XML_ELT, "Items")    ! Add a node to root
 forall(i in ITEMS)
   n:=addnode(ResData, Node, i, SOLTAKE(i))        ! Add a node to "Items"

 setindentmode(ResData, XML_NONE)      ! Output file without whitespace
 jsonsave(ResData, "burglarout.json")  ! Save solution to JSON format file
 setindentmode(ResData, XML_AUTO)      ! Use automatic indentation
 save(ResData, Root, "")               ! Display XML format solution on screen
 jsonsave(ResData, Root, "")           ! Display JSON format solution on screen
 
end-model

© 2001-2024 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.