| |||||||||
Folio - Examples from 'Getting Started' Description Different versions of a portfolio optimization problem. Basic modelling and solving tasks:
Source Files By clicking on a file name, a preview is opened at the bottom of this page. Data Files folioenumsol.cpp /******************************************************** Xpress-BCL C++ Example Problems =============================== file folioenumsol.cpp ````````````````````` Modeling a MIP problem to perform portfolio optimization. Same model as in foliomip3.cpp. -- Using the solution enumerator -- (c) 2009-2024 Fair Isaac Corporation author: S.Heipcke, June 2009, rev. Jul. 2014 ********************************************************/ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include "xprb_cpp.h" #include "xprs.h" #include "xprs_mse_defaulthandler.h" using namespace std; using namespace ::dashoptimization; #define MAXNUM 7 // Max. number of different assets #define MAXRISK 1.0/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;s<NSHARES;s++) { frac[s] = p.newVar("frac", XPRB_PL, 0, MAXVAL); buy[s] = p.newVar("buy", XPRB_BV); } // Objective: total return for(s=0;s<NSHARES;s++) le += RET[s]*frac[s]; Return = p.newCtr(le); p.setObj(Return); // Set the objective function // Limit the percentage of high-risk values for(s=0;s<NRISK;s++) Risk += frac[RISK[s]]; p.newCtr(Risk <= MAXRISK); // Limits on geographical distribution MinReg = new XPRBexpr[NREGIONS]; MaxReg = new XPRBexpr[NREGIONS]; for(r=0;r<NREGIONS;r++) { for(s=0;s<NSHARES;s++) if(LOC[r][s]>0) { 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;t<NTYPES;t++) { for(s=0;s<NSHARES;s++) if(SECT[t][s]>0) LimSec[t] += frac[s]; p.newCtr(LimSec[t] <= MAXSEC); } // Spend all the capital for(s=0;s<NSHARES;s++) Cap += frac[s]; p.newCtr(Cap == 1); // Limit the total number of assets for(s=0;s<NSHARES;s++) Num += buy[s]; p.newCtr(Num <= MAXNUM); // Linking the variables for(s=0;s<NSHARES;s++) { p.newCtr(frac[s] <= MAXVAL*buy[s]); p.newCtr(frac[s] >= MINVAL*buy[s]); } // Create a MIP solution pool & enumerator XPRSmipsolpool msp; XPRSmipsolenum mse; XPRS_mse_create(&mse); XPRS_msp_create(&msp); // Load matrix into the Optimizer, then hand over to sol. enumerator p.loadMat(); // Disable heuristics to avoid duplicate solutions being stored XPRSsetintcontrol(p.getXPRSprob(), XPRS_HEUREMPHASIS, 0); int nMaxSols = 15; // Max. number of solutions to return // Solve the problem XPRS_mse_maxim(mse, p.getXPRSprob(), msp, XPRS_mse_defaulthandler, NULL, &nMaxSols); // 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_mse_getintattrib(mse, XPRS_MSE_SOLUTIONS, &nSols); xsol = (double *) malloc(nCols * sizeof(double)); solIDs = (int *) malloc(nSols * sizeof(int)); // Get the solution IDs XPRS_mse_getsollist(mse, XPRS_MSE_METRIC_MIPOBJECT, 1, nSols, solIDs, &nSols, NULL); // Display all solutions from the pool for(i=0; i<nSols; i++) { // Get the solution XPRS_msp_getsol(msp, solIDs[i], NULL, xsol, 0, nCols - 1, NULL); p.loadMIPSol(xsol, nCols, 0); // Load the solution into BCL print_sol(i+1); // Display the solution } free(xsol); XPRS_mse_destroy(mse); XPRS_msp_destroy(msp); return 0; } // Solution printing void print_sol(int num) { int s; cout << "Solution " << num << ": Total return: " << Return.getAct() << endl; for(s=0;s<NSHARES;s++) if(buy[s].getSol()>0.5) cout << " " << SHARES_n[s] << ": " << frac[s].getSol()*100 << "% (" << buy[s].getSol() << ")" << endl; } | |||||||||
© Copyright 2024 Fair Isaac Corporation. |