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





runfoliodistr.mos

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

   file runfoliodistr.mos
   ``````````````````````
   Master model running portfolio optimization model
   on a remote Mosel instance.

   Runs model foliomemio.mos.
 
   *** ATTENTION: This model will return an error if ***
   *** no more than one Xpress licence is available. ***
      
  (c) 2010 Fair Isaac Corporation
      author: S.Heipcke, July 2010
*******************************************************!)

model "Run portfolio optimization model remotely"
 uses "mmjobs"                       ! Use multiple model handling
 uses "mmsystem", "mmxprs"

 parameters
  MODELFILE = "foliomemio"           ! Optimization model
  INPUTFILE = "folio10.dat"          ! File with problem data
  OUTPUTFILE = "solout.dat"          ! File for solution output
 
  MAXRISK = 1/3                      ! Max. investment into high-risk values
  MINREG = 0.2                       ! Min. investment per geogr. region
  MAXREG = 0.5                       ! Max. investment per geogr. region
  MAXSEC = 0.25                      ! Max. investment per ind. sector
  MAXVAL = 0.2                       ! Max. investment per share
  MINVAL = 0.1                       ! Min. investment per share
  MAXNUM = 15                        ! Max. number of different assets
 end-parameters
 
 forward procedure write_html_results

 declarations
  SHARES: set of string              ! Set of shares
  returnsol: real                    ! Solution values
  numsharessol,status: integer
  fracsol: dynamic array(SHARES) of real  ! Fraction of capital used per share
  buysol: dynamic array(SHARES) of real   ! 1 if asset is in portfolio, 0 otherwise

  foliomod: Model                    ! Mosel model
  moselinst: Mosel                   ! Mosel instance
 end-declarations

! Compile the optimization model locally
 if compile(MODELFILE+".mos") <> 0 then
  writeln("Error during model compilation")
  exit(1)
 end-if

! Start a remote Mosel instance:
! "" stands for the node running this model; try IP addresses or host names
 if connect(moselinst, "")<>0 then exit(2); end-if

! Load the optimization model into the remote instance
 load(moselinst, foliomod, "rmt:"+MODELFILE+".bim")
 fdelete(MODELFILE+".bim")


 run(foliomod, "MAXRISK=" + MAXRISK + ",MINREG=" + MINREG + 
  ",MAXREG=" + MAXREG + ",MAXSEC=" + MAXSEC +
  ",MAXVAL=" + MAXVAL + ",MINVAL=" + MINVAL +
  ",MAXNUM=" + MAXNUM + ",DATAFILE='rmt:" + INPUTFILE +
  "',OUTPUTFILE='rmt:" + OUTPUTFILE + "'")
  wait                              ! Wait for model termination
  dropnextevent                     ! Ignore termination event message

 initializations from OUTPUTFILE
  returnsol as 'RETSOL'
  numsharessol as 'NUMSHARES'
  fracsol as 'FRAC'
  buysol as 'BUY'
  status as 'SOLSTATUS'
 end-initializations

 case status of
  XPRS_OPT: writeln("Problem solved to optimality")
  XPRS_UNF: writeln("Problem solving unfinished")
  XPRS_INF: writeln("Problem is infeasible")
  XPRS_UNB,XPRS_OTH:  writeln("No solution available")
 end-case 

 ! Solution printing
 writeln("Total return: ", returnsol)
 writeln("Number of shares: ", numsharessol)
 forall(s in SHARES | fracsol(s)>0)
  writeln(s, ": ", fracsol(s)*100, "% (", buysol(s), ")")
 
 write_html_results

! *********** Writing an HTML result file ***********
 procedure write_html_results
  setparam("datetimefmt", "%0d-%N-%y, %0H:%0M:%0S")

  HTMLFILE:= INPUTFILE + "_sol.html"
  fopen(HTMLFILE, F_OUTPUT)
  writeln("<html>")
  writeln("<head>")
  writeln("<style type='text/css'>")
  writeln("body {font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; color: 000055 }")
  writeln("table td {background-color: ffffaa; text-align: left }")
  writeln("table th {background-color: 053055; color: ffcc88}")
 writeln("</style>")
  writeln("</head>")

  writeln("<body>")
  writeln("<center><h2>Portfolio Optimization Results</h2></center>")
  writeln("<table width='100%' cellpadding='5' cellspacing='0' border=0>")
  writeln("<tr><td width='55%'><font color='#000055'><b>Total return: ",
          returnsol, "</b></font></td><td><font color='#885533'><b>Problem instance: ",
          INPUTFILE,"</b></font></td></tr>")
  writeln("<tr><td><font color='#000055'><b>Number of shares: ", numsharessol, "</b></font></td><td><font color='#885533'><b>Date: ", datetime(SYS_NOW),"</b></font></td></tr>")
  writeln("<tr><td colspan='2'>&nbsp;</td></tr>")
  writeln("</table>")

  writeln("<table cellpadding='2' cellspacing='1' width='100%'>")
  writeln("<tr><th>Value</th><th>Percentage</th></tr>")
 forall(s in SHARES | fracsol(s)>0) 
   writeln("<tr><td>", s, "</td><td>", strfmt(fracsol(s)*100,4,2), 
           "%</td></tr>")
  writeln("</table>")
  writeln("</body>")
  writeln("</html>")
  fclose(F_OUTPUT)
 end-procedure

end-model

Back to examples browserPrevious exampleNext example