/******************************************************** Xpress-BCL C++ Example Problems =============================== file foliosolpool.cpp ``````````````````` Modeling a MIP problem to perform portfolio optimization. Same model as in foliomip3.cpp. -- Using the MIP solution pool -- (c) 2009 Fair Isaac Corporation author: S.Heipcke, May 2009, rev. Mar. 2011 ********************************************************/ #include #include #include #include #include #include "xprb_cpp.h" #include "xprs.h" using namespace std; using namespace ::dashoptimization; #define MAXNUM 7 // Max. number of different assets #define MAXRISK 1/3 // Max. investment into high-risk values #define MINREG 0,2 // Min. investment per geogr. region #define MAXREG 0,5 // Max. investment per geogr. region #define MAXSEC 0,25 // Max. investment per ind. sector #define MAXVAL 0,2 // Max. investment per share #define MINVAL 0,1 // Min. investment per share #define DATAFILE "folio10.cdat" // File with problem data #define MAXENTRIES 10000 int NSHARES; // Number of shares int NRISK; // Number of high-risk shares int NREGIONS; // Number of geographical regions int NTYPES; // Number of share types double *RET; // Estimated return in investment int *RISK; // High-risk values among shares char **LOC; // Geogr. region of shares char **SECT; // Industry sector of shares char **SHARES_n; char **REGIONS_n; char **TYPES_n; XPRBvar *frac; // Fraction of capital used per share XPRBvar *buy; // 1 if asset is in portfolio, 0 otherwise XPRBctr Return; #include "readfoliodata.c_" void print_sol(int num); int main(int argc, char **argv) { int s,r,t; XPRBprob p("FolioMIP3"); // Initialize a new problem in BCL XPRBexpr Risk,Cap,Num,le; XPRBexpr *MinReg, *MaxReg, *LimSec, LinkL, LinkU; readdata(DATAFILE); // Data input from file // Create the decision variables (including upper bounds for `frac') frac = new XPRBvar[NSHARES]; buy = new XPRBvar[NSHARES]; for(s=0;s0) { MinReg[r] += frac[s]; MaxReg[r] += frac[s]; } p.newCtr(MinReg[r] >= MINREG); p.newCtr(MaxReg[r] <= MAXREG); } // Diversification across industry sectors LimSec = new XPRBexpr[NTYPES]; for(t=0;t0) LimSec[t] += frac[s]; p.newCtr(LimSec[t] <= MAXSEC); } // Spend all the capital for(s=0;s= MINVAL*buy[s]); } // Create a MIP solution pool and attach it to the problem // (so it collects the solutions) XPRSmipsolpool msp; XPRS_msp_create(&msp); XPRS_msp_probattach(msp, p.getXPRSprob()); // Avoid storing of duplicate solutions (3: compare discrete variables only) XPRS_msp_setintcontrol(msp, 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 nSols, nCols, i; double *xsol; int *solIDs; XPRSgetintattrib(p.getXPRSprob(), XPRS_ORIGINALCOLS, &nCols); XPRS_msp_getintattrib(msp, XPRS_MSP_SOLUTIONS, &nSols); xsol = (double *) malloc(nCols * sizeof(double)); solIDs = (int *) malloc(nSols * sizeof(int)); // Get the solution IDs XPRS_msp_getsollist(msp, p.getXPRSprob(), XPRS_MSP_SOLPRB_OBJ, 1, 1, nSols, solIDs, &nSols, NULL); // Display all solutions from the pool for(i=0; i0,5) cout << " " << SHARES_n[s] << ": " << frac[s].getSol()*100 << "% (" << buy[s].getSol() << ")" << endl; }