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

Transport - data formats for online use

Description
  • transport_html.mos: Solution output in HTML format, using XML functionality or as text
  • transport_xml.mos: Reading and writing data in XML format (requires transprt.xml)
  • transport_json.mos: Reading and writing data in JSON format (requires transprt.json)
Further explanation of this example: 'Mosel User Guide', Section 17.3 Graphics and GUIs

transportdata.zip[download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
transport_html.mos[download]
transport_xml.mos[download]
transport_json.mos[download]

Data Files





transport_json.mos

(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file transport_json.mos
   ```````````````````````
   Reading and writing data in JSON format.
   
   (c) 2014 Fair Isaac Corporation
       author: S.Heipcke, Oct. 2014
*******************************************************!)

model "Transport (JSON)"
 uses "mmxprs", "mmxml"

 declarations
  REGION: set of string                 ! Set of customer regions
  PLANT: set of string                  ! Set of plants

  DEMAND: array(REGION) of real         ! Demand at regions
  PLANTCAP: array(PLANT) of real        ! Production capacity at plants
  PLANTCOST: array(PLANT) of real       ! Unit production cost at plants
  TRANSCAP: dynamic array(PLANT,REGION) of real
                                        ! Capacity on each route plant->region
  DISTANCE: dynamic array(PLANT,REGION) of real
                                        ! Distance of each route plant->region
  FUELCOST: real                        ! Fuel cost per unit distance

  MaxCap: array(PLANT) of linctr        ! Capacity constraints
  flow: dynamic array(PLANT,REGION) of mpvar    ! Flow on each route

  AllData: xmldoc                       ! XML document
  NodeList: list of integer             ! List of XML nodes
 end-declarations
 

!**** Reading data from an XML file ****
 jsonload(AllData, "transprt.json")     ! Load the entire document
! save(AllData,"")

 getnodes(AllData, "jsv/demand/*", NodeList) 
 forall(l in NodeList)
   DEMAND(getname(AllData,l)):= getrealvalue(AllData, l) 

 getnodes(AllData, "jsv/plantdata/*", NodeList) 
 forall(l in NodeList) do
   PLANTCAP(getname(AllData,l)):= 
     getrealvalue(AllData, getnode(AllData,l,"capacity"))    
   PLANTCOST(getname(AllData,l)):= 
     getrealvalue(AllData, getnode(AllData,l,"cost"))    
 end-do

 getnodes(AllData, "jsv/routes/route", NodeList) 
 forall(l in NodeList) do
   DISTANCE(getstrvalue(AllData,getnode(AllData,l,"from")),
            getstrvalue(AllData,getnode(AllData,l,"to"))):=
     getrealvalue(AllData,getnode(AllData,l,"distance"))
   TRANSCAP(getstrvalue(AllData,getnode(AllData,l,"from")),
            getstrvalue(AllData,getnode(AllData,l,"to"))):=
     getrealvalue(AllData,getnode(AllData,l,"capacity"))
 end-do

 FUELCOST:= getrealvalue(AllData, getnode(AllData, "jsv/fuelcost"))

!**** Problem statement + solving ****

! Create the flow variables that exist
 forall(p in PLANT, r in REGION | exists(TRANSCAP(p,r)) ) create(flow(p,r))
 
! Objective: minimize total cost
 MinCost:= sum(p in PLANT, r in REGION | exists(flow(p,r))) 
            (FUELCOST * DISTANCE(p,r) + PLANTCOST(p)) * flow(p,r)
 
! Limits on plant capacity
 forall(p in PLANT) MaxCap(p):= sum(r in REGION) flow(p,r) <= PLANTCAP(p)

! Satisfy all demands
 forall(r in REGION) sum(p in PLANT) flow(p,r) = DEMAND(r)
 
! Bounds on flows
 forall(p in PLANT, r in REGION | exists(flow(p,r))) 
  flow(p,r) <= TRANSCAP(p,r)

 minimize(MinCost)                       ! Solve the problem

!**** Create solution representation in XML format ****
 declarations
  ResData: xmldoc                        ! XML document
  Sol,Plant,Node,Total: integer          ! XML nodes
 end-declarations

 Sol:=addnode(ResData, 0, XML_ELT, "jsv")     ! Create root node "jsv"
 Node:=addnode(ResData, Sol, "Objective", MinCost.sol)  ! Obj. value as node
 Node:=addnode(ResData, Sol, "RunDate", text(datetime(SYS_NOW)))

 forall(p in PLANT) do
   Plant:=addnode(ResData, Sol, XML_ELT, p)   ! Add a node to "solution"
   forall(r in REGION | flow(p,r).sol>0)
     Node:=addnode(ResData, Plant, r, flow(p,r).sol) ! Add a node to "plant"
   Total:=addnode(ResData, Plant, "total", 
                  sum(r in REGION)flow(p,r).sol)  ! Add node with total flow
 end-do
 
 jsonsave(ResData, "transportres.json")  ! Save solution to JSON format file
 jsonsave(ResData, Sol, "")              ! Display JSON format sol. on screen

end-model

Back to examples browserPrevious exampleNext example