Initializing help system before first use

Folio - Examples from 'Getting Started'


Type: Portfolio optimization
Rating: 3 (intermediate)
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)
  • modeling and solving QCQP problems (folioqc, 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 sets folio5.cdat, 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)
File(s): foliolp.java, folioinit.java, folioinit2.java, foliodata.java, foliomip1.java, foliomip2.java, folioqp.java, folioqc.java, folioheur.java, foliomip3.java, foliocb.java, foliosolpool.java, folioenumsol.java, folioinfeas.java, folioiis.java, foliomiis.java, foliorep.java
Data file(s): foliocpplp.dat, foliocppqp.dat, folio5.cdat, folio10.cdat

foliolp.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliolp.java
 * `````````````````
 * Modeling a small LP problem
 * to perform portfolio optimization.
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, 2003, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;

public class foliolp {
  static final int NSHARES = 10; /* Number of shares */
  static final int NRISK = 5; /* Number of high-risk shares */
  static final int NNA = 4; /* Number of North-American shares */

  static final double[] RET = {5, 17, 26, 12, 8, 9, 7, 6, 31, 21};
  /* Estimated return in investment  */
  static final int[] RISK = {1, 2, 3, 8, 9}; /* High-risk values among shares */
  static final int[] NA = {0, 1, 2, 3}; /* Shares issued in N.-America */

  static final String[] LPSTATUS = {
    "not loaded",
    "optimal",
    "infeasible",
    "worse than cutoff",
    "unfinished",
    "unbounded",
    "cutoff in dual",
    "unsolved",
    "nonconvex"
  };

  public static void main(String[] args) {
    try (XPRBprob p = new XPRBprob("FolioLP"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s;
      XPRBexpr Risk, Na, Return, Cap;
      XPRBvar[] frac; /* Fraction of capital used per share */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) frac[s] = p.newVar("frac"); /*, XPRB.PL, 0, 0.3); */

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

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

      /* Equivalent:
         XPRBctr CRisk;
         CRisk = p.newCtr("Risk");
         for(s=0;s<NRISK;s++) CRisk.addTerm(frac[RISK[s]], 1);
         CRisk.setType(XPRB.L);
         CRisk.addTerm(1.0/3);
      */

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

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

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

      /* Export matrix to a file */
      try {
        p.exportProb(XPRB.MPS, "Folio");
        p.setSense(XPRB.MAXIM);
        p.exportProb(XPRB.LP, "Folio");
      } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
      }

      /* Disable all BCL and Optimizer message printing, except error messages */
      /*  p.setMsgLevel(1); */

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.lpOptimize("");

      System.out.println("Problem status: " + LPSTATUS[p.getLPStat()]);

      /* Solution printing */
      System.out.println("Total return: " + p.getObjVal());
      for (s = 0; s < NSHARES; s++) System.out.println(s + ": " + frac[s].getSol() * 100 + "%");
    }
  }
}

folioinit.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file folioinit.java
 * ```````````````````
 * Modeling a small LP problem
 * to perform portfolio optimization.
 * Initialization with error handling.
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, 2003, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;

public class folioinit {
  static final int NSHARES = 10; /* Number of shares */
  static final int NRISK = 5; /* Number of high-risk shares */
  static final int NNA = 4; /* Number of North-American shares */

  static final double[] RET = {5, 17, 26, 12, 8, 9, 7, 6, 31, 21};
  /* Estimated return in investment  */
  static final int[] RISK = {1, 2, 3, 8, 9}; /* High-risk values among shares */
  static final int[] NA = {0, 1, 2, 3}; /* Shares issued in N.-America */

  static final String[] LPSTATUS = {
    "not loaded",
    "optimal",
    "infeasible",
    "worse than cutoff",
    "unfinished",
    "unbounded",
    "cutoff in dual",
    "unsolved",
    "nonconvex"
  };

  private static void solveProb(XPRB bcl) {
    try (XPRBprob p = bcl.newProb("FolioLP"); /* Create a new problem in BCL */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s;
      XPRBexpr Risk, Na, Return, Cap;
      XPRBvar[] frac; /* Fraction of capital used per share */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) frac[s] = p.newVar("frac"); /*, XPRB.PL, 0, 0.3); */

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

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

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

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

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

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.lpOptimize("");

      System.out.println("Problem status: " + LPSTATUS[p.getLPStat()]);

      /* Solution printing */
      System.out.println("Total return: " + p.getObjVal());
      for (s = 0; s < NSHARES; s++) System.out.println(s + ": " + frac[s].getSol() * 100 + "%");
    }
  }

  public static void main(String[] args) {
    try (XPRB bcl = new XPRB()) {
        /* Initialize BCL */
      solveProb(bcl);
    } catch (XPRBlicenseError e) {
      System.out.println("Initialization failed (licensing problem).");
      System.exit(1);
    } catch (XPRBerror e) {
      System.out.println("Initialization failed.");
      System.exit(1);
    }
  }
}

folioinit2.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file folioinit2.java
 * ````````````````````
 * Modeling a small LP problem
 * to perform portfolio optimization.
 * Initialization with error handling.
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, 2003, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;

public class folioinit2 {
  static final int NSHARES = 10; /* Number of shares */
  static final int NRISK = 5; /* Number of high-risk shares */
  static final int NNA = 4; /* Number of North-American shares */

  static final double[] RET = {5, 17, 26, 12, 8, 9, 7, 6, 31, 21};
  /* Estimated return in investment  */
  static final int[] RISK = {1, 2, 3, 8, 9}; /* High-risk values among shares */
  static final int[] NA = {0, 1, 2, 3}; /* Shares issued in N.-America */

  static final String[] LPSTATUS = {
    "not loaded",
    "optimal",
    "infeasible",
    "worse than cutoff",
    "unfinished",
    "unbounded",
    "cutoff in dual",
    "unsolved",
    "nonconvex"
  };

  public static void main(String[] args) {
    try (XPRB bcl = new XPRB()) {
        /* Initialize BCL */

      try (XPRBprob p = bcl.newProb("FolioLP"); /* Create a new problem in BCL */
          XPRBexprContext context =
              new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
        int s;
        XPRBexpr Risk, Na, Return, Cap;
        XPRBvar[] frac; /* Fraction of capital used per share */

        /* Create the decision variables */
        frac = new XPRBvar[NSHARES];
        for (s = 0; s < NSHARES; s++) frac[s] = p.newVar("frac"); /*, XPRB.PL, 0, 0.3); */

        /* Objective: total return */
        Return = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
        p.setObj(Return); /* Set the objective function */

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

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

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

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

        /* Solve the problem */
        p.setSense(XPRB.MAXIM);
        p.lpOptimize("");

        System.out.println("Problem status: " + LPSTATUS[p.getLPStat()]);

        /* Solution printing */
        System.out.println("Total return: " + p.getObjVal());
        for (s = 0; s < NSHARES; s++) System.out.println(s + ": " + frac[s].getSol() * 100 + "%");
      }
    } catch (XPRBlicenseError e) {
      System.out.println("Initialization failed (licensing problem).");
      System.exit(1);
    } catch (XPRBerror e) {
      System.out.println("Initialization failed.");
      System.exit(1);
    }
  }
}

foliodata.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliodata.java
 * ```````````````````
 * Modeling a small LP problem
 * to perform portfolio optimization.
 * -- Data input from file --
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, 2003, rev. June 2008, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;

public class foliodata {
  static final String DATAFILE = System.getProperty("XPRBDATA") + "/GS/foliocpplp.dat";

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

  static double[] RET; /* Estimated return in investment  */
  static final String[] RISK = {
    "hardware", "theater", "telecom", "software", "electronics"
  }; /* High-risk values among shares */
  static final String[] NA = {"treasury", "hardware", "theater", "telecom"};
  /* Shares issued in N.-America */

  static final String[] LPSTATUS = {
    "not loaded",
    "optimal",
    "infeasible",
    "worse than cutoff",
    "unfinished",
    "unbounded",
    "cutoff in dual",
    "unsolved",
    "nonconvex"
  };

  static XPRBindexSet SHARES; /* Set of shares */

  private static void readData(XPRBprob p) throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    SHARES = p.newIndexSet("Shares", NSHARES); /* Create the `SHARES' index set */
    RET = new double[NSHARES];

    /* Read `RET' data from file */
    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(true); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    do {
        /* Read data file and fill the index sets */
      do {
        st.nextToken();
      } while (st.ttype == st.TT_EOL); /* Skip empty lines and comment lines */
      if (st.ttype != st.TT_WORD && st.ttype != '"') break;
      s = SHARES.addElement(st.sval);
      if (st.nextToken() != st.TT_NUMBER) break;
      RET[s] = st.nval;
    } while (st.nextToken() == st.TT_EOL);

    datafile.close();

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

  public static void main(String[] args) throws Exception {
    try (XPRBprob p = new XPRBprob("FolioLP"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s;
      XPRBexpr Risk, Na, Return, Cap;
      XPRBvar[] frac; /* Fraction of capital used per share */

      readData(p); /* Read data from file */

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

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

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

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

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

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

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.lpOptimize("");

      System.out.println("Problem status: " + LPSTATUS[p.getLPStat()]);

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

foliomip1.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliomip1.java
 * ```````````````````
 * Modeling a small LP problem
 * to perform portfolio optimization.
 * -- Limiting the total number of assets --
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, 2003, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;

public class foliomip1 {
  static final int MAXNUM = 4; /* Max. number of different assets */
  static final int NSHARES = 10; /* Number of shares */
  static final int NRISK = 5; /* Number of high-risk shares */
  static final int NNA = 4; /* Number of North-American shares */

  static final double[] RET = {5, 17, 26, 12, 8, 9, 7, 6, 31, 21};
  /* Estimated return in investment  */
  static final int[] RISK = {1, 2, 3, 8, 9}; /* High-risk values among shares */
  static final int[] NA = {0, 1, 2, 3}; /* Shares issued in N.-America */

  static final String[] MIPSTATUS = {
    "not loaded",
    "not optimized",
    "LP optimized",
    "unfinished (no solution)",
    "unfinished (solution found)",
    "infeasible",
    "optimal",
    "unbounded"
  };

  public static void main(String[] args) {
    try (XPRBprob p = new XPRBprob("FolioMIP1"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s;
      XPRBexpr Risk, Na, Return, Cap, Num;
      XPRBvar[] frac; /* Fraction of capital used per share */
      XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac", XPRB.PL, 0, 0.3);
        buy[s] = p.newVar("buy", XPRB.BV);
      }

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      Risk = new XPRBexpr();
      for (s = 0; s < NRISK; s++) Risk.add(frac[RISK[s]]);
      p.newCtr(Risk.lEql(1.0 / 3));

      /* Minimum amount of North-American values */
      Na = new XPRBexpr();
      for (s = 0; s < NNA; s++) Na.add(frac[NA[s]]);
      p.newCtr(Na.gEql(0.5));

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

      /* Limit the total number of assets */
      Num = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Num.add(buy[s]);
      p.newCtr(Num.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s]));

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.mipOptimize("");

      System.out.println("Problem status: " + MIPSTATUS[p.getMIPStat()]);

      /* Solution printing */
      System.out.println("Total return: " + p.getObjVal());
      for (s = 0; s < NSHARES; s++)
        System.out.println(s + ": " + frac[s].getSol() * 100 + "% (" + buy[s].getSol() + ")");
    }
  }
}

foliomip2.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliomip2.java
 * ```````````````````
 * Modeling a small LP problem
 * to perform portfolio optimization.
 * -- Imposing a minimum investment per share --
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, 2003, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;

public class foliomip2 {
  static final int NSHARES = 10; /* Number of shares */
  static final int NRISK = 5; /* Number of high-risk shares */
  static final int NNA = 4; /* Number of North-American shares */

  static final double[] RET = {5, 17, 26, 12, 8, 9, 7, 6, 31, 21};
  /* Estimated return in investment  */
  static final int[] RISK = {1, 2, 3, 8, 9}; /* High-risk values among shares */
  static final int[] NA = {0, 1, 2, 3}; /* Shares issued in N.-America */

  public static void main(String[] args) {
    try (XPRBprob p = new XPRBprob("FolioSC"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s;
      XPRBexpr Risk, Na, Return, Cap, Num;
      XPRBvar[] frac; /* Fraction of capital used per share */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac", XPRB.SC, 0, 0.3);
        frac[s].setLim(0.1);
      }

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      Risk = new XPRBexpr();
      for (s = 0; s < NRISK; s++) Risk.add(frac[RISK[s]]);
      p.newCtr(Risk.lEql(1.0 / 3));

      /* Minimum amount of North-American values */
      Na = new XPRBexpr();
      for (s = 0; s < NNA; s++) Na.add(frac[NA[s]]);
      p.newCtr(Na.gEql(0.5));

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

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.mipOptimize("");

      /* Solution printing */
      System.out.println("Total return: " + p.getObjVal());
      for (s = 0; s < NSHARES; s++) System.out.println(s + ": " + frac[s].getSol() * 100 + "%");
    }
  }
}

folioqp.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file folioqp.java
 * `````````````````
 * Modeling a small QP problem
 * to perform portfolio optimization.
 * -- 1. QP: minimize variance
 * 2. MIQP: limited number of assets ---
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, 2003, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;

public class folioqp {
  static final String DATAFILE = System.getProperty("XPRBDATA") + "/GS/foliocppqp.dat";

  static final int TARGET = 9; /* Target yield */
  static final int MAXNUM = 4; /* Max. number of different assets */
  static final int NSHARES = 10; /* Number of shares */
  static final int NNA = 4; /* Number of North-American shares */

  static final double[] RET = {5, 17, 26, 12, 8, 9, 7, 6, 31, 21};
  /* Estimated return in investment  */
  static final int[] NA = {0, 1, 2, 3}; /* Shares issued in N.-America */
  static double[][] VAR; /* Variance/covariance matrix of
                                         estimated returns */

  private static void readData() throws IOException {
    int s, t;
    FileReader datafile = null;
    StreamTokenizer st = null;

    VAR = new double[NSHARES][NSHARES];

    /* Read `VAR' data from file */
    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(true); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    for (s = 0; s < NSHARES; s++) {
      do {
        st.nextToken();
      } while (st.ttype == st.TT_EOL); /* Skip empty lines and comment lines */
      for (t = 0; t < NSHARES; t++) {
        if (st.ttype != st.TT_NUMBER) break;
        VAR[s][t] = st.nval;
        st.nextToken();
      }
    }

    datafile.close();
  }

  public static void main(String[] args) throws Exception {
    try (XPRBprob p = new XPRBprob("FolioQP"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s, t;
      XPRBexpr Risk, Na, Return, Cap, Num, Variance;
      XPRBvar[] frac; /* Fraction of capital used per share */
      XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

      readData(); /* Read data from file */

      /***** First problem: unlimited number of assets *****/

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) frac[s] = p.newVar("frac", XPRB.PL, 0, 0.3);

      /* Objective: mean variance */
      Variance = new XPRBexpr();
      for (s = 0; s < NSHARES; s++)
        for (t = 0; t < NSHARES; t++) Variance.add(frac[s].mul(frac[t]).mul(VAR[s][t]));
      p.setObj(Variance); /* Set the objective function */

      /* Minimum amount of North-American values */
      Na = new XPRBexpr();
      for (s = 0; s < NNA; s++) Na.add(frac[NA[s]]);
      p.newCtr(Na.gEql(0.5));

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

      /* Target yield */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.newCtr(Return.gEql(TARGET));

      /* Solve the problem */
      p.setSense(XPRB.MINIM);
      p.lpOptimize("");

      /* Solution printing */
      System.out.println("With a target of " + TARGET + " minimum variance is " + p.getObjVal());
      for (s = 0; s < NSHARES; s++) System.out.println(s + ": " + frac[s].getSol() * 100 + "%");

      /***** Second problem: limit total number of assets *****/

      /* Create the decision variables */
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) buy[s] = p.newVar("buy", XPRB.BV);

      /* Limit the total number of assets */
      Num = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Num.add(buy[s]);
      p.newCtr(Num.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s]));

      /* Solve the problem */
      p.mipOptimize("");

      /* Solution printing */
      System.out.println(
          "With a target of "
              + TARGET
              + " and at most "
              + MAXNUM
              + " assets, minimum variance is "
              + p.getObjVal());
      for (s = 0; s < NSHARES; s++)
        System.out.println(s + ": " + frac[s].getSol() * 100 + "% (" + buy[s].getSol() + ")");
    }
  }
}

folioqc.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file folioqc.java
 * `````````````````
 * Modeling a small QCQP problem
 * to perform portfolio optimization.
 * -- Maximize return with limit on variance ---
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, July 2008, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;

public class folioqc {
  static final String DATAFILE = System.getProperty("XPRBDATA") + "/GS/foliocppqp.dat";

  static final double MAXVAR = 0.55; /* Max. allowed variance */
  static final int NSHARES = 10; /* Number of shares */
  static final int NNA = 4; /* Number of North-American shares */

  static final double[] RET = {5, 17, 26, 12, 8, 9, 7, 6, 31, 21};
  /* Estimated return in investment */
  static final int[] NA = {0, 1, 2, 3}; /* Shares issued in N.-America */
  static double[][] VAR; /* Variance/covariance matrix of
                                            estimated returns */

  private static void readData() throws IOException {
    int s, t;
    FileReader datafile = null;
    StreamTokenizer st = null;

    VAR = new double[NSHARES][NSHARES];

    /* Read `VAR' data from file */
    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(true); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    for (s = 0; s < NSHARES; s++) {
      do {
        st.nextToken();
      } while (st.ttype == st.TT_EOL); /* Skip empty lines and comment lines */
      for (t = 0; t < NSHARES; t++) {
        if (st.ttype != st.TT_NUMBER) break;
        VAR[s][t] = st.nval;
        st.nextToken();
      }
    }

    datafile.close();
  }

  public static void main(String[] args) throws Exception {
    try (XPRBprob p = new XPRBprob("FolioQC"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s, t;
      XPRBexpr Risk, Na, Return, Cap, Num, Variance;
      XPRBvar[] frac; /* Fraction of capital used per share */

      readData(); /* Read data from file */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) frac[s] = p.newVar("frac", XPRB.PL, 0, 0.3);

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

      /* Minimum amount of North-American values */
      Na = new XPRBexpr();
      for (s = 0; s < NNA; s++) Na.add(frac[NA[s]]);
      p.newCtr(Na.gEql(0.5));

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

      /* Limit variance */
      Variance = new XPRBexpr();
      for (s = 0; s < NSHARES; s++)
        for (t = 0; t < NSHARES; t++) Variance.add(frac[s].mul(frac[t]).mul(VAR[s][t]));
      p.newCtr(Variance.lEql(MAXVAR));

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.lpOptimize("");

      /* Solution printing */
      System.out.println("With a max. variance of " + MAXVAR + " total return is " + p.getObjVal());
      for (s = 0; s < NSHARES; s++) System.out.println(s + ": " + frac[s].getSol() * 100 + "%");
    }
  }
}

folioheur.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file folioheur.java
 * ```````````````````
 * Modeling a small LP problem
 * to perform portfolio optimization.
 * -- Heuristic solution --
 *
 * (c) 2008-2024 Fair Isaac Corporation
 * author: S.Heipcke, 2003, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;

public class folioheur {
  static final int MAXNUM = 4; /* Max. number of different assets */
  static final int NSHARES = 10; /* Number of shares */
  static final int NRISK = 5; /* Number of high-risk shares */
  static final int NNA = 4; /* Number of North-American shares */

  static final double[] RET = {5, 17, 26, 12, 8, 9, 7, 6, 31, 21};
  /* Estimated return in investment  */
  static final int[] RISK = {1, 2, 3, 8, 9}; /* High-risk values among shares */
  static final int[] NA = {0, 1, 2, 3}; /* Shares issued in N.-America */

  static XPRBvar[] frac; /* Fraction of capital used per share */
  static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

  public static void main(String[] args) throws XPRSprobException, XPRSexception {
    try (XPRBprob p = new XPRBprob("FolioMIPHeur"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext(); /* Release XPRBexpr instances at end of block. */
        XPRS xprs = new XPRS()) {
        /* Initialize Xpress-Optimizer */
      int s;
      XPRBexpr Risk, Na, Return, Cap, Num;

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac", XPRB.PL, 0, 0.3);
        buy[s] = p.newVar("buy", XPRB.BV);
      }

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      Risk = new XPRBexpr();
      for (s = 0; s < NRISK; s++) Risk.add(frac[RISK[s]]);
      p.newCtr(Risk.lEql(1.0 / 3));

      /* Minimum amount of North-American values */
      Na = new XPRBexpr();
      for (s = 0; s < NNA; s++) Na.add(frac[NA[s]]);
      p.newCtr(Na.gEql(0.5));

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

      /* Limit the total number of assets */
      Num = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Num.add(buy[s]);
      p.newCtr(Num.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s]));

      /* Solve problem heuristically */
      p.setSense(XPRB.MAXIM);
      solveHeur(p);

      /* Solve the problem */
      p.mipOptimize("");

      /* Solution printing */
      if (p.getMIPStat() == XPRB.MIP_SOLUTION || p.getMIPStat() == XPRB.MIP_OPTIMAL) {
        System.out.println("Exact solution: Total return: " + p.getObjVal());
        for (s = 0; s < NSHARES; s++) System.out.println(s + ": " + frac[s].getSol() * 100 + "%");
      } else System.out.println("Heuristic solution is optimal.");

      /* Delete the problem */
    }
  }

  static void solveHeur(XPRBprob p) throws XPRSprobException {
    XPRSprob op;
    XPRBbasis basis;
    int s, ifgsol;
    double solval, TOL;
    double[] fsol;

    op = p.getXPRSprob(); /* Retrieve the Optimizer problem */
    op.setIntControl(XPRS.CUTSTRATEGY, 0); /* Disable automatic cuts */
    op.setIntControl(XPRS.PRESOLVE, 0); /* Switch presolve off */
    TOL = op.getDblControl(XPRS.FEASTOL); /* Get feasibility tolerance */

    p.mipOptimize("l"); /* Solve the LP-problem */
    basis = p.saveBasis(); /* Save the current basis */

    /* Fix all variables `buy' for which `frac' is at 0 or at a relatively
    large value */
    fsol = new double[NSHARES];
    for (s = 0; s < NSHARES; s++) {
      fsol[s] = frac[s].getSol(); /* Get the solution values of `frac' */
      if (fsol[s] < TOL) buy[s].setUB(0);
      else if (fsol[s] > 0.2 - TOL) buy[s].setLB(1);
    }

    p.mipOptimize("c"); /* Continue solving the MIP-problem */
    ifgsol = 0;
    solval = 0;
    if (p.getMIPStat() == XPRB.MIP_SOLUTION || p.getMIPStat() == XPRB.MIP_OPTIMAL) {
        /* If an integer feas. solution was found */
      ifgsol = 1;
      solval = p.getObjVal(); /* Get the value of the best solution */
      System.out.println("Heuristic solution: Total return: " + p.getObjVal());
      for (s = 0; s < NSHARES; s++) System.out.println(s + ": " + frac[s].getSol() * 100 + "%");
    }

    /* Reset variables to their original bounds */
    for (s = 0; s < NSHARES; s++)
      if ((fsol[s] < TOL) || (fsol[s] > 0.2 - TOL)) {
        buy[s].setLB(0);
        buy[s].setUB(1);
      }

    p.loadBasis(basis); /* Load the saved basis: bound changes are
                                     immediately passed on from BCL to the
                                     Optimizer if the problem has not been modified
                                     in any other way, so that there is no need to
                                     reload the matrix */
    basis = null; /* No need to store the saved basis any longer */
    if (ifgsol == 1) op.setDblControl(XPRS.MIPABSCUTOFF, solval + TOL);
    /* Set the cutoff to the best known solution */
  }
}

foliomip3.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliomip3.java
 * ```````````````````
 * Modeling a MIP problem
 * to perform portfolio optimization.
 * -- Extending the problem with constraints on
 * the geographical and sectorial distributions --
 * -- Working with a larger data set --
 *
 * (c) 2009-2024 Fair Isaac Corporation
 * author: S.Heipcke, Y.Colombani, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;
import java.util.*;

public class foliomip3 {
  static final String DATAFILE = "folio10.cdat";

  static final int MAXNUM = 7; /* Max. number of different assets */
  static final double MAXRISK = 1.0 / 3; /* Max. investment into high-risk values */
  static final double MINREG = 0.2; /* Min. investment per geogr. region */
  static final double MAXREG = 0.5; /* Max. investment per geogr. region */
  static final double MAXSEC = 0.25; /* Max. investment per ind. sector */
  static final double MAXVAL = 0.2; /* Max. investment per share */
  static final double MINVAL = 0.1; /* Min. investment per share */

  static int NSHARES; /* Number of shares */
  static int NRISK; /* Number of high-risk shares */
  static int NREGIONS; /* Number of geographical regions */
  static int NTYPES; /* Number of share types */

  static double[] RET; /* Estimated return in investment  */
  static int[] RISK; /* High-risk values among shares */
  static boolean LOC[][]; /* Geogr. region of shares */
  static boolean SEC[][]; /* Industry sector of shares */

  static String SHARES_n[];
  static String REGIONS_n[];
  static String TYPES_n[];

  static final String[] MIPSTATUS = {
    "not loaded",
    "not optimized",
    "LP optimized",
    "unfinished (no solution)",
    "unfinished (solution found)",
    "infeasible",
    "optimal",
    "unbounded"
  };

  public static void main(String[] args) throws IOException {
    try {
      readData(); /* Read data from file */
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try (XPRBprob p = new XPRBprob("FolioMIP3"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s, t, r;
      XPRBexpr Risk, Return, Cap, Num, LinkL, LinkU;
      XPRBexpr[] MinReg, MaxReg, LimSec;
      XPRBvar[] frac; /* Fraction of capital used per share */
      XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac", XPRB.PL, 0, MAXVAL);
        buy[s] = p.newVar("buy", XPRB.BV);
      }

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      Risk = new XPRBexpr();
      for (s = 0; s < NRISK; s++) Risk.add(frac[RISK[s]]);
      p.newCtr(Risk.lEql(MAXRISK));

      /* Limits on geographical distribution */
      MinReg = new XPRBexpr[NREGIONS];
      MaxReg = new XPRBexpr[NREGIONS];
      for (r = 0; r < NREGIONS; r++) {
        MinReg[r] = new XPRBexpr();
        MaxReg[r] = new XPRBexpr();
        for (s = 0; s < NSHARES; s++)
          if (LOC[r][s]) {
            MinReg[r].add(frac[s]);
            MaxReg[r].add(frac[s]);
          }
        p.newCtr(MinReg[r].gEql(MINREG));
        p.newCtr(MaxReg[r].lEql(MAXREG));
      }

      /* Diversification across industry sectors */
      LimSec = new XPRBexpr[NTYPES];
      for (t = 0; t < NTYPES; t++) {
        LimSec[t] = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) if (SEC[t][s]) LimSec[t].add(frac[s]);
        p.newCtr(LimSec[t].lEql(MAXSEC));
      }

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

      /* Limit the total number of assets */
      Num = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Num.add(buy[s]);
      p.newCtr(Num.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s].mul(MAXVAL)));
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].gEql(buy[s].mul(MINVAL)));

      /* p.exportProb("javamat.lp"); */

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.mipOptimize("");

      System.out.println("Problem status: " + MIPSTATUS[p.getMIPStat()]);

      /* Solution printing */
      System.out.println("Total return: " + p.getObjVal());
      for (s = 0; s < NSHARES; s++)
        if (buy[s].getSol() > 0.5)
          System.out.println(
              "  " + s + ": " + frac[s].getSol() * 100 + "% (" + buy[s].getSol() + ")");
    }
  }

  /***********************Data input routines***************************/

  /***************************/
  /* Input a list of strings */
  /***************************/
  private static String[] read_str_list(StreamTokenizer st) throws IOException {
    LinkedList<String> l = new LinkedList<String>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_WORD) {
      l.addLast(st.sval);
    }

    String a[] = new String[l.size()];
    l.toArray(a);
    return a;
  }

  /************************/
  /* Input a list of ints */
  /************************/
  private static int[] read_int_list(StreamTokenizer st) throws IOException {
    LinkedList<Integer> l = new LinkedList<Integer>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      l.addLast((int) st.nval);
    }

    int a[] = new int[l.size()];
    for (int i = 0; i < l.size(); i++) a[i] = ((Integer) l.get(i)).intValue();
    return a;
  }

  /****************************/
  /* Input a table of doubles */
  /****************************/
  private static void read_dbl_table(StreamTokenizer st, double tbl[]) throws IOException {
    int n = 0;

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      tbl[n++] = st.nval;
    }
  }

  /************************************/
  /* Input a sparse table of booleans */
  /************************************/
  private static boolean[][] read_bool_table(StreamTokenizer st, int nrow, int ncol)
      throws IOException {
    int i;
    boolean tbl[][] = new boolean[nrow][ncol];

    st.nextToken(); /* Skip ':' */
    for (int r = 0; r < nrow; r++) {
      while (st.nextToken() == st.TT_NUMBER) tbl[r][(int) st.nval] = true;
    }
    return tbl;
  }

  private static void readData() throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(false); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    while (st.nextToken() == st.TT_WORD) {
      if (st.sval.equals("SHARES") && NSHARES == 0) {
        SHARES_n = read_str_list(st);
        NSHARES = SHARES_n.length;
      } else if (st.sval.equals("REGIONS") && NREGIONS == 0) {
        REGIONS_n = read_str_list(st);
        NREGIONS = REGIONS_n.length;
      } else if (st.sval.equals("TYPES") && NTYPES == 0) {
        TYPES_n = read_str_list(st);
        NTYPES = TYPES_n.length;
      } else if (st.sval.equals("RISK") && NRISK == 0) {
        RISK = read_int_list(st);
        NRISK = RISK.length;
      } else if (st.sval.equals("RET") && NSHARES > 0) {
        RET = new double[NSHARES];
        read_dbl_table(st, RET);
      } else if (st.sval.equals("LOC") && NSHARES > 0 && NREGIONS > 0)
        LOC = read_bool_table(st, NREGIONS, NSHARES);
      else if (st.sval.equals("SEC") && NSHARES > 0 && NTYPES > 0)
        SEC = read_bool_table(st, NTYPES, NSHARES);
      else break;
    }

    /*
      for(int i=0;i<NREGIONS;i++) {
      for(int j=0;j<NSHARES;j++)
      System.out.print(" "+LOC[i][j]);
      System.out.println();
      }
    */
    datafile.close();
  }
}

foliocb.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliocb.java
 * `````````````````
 * Modeling a MIP problem
 * to perform portfolio optimization.
 *
 * Same model as in foliomip3.java.
 * -- Defining an integer solution callback --
 *
 * (c) 2009-2024 Fair Isaac Corporation
 * author: S.Heipcke, Y.Colombani, May 2009, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;
import java.util.*;

public class foliocb {
  static final String DATAFILE = "folio10.cdat";

  static final int MAXNUM = 15; /* Max. number of different assets */
  static final double MAXRISK = 1.0 / 3; /* Max. investment into high-risk values */
  static final double MINREG = 0.2; /* Min. investment per geogr. region */
  static final double MAXREG = 0.5; /* Max. investment per geogr. region */
  static final double MAXSEC = 0.25; /* Max. investment per ind. sector */
  static final double MAXVAL = 0.2; /* Max. investment per share */
  static final double MINVAL = 0.1; /* Min. investment per share */

  static int NSHARES; /* Number of shares */
  static int NRISK; /* Number of high-risk shares */
  static int NREGIONS; /* Number of geographical regions */
  static int NTYPES; /* Number of share types */

  static double[] RET; /* Estimated return in investment  */
  static int[] RISK; /* High-risk values among shares */
  static boolean LOC[][]; /* Geogr. region of shares */
  static boolean SEC[][]; /* Industry sector of shares */

  static String SHARES_n[];
  static String REGIONS_n[];
  static String TYPES_n[];

  static final String[] MIPSTATUS = {
    "not loaded",
    "not optimized",
    "LP optimized",
    "unfinished (no solution)",
    "unfinished (solution found)",
    "infeasible",
    "optimal",
    "unbounded"
  };

  static XPRBvar[] frac; /* Fraction of capital used per share */
  static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

  public static void main(String[] args) throws XPRSexception {
    int s, t, r;
    IntSolCallback cb;

    try {
      readData(); /* Read data from file */
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try (XPRBprob p =
            new XPRBprob("FolioMIP3"); // Initialize BCL and create a new problem in BCL */
        XPRS xprs = new XPRS(); // Initialize Xpress-Optimizer */
        XPRBexprContext context = new XPRBexprContext()) // Automatic cleanup of expressions
    {
      XPRBexpr Risk = null, Return = null, Cap = null, Num = null;
      XPRBexpr[] MinReg = null, MaxReg = null, LimSec = null;

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac", XPRB.PL, 0, MAXVAL);
        buy[s] = p.newVar("buy", XPRB.BV);
      }

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      Risk = new XPRBexpr();
      for (s = 0; s < NRISK; s++) Risk.add(frac[RISK[s]]);
      p.newCtr(Risk.lEql(MAXRISK));

      /* Limits on geographical distribution */
      MinReg = new XPRBexpr[NREGIONS];
      MaxReg = new XPRBexpr[NREGIONS];
      for (r = 0; r < NREGIONS; r++) {
        MinReg[r] = new XPRBexpr();
        MaxReg[r] = new XPRBexpr();
        for (s = 0; s < NSHARES; s++)
          if (LOC[r][s]) {
            MinReg[r].add(frac[s]);
            MaxReg[r].add(frac[s]);
          }
        p.newCtr(MinReg[r].gEql(MINREG));
        p.newCtr(MaxReg[r].lEql(MAXREG));
      }

      /* Diversification across industry sectors */
      LimSec = new XPRBexpr[NTYPES];
      for (t = 0; t < NTYPES; t++) {
        LimSec[t] = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) if (SEC[t][s]) LimSec[t].add(frac[s]);
        p.newCtr(LimSec[t].lEql(MAXSEC));
      }

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

      /* Limit the total number of assets */
      Num = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Num.add(buy[s]);
      p.newCtr(Num.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s].mul(MAXVAL)));
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].gEql(buy[s].mul(MINVAL)));

      /* Define an integer solution callback */
      cb = new IntSolCallback();
      p.getXPRSprob().addIntSolListener(cb, p);

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.mipOptimize("");

      System.out.println("Problem status: " + MIPSTATUS[p.getMIPStat()]);

      /* Solution printing */
      System.out.println("Total return: " + p.getObjVal());
      for (s = 0; s < NSHARES; s++)
        if (buy[s].getSol() > 0.5)
          System.out.println(s + ": " + frac[s].getSol() * 100 + "% (" + buy[s].getSol() + ")");
    }
  }

  /**************************Solution printing****************************/

  static class IntSolCallback implements XPRSintSolListener {
    public void XPRSintSolEvent(XPRSprob oprob, Object data) {
      int num, s;
      XPRBprob bprob;

      try {
        bprob = (XPRBprob) data;
        bprob.beginCB(oprob);
        num = oprob.getIntAttrib(XPRS.MIPSOLS); /* Get number of the solution */
        bprob.sync(XPRB.XPRS_SOL); /* Update BCL solution values */

        System.out.println("Solution " + num + ": Total return: " + bprob.getObjVal());

        for (s = 0; s < NSHARES; s++)
          if (buy[s].getSol() > 0.5)
            System.out.println(
                "  " + s + ": " + frac[s].getSol() * 100 + "% (" + buy[s].getSol() + ")");
        bprob.endCB();
      } catch (XPRSprobException e) {
        System.out.println("Error " + e.getCode() + ": " + e.getMessage());
      }
    }
  }

  /***********************Data input routines***************************/

  /***************************/
  /* Input a list of strings */
  /***************************/
  private static String[] read_str_list(StreamTokenizer st) throws IOException {
    LinkedList<String> l = new LinkedList<String>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_WORD) {
      l.addLast(st.sval);
    }

    String a[] = new String[l.size()];
    l.toArray(a);
    return a;
  }

  /************************/
  /* Input a list of ints */
  /************************/
  private static int[] read_int_list(StreamTokenizer st) throws IOException {
    LinkedList<Integer> l = new LinkedList<Integer>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      l.addLast((int) st.nval);
    }

    int a[] = new int[l.size()];
    for (int i = 0; i < l.size(); i++) a[i] = ((Integer) l.get(i)).intValue();
    return a;
  }

  /****************************/
  /* Input a table of doubles */
  /****************************/
  private static void read_dbl_table(StreamTokenizer st, double tbl[]) throws IOException {
    int n = 0;

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      tbl[n++] = st.nval;
    }
  }

  /************************************/
  /* Input a sparse table of booleans */
  /************************************/
  private static boolean[][] read_bool_table(StreamTokenizer st, int nrow, int ncol)
      throws IOException {
    int i;
    boolean tbl[][] = new boolean[nrow][ncol];

    st.nextToken(); /* Skip ':' */
    for (int r = 0; r < nrow; r++) {
      while (st.nextToken() == st.TT_NUMBER) tbl[r][(int) st.nval] = true;
    }
    return tbl;
  }

  private static void readData() throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(false); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    while (st.nextToken() == st.TT_WORD) {
      if (st.sval.equals("SHARES") && NSHARES == 0) {
        SHARES_n = read_str_list(st);
        NSHARES = SHARES_n.length;
      } else if (st.sval.equals("REGIONS") && NREGIONS == 0) {
        REGIONS_n = read_str_list(st);
        NREGIONS = REGIONS_n.length;
      } else if (st.sval.equals("TYPES") && NTYPES == 0) {
        TYPES_n = read_str_list(st);
        NTYPES = TYPES_n.length;
      } else if (st.sval.equals("RISK") && NRISK == 0) {
        RISK = read_int_list(st);
        NRISK = RISK.length;
      } else if (st.sval.equals("RET") && NSHARES > 0) {
        RET = new double[NSHARES];
        read_dbl_table(st, RET);
      } else if (st.sval.equals("LOC") && NSHARES > 0 && NREGIONS > 0)
        LOC = read_bool_table(st, NREGIONS, NSHARES);
      else if (st.sval.equals("SEC") && NSHARES > 0 && NTYPES > 0)
        SEC = read_bool_table(st, NTYPES, NSHARES);
      else break;
    }

    /*
      for(int i=0;i<NREGIONS;i++) {
      for(int j=0;j<NSHARES;j++)
      System.out.print(" "+LOC[i][j]);
      System.out.println();
      }
    */
    datafile.close();
  }
}

foliosolpool.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliosolpool.java
 * ``````````````````````
 * Modeling a MIP problem
 * to perform portfolio optimization.
 *
 * Same model as in foliomip3.java.
 * -- Using the MIP solution pool --
 *
 * (c) 2009-2024 Fair Isaac Corporation
 * author: S.Heipcke, Y.Colombani, June 2009, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;
import java.util.*;

public class foliosolpool {
  static final String DATAFILE = "folio10.cdat";

  static final int MAXNUM = 7; /* Max. number of different assets */
  static final double MAXRISK = 1.0 / 3; /* Max. investment into high-risk values */
  static final double MINREG = 0.2; /* Min. investment per geogr. region */
  static final double MAXREG = 0.5; /* Max. investment per geogr. region */
  static final double MAXSEC = 0.25; /* Max. investment per ind. sector */
  static final double MAXVAL = 0.2; /* Max. investment per share */
  static final double MINVAL = 0.1; /* Min. investment per share */

  static int NSHARES; /* Number of shares */
  static int NRISK; /* Number of high-risk shares */
  static int NREGIONS; /* Number of geographical regions */
  static int NTYPES; /* Number of share types */

  static double[] RET; /* Estimated return in investment  */
  static int[] RISK; /* High-risk values among shares */
  static boolean LOC[][]; /* Geogr. region of shares */
  static boolean SEC[][]; /* Industry sector of shares */

  static String SHARES_n[];
  static String REGIONS_n[];
  static String TYPES_n[];

  static XPRBvar[] frac; /* Fraction of capital used per share */
  static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

  public static void main(String[] args) throws IOException, XPRSexception {
    try {
      readData(); /* Read data from file */
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try (XPRBprob p = new XPRBprob("FolioMIP3"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext(); /* Release XPRBexpr instances at end of block. */
        XPRS xprs = new XPRS()) {
        /* Initialize Xpress-Optimizer */
      int s, t, r, i;
      XPRBexpr Risk, Return, Cap, Num, LinkL, LinkU;
      XPRBexpr[] MinReg, MaxReg, LimSec;

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac", XPRB.PL, 0, MAXVAL);
        buy[s] = p.newVar("buy", XPRB.BV);
      }

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      Risk = new XPRBexpr();
      for (s = 0; s < NRISK; s++) Risk.add(frac[RISK[s]]);
      p.newCtr(Risk.lEql(MAXRISK));

      /* Limits on geographical distribution */
      MinReg = new XPRBexpr[NREGIONS];
      MaxReg = new XPRBexpr[NREGIONS];
      for (r = 0; r < NREGIONS; r++) {
        MinReg[r] = new XPRBexpr();
        MaxReg[r] = new XPRBexpr();
        for (s = 0; s < NSHARES; s++)
          if (LOC[r][s]) {
            MinReg[r].add(frac[s]);
            MaxReg[r].add(frac[s]);
          }
        p.newCtr(MinReg[r].gEql(MINREG));
        p.newCtr(MaxReg[r].lEql(MAXREG));
      }

      /* Diversification across industry sectors */
      LimSec = new XPRBexpr[NTYPES];
      for (t = 0; t < NTYPES; t++) {
        LimSec[t] = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) if (SEC[t][s]) LimSec[t].add(frac[s]);
        p.newCtr(LimSec[t].lEql(MAXSEC));
      }

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

      /* Limit the total number of assets */
      Num = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Num.add(buy[s]);
      p.newCtr(Num.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s].mul(MAXVAL)));
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].gEql(buy[s].mul(MINVAL)));

      /* Create a mip solution pool and attach it to the problem
      (so it collects the solutions) */
      XPRSmipsolpool msp = new XPRSmipsolpool();
      msp.probAttach(p.getXPRSprob());

      /* Avoid storing of duplicate solutions (3: compare discrete variables only) */
      msp.setIntControl(XPRS.MSP_DUPLICATESOLUTIONSPOLICY, 3);

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.mipOptimize("");

      /* Setup some resources to iterate through the solutions stored
      in the MIP solution pool */
      int nCols = p.getXPRSprob().getIntAttrib(XPRS.ORIGINALCOLS);
      double[] xsol = new double[nCols];

      /* Get the number of solutions and solution IDs from the mip solution pool */
      int nSols = msp.attributes().getSolutions();
      int[] iSolutionIds = new int[nSols];
      msp.getSolList(p.getXPRSprob(), XPRS.MSP_SOLPRB_OBJ, 1, 1, nSols, iSolutionIds, null, null);

      for (i = 0; i < nSols; i++) {
          /* Get the solution */
        msp.getSol(iSolutionIds[i], null, xsol, 0, nCols - 1, null);
        p.loadMIPSol(xsol); /* Load the solution into BCL */
        print_sol(p, i + 1); /* Display the solution */
      }
    }
  }

  /**************************Solution printing****************************/
  static void print_sol(XPRBprob p, int num) {
    int s;
    System.out.println("Solution " + num + ": Total return: " + p.getObjVal());
    for (s = 0; s < NSHARES; s++)
      if (buy[s].getSol() > 0.5)
        System.out.println(
            "  " + SHARES_n[s] + ": " + frac[s].getSol() * 100 + "% (" + buy[s].getSol() + ")");
  }

  /***********************Data input routines***************************/

  /***************************/
  /* Input a list of strings */
  /***************************/
  private static String[] read_str_list(StreamTokenizer st) throws IOException {
    LinkedList<String> l = new LinkedList<String>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_WORD) {
      l.addLast(st.sval);
    }

    String a[] = new String[l.size()];
    l.toArray(a);
    return a;
  }

  /************************/
  /* Input a list of ints */
  /************************/
  private static int[] read_int_list(StreamTokenizer st) throws IOException {
    LinkedList<Integer> l = new LinkedList<Integer>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      l.addLast((int) st.nval);
    }

    int a[] = new int[l.size()];
    for (int i = 0; i < l.size(); i++) a[i] = ((Integer) l.get(i)).intValue();
    return a;
  }

  /****************************/
  /* Input a table of doubles */
  /****************************/
  private static void read_dbl_table(StreamTokenizer st, double tbl[]) throws IOException {
    int n = 0;

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      tbl[n++] = st.nval;
    }
  }

  /************************************/
  /* Input a sparse table of booleans */
  /************************************/
  private static boolean[][] read_bool_table(StreamTokenizer st, int nrow, int ncol)
      throws IOException {
    int i;
    boolean tbl[][] = new boolean[nrow][ncol];

    st.nextToken(); /* Skip ':' */
    for (int r = 0; r < nrow; r++) {
      while (st.nextToken() == st.TT_NUMBER) tbl[r][(int) st.nval] = true;
    }
    return tbl;
  }

  private static void readData() throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(false); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    while (st.nextToken() == st.TT_WORD) {
      if (st.sval.equals("SHARES") && NSHARES == 0) {
        SHARES_n = read_str_list(st);
        NSHARES = SHARES_n.length;
      } else if (st.sval.equals("REGIONS") && NREGIONS == 0) {
        REGIONS_n = read_str_list(st);
        NREGIONS = REGIONS_n.length;
      } else if (st.sval.equals("TYPES") && NTYPES == 0) {
        TYPES_n = read_str_list(st);
        NTYPES = TYPES_n.length;
      } else if (st.sval.equals("RISK") && NRISK == 0) {
        RISK = read_int_list(st);
        NRISK = RISK.length;
      } else if (st.sval.equals("RET") && NSHARES > 0) {
        RET = new double[NSHARES];
        read_dbl_table(st, RET);
      } else if (st.sval.equals("LOC") && NSHARES > 0 && NREGIONS > 0)
        LOC = read_bool_table(st, NREGIONS, NSHARES);
      else if (st.sval.equals("SEC") && NSHARES > 0 && NTYPES > 0)
        SEC = read_bool_table(st, NTYPES, NSHARES);
      else break;
    }

    /*
      for(int i=0;i<NREGIONS;i++) {
      for(int j=0;j<NSHARES;j++)
      System.out.print(" "+LOC[i][j]);
      System.out.println();
      }
    */
    datafile.close();
  }
}

folioenumsol.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file folioenumsol.java
 * ``````````````````````
 * Modeling a MIP problem
 * to perform portfolio optimization.
 *
 * Same model as in foliomip3.java.
 * -- Using the solution enumerator --
 *
 * (c) 2009-2024 Fair Isaac Corporation
 * author: S.Heipcke, Y.Colombani, June 2009, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;
import java.util.*;

public class folioenumsol {
  static final String DATAFILE = "folio10.cdat";

  static final int MAXNUM = 7; /* Max. number of different assets */
  static final double MAXRISK = 1.0 / 3; /* Max. investment into high-risk values */
  static final double MINREG = 0.2; /* Min. investment per geogr. region */
  static final double MAXREG = 0.5; /* Max. investment per geogr. region */
  static final double MAXSEC = 0.25; /* Max. investment per ind. sector */
  static final double MAXVAL = 0.2; /* Max. investment per share */
  static final double MINVAL = 0.1; /* Min. investment per share */

  static int NSHARES; /* Number of shares */
  static int NRISK; /* Number of high-risk shares */
  static int NREGIONS; /* Number of geographical regions */
  static int NTYPES; /* Number of share types */

  static double[] RET; /* Estimated return in investment  */
  static int[] RISK; /* High-risk values among shares */
  static boolean LOC[][]; /* Geogr. region of shares */
  static boolean SEC[][]; /* Industry sector of shares */

  static String SHARES_n[];
  static String REGIONS_n[];
  static String TYPES_n[];

  static XPRBvar[] frac; /* Fraction of capital used per share */
  static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

  public static void main(String[] args) throws IOException, XPRSexception {
    try {
      readData(); /* Read data from file */
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try (XPRBprob p = new XPRBprob("FolioMIP3"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext(); /* Release XPRBexpr instances at end of block. */
        XPRS xprs = new XPRS()) {
        /* Initialize Xpress-Optimizer */
      int s, t, r, i;
      XPRBexpr Risk, Return, Cap, Num, LinkL, LinkU;
      XPRBexpr[] MinReg, MaxReg, LimSec;

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac", XPRB.PL, 0, MAXVAL);
        buy[s] = p.newVar("buy", XPRB.BV);
      }

      /* Objective: total return */
      Return = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Return.add(frac[s].mul(RET[s]));
      p.setObj(Return); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      Risk = new XPRBexpr();
      for (s = 0; s < NRISK; s++) Risk.add(frac[RISK[s]]);
      p.newCtr(Risk.lEql(MAXRISK));

      /* Limits on geographical distribution */
      MinReg = new XPRBexpr[NREGIONS];
      MaxReg = new XPRBexpr[NREGIONS];
      for (r = 0; r < NREGIONS; r++) {
        MinReg[r] = new XPRBexpr();
        MaxReg[r] = new XPRBexpr();
        for (s = 0; s < NSHARES; s++)
          if (LOC[r][s]) {
            MinReg[r].add(frac[s]);
            MaxReg[r].add(frac[s]);
          }
        p.newCtr(MinReg[r].gEql(MINREG));
        p.newCtr(MaxReg[r].lEql(MAXREG));
      }

      /* Diversification across industry sectors */
      LimSec = new XPRBexpr[NTYPES];
      for (t = 0; t < NTYPES; t++) {
        LimSec[t] = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) if (SEC[t][s]) LimSec[t].add(frac[s]);
        p.newCtr(LimSec[t].lEql(MAXSEC));
      }

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

      /* Limit the total number of assets */
      Num = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) Num.add(buy[s]);
      p.newCtr(Num.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s].mul(MAXVAL)));
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].gEql(buy[s].mul(MINVAL)));

      /* Create a mip solution pool & enumerator */
      XPRSmipsolpool msp = new XPRSmipsolpool();
      XPRSmipsolenum mse = new XPRSmipsolenum();

      /* Load matrix into the Optimizer, then hand over to solution enumerator */
      p.loadMat();

      /* Disable heuristics to avoid duplicate solutions being stored */
      p.getXPRSprob().setIntControl(XPRS.HEUREMPHASIS, 0);

      /* Solve the problem */
      IntHolder nMaxSols = new IntHolder(15); // Max. number of solutions to return
      mse.maxim(p.getXPRSprob(), msp, new XPRSdefaultMipSolEnumHandler(), null, nMaxSols);

      /* Setup some resources to iterate through the solutions stored
      in the MIP solution pool */
      int nCols = p.getXPRSprob().getIntAttrib(XPRS.ORIGINALCOLS);
      double[] xsol = new double[nCols];

      /* Get the number of solutions and solution IDs from the mip solution pool */
      int nSols = mse.attributes().getSolutions();
      int[] iSolutionIds = new int[nSols];
      msp.getSolList(p.getXPRSprob(), XPRS.MSP_SOLPRB_OBJ, 1, 1, nSols, iSolutionIds, null, null);

      for (i = 0; i < nSols; i++) {
          /* Get the solution */
        msp.getSol(iSolutionIds[i], null, xsol, 0, nCols - 1, null);
        p.loadMIPSol(xsol); /* Load the solution into BCL */
        print_sol(p, i + 1); /* Display the solution */
      }
    }
  }

  /**************************Solution printing****************************/
  static void print_sol(XPRBprob p, int num) {
    int s;
    System.out.println("Solution " + num + ": Total return: " + p.getObjVal());
    for (s = 0; s < NSHARES; s++)
      if (buy[s].getSol() > 0.5)
        System.out.println(
            "  " + SHARES_n[s] + ": " + frac[s].getSol() * 100 + "% (" + buy[s].getSol() + ")");
  }

  /***********************Data input routines***************************/

  /***************************/
  /* Input a list of strings */
  /***************************/
  private static String[] read_str_list(StreamTokenizer st) throws IOException {
    LinkedList<String> l = new LinkedList<String>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_WORD) {
      l.addLast(st.sval);
    }

    String a[] = new String[l.size()];
    l.toArray(a);
    return a;
  }

  /************************/
  /* Input a list of ints */
  /************************/
  private static int[] read_int_list(StreamTokenizer st) throws IOException {
    LinkedList<Integer> l = new LinkedList<Integer>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      l.addLast((int) st.nval);
    }

    int a[] = new int[l.size()];
    for (int i = 0; i < l.size(); i++) a[i] = ((Integer) l.get(i)).intValue();
    return a;
  }

  /****************************/
  /* Input a table of doubles */
  /****************************/
  private static void read_dbl_table(StreamTokenizer st, double tbl[]) throws IOException {
    int n = 0;

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      tbl[n++] = st.nval;
    }
  }

  /************************************/
  /* Input a sparse table of booleans */
  /************************************/
  private static boolean[][] read_bool_table(StreamTokenizer st, int nrow, int ncol)
      throws IOException {
    int i;
    boolean tbl[][] = new boolean[nrow][ncol];

    st.nextToken(); /* Skip ':' */
    for (int r = 0; r < nrow; r++) {
      while (st.nextToken() == st.TT_NUMBER) tbl[r][(int) st.nval] = true;
    }
    return tbl;
  }

  private static void readData() throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(false); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    while (st.nextToken() == st.TT_WORD) {
      if (st.sval.equals("SHARES") && NSHARES == 0) {
        SHARES_n = read_str_list(st);
        NSHARES = SHARES_n.length;
      } else if (st.sval.equals("REGIONS") && NREGIONS == 0) {
        REGIONS_n = read_str_list(st);
        NREGIONS = REGIONS_n.length;
      } else if (st.sval.equals("TYPES") && NTYPES == 0) {
        TYPES_n = read_str_list(st);
        NTYPES = TYPES_n.length;
      } else if (st.sval.equals("RISK") && NRISK == 0) {
        RISK = read_int_list(st);
        NRISK = RISK.length;
      } else if (st.sval.equals("RET") && NSHARES > 0) {
        RET = new double[NSHARES];
        read_dbl_table(st, RET);
      } else if (st.sval.equals("LOC") && NSHARES > 0 && NREGIONS > 0)
        LOC = read_bool_table(st, NREGIONS, NSHARES);
      else if (st.sval.equals("SEC") && NSHARES > 0 && NTYPES > 0)
        SEC = read_bool_table(st, NTYPES, NSHARES);
      else break;
    }

    /*
      for(int i=0;i<NREGIONS;i++) {
      for(int j=0;j<NSHARES;j++)
      System.out.print(" "+LOC[i][j]);
      System.out.println();
      }
    */
    datafile.close();
  }
}

folioinfeas.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file folioinfeas.java
 * `````````````````````
 * Modeling a MIP problem
 * to perform portfolio optimization.
 *
 * Same model as in foliomip3.java.
 * -- Infeasible model parameter values --
 * -- Handling infeasibility through auxiliary variables --
 *
 * (c) 2009-2024 Fair Isaac Corporation
 * author: S.Heipcke, June 2009, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class folioinfeas {
  static final String DATAFILE = "folio10.cdat";

  static final int MAXNUM = 4; /* Max. number of different assets */
  static final double MAXRISK = 1.0 / 3; /* Max. investment into high-risk values */
  static final double MINREG = 0.3; /* Min. investment per geogr. region */
  static final double MAXREG = 0.5; /* Max. investment per geogr. region */
  static final double MAXSEC = 0.15; /* Max. investment per ind. sector */
  static final double MAXVAL = 0.2; /* Max. investment per share */
  static final double MINVAL = 0.1; /* Min. investment per share */

  static int NSHARES; /* Number of shares */
  static int NRISK; /* Number of high-risk shares */
  static int NREGIONS; /* Number of geographical regions */
  static int NTYPES; /* Number of share types */

  static double[] RET; /* Estimated return in investment  */
  static int[] RISK; /* High-risk values among shares */
  static boolean LOC[][]; /* Geogr. region of shares */
  static boolean SEC[][]; /* Industry sector of shares */

  static String SHARES_n[];
  static String REGIONS_n[];
  static String TYPES_n[];

  static final String[] MIPSTATUS = {
    "not loaded",
    "not optimized",
    "LP optimized",
    "unfinished (no solution)",
    "unfinished (solution found)",
    "infeasible",
    "optimal",
    "unbounded"
  };

  private static DecimalFormat form = new DecimalFormat("0.00");

  public static void main(String[] args) throws IOException {
    try {
      readData(); /* Read data from file */
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try (XPRBprob p = new XPRBprob("FolioMIP3inf"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
      int s, t, r;
      XPRBexpr LinkL, LinkU, le, le2;
      XPRBctr Risk, Return, Cap, Num;
      XPRBctr[] MinReg, MaxReg, LimSec;
      XPRBvar[] frac; /* Fraction of capital used per share */
      XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac", XPRB.PL, 0, MAXVAL);
        buy[s] = p.newVar("buy", XPRB.BV);
      }

      /* Objective: total return */
      le = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) le.add(frac[s].mul(RET[s]));
      Return = p.newCtr("Return", le);
      p.setObj(le); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      le = new XPRBexpr();
      for (s = 0; s < NRISK; s++) le.add(frac[RISK[s]]);
      Risk = p.newCtr("Risk", le.lEql(MAXRISK));

      /* Limits on geographical distribution */
      MinReg = new XPRBctr[NREGIONS];
      MaxReg = new XPRBctr[NREGIONS];
      for (r = 0; r < NREGIONS; r++) {
        le = new XPRBexpr();
        le2 = new XPRBexpr();
        for (s = 0; s < NSHARES; s++)
          if (LOC[r][s]) {
            le.add(frac[s]);
            le2.add(frac[s]);
          }
        MinReg[r] = p.newCtr("MinReg", le.gEql(MINREG));
        MaxReg[r] = p.newCtr("MaxReg", le2.lEql(MAXREG));
      }

      /* Diversification across industry sectors */
      LimSec = new XPRBctr[NTYPES];
      for (t = 0; t < NTYPES; t++) {
        le = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) if (SEC[t][s]) le.add(frac[s]);
        LimSec[t] = p.newCtr("LimSec", le.lEql(MAXSEC));
      }

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

      /* Limit the total number of assets */
      le = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) le.add(buy[s]);
      Num = p.newCtr("Num", le.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s].mul(MAXVAL)));
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].gEql(buy[s].mul(MINVAL)));

      /* Solve the problem */
      p.setSense(XPRB.MAXIM);
      p.mipOptimize("");

      System.out.println("Problem status: " + MIPSTATUS[p.getMIPStat()]);

      if (p.getMIPStat() == XPRB.MIP_INFEAS) {
        System.out.println("Original problem infeasible. Adding deviation variables");

        XPRBvar devRisk, devNum;
        XPRBvar[] devMinReg, devMaxReg, devSec;

        /* Define deviation variables and add them to the constraints
        to make problem solvable */
        devRisk = p.newVar("devRisk");
        Risk.add(devRisk.mul(-1));

        devMinReg = new XPRBvar[NREGIONS];
        devMaxReg = new XPRBvar[NREGIONS];
        for (r = 0; r < NREGIONS; r++) {
            /* Only allow small deviations */
          devMinReg[r] = p.newVar("devMinReg", XPRB.PL, 0, MAXREG / 2);
          MinReg[r].add(devMinReg[r]);
          devMaxReg[r] = p.newVar("devMaxReg", XPRB.PL, 0, MAXREG / 2);
          MaxReg[r].add(devMaxReg[r].mul(-1));
        }

        devSec = new XPRBvar[NTYPES];
        for (t = 0; t < NTYPES; t++) {
          devSec[t] = p.newVar("devSec", XPRB.PL, 0, MAXSEC / 2);
          LimSec[t].add(devSec[t].mul(-1));
        }

        devNum = p.newVar("devNum");
        Num.add(devNum.mul(-1));

        /* Resolve the problem with penalty terms added to the objective */
        double penalty = -10;
        Return.add(devRisk.mul(penalty));
        for (r = 0; r < NREGIONS; r++)
          Return.add(devMinReg[r].mul(penalty).add(devMaxReg[r].mul(penalty)));
        for (t = 0; t < NTYPES; t++) Return.add(devSec[t].mul(penalty));
        Return.add(devNum.mul(penalty));
        p.setObj(Return); /* Set the new objective function */

        p.mipOptimize("");

        if (p.getMIPStat() == XPRB.MIP_INFEAS) {
          System.out.println("No solution after relaxation");
          return;
        } else {
          System.out.println("Constraint violations:");
          System.out.println("  Constraint            Activity  Deviation  Bound(s)");
          System.out.println(
              "  Risk                    "
                  + form.format(Risk.getAct() + devRisk.getSol())
                  + "\t  "
                  + form.format(devRisk.getSol())
                  + "\t  ("
                  + form.format(MAXRISK)
                  + ")");
          for (r = 0; r < NREGIONS; r++) {
            double sumf = 0;
            for (s = 0; s < NSHARES; s++) if (LOC[r][s]) sumf += frac[s].getSol();
            StringBuffer sb = new StringBuffer(REGIONS_n[r] + "          ");
            sb.setLength(11);
            System.out.println(
                "  Region "
                    + sb.toString()
                    + "      "
                    + form.format(sumf)
                    + "\t  "
                    + form.format(devMaxReg[r].getSol() - devMinReg[r].getSol())
                    + "\t  ("
                    + MINREG
                    + "-"
                    + MAXREG
                    + ")");
          }
          for (t = 0; t < NTYPES; t++) {
            StringBuffer sb = new StringBuffer(TYPES_n[t] + "          ");
            sb.setLength(14);
            System.out.println(
                "  Sector "
                    + sb.toString()
                    + "   "
                    + form.format(LimSec[t].getAct() + devSec[t].getSol())
                    + "\t  "
                    + form.format(devSec[t].getSol())
                    + "\t  ("
                    + MAXSEC
                    + ")");
          }
          System.out.println(
              "  Number of assets        "
                  + form.format(Num.getAct() + devNum.getSol())
                  + "\t  "
                  + form.format(devNum.getSol())
                  + "\t  ("
                  + MAXNUM
                  + ")");
        }
      }

      /* Solution printing */
      System.out.println("Total return: " + form.format(p.getObjVal()));
      for (s = 0; s < NSHARES; s++)
        if (buy[s].getSol() > 0.5)
          System.out.println(s + ": " + frac[s].getSol() * 100 + "% (" + buy[s].getSol() + ")");
    }
  }

  /***********************Data input routines***************************/

  /***************************/
  /* Input a list of strings */
  /***************************/
  private static String[] read_str_list(StreamTokenizer st) throws IOException {
    LinkedList<String> l = new LinkedList<String>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_WORD) {
      l.addLast(st.sval);
    }

    String a[] = new String[l.size()];
    l.toArray(a);
    return a;
  }

  /************************/
  /* Input a list of ints */
  /************************/
  private static int[] read_int_list(StreamTokenizer st) throws IOException {
    LinkedList<Integer> l = new LinkedList<Integer>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      l.addLast((int) st.nval);
    }

    int a[] = new int[l.size()];
    for (int i = 0; i < l.size(); i++) a[i] = ((Integer) l.get(i)).intValue();
    return a;
  }

  /****************************/
  /* Input a table of doubles */
  /****************************/
  private static void read_dbl_table(StreamTokenizer st, double tbl[]) throws IOException {
    int n = 0;

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      tbl[n++] = st.nval;
    }
  }

  /************************************/
  /* Input a sparse table of booleans */
  /************************************/
  private static boolean[][] read_bool_table(StreamTokenizer st, int nrow, int ncol)
      throws IOException {
    int i;
    boolean tbl[][] = new boolean[nrow][ncol];

    st.nextToken(); /* Skip ':' */
    for (int r = 0; r < nrow; r++) {
      while (st.nextToken() == st.TT_NUMBER) tbl[r][(int) st.nval] = true;
    }
    return tbl;
  }

  private static void readData() throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(false); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    while (st.nextToken() == st.TT_WORD) {
      if (st.sval.equals("SHARES") && NSHARES == 0) {
        SHARES_n = read_str_list(st);
        NSHARES = SHARES_n.length;
      } else if (st.sval.equals("REGIONS") && NREGIONS == 0) {
        REGIONS_n = read_str_list(st);
        NREGIONS = REGIONS_n.length;
      } else if (st.sval.equals("TYPES") && NTYPES == 0) {
        TYPES_n = read_str_list(st);
        NTYPES = TYPES_n.length;
      } else if (st.sval.equals("RISK") && NRISK == 0) {
        RISK = read_int_list(st);
        NRISK = RISK.length;
      } else if (st.sval.equals("RET") && NSHARES > 0) {
        RET = new double[NSHARES];
        read_dbl_table(st, RET);
      } else if (st.sval.equals("LOC") && NSHARES > 0 && NREGIONS > 0)
        LOC = read_bool_table(st, NREGIONS, NSHARES);
      else if (st.sval.equals("SEC") && NSHARES > 0 && NTYPES > 0)
        SEC = read_bool_table(st, NTYPES, NSHARES);
      else break;
    }

    /*
      for(int i=0;i<NREGIONS;i++) {
      for(int j=0;j<NSHARES;j++)
      System.out.print(" "+LOC[i][j]);
      System.out.println();
      }
    */
    datafile.close();
  }
}

folioiis.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file folioiis.java
 * ``````````````````
 * Modeling a MIP problem
 * to perform portfolio optimization.
 *
 * Same model as in foliomip3.java.
 * -- Infeasible model parameter values --
 * -- Retrieving IIS --
 *
 * (c) 2009-2024 Fair Isaac Corporation
 * author: S.Heipcke, June 2009, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;
import java.util.*;

public class folioiis {
  static final String DATAFILE = "folio10.cdat";

  static final int MAXNUM = 5; /* Max. number of different assets */
  static final double MAXRISK = 1.0 / 3; /* Max. investment into high-risk values */
  static final double MINREG = 0.1; /* Min. investment per geogr. region */
  static final double MAXREG = 0.2; /* Max. investment per geogr. region */
  static final double MAXSEC = 0.1; /* Max. investment per ind. sector */
  static final double MAXVAL = 0.2; /* Max. investment per share */
  static final double MINVAL = 0.1; /* Min. investment per share */

  static int NSHARES; /* Number of shares */
  static int NRISK; /* Number of high-risk shares */
  static int NREGIONS; /* Number of geographical regions */
  static int NTYPES; /* Number of share types */

  static double[] RET; /* Estimated return in investment  */
  static int[] RISK; /* High-risk values among shares */
  static boolean LOC[][]; /* Geogr. region of shares */
  static boolean SEC[][]; /* Industry sector of shares */

  static String SHARES_n[];
  static String REGIONS_n[];
  static String TYPES_n[];

  public static void main(String[] args) throws IOException {
    try {
      readData(); /* Read data from file */
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try (XPRBprob p = new XPRBprob("FolioMIP3inf"); // Initialize BCL and create a new problem */
        XPRS xprs = new XPRS(); // Initialize Xpress-Optimizer */
        XPRBexprContext context = new XPRBexprContext()) // Automatic cleanup of expressions
    {
      int s, t, r;
      XPRBexpr le, le2;
      XPRBctr Risk, Return, Cap, Num;
      XPRBctr[] MinReg, MaxReg, LimSec;
      XPRBvar[] frac; /* Fraction of capital used per share */
      XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac_" + SHARES_n[s], XPRB.PL, 0, MAXVAL);
        buy[s] = p.newVar("buy_" + SHARES_n[s], XPRB.BV);
      }

      /* Objective: total return */
      le = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) le.add(frac[s].mul(RET[s]));
      Return = p.newCtr("Return", le);
      p.setObj(le); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      le = new XPRBexpr();
      for (s = 0; s < NRISK; s++) le.add(frac[RISK[s]]);
      Risk = p.newCtr("Risk", le.lEql(MAXRISK));

      /* Limits on geographical distribution */
      MinReg = new XPRBctr[NREGIONS];
      MaxReg = new XPRBctr[NREGIONS];
      for (r = 0; r < NREGIONS; r++) {
        le = new XPRBexpr();
        le2 = new XPRBexpr();
        for (s = 0; s < NSHARES; s++)
          if (LOC[r][s]) {
            le.add(frac[s]);
            le2.add(frac[s]);
          }
        MinReg[r] = p.newCtr("MinReg(" + REGIONS_n[r] + ")", le.gEql(MINREG));
        MaxReg[r] = p.newCtr("MaxReg(" + REGIONS_n[r] + ")", le2.lEql(MAXREG));
      }

      /* Diversification across industry sectors */
      LimSec = new XPRBctr[NTYPES];
      for (t = 0; t < NTYPES; t++) {
        le = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) if (SEC[t][s]) le.add(frac[s]);
        LimSec[t] = p.newCtr("LimSec(" + TYPES_n[t] + ")", le.lEql(MAXSEC));
      }

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

      /* Limit the total number of assets */
      le = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) le.add(buy[s]);
      Num = p.newCtr("Num", le.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s].mul(MAXVAL)));
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].gEql(buy[s].mul(MINVAL)));

      /* Solve the problem (LP) */
      p.setSense(XPRB.MAXIM);
      p.lpOptimize("");

      if (p.getLPStat() == XPRB.LP_INFEAS) {
        System.out.println("LP infeasible. Retrieving IIS.");

        int numiis = p.getNumIIS(); /* Get the number of independent IIS */
        System.out.println("Number of IIS: " + numiis);

        XPRSprob op = p.getXPRSprob(); /* Retrieve the Optimizer problem */

        ArrayList iisctr, iisvar;
        int numv, numc, i;

        iisvar = new ArrayList();
        iisctr = new ArrayList();

        for (s = 1; s <= numiis; s++) {
          p.getIIS(iisvar, iisctr, s);
          numv = iisvar.size();
          numc = iisctr.size();
          System.out.println("IIS " + s + ":  " + numv + " variables, " + numc + " constraints");
          if (numv > 0) {
              /* Print all variables in the IIS */
            System.out.print("        Variables: ");
            for (i = 0; i < numv; i++) System.out.print(((XPRBvar) iisvar.get(i)).getName() + " ");
            System.out.println();
          }
          if (numc > 0) {
              /* Print all constraints in the IIS */
            System.out.print("        Constraints: ");
            for (i = 0; i < numc; i++) System.out.print(((XPRBctr) iisctr.get(i)).getName() + " ");
            System.out.println();
          }
        }
      }
    }
  }

  /***********************Data input routines***************************/

  /***************************/
  /* Input a list of strings */
  /***************************/
  private static String[] read_str_list(StreamTokenizer st) throws IOException {
    LinkedList<String> l = new LinkedList<String>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_WORD) {
      l.addLast(st.sval);
    }

    String a[] = new String[l.size()];
    l.toArray(a);
    return a;
  }

  /************************/
  /* Input a list of ints */
  /************************/
  private static int[] read_int_list(StreamTokenizer st) throws IOException {
    LinkedList<Integer> l = new LinkedList<Integer>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      l.addLast((int) st.nval);
    }

    int a[] = new int[l.size()];
    for (int i = 0; i < l.size(); i++) a[i] = ((Integer) l.get(i)).intValue();
    return a;
  }

  /****************************/
  /* Input a table of doubles */
  /****************************/
  private static void read_dbl_table(StreamTokenizer st, double tbl[]) throws IOException {
    int n = 0;

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      tbl[n++] = st.nval;
    }
  }

  /************************************/
  /* Input a sparse table of booleans */
  /************************************/
  private static boolean[][] read_bool_table(StreamTokenizer st, int nrow, int ncol)
      throws IOException {
    int i;
    boolean tbl[][] = new boolean[nrow][ncol];

    st.nextToken(); /* Skip ':' */
    for (int r = 0; r < nrow; r++) {
      while (st.nextToken() == st.TT_NUMBER) tbl[r][(int) st.nval] = true;
    }
    return tbl;
  }

  private static void readData() throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(false); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    while (st.nextToken() == st.TT_WORD) {
      if (st.sval.equals("SHARES") && NSHARES == 0) {
        SHARES_n = read_str_list(st);
        NSHARES = SHARES_n.length;
      } else if (st.sval.equals("REGIONS") && NREGIONS == 0) {
        REGIONS_n = read_str_list(st);
        NREGIONS = REGIONS_n.length;
      } else if (st.sval.equals("TYPES") && NTYPES == 0) {
        TYPES_n = read_str_list(st);
        NTYPES = TYPES_n.length;
      } else if (st.sval.equals("RISK") && NRISK == 0) {
        RISK = read_int_list(st);
        NRISK = RISK.length;
      } else if (st.sval.equals("RET") && NSHARES > 0) {
        RET = new double[NSHARES];
        read_dbl_table(st, RET);
      } else if (st.sval.equals("LOC") && NSHARES > 0 && NREGIONS > 0)
        LOC = read_bool_table(st, NREGIONS, NSHARES);
      else if (st.sval.equals("SEC") && NSHARES > 0 && NTYPES > 0)
        SEC = read_bool_table(st, NTYPES, NSHARES);
      else break;
    }

    /*
      for(int i=0;i<NREGIONS;i++) {
      for(int j=0;j<NSHARES;j++)
      System.out.print(" "+LOC[i][j]);
      System.out.println();
      }
    */
    datafile.close();
  }
}

foliomiis.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliomiis.java
 * ```````````````````
 * Modeling a MIP problem
 * to perform portfolio optimization.
 *
 * Same model as in foliomip3.java.
 * -- Infeasible model parameter values --
 * -- Retrieving MIIS --
 *
 * (c) 2011-2024 Fair Isaac Corporation
 * author: S.Heipcke, Mar. 2011, rev. Dec. 2011
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;
import java.util.*;

public class foliomiis {
  static final String DATAFILE = "folio5.cdat";

  static final int MAXNUM = 5; /* Max. number of different assets */
  static final double MAXRISK = 1.0 / 4; /* Max. investment into high-risk values */
  static final double MINREG = 0.1; /* Min. investment per geogr. region */
  static final double MAXREG = 0.25; /* Max. investment per geogr. region */
  static final double MAXSEC = 0.15; /* Max. investment per ind. sector */
  static final double MAXVAL = 0.225; /* Max. investment per share */
  static final double MINVAL = 0.1; /* Min. investment per share */

  static int NSHARES; /* Number of shares */
  static int NRISK; /* Number of high-risk shares */
  static int NREGIONS; /* Number of geographical regions */
  static int NTYPES; /* Number of share types */

  static double[] RET; /* Estimated return in investment  */
  static int[] RISK; /* High-risk values among shares */
  static boolean LOC[][]; /* Geogr. region of shares */
  static boolean SEC[][]; /* Industry sector of shares */

  static String SHARES_n[];
  static String REGIONS_n[];
  static String TYPES_n[];

  public static void main(String[] args) throws IOException {
    try {
      readData(); /* Read data from file */
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try (XPRBprob p =
            new XPRBprob("FolioMIP3inf"); /* Initialize BCL and create a new problem in BCL */
        XPRBexprContext context =
            new XPRBexprContext(); /* Release XPRBexpr instances at end of block. */
        XPRS xprs = new XPRS()) {
        /* Initialize Xpress-Optimizer */
      int s, t, r;
      XPRBexpr LinkL, LinkU, le, le2;
      XPRBctr Risk, Return, Cap, Num;
      XPRBctr[] MinReg, MaxReg, LimSec;
      XPRBvar[] frac; /* Fraction of capital used per share */
      XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac_" + SHARES_n[s], XPRB.PL, 0, MAXVAL);
        buy[s] = p.newVar("buy_" + SHARES_n[s], XPRB.BV);
      }

      /* Objective: total return */
      le = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) le.add(frac[s].mul(RET[s]));
      Return = p.newCtr("Return", le);
      p.setObj(le); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      le = new XPRBexpr();
      for (s = 0; s < NRISK; s++) le.add(frac[RISK[s]]);
      Risk = p.newCtr("Risk", le.lEql(MAXRISK));

      /* Limits on geographical distribution */
      MinReg = new XPRBctr[NREGIONS];
      MaxReg = new XPRBctr[NREGIONS];
      for (r = 0; r < NREGIONS; r++) {
        le = new XPRBexpr();
        le2 = new XPRBexpr();
        for (s = 0; s < NSHARES; s++)
          if (LOC[r][s]) {
            le.add(frac[s]);
            le2.add(frac[s]);
          }
        MinReg[r] = p.newCtr("MinReg(" + REGIONS_n[r] + ")", le.gEql(MINREG));
        MaxReg[r] = p.newCtr("MaxReg(" + REGIONS_n[r] + ")", le2.lEql(MAXREG));
      }

      /* Diversification across industry sectors */
      LimSec = new XPRBctr[NTYPES];
      for (t = 0; t < NTYPES; t++) {
        le = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) if (SEC[t][s]) le.add(frac[s]);
        LimSec[t] = p.newCtr("LimSec(" + TYPES_n[t] + ")", le.lEql(MAXSEC));
      }

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

      /* Limit the total number of assets */
      le = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) le.add(buy[s]);
      Num = p.newCtr("Num", le.lEql(MAXNUM));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s].mul(MAXVAL)));
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].gEql(buy[s].mul(MINVAL)));

      /* Solve the problem (MIP) */
      p.setSense(XPRB.MAXIM);
      p.mipOptimize("");

      if (p.getMIPStat() == XPRB.MIP_INFEAS) {
        System.out.println("MIP infeasible. Retrieving IIS.");

        int numiis = p.getNumIIS(); /* Get the number of independent IIS */
        System.out.println("Number of IIS: " + numiis);

        XPRSprob op = p.getXPRSprob(); /* Retrieve the Optimizer problem */

        ArrayList iisctr, iisvar;
        int numv, numc, i;

        iisvar = new ArrayList();
        iisctr = new ArrayList();

        for (s = 1; s <= numiis; s++) {
          p.getIIS(iisvar, iisctr, s);
          numv = iisvar.size();
          numc = iisctr.size();
          System.out.println("IIS " + s + ":  " + numv + " variables, " + numc + " constraints");
          if (numv > 0) {
              /* Print all variables in the IIS */
            System.out.print("        Variables: ");
            for (i = 0; i < numv; i++) System.out.print(((XPRBvar) iisvar.get(i)).getName() + " ");
            System.out.println();
          }
          if (numc > 0) {
              /* Print all constraints in the IIS */
            System.out.print("        Constraints: ");
            for (i = 0; i < numc; i++) System.out.print(((XPRBctr) iisctr.get(i)).getName() + " ");
            System.out.println();
          }
        }
      } else System.out.println("Feasible or search unfinished!");
    }
  }

  /***********************Data input routines***************************/

  /***************************/
  /* Input a list of strings */
  /***************************/
  private static String[] read_str_list(StreamTokenizer st) throws IOException {
    LinkedList<String> l = new LinkedList<String>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_WORD) {
      l.addLast(st.sval);
    }

    String a[] = new String[l.size()];
    l.toArray(a);
    return a;
  }

  /************************/
  /* Input a list of ints */
  /************************/
  private static int[] read_int_list(StreamTokenizer st) throws IOException {
    LinkedList<Integer> l = new LinkedList<Integer>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      l.addLast((int) st.nval);
    }

    int a[] = new int[l.size()];
    for (int i = 0; i < l.size(); i++) a[i] = ((Integer) l.get(i)).intValue();
    return a;
  }

  /****************************/
  /* Input a table of doubles */
  /****************************/
  private static void read_dbl_table(StreamTokenizer st, double tbl[]) throws IOException {
    int n = 0;

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      tbl[n++] = st.nval;
    }
  }

  /************************************/
  /* Input a sparse table of booleans */
  /************************************/
  private static boolean[][] read_bool_table(StreamTokenizer st, int nrow, int ncol)
      throws IOException {
    int i;
    boolean tbl[][] = new boolean[nrow][ncol];

    st.nextToken(); /* Skip ':' */
    for (int r = 0; r < nrow; r++) {
      while (st.nextToken() == st.TT_NUMBER) tbl[r][(int) st.nval] = true;
    }
    return tbl;
  }

  private static void readData() throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(false); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    while (st.nextToken() == st.TT_WORD) {
      if (st.sval.equals("SHARES") && NSHARES == 0) {
        SHARES_n = read_str_list(st);
        NSHARES = SHARES_n.length;
      } else if (st.sval.equals("REGIONS") && NREGIONS == 0) {
        REGIONS_n = read_str_list(st);
        NREGIONS = REGIONS_n.length;
      } else if (st.sval.equals("TYPES") && NTYPES == 0) {
        TYPES_n = read_str_list(st);
        NTYPES = TYPES_n.length;
      } else if (st.sval.equals("RISK") && NRISK == 0) {
        RISK = read_int_list(st);
        NRISK = RISK.length;
      } else if (st.sval.equals("RET") && NSHARES > 0) {
        RET = new double[NSHARES];
        read_dbl_table(st, RET);
      } else if (st.sval.equals("LOC") && NSHARES > 0 && NREGIONS > 0)
        LOC = read_bool_table(st, NREGIONS, NSHARES);
      else if (st.sval.equals("SEC") && NSHARES > 0 && NTYPES > 0)
        SEC = read_bool_table(st, NTYPES, NSHARES);
      else break;
    }

    /*
      for(int i=0;i<NREGIONS;i++) {
      for(int j=0;j<NSHARES;j++)
      System.out.print(" "+LOC[i][j]);
      System.out.println();
      }
    */
    datafile.close();
  }
}

foliorep.java
/********************************************************
 * Xpress-BCL Java Example Problems
 * ================================
 *
 * file foliorep.java
 * ``````````````````
 * Modeling a MIP problem
 * to perform portfolio optimization.
 *
 * Same model as in foliomip3.java.
 * -- Infeasible model parameter values --
 * -- Retrieving IIS --
 *
 * (c) 2009-2024 Fair Isaac Corporation
 * author: S.Heipcke, June 2009, rev. Nov. 2014
 ********************************************************/

import com.dashoptimization.*;
import java.io.*;
import java.util.*;

public class foliorep {
  static final String DATAFILE = "folio10.cdat";

  static final int MAXNUM = 4; /* Max. number of different assets */
  static final double MAXRISK = 1.0 / 3; /* Max. investment into high-risk values */
  static final double MINREG = 0.3; /* Min. investment per geogr. region */
  static final double MAXREG = 0.5; /* Max. investment per geogr. region */
  static final double MAXSEC = 0.15; /* Max. investment per ind. sector */
  static final double MAXVAL = 0.2; /* Max. investment per share */
  static final double MINVAL = 0.1; /* Min. investment per share */

  static int NSHARES; /* Number of shares */
  static int NRISK; /* Number of high-risk shares */
  static int NREGIONS; /* Number of geographical regions */
  static int NTYPES; /* Number of share types */

  static double[] RET; /* Estimated return in investment  */
  static int[] RISK; /* High-risk values among shares */
  static boolean LOC[][]; /* Geogr. region of shares */
  static boolean SEC[][]; /* Industry sector of shares */

  static String SHARES_n[];
  static String REGIONS_n[];
  static String TYPES_n[];

  static XPRBvar[] frac; /* Fraction of capital used per share */
  static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */

  public static void main(String[] args) throws IOException {
    try {
      readData(); /* Read data from file */
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try (XPRBprob p = new XPRBprob("FolioMIP3inf"); /* Initialize BCL and create a new problem */
        XPRBexprContext context =
            new XPRBexprContext(); /* Release XPRBexpr instances at end of block. */
        XPRS xprs = new XPRS()) {
        /* Initialize Xpress-Optimizer */
      int s, t, r;
      XPRBexpr LinkL, LinkU, le, le2;
      XPRBctr Risk, Return, Cap, Num;
      XPRBctr[] MinReg, MaxReg, LimSec;
      ArrayList<XPRBctr> AllCtrs =
          new ArrayList<XPRBctr>(); /* Array of ctrs that will be allowed to be relaxed */

      /* Create the decision variables */
      frac = new XPRBvar[NSHARES];
      buy = new XPRBvar[NSHARES];
      for (s = 0; s < NSHARES; s++) {
        frac[s] = p.newVar("frac_" + SHARES_n[s], XPRB.PL, 0, MAXVAL);
        buy[s] = p.newVar("buy_" + SHARES_n[s], XPRB.BV);
      }

      /* Objective: total return */
      le = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) le.add(frac[s].mul(RET[s]));
      Return = p.newCtr("Return", le);
      p.setObj(le); /* Set the objective function */

      /* Limit the percentage of high-risk values */
      le = new XPRBexpr();
      for (s = 0; s < NRISK; s++) le.add(frac[RISK[s]]);
      AllCtrs.add(Risk = p.newCtr("Risk", le.lEql(MAXRISK)));

      /* Limits on geographical distribution */
      MinReg = new XPRBctr[NREGIONS];
      MaxReg = new XPRBctr[NREGIONS];
      for (r = 0; r < NREGIONS; r++) {
        le = new XPRBexpr();
        le2 = new XPRBexpr();
        for (s = 0; s < NSHARES; s++)
          if (LOC[r][s]) {
            le.add(frac[s]);
            le2.add(frac[s]);
          }
        AllCtrs.add(MinReg[r] = p.newCtr("MinReg(" + REGIONS_n[r] + ")", le.gEql(MINREG)));
        AllCtrs.add(MaxReg[r] = p.newCtr("MaxReg(" + REGIONS_n[r] + ")", le2.lEql(MAXREG)));
      }

      /* Diversification across industry sectors */
      LimSec = new XPRBctr[NTYPES];
      for (t = 0; t < NTYPES; t++) {
        le = new XPRBexpr();
        for (s = 0; s < NSHARES; s++) if (SEC[t][s]) le.add(frac[s]);
        AllCtrs.add(LimSec[t] = p.newCtr("LimSec(" + TYPES_n[t] + ")", le.lEql(MAXSEC)));
      }

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

      /* Limit the total number of assets */
      le = new XPRBexpr();
      for (s = 0; s < NSHARES; s++) le.add(buy[s]);
      AllCtrs.add(Num = p.newCtr("Num", le.lEql(MAXNUM)));

      /* Linking the variables */
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].lEql(buy[s].mul(MAXVAL)));
      for (s = 0; s < NSHARES; s++) p.newCtr(frac[s].gEql(buy[s].mul(MINVAL)));

      /* Solve the problem (LP) */
      p.setMsgLevel(1);
      p.setSense(XPRB.MAXIM);
      p.lpOptimize("");

      if (p.getLPStat() == XPRB.LP_INFEAS) {
        System.out.println("LP infeasible. Start infeasibility repair.");

        XPRSprob op = p.getXPRSprob(); /* Retrieve the Optimizer problem */

        /* Must use the weighted infeasibility repair method since
        only some constraints of each type may be relaxed */

        /*
          lrp: (affects = and <= rows)
          ax - aux_var  = b
          ax - aux_var <= b
          grp: (affects = and >= rows)
          ax + aux_var  = b
          ax + aux_var >= b
          lbp:
          x_i + aux_var >= l
          ubp:
          x_i - aux_var <= u
        */

        int nrow, ncol;
        double[] lrp, grp, lbp, ubp;
        ncol = op.getIntAttrib(XPRS.ORIGINALCOLS);
        nrow = op.getIntAttrib(XPRS.ORIGINALROWS);
        lrp = new double[nrow];
        grp = new double[nrow];
        lbp = new double[ncol];
        ubp = new double[ncol];

        lrp[Risk.getRowNum()] = 1;
        for (r = 0; r < NREGIONS; r++) lrp[MaxReg[r].getRowNum()] = 1;
        for (t = 0; t < NTYPES; t++) lrp[LimSec[t].getRowNum()] = 1;
        lrp[Num.getRowNum()] = 1;
        for (r = 0; r < NREGIONS; r++) grp[MinReg[r].getRowNum()] = 1;

        String rstat[] = {
          "relaxed optimum found", "relaxed problem infeasible",
          "relaxed problem unbounded", "solution nonoptimal for original objective",
          "error", "numerical instability"
        };
        IntHolder repstatus = new IntHolder();
        for (double delta = 0.001; delta < 10; delta *= 10) {
          op.repairWeightedInfeas(repstatus, lrp, grp, lbp, ubp, 'd', delta, "");
          System.out.println("delta = " + delta + ": Status: " + rstat[repstatus.value]);
          if (repstatus.value == 0) {
            p.sync(XPRB.XPRS_SOLMIP);
            print_sol_opt(p);
            print_violated(AllCtrs);
          }
          System.out.println();
        }
      }
    }
  }

  /**************************Solution printing****************************/

  static void print_sol_opt(XPRBprob p) {
    System.out.println("  Total return: " + p.getObjVal());
    for (int s = 0; s < NSHARES; s++)
      if (buy[s].getSol() > 0.5)
        System.out.printf(
            "  %s: %g%% (%g)\n", SHARES_n[s], frac[s].getSol() * 100, buy[s].getSol());
  }

  static void print_violated(ArrayList<XPRBctr> ctrs) {
    System.out.println(" Violated (relaxed) constraints:");
    for (XPRBctr c : ctrs) {
      String type;
      double viol, slack = c.getSlack();
      switch (c.getType()) {
        case XPRB.E:
          viol = Math.abs(slack);
          type = " =";
          break;
        case XPRB.G:
          viol = slack;
          type = ">=";
          break;
        case XPRB.L:
          viol = -slack;
          type = "<=";
          break;
        default:
          System.out.println("  unexpected constraint type");
          continue;
      }
      if (viol > 1e-6) System.out.printf("  %s constraint %s by %g\n", type, c.getName(), -slack);
    }
  }

  /***********************Data input routines***************************/

  /***************************/
  /* Input a list of strings */
  /***************************/
  private static String[] read_str_list(StreamTokenizer st) throws IOException {
    LinkedList<String> l = new LinkedList<String>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_WORD) {
      l.addLast(st.sval);
    }

    String a[] = new String[l.size()];
    l.toArray(a);
    return a;
  }

  /************************/
  /* Input a list of ints */
  /************************/
  private static int[] read_int_list(StreamTokenizer st) throws IOException {
    LinkedList<Integer> l = new LinkedList<Integer>();

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      l.addLast((int) st.nval);
    }

    int a[] = new int[l.size()];
    for (int i = 0; i < l.size(); i++) a[i] = ((Integer) l.get(i)).intValue();
    return a;
  }

  /****************************/
  /* Input a table of doubles */
  /****************************/
  private static void read_dbl_table(StreamTokenizer st, double tbl[]) throws IOException {
    int n = 0;

    st.nextToken(); /* Skip ':' */
    while (st.nextToken() == st.TT_NUMBER) {
      tbl[n++] = st.nval;
    }
  }

  /************************************/
  /* Input a sparse table of booleans */
  /************************************/
  private static boolean[][] read_bool_table(StreamTokenizer st, int nrow, int ncol)
      throws IOException {
    int i;
    boolean tbl[][] = new boolean[nrow][ncol];

    st.nextToken(); /* Skip ':' */
    for (int r = 0; r < nrow; r++) {
      while (st.nextToken() == st.TT_NUMBER) tbl[r][(int) st.nval] = true;
    }
    return tbl;
  }

  private static void readData() throws IOException {
    int s;
    FileReader datafile = null;
    StreamTokenizer st = null;

    datafile = new FileReader(DATAFILE); /* Open the data file */
    st = new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
    st.commentChar('!'); /* Use the character '!' for comments */
    st.eolIsSignificant(false); /* Return end-of-line character */
    st.parseNumbers(); /* Read numbers as numbers (not strings) */

    while (st.nextToken() == st.TT_WORD) {
      if (st.sval.equals("SHARES") && NSHARES == 0) {
        SHARES_n = read_str_list(st);
        NSHARES = SHARES_n.length;
      } else if (st.sval.equals("REGIONS") && NREGIONS == 0) {
        REGIONS_n = read_str_list(st);
        NREGIONS = REGIONS_n.length;
      } else if (st.sval.equals("TYPES") && NTYPES == 0) {
        TYPES_n = read_str_list(st);
        NTYPES = TYPES_n.length;
      } else if (st.sval.equals("RISK") && NRISK == 0) {
        RISK = read_int_list(st);
        NRISK = RISK.length;
      } else if (st.sval.equals("RET") && NSHARES > 0) {
        RET = new double[NSHARES];
        read_dbl_table(st, RET);
      } else if (st.sval.equals("LOC") && NSHARES > 0 && NREGIONS > 0)
        LOC = read_bool_table(st, NREGIONS, NSHARES);
      else if (st.sval.equals("SEC") && NSHARES > 0 && NTYPES > 0)
        SEC = read_bool_table(st, NTYPES, NSHARES);
      else break;
    }

    /*
      for(int i=0;i<NREGIONS;i++) {
      for(int j=0;j<NSHARES;j++)
      System.out.print(" "+LOC[i][j]);
      System.out.println();
      }
    */
    datafile.close();
  }
}

© 2001-2024 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.