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

Folio - Examples from 'Getting Started'

Description
Different versions of a portfolio optimization problem.

Basic modelling and solving tasks:
  • modeling and solving a small LP problem (foliolp)
  • performing explicit initialization (folioinit)
  • data input from file, index sets (foliodata, requires foliocpplp.dat)
  • modeling and solving a small MIP problem with binary variables (foliomip1)
  • modeling and solving a small MIP problem with semi-continuous variables (foliomip2)
  • modeling and solving QP and MIQP problems (folioqp, requires foliocppqp.dat)
  • heuristic solution of a MIP problem (folioheur)
Advanced modeling and solving tasks:
  • enlarged version of the basic MIP model (foliomip3, to be used with data set folio10.cdat)
  • defining an integer solution callback (foliocb)
  • using the MIP solution pool (foliosolpool)
  • using the solution enumerator (folioenumsol)
  • handling infeasibility through deviation variables (folioinfeas)
  • retrieving IIS (folioiis, foliomiis)
  • using the built-in infeasibility repair functionality (foliorep)
Further explanation of this example: 'Getting Started with BCL' for the basic modelling and solving tasks; 'Advanced Evaluators Guide' for solution enumeration and infeasibilit handling


Source Files

Data Files





foliodata.cs

/********************************************************
  Xpress-BCL C# Example Problems
  ==============================

  file foliodata.cs
  `````````````````
  Modeling a small LP problem
  to perform portfolio optimization.
  -- Data input from file --

  (c) 2008-2024 Fair Isaac Corporation
      authors: S.Heipcke, D.Brett.
********************************************************/

using System;
using System.Text;
using System.IO;
using BCL;


namespace Examples
{

    public class TestUGFolioData
    {

        //Define XPRBDATAPATH to wherever you have placed the data folder; here we expect it to be same directory as compiled example.
        static string XPRBDATAPATH = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName + "/Data";
        static string DATAFILE = XPRBDATAPATH + "/GS/foliocpplp.dat";

        const int NSHARES = 10;          // Number of shares
        const int NRISK = 5;             // Number of high-risk shares
        const int NNA = 4;               // Number of North-American shares

        // Estimated return in investment
        double[] RET = new double[NSHARES];

        // High-risk values among shares
        string[] RISK = {"hardware", "theater", "telecom", "software",
                "electronics"};

        // Shares issued in N.-America
        string[] NA = {"treasury", "hardware", "theater", "telecom"};

    // Set of shares
        XPRBindexSet SHARES;

        // Initialize a new problem in BCL
        XPRBprob p = new XPRBprob("FolioLP");

        public void readData()
        {
            double value;
            int s;
            string name;
            FileStream file;
            StreamReader fileStreamIn;

        // Create the `SHARES' index set
            SHARES=p.newIndexSet("Shares",NSHARES);

            // Read `RET' data from file
            file = new FileStream(DATAFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            object[] outdata = new object[2];
            for (s = 0; s < NSHARES; s++)
            {
                p.XPRBreadline(fileStreamIn, 200, "{S} {g}", out outdata);
                name = (string)outdata[0];
                value = (double)outdata[1];
                RET[SHARES + name] = value;
            }
            fileStreamIn.Close();
            file.Close();

            SHARES.print();                     // Print out the set contents
        }

        public static void Main()
        {
            XPRB.init();
            int s;
            XPRBexpr Risk,Na,Return,Cap;

            // Fraction of capital used per share
            XPRBvar[] frac = new XPRBvar[NSHARES];

            TestUGFolioData TestInstance = new TestUGFolioData();

            // Read data from file
            TestInstance.readData();

            // Create the decision variables
            for (s = 0; s < NSHARES; s++)
                frac[s] = TestInstance.p.newVar("frac");

            // Objective: total return
            Return = new XPRBexpr();
            for (s = 0; s < NSHARES; s++)
                Return += TestInstance.RET[s] * frac[s];

            // Set the objective function
            TestInstance.p.setObj(TestInstance.p.newCtr("Objective", Return));

            // Limit the percentage of high-risk values
            Risk = new XPRBexpr();
            for (s = 0; s < NRISK; s++)
                Risk+=frac[TestInstance.SHARES.getIndex(TestInstance.RISK[s])];
            TestInstance.p.newCtr("Risk", Risk <= 1.0 / 3);

            // Minimum amount of North-American values
            Na = new XPRBexpr();
            for (s = 0; s < NNA; s++)
                Na += frac[TestInstance.SHARES.getIndex(TestInstance.NA[s])];
            TestInstance.p.newCtr("NA", Na >= 0.5);

            // Spend all the capital
            Cap = new XPRBexpr();
            for(s=0;s<NSHARES;s++) Cap += frac[s];
            TestInstance.p.newCtr("Cap", Cap == 1);

            // Upper bounds on the investment per share
            for(s=0;s<NSHARES;s++) frac[s].setUB(0.3);

            // Solve the problem
            TestInstance.p.setSense(BCLconstant.XPRB_MAXIM);
            TestInstance.p.lpOptimize();              /* Solve the LP-problem */

            // Solution printing
            System.Console.WriteLine("Total return: " +
                              TestInstance.p.getObjVal());
            for(s=0;s<NSHARES;s++)
                System.Console.WriteLine(TestInstance.SHARES.getIndexName(s) +
                         ": " + frac[s].getSol() * 100 + "%");

            return;
        }

    }

}
Back to examples browserPrevious example