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

Folio - Advanced modelling and solving tasks

Description
Advanced modelling and solving tasks for a portfolio optimization problem:
  • Automated solver tuning (foliolptune.mos)
  • Defining an integer solution callback (foliocb.mos)
  • Using the solution enumerator for multiple MIP solutions (folioenumsol.mos)
  • Handling infeasibility
    • handling infeasibility through deviation variables (folioinfeas.mos)
    • retrieving infeasible row/column from presolve (folioinfcause.mos)
    • retrieving IIS - LP or MIP infeasible (folioiis.mos, foliomiis.mos)
    • using the built-in infeasibility repair functionality (foliorep.mos)
    • same as foliorep, using an 'mpsol' object (foliorep_sol.mos)
  • Data transfer in memory
    • running foliomemio.mos with data transfer in memory (runfolio.mos)
    • same running foliomemio2.mos, grouping tables with identical index sets in "initializations" blocks (runfolio2.mos)
    • master model running several model instances in parallel (runfoliopar.mos)
  • Remote models on a distributed architecture
    • running foliomemio.mos on a remote instance of Mosel (runfoliodistr.mos)
    • master model running several model instances in parallel, each on a different (remote) instance of Mosel (runfoliopardistr.mos)
  • Remote execution via XPRD
    • See examples in the Mosel Whitepapers directory moselpar/XPRD
  • XML and JSON data formats
    • reading data from an XML file, solution output in XML format on screen and to a new file (folioxml.mos, folioxmlqp.mos)
    • generate HTML output file as an XML document (runfolioxml.mos)
    • using JSON-format data files, reading data from a JSON file, solution output in JSON format on screen and to a new file (foliojson.mos)
  • HTTP
    • starting an HTTP server managing requests from HTTP clients (foliohttpsrv.mos)
    • HTTP client exchanging XML data files with an HTTP server (foliohttpclient.mos)


Source Files

Data Files





folioxml.mos

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

   file folioxml.mos
   `````````````````
   Modeling a small LP problem 
   to perform portfolio optimization.
   -- Reading/writing data in XML format --
   
  (c) 2010 Fair Isaac Corporation
      author: S.Heipcke, July 2010, rev. July 2013
*******************************************************!)

model "Portfolio optimization with LP"
 uses "mmxprs"
 uses "mmxml"                        ! XML interface functions

 parameters
  DATAFILE= "folio.xml"              ! File with problem data
  OUTFILE= "result.xml"              ! Output file
  MAXRISK = 1/3                      ! Max. investment into high-risk values
  MAXVAL = 0.3                       ! Max. investment per share
  MINAM = 0.5                        ! Min. investment into N.-American values
 end-parameters

 declarations
  SHARES: set of string              ! Set of shares
  RISK: set of string                ! Set of high-risk values among shares
  NA: set of string                  ! Set of shares issued in N.-America
  RET: array(SHARES) of real         ! Estimated return in investment

  AllData, ResData: xmldoc           ! XML document
  Share,Root,Sol: integer            ! XML nodes
  NodeList: list of integer
 end-declarations

! Reading data from an XML file
 load(AllData, DATAFILE)

 getnodes(AllData, "portfolio/share", NodeList) 
 RISK:= union(l in NodeList | getattr(AllData,l,"risk")="high") 
         {getstrattr(AllData,l,"name")} 
 NA:= union(l in NodeList | getattr(AllData,l,"region")="NA") 
         {getstrattr(AllData,l,"name")} 
 
(! Alternatively:
 getnodes(AllData, "portfolio/share[@risk='high']/attribute::name", NodeList)
 RISK:=union(r in NodeList) {getstrvalue(AllData,r)}
 getnodes(AllData, "portfolio/share[@region='NA']/attribute::name", NodeList)
 NA:=union(r in NodeList) {getstrvalue(AllData,r)}
 getnodes(AllData, "portfolio/share", NodeList) 
!)

 forall(l in NodeList)
   RET(getstrattr(AllData,l,"name")):= getintattr(AllData, l, "ret") 


 declarations
  frac: array(SHARES) of mpvar       ! Fraction of capital used per share
 end-declarations

! Objective: total return
 Return:= sum(s in SHARES) RET(s)*frac(s) 

! Limit the percentage of high-risk values
 sum(s in RISK) frac(s) <= MAXRISK

! Minimum amount of North-American values
 sum(s in NA) frac(s) >= MINAM

! Spend all the capital
 sum(s in SHARES) frac(s) = 1
 
! Upper bounds on the investment per share
 forall(s in SHARES) frac(s) <= MAXVAL

! Solve the problem
 maximize(Return)

! Solution printing
 writeln("Total return: ", getobjval)
 forall(s in SHARES) 
  writeln(strfmt(s,-12), ": \t", strfmt(getsol(frac(s))*100,5,2), "%")


! Create solution representation in XML format
 Root:=addnode(ResData, 0, XML_ELT, "result")    ! Create root node "result"
 Sol:= addnode(ResData, Root, XML_ELT, "solution") ! Add a "solution" node
 setattr(ResData, Sol, "value", getobjval)       ! ... with attribute "value"
 forall(s in SHARES) do
   Share:=addnode(ResData, Sol, XML_ELT,"share") ! Add a node to "solution"
   setattr(ResData, Share, "name", s)            ! ... with attribute "name"
   setvalue(ResData, Share, frac(s).sol)         ! ... and solution value
 end-do
 
 save(ResData, OUTFILE)        ! Save solution to XML format file
 save(ResData, Sol, "")        ! Display XML format solution on screen

end-model 

Back to examples browserPrevious exampleNext example