/******************************************************** 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-2023 Fair Isaac Corporation author: S.Heipcke, Y.Colombani, rev. Dec. 2011 ********************************************************/ import java.io.*; import java.lang.*; import java.util.*; import com.dashoptimization.*; public class foliomip3 { static final String DATAFILE = "folio10.cdat"; static final int MAXNUM = 7; /* Max. number of different assets */ static final double MAXRISK = 1/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 { 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 */ 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 */ /* Create the decision variables */ frac = new XPRBvar[NSHARES]; buy = new XPRBvar[NSHARES]; for(s=0;s0,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 l=new LinkedList(); 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 l=new LinkedList(); st.nextToken(); /* Skip ':' */ while(st.nextToken()==st.TT_NUMBER) { l.addLast((int)st.nval); } int a[]=new int[l.size()]; for(int i=0;i0) { 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