| |||||||||||||||
Transport - data formats for online use Description
Source Files By clicking on a file name, a preview is opened at the bottom of this page.
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 | |||||||||||||||
© Copyright 2024 Fair Isaac Corporation. |