foliolp.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file foliolp.cpp
````````````````
Modeling a small LP problem
to perform portfolio optimization.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Aug. 2003, rev. Mar. 2011
********************************************************/
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#define NSHARES 10 // Number of shares
#define NRISK 5 // Number of high-risk shares
#define NNA 4 // Number of North-American shares
double RET[] = {5,17,26,12,8,9,7,6,31,21}; // Estimated return in investment
int RISK[] = {1,2,3,8,9}; // High-risk values among shares
int NA[] = {0,1,2,3}; // Shares issued in N.-America
int main(int argc, char **argv)
{
int s;
XPRBprob p("FolioLP"); // Initialize a new problem in BCL
XPRBexpr Risk,Na,Return,Cap;
XPRBvar frac[NSHARES]; // Fraction of capital used per share
// Create the decision variables
for(s=0;s= 0.5);
// Spend all the capital
for(s=0;s |
|
folioinit.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file folioinit.cpp
``````````````````
Modeling a small LP problem
to perform portfolio optimization.
Explicit initialization.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Aug. 2003, rev. Mar. 2011
********************************************************/
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#define NSHARES 10 // Number of shares
#define NRISK 5 // Number of high-risk shares
#define NNA 4 // Number of North-American shares
double RET[] = {5,17,26,12,8,9,7,6,31,21}; // Estimated return in investment
int RISK[] = {1,2,3,8,9}; // High-risk values among shares
int NA[] = {0,1,2,3}; // Shares issued in N.-America
void solveProb()
{
int s;
XPRBprob p("FolioLP"); // Initialize a new problem in BCL
XPRBexpr Risk,Na,Return,Cap;
XPRBvar frac[NSHARES]; // Fraction of capital used per share
// Create the decision variables
for(s=0;s= 0.5);
// Spend all the capital
for(s=0;s |
|
foliodata.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file foliodata.cpp
``````````````````
Modeling a small LP problem
to perform portfolio optimization.
-- Data input from file --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Aug. 2003, rev. Mar. 2011
********************************************************/
#include
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#define DATAFILE XPRBDATAPATH "/GS/foliocpplp.dat"
#define NSHARES 10 // Number of shares
#define NRISK 5 // Number of high-risk shares
#define NNA 4 // Number of North-American shares
double RET[NSHARES]; // Estimated return in investment
char RISK[][100] = {"hardware", "theater", "telecom", "software",
"electronics"}; // High-risk values among shares
char NA[][100] = {"treasury", "hardware", "theater", "telecom"};
// Shares issued in N.-America
XPRBindexSet SHARES; // Set of shares
XPRBprob p("FolioLP"); // Initialize a new problem in BCL
void readData(void)
{
double value;
int s;
FILE *datafile;
char name[100];
SHARES=p.newIndexSet("Shares",NSHARES); // Create the `SHARES' index set
// Read `RET' data from file
datafile=fopen(DATAFILE,"r");
for(s=0;s= 0.5);
// Spend all the capital
for(s=0;s |
|
foliomip1.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file foliomip1.cpp
``````````````````
Modeling a small MIP problem
to perform portfolio optimization.
-- Limiting the total number of assets --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Aug. 2003, rev. Mar. 2011
********************************************************/
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#define MAXNUM 4 // Max. number of different assets
#define NSHARES 10 // Number of shares
#define NRISK 5 // Number of high-risk shares
#define NNA 4 // Number of North-American shares
double RET[] = {5,17,26,12,8,9,7,6,31,21}; // Estimated return in investment
int RISK[] = {1,2,3,8,9}; // High-risk values among shares
int NA[] = {0,1,2,3}; // Shares issued in N.-America
int main(int argc, char **argv)
{
int s;
XPRBprob p("FolioMIP1"); // Initialize a new problem in BCL
XPRBexpr Risk,Na,Return,Cap,Num;
XPRBvar frac[NSHARES]; // Fraction of capital used per share
XPRBvar buy[NSHARES]; // 1 if asset is in portfolio, 0 otherwise
// Create the decision variables (including upper bounds for `frac')
for(s=0;s= 0.5);
// Spend all the capital
for(s=0;s |
|
foliomip2.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file foliomip2.cpp
``````````````````
Modeling a small MIP problem
to perform portfolio optimization.
-- Imposing a minimum investment per share --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Aug. 2003, rev. Mar. 2011
********************************************************/
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#define NSHARES 10 // Number of shares
#define NRISK 5 // Number of high-risk shares
#define NNA 4 // Number of North-American shares
double RET[] = {5,17,26,12,8,9,7,6,31,21}; // Estimated return in investment
int RISK[] = {1,2,3,8,9}; // High-risk values among shares
int NA[] = {0,1,2,3}; // Shares issued in N.-America
int main(int argc, char **argv)
{
int s;
XPRBprob p("FolioSC"); // Initialize a new problem in BCL
XPRBexpr Risk,Na,Return,Cap;
XPRBvar frac[NSHARES]; // Fraction of capital used per share
// Create the decision variables
for(s=0;s= 0.5);
// Spend all the capital
for(s=0;s |
|
folioqp.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file folioqp.cpp
````````````````
Modeling a small QP problem
to perform portfolio optimization.
-- 1. QP: minimize variance
2. MIQP: limited number of assets ---
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Aug. 2003, rev. Mar. 2011
********************************************************/
#include
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#define DATAFILE XPRBDATAPATH "/GS/foliocppqp.dat"
#define TARGET 9 // Target yield
#define MAXNUM 4 // Max. number of different assets
#define NSHARES 10 // Number of shares
#define NNA 4 // Number of North-American shares
double RET[] = {5,17,26,12,8,9,7,6,31,21}; // Estimated return in investment
int NA[] = {0,1,2,3}; // Shares issued in N.-America
double VAR[NSHARES][NSHARES]; // Variance/covariance matrix of
// estimated returns
int main(int argc, char **argv)
{
int s,t;
XPRBprob p("FolioQP"); // Initialize a new problem in BCL
XPRBexpr Na,Return,Cap,Num,Variance;
XPRBvar frac[NSHARES]; // Fraction of capital used per share
XPRBvar buy[NSHARES]; // 1 if asset is in portfolio, 0 otherwise
FILE *datafile;
// Read `VAR' data from file
datafile=fopen(DATAFILE,"r");
for(s=0;s= 0.5);
// Spend all the capital
for(s=0;s= TARGET);
// Solve the problem
p.setSense(XPRB_MINIM);
p.lpOptimize("");
// Solution printing
cout << "With a target of " << TARGET << " minimum variance is " <<
p.getObjVal() << endl;
for(s=0;s |
|
folioqc.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file folioqp.cpp
````````````````
Modeling a small QCQP problem
to perform portfolio optimization.
-- Maximize return with limit on variance ---
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, July 2008, rev. Mar. 2011
********************************************************/
#include
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#define DATAFILE XPRBDATAPATH "/GS/foliocppqp.dat"
#define MAXVAR 0.55 // Max. allowed variance
#define NSHARES 10 // Number of shares
#define NNA 4 // Number of North-American shares
double RET[] = {5,17,26,12,8,9,7,6,31,21}; // Estimated return in investment
int NA[] = {0,1,2,3}; // Shares issued in N.-America
double VAR[NSHARES][NSHARES]; // Variance/covariance matrix of
// estimated returns
int main(int argc, char **argv)
{
int s,t;
XPRBprob p("FolioQC"); // Initialize a new problem in BCL
XPRBexpr Na,Return,Cap,Variance;
XPRBvar frac[NSHARES]; // Fraction of capital used per share
FILE *datafile;
// Read `VAR' data from file
datafile=fopen(DATAFILE,"r");
for(s=0;s= 0.5);
// Spend all the capital
for(s=0;s |
|
folioheur.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file folioheur.cpp
``````````````````
Modeling a small MIP problem
to perform portfolio optimization.
-- Heuristic solution --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Aug. 2003, rev. Mar. 2011
********************************************************/
#include
#include "xprb_cpp.h"
#include "xprs.h"
using namespace std;
using namespace ::dashoptimization;
#define MAXNUM 4 // Max. number of shares to be selected
#define NSHARES 10 // Number of shares
#define NRISK 5 // Number of high-risk shares
#define NNA 4 // Number of North-American shares
void solveHeur();
double RET[] = {5,17,26,12,8,9,7,6,31,21}; // Estimated return in investment
int RISK[] = {1,2,3,8,9}; // High-risk values among shares
int NA[] = {0,1,2,3}; // Shares issued in N.-America
XPRBprob p("FolioMIPHeur"); // Initialize a new problem in BCL
XPRBvar frac[NSHARES]; // Fraction of capital used per share
XPRBvar buy[NSHARES]; // 1 if asset is in portfolio, 0 otherwise
int main(int argc, char **argv)
{
int s;
XPRBexpr Risk,Na,Return,Cap,Num;
// Create the decision variables (including upper bounds for `frac')
for(s=0;s= 0.5);
// Spend all the capital
for(s=0;s 0.2-TOL) buy[s].setLB(1);
}
p.mipOptimize("c"); // Solve the MIP-problem
ifgsol=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
cout << "Heuristic solution: Total return: " << p.getObjVal() << endl;
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.reset(); // No need to store the saved basis any longer
if(ifgsol==1)
XPRSsetdblcontrol(p.getXPRSprob(), XPRS_MIPABSCUTOFF, solval+TOL);
// Set the cutoff to the best known solution
}
|
|
foliomip3.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file foliomip3.cpp
``````````````````
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 Fair Isaac Corporation
author: S.Heipcke, May 2009, rev. Mar. 2011
********************************************************/
#include
#include
#include
#include
#include
#include "xprb_cpp.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;
#include "readfoliodata.c_"
int main(int argc, char **argv)
{
int s,r,t;
XPRBprob p("FolioMIP3"); // Initialize a new problem in BCL
XPRBexpr Risk,Return,Cap,Num;
XPRBexpr *MinReg, *MaxReg, *LimSec, 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)
{
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]);
}
// 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;
// Solution printing
cout << "Total return: " << p.getObjVal() << endl;
for(s=0;s0.5)
cout << SHARES_n[s] << ": " << frac[s].getSol()*100 << "% (" << buy[s].getSol()
<< ")" << endl;
return 0;
}
|
|
readfoliodata.c_ |
/********************************************************
Xpress-BCL C Example Problems
=============================
file readfoliodata.c_
`````````````````````
Include file reading data for model foliomip3
from a text file.
(c) 2009 Fair Isaac Corporation
author: Y.Colombani, May 2009
********************************************************/
/****************************************************/
/* Skip empty lines & comments and find next record */
/****************************************************/
int nextrec(FILE *f,char *rec)
{
int c;
do
{
c=fgetc(f);
if(c==EOF) return 0;
else
if(c=='!')
{
do
{
c=fgetc(f);
if(c==EOF) return 0;
}
while(c!='\n');
}
} while (isspace(c));
rec[0]=c;
return fscanf(f,"%128s",rec+1);
}
/***************************/
/* Input a list of strings */
/***************************/
int read_str_list(FILE *f,char ***list,int *size)
{
int n;
char word[128];
char *buf[MAXENTRIES];
n=0;
while(fscanf(f,"%128s",word)>0)
if(strcmp(word,";")==0) break;
else
if(word[strlen(word)-1]==';')
{
word[strlen(word)-1]='\0';
buf[n++]=strdup(word);
break;
}
else
buf[n++]=strdup(word);
*size=n;
*list=(char **)malloc(sizeof(char *)*n);
memcpy(*list,buf,sizeof(char *)*n);
return 0;
}
/************************/
/* Input a list of ints */
/************************/
int read_int_list(FILE *f,int **list,int *size)
{
int n,c;
int word;
int buf[MAXENTRIES];
n=0;
while(fscanf(f,"%d",&word)>0)
buf[n++]=word;
do
{
c=fgetc(f);
} while(isspace(c));
*size=n;
*list=(int *)malloc(sizeof(int)*n);
memcpy(*list,buf,sizeof(int)*n);
return 0;
}
/****************************/
/* Input a table of doubles */
/****************************/
int read_dbl_table(FILE *f,double **table,int size)
{
int n,c;
*table=(double *)malloc(sizeof(double)*size);
for(n=0;n0) tbl[r]=tbl[r-1]+ncol;
while(fscanf(f,"%d",&i)>0)
tbl[r][i]=1;
do
{
c=fgetc(f);
} while(isspace(c));
}
return 0;
}
int readdata(const char *filename)
{
FILE *f;
char rec[128];
f=fopen(filename,"r");
if(f==NULL) return 1;
while(nextrec(f,rec)>0)
{
if(strcmp(rec,"SHARES:")==0 && NSHARES==0)
read_str_list(f,&SHARES_n,&NSHARES);
else
if(strcmp(rec,"REGIONS:")==0 && NREGIONS==0)
read_str_list(f,®IONS_n,&NREGIONS);
else
if(strcmp(rec,"TYPES:")==0 && NTYPES==0)
read_str_list(f,&TYPES_n,&NTYPES);
else
if(strcmp(rec,"RISK:")==0 && NRISK==0)
read_int_list(f,&RISK,&NRISK);
else
if(strcmp(rec,"RET:")==0 && NSHARES>0)
read_dbl_table(f,&RET,NSHARES);
else
if(strcmp(rec,"LOC:")==0 && NSHARES>0 && NREGIONS>0)
read_bool_table(f,&LOC,NREGIONS,NSHARES);
else
if(strcmp(rec,"SEC:")==0 && NSHARES>0 && NTYPES>0)
read_bool_table(f,&SECT,NTYPES,NSHARES);
else
break;
}
fclose(f);
return NSHARES<1 || NRISK<1 || NREGIONS<1 || NTYPES<1 ||
RET==NULL || RISK==NULL || LOC==NULL || SECT==NULL;
}
void testprintout(void)
{
int i,j;
printf("Shares(%d):",NSHARES);
for(i=0;i |
|
foliocb.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file foliocb.cpp
````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.cpp.
-- Defining an integer solution callback --
(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 15 // 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
#include "readfoliodata.c_"
void XPRS_CC print_sol(XPRSprob oprob, void *vp);
int main(int argc, char **argv)
{
int s,r,t;
XPRBprob p("FolioMIP3"); // Initialize a new problem in BCL
XPRBexpr Risk,Return,Cap,Num;
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]);
}
// Define an integer solution callback
XPRSsetcbintsol(p.getXPRSprob(), print_sol, &p);
// 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;
return 0;
}
/**************************Solution printing****************************/
void XPRS_CC print_sol(XPRSprob oprob, void *vp)
{
int num,s;
XPRBprob *bprob;
bprob = (XPRBprob *)vp;
bprob->beginCB(oprob);
XPRSgetintattrib(oprob, XPRS_MIPSOLS, &num); // Get number of the solution
bprob->sync(XPRB_XPRS_SOL); // Update BCL solution values
cout << "Solution " << num << ": Total return: " << bprob->getObjVal() << endl;
for(s=0;s0.5)
cout << " " << SHARES_n[s] << ": " << frac[s].getSol()*100 << "% (" <<
buy[s].getSol() << ")" << endl;
bprob->endCB();
}
|
|
foliosolpool.cpp |
/********************************************************
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.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;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;
}
|
|
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 Fair Isaac Corporation
author: S.Heipcke, June 2009, rev. Jul. 2014
********************************************************/
#include
#include
#include
#include
#include
#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;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 & 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_HEURSTRATEGY, 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; i0.5)
cout << " " << SHARES_n[s] << ": " << frac[s].getSol()*100 << "% (" << buy[s].getSol()
<< ")" << endl;
}
|
|
folioinfeas.cpp |
/********************************************************
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 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.0/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;
}
|
|
folioiis.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file folioiis.cpp
`````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.cpp.
-- Infeasible model parameter values --
-- Retrieving IIS --
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, June 2009, rev. Mar. 2011
********************************************************/
#include
#include
#include
#include
#include
#include "xprb_cpp.h"
#include "xprs.h"
using namespace std;
using namespace ::dashoptimization;
#define MAXNUM 5 // Max. number of different assets
#define MAXRISK 1.0/3 // Max. investment into high-risk values
#define MINREG 0.1 // Min. investment per geogr. region
#define MAXREG 0.2 // Max. investment per geogr. region
#define MAXSEC 0.1 // 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("FolioMIP3inf"); // 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(XPRBnewname("MinReg(%s)",REGIONS_n[r]), le >= MINREG);
MaxReg[r] = p.newCtr(XPRBnewname("MaxReg(%s)",REGIONS_n[r]), le2 <= MAXREG);
}
// Diversification across industry sectors
LimSec = new XPRBctr[NTYPES];
for(t=0;t0) le += frac[s];
LimSec[t] = p.newCtr(XPRBnewname("LimSec(%s)",TYPES_n[t]), le <= MAXSEC);
}
// Spend all the capital
for(s=0;s= MINVAL*buy[s]);
}
// Solve the problem (LP)
p.setSense(XPRB_MAXIM);
p.lpOptimize("");
if (p.getLPStat()==XPRB_LP_INFEAS)
{
cout << "LP infeasible. Retrieving IIS." << endl;
int numiis = p.getNumIIS(); // Get the number of independent IIS
cout << "Number of IIS: " << numiis << endl;
XPRSprob op = p.getXPRSprob(); // Retrieve the Optimizer problem
/**** Obtain variable and constraint names for use in printout ****/
int ncol, nrow, len, numc, numv, i;
char *vnames, *cnames;
int *viis,*ciis;
char **vindex,**cindex;
// Retrieve variable names
XPRSgetintattrib(op, XPRS_ORIGINALCOLS, &ncol);
XPRSgetnamelist(op, 2, NULL, 0, &len, 0, ncol-1);
// Get number of bytes required for retrieving names
vnames = new char[len];
vindex = new char*[ncol];
XPRSgetnamelist(op, 2, vnames, len, NULL, 0, ncol-1);
vindex[0]=vnames;
for(i=1; i0)
{
cout << " Variables: "; // Print all variables in the IIS
for(i=0;i0)
{
cout << " Constraints: "; // Print all constraints in the IIS
for(i=0;i |
|
foliorep.cpp |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file foliorep.cpp
`````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.cpp.
-- Infeasible model parameter values --
-- Repairing infeasibilities --
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, June 2009, rev. Mar. 2011
********************************************************/
#include
#include
#include
#include
#include
#include
#include
#include "xprb_cpp.h"
#include "xprs.h"
using namespace std;
using namespace ::dashoptimization;
#define MAXNUM 4 // Max. number of different assets
#define MAXRISK 1.0/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;
XPRBvar *frac; // Fraction of capital used per share
XPRBvar *buy; // 1 if asset is in portfolio, 0 otherwise
#include "readfoliodata.c_"
void print_sol_opt(XPRBprob &p);
void print_violated(vector &ctrs);
int main(int argc, char **argv)
{
int s, r, t;
XPRBprob p("FolioMIP3inf"); // Initialize a new problem in BCL
XPRBctr Return, *Risk, *Num, *MinReg, *MaxReg, *LimSec;
XPRBexpr le, le2, Cap, 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 allCtr(2 + 2 * NREGIONS + NTYPES);
int allCtrCount = 0;
/* init constraint pointers */
Risk = &allCtr[allCtrCount++];
Num = &allCtr[allCtrCount++];
MinReg = &allCtr[allCtrCount]; allCtrCount += NREGIONS;
MaxReg = &allCtr[allCtrCount]; allCtrCount += NREGIONS;
LimSec = &allCtr[allCtrCount]; allCtrCount += NTYPES;
// Limit the percentage of high-risk values
le = 0;
for (s = 0; s0) {
le += frac[s];
le2 += frac[s];
}
MinReg[r] = p.newCtr(XPRBnewname("MinReg(%s)", REGIONS_n[r]), le >= MINREG);
MaxReg[r] = p.newCtr(XPRBnewname("MaxReg(%s)", REGIONS_n[r]), le2 <= MAXREG);
}
// Diversification across industry sectors
for (t = 0; t0) le += frac[s];
LimSec[t] = p.newCtr(XPRBnewname("LimSec(%s)", TYPES_n[t]), le <= MAXSEC);
}
// Spend all the capital
for (s = 0; s= MINVAL*buy[s]);
}
// Solve the problem (LP)
p.setMsgLevel(1);
p.setSense(XPRB_MAXIM);
p.lpOptimize("");
if (p.getLPStat() == XPRB_LP_INFEAS) {
cout << "LP infeasible. Start infeasibility repair." << endl;
XPRSprob op = p.getXPRSprob(); // Retrieve the Optimizer problem
// Must use the weighted infeasibility repair method since
// only some constraints of each type may be relaxed
/*
lrp: (affects = and <= rows)
ax - aux_var = b
ax - aux_var <= b
grp: (affects = and >= rows)
ax + aux_var = b
ax + aux_var >= b
lbp:
x_i + aux_var >= l
ubp:
x_i - aux_var <= u
*/
int ncol, nrow, repstatus;
double *lrp, *grp, *lbp, *ubp;
XPRSgetintattrib(op, XPRS_ORIGINALCOLS, &ncol);
XPRSgetintattrib(op, XPRS_ORIGINALROWS, &nrow);
lrp = new double[nrow];
grp = new double[nrow];
lbp = new double[ncol];
ubp = new double[ncol];
memset(lrp, 0, nrow*sizeof(double));
memset(grp, 0, nrow*sizeof(double));
memset(lbp, 0, ncol*sizeof(double));
memset(ubp, 0, ncol*sizeof(double));
lrp[Risk->getRowNum()] = 1;
for (r = 0; rgetRowNum()] = 1;
for (r = 0; r 0.5)
cout << " " << SHARES_n[s] << " : " << frac[s].getSol() * 100 << "% (" << buy[s].getSol() << ")" << endl;
}
void print_violated(vector &ctrs)
{
char *type = NULL;
cout << " Violated (relaxed) constraints:" << endl;
for (vector::iterator c = ctrs.begin(); c != ctrs.end(); c++) {
double viol, slack = c->getSlack();
switch (c->getType()) {
case XPRB_E: viol = abs(slack); type = " ="; break;
case XPRB_G: viol = slack; type = ">="; break;
case XPRB_L: viol = -slack; type = "<="; break;
default: cout << " unexpected constraint type" << endl; continue;
}
if (viol > 1e-6) cout << " " << type << " constraint " << c->getName() << " by " << -slack << endl;
}
}
|
|