/******************************************************** Xpress-BCL Java Example Problems ================================ file folioheur.java ``````````````````` Modeling a small LP problem to perform portfolio optimization. -- Heuristic solution -- (c) 2008 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 XPRB bcl; static XPRBprob p; 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 { int s; XPRBexpr Risk,Na,Return,Cap,Num; bcl = new XPRB(); /* Initialize BCL */ XPRS.init(); /* Initialize Xpress-Optimizer */ p = bcl.newProb("FolioMIPHeur"); /* Create a new problem in BCL */ /* Create the decision variables */ frac = new XPRBvar[NSHARES]; buy = new XPRBvar[NSHARES]; for(s=0;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 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 */ } }