/******************************************************** Xpress-BCL C++ Example Problems =============================== file folioinfeas.cpp ```````````````````` Modeling a MIP problem to perform portfolio optimization. Same model as in foliomip3.cpp. -- Infeasible model parameter values -- -- Handling infeasibility through auxiliary variables -- (c) 2009-2024 Fair Isaac Corporation author: S.Heipcke, June 2009, rev. Mar. 2011 ********************************************************/ #include #include #include #include #include #include "xprb_cpp.h" using namespace std; using namespace ::dashoptimization; #define MAXNUM 4 // Max. number of different assets #define MAXRISK 1/3 // Max. investment into high-risk values #define MINREG 0,3 // Min. investment per geogr. region #define MAXREG 0,5 // Max. investment per geogr. region #define MAXSEC 0,15 // 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; #include "readfoliodata.c_" int main(int argc, char **argv) { int s,r,t; XPRBprob p("FolioMIP3"); // Initialize a new problem in BCL XPRBctr Risk,Return,Num,*MinReg, *MaxReg, *LimSec; XPRBexpr le, le2, Cap, LinkL, LinkU; XPRBvar *frac; // Fraction of capital used per share XPRBvar *buy; // 1 if asset is in portfolio, 0 otherwise 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) { le += frac[s]; le2 += frac[s]; } MinReg[r] = p.newCtr(le >= MINREG); MaxReg[r] = p.newCtr(le2 <= MAXREG); } // Diversification across industry sectors LimSec = new XPRBctr[NTYPES]; for(t=0;t0) le += frac[s]; LimSec[t] = p.newCtr(le <= MAXSEC); } // Spend all the capital for(s=0;s= MINVAL*buy[s]); } // Solve the problem p.setSense(XPRB_MAXIM); p.mipOptimize(""); char *MIPSTATUS[] = {"not loaded", "not optimized", "LP optimized", "unfinished (no solution)", "unfinished (solution found)", "infeasible", "optimal", "unbounded"}; cout << "Problem status: " << MIPSTATUS[p.getMIPStat()] << endl; if (p.getMIPStat()==XPRB_MIP_INFEAS) { cout << "Original problem infeasible. Adding deviation variables" << endl; // Define deviation variables and add them to the constraints // to make problem solvable */ XPRBvar devRisk, devNum, *devMinReg, *devMaxReg, *devSec; devRisk = p.newVar("devRisk"); Risk -= devRisk; devMinReg = new XPRBvar[NREGIONS]; devMaxReg = new XPRBvar[NREGIONS]; for(r=0;r0) sumf+=frac[s].getSol(); printf(" Region %-11s %1,2f\t %1,2f\t (%g-%g)\n", REGIONS_n[r], sumf, devMaxReg[r].getSol()-devMinReg[r].getSol(), MINREG, MAXREG); } for(t=0;t0,5) cout << SHARES_n[s] << ": " << frac[s].getSol()*100 << "% (" << buy[s].getSol() << ")" << endl; return 0; }