Initializing help system before first use

Folio - Examples from 'Getting Started'


Type: Portfolio optimization
Rating: 3
Description: Different versions of a portfolio optimization problem.
Basic modelling and solving tasks:
  • modeling and solving a small LP problem (foliolp, using variable arrays: folioarr)
  • performing explicit initialization (folioinit*)
  • data input from file, index sets (foliodata, requires foliocpplp.dat)
  • modeling and solving a small MIP problem with binary variables (foliomip1)
  • modeling and solving a small MIP problem with semi-continuous variables (foliomip2)
  • modeling and solving QP and MIQP problems (folioqp, requires foliocppqp.dat)
  • modeling and solving QCQP problems (folioqc, requires foliocppqp.dat)
  • heuristic solution of a MIP problem (folioheur)
Advanced modeling and solving tasks:
  • enlarged version of the basic MIP model (foliomip3 with include file readfoliodata.c_, to be used with data sets folio5.cdat, folio10.cdat)
  • defining an integer solution callback (foliocb)
  • using the MIP solution pool (foliosolpool)
  • using the solution enumerator (folioenumsol)
  • handling infeasibility through deviation variables (folioinfeas)
  • retrieving IIS (folioiis, foliomiis)
  • using the built-in infeasibility repair functionality (foliorep)
File(s): foliolp.c, folioarr.c, folioinit.c, folioinit2.c, foliodata.c, foliomip1.c, foliomip2.c, folioqp.c, folioqc.c, folioheur.c, foliomip3.c, readfoliodata.c_, foliocb.c, foliosolpool.c, folioenumsol.c, folioinfeas.c, folioiis.c, foliomiis.c, foliorep.c
Data file(s): foliocpplp.dat, foliocppqp.dat, folio5.cdat, folio10.cdat

foliolp.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliolp.c
  ``````````````
  Modeling a small LP problem 
  to perform portfolio optimization.

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"

#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 prob;         
 XPRBctr Risk,Na,Return,Cap;
 XPRBvar frac[NSHARES];              /* Fraction of capital used per share */
 char *LPSTATUS[] = {"not loaded", "optimal", "infeasible", 
          "worse than cutoff", "unfinished", "unbounded", "cutoff in dual",
	  "unsolved", "nonconvex"};

 prob = XPRBnewprob("FolioLP");      /* Initialize a new problem in BCL */

/* Create the decision variables */
 for(s=0;s

folioarr.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioarr.c
  ```````````````
  Modeling a small LP problem 
  to perform portfolio optimization.
  -- Array-based formulation --

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"

#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 prob;         
 XPRBctr Risk,Na,Return,Cap;
 XPRBarrvar frac;                    /* Fraction of capital used per share */

 prob = XPRBnewprob("FolioLP");      /* Initialize a new problem in BCL */

/* Create the decision variables */
 frac = XPRBnewarrvar(prob, NSHARES, XPRB_PL, "frac", 0, XPRB_INFINITY);
  
/* Objective: total return */
 Return = XPRBnewarrsum(prob, "Return", frac, RET, XPRB_N, 0);
 XPRBsetobj(prob,Return);            /* Set the objective function */

/* Limit the percentage of high-risk values */
 Risk = XPRBnewctr(prob, "Risk", XPRB_L);
 for(s=0;s

folioinit.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioinit.c
  ````````````````
  Modeling a small LP problem 
  to perform portfolio optimization.
  -- Explicit initialization. --

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"

#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(void)
{
 int s;
 XPRBprob prob;         
 XPRBctr Risk,Na,Return,Cap;
 XPRBvar frac[NSHARES];              /* Fraction of capital used per share */
 char *LPSTATUS[] = {"not loaded", "optimal", "infeasible", 
          "worse than cutoff", "unfinished", "unbounded", "cutoff in dual",
	  "unsolved", "nonconvex"};

 prob = XPRBnewprob("FolioLP");      /* Initialize a new problem in BCL */

/* Create the decision variables */
 for(s=0;s

folioinit2.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioinit.c
  ````````````````
  Modeling a small LP problem 
  to perform portfolio optimization.
  -- Explicit initialization. --

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"

#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 prob;         
 XPRBctr Risk,Na,Return,Cap;
 XPRBvar frac[NSHARES];              /* Fraction of capital used per share */
 char *LPSTATUS[] = {"not loaded", "optimal", "infeasible", 
          "worse than cutoff", "unfinished", "unbounded", "cutoff in dual",
	  "unsolved", "nonconvex"};

 if(XPRBinit() != 0)
 {
   printf("Initialization failed.\n");
   return 1;
 }

 prob = XPRBnewprob("FolioLP");      /* Initialize a new problem in BCL */

/* Create the decision variables */
 for(s=0;s

foliodata.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliodata.c
  ````````````````
  Modeling a small LP problem 
  to perform portfolio optimization.
  -- Data input from file --

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"

#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 */

XPRBidxset SHARES;                   /* Set of shares */

XPRBprob prob;

void readData(void)
{
 double value;
 int s;
 FILE *datafile;
 char name[100];
                                     /* Create the `SHARES' index set */
 SHARES = XPRBnewidxset(prob, "Shares", NSHARES);

/* Read `RET' data from file */
 datafile = fopen(DATAFILE,"r");
 for(s=0;s

foliomip1.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliomip1.c
  ````````````````
  Modeling a small MIP problem 
  to perform portfolio optimization.
   -- Limiting the total number of assets --

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"

#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 prob;         
 XPRBctr 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 */
 char *MIPSTATUS[] = {"not loaded", "not optimized", "LP optimized",  
                      "unfinished (no solution)", 
                      "unfinished (solution found)", "infeasible", "optimal",
                      "unbounded"};

 prob = XPRBnewprob("FolioMIP1");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 for(s=0;s

foliomip2.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliomip2.c
  ````````````````
  Modeling a small MIP problem 
  to perform portfolio optimization.
  -- Imposing a minimum investment per share --

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"

#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 prob;         
 XPRBctr Risk,Na,Return,Cap;
 XPRBvar frac[NSHARES];            /* Fraction of capital used per share */

 prob = XPRBnewprob("FolioSC");    /* Initialize a new problem in BCL */

/* Create the decision variables */
 for(s=0;s

folioqp.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioqp.c
  ``````````````
  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, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"

#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 prob;
 XPRBctr Variance,Na,Return,Cap,Num;
 XPRBvar frac[NSHARES];            /* Fraction of capital used per share */
 XPRBvar buy[NSHARES];             /* 1 if asset is in portfolio, 0 otherwise */
 FILE *datafile;

 prob = XPRBnewprob("FolioQP");    /* Initialize a new problem in BCL */

/* Read `VAR' data from file */
 datafile=fopen(DATAFILE,"r");
 for(s=0;s

folioqc.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioqc.c
  ``````````````
  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 "xprb.h"

#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 prob;
 XPRBctr Variance,Na,Return,Cap;
 XPRBvar frac[NSHARES];            /* Fraction of capital used per share */
 FILE *datafile;

 prob = XPRBnewprob("FolioQP");    /* Initialize a new problem in BCL */

/* Read `VAR' data from file */
 datafile=fopen(DATAFILE,"r");
 for(s=0;s

folioheur.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioheur.c
  ````````````````
  Modeling a small MIP problem 
  to perform portfolio optimization.
   -- Heuristic solution --

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Dec. 2003, rev. Mar. 2011
********************************************************/

#include 
#include "xprb.h"
#include "xprs.h"

#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 */

void solveHeur(void);

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 prob;         
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;
 XPRBctr Risk,Na,Return,Cap,Num;

 prob = XPRBnewprob("FolioMIPHeur");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 for(s=0;s 0.2-TOL)  XPRBsetlb(buy[s], 1);
 }

 XPRBmipoptimize(prob, "c");       /* Continue solving the MIP-problem */
 ifgsol=0;
 if(XPRBgetmipstat(prob)==XPRB_MIP_SOLUTION || 
    XPRBgetmipstat(prob)==XPRB_MIP_OPTIMAL)
 {                                 /* If an integer feas. solution was found */
  ifgsol=1;
  solval=XPRBgetobjval(prob);      /* Get the value of the best solution */
  printf("Heuristic solution: Total return: %g\n", XPRBgetobjval(prob));
  for(s=0;s 0.2-TOL))
  {
   XPRBsetlb(buy[s], 0);
   XPRBsetub(buy[s], 1);
  }

 XPRBloadbasis(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 */
 XPRBdelbasis(basis);       /* No need to store the saved basis any longer */
 if(ifgsol==1)
  XPRSsetdblcontrol(XPRBgetXPRSprob(prob), XPRS_MIPABSCUTOFF, solval+TOL);
                            /* Set the cutoff to the best known solution */
} 

foliomip3.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliomip3.c
  ````````````````
  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 "xprb.h"


#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 **SEC;                        /* 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 prob;         
 XPRBctr Risk,Return,Cap,Num;
 XPRBctr *MinReg, *MaxReg, *LimSec, LinkL, LinkU;
 XPRBvar *frac;                   /* Fraction of capital used per share */
 XPRBvar *buy;                    /* 1 if asset is in portfolio, 0 otherwise */
 char *MIPSTATUS[] = {"not loaded", "not optimized", "LP optimized",  
                      "unfinished (no solution)", 
                      "unfinished (solution found)", "infeasible", "optimal",
                      "unbounded"};

 readdata(DATAFILE);               /* Data input from file */ 

 prob = XPRBnewprob("FolioMIP3");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 frac = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 buy = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 for(s=0;s0)
   {
    XPRBaddterm(MinReg[r], frac[s], 1); 
    XPRBaddterm(MaxReg[r], frac[s], 1);
   } 
  XPRBaddterm(MinReg[r], NULL, MINREG);
  XPRBaddterm(MaxReg[r], NULL, MAXREG);
 } 

/* Diversification across industry sectors */
 LimSec = (XPRBctr*)malloc(NTYPES*sizeof(XPRBctr));
 for(t=0;t0) XPRBaddterm(LimSec[t], frac[s], 1); 
  XPRBaddterm(LimSec[t], NULL, MAXSEC);
 } 

/* Spend all the capital */
 Cap = XPRBnewctr(prob, "Cap", XPRB_E);
 for(s=0;s0.5)
   printf("  %d : %g%% (%g)\n", s, XPRBgetsol(frac[s])*100, XPRBgetsol(buy[s]));

 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,&SEC,NTYPES,NSHARES);
  else
   break;
 }

 fclose(f);
 return NSHARES<1 || NRISK<1 || NREGIONS<1 || NTYPES<1 ||
        RET==NULL || RISK==NULL || LOC==NULL || SEC==NULL;
}

void testprintout(void)
{
 int i,j;

 printf("Shares(%d):",NSHARES);
 for(i=0;i

foliocb.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliocb.c
  ``````````````
  Modeling a MIP problem 
  to perform portfolio optimization.

  Same model as in foliomip3.c.
  -- Defining an integer solution callback --

  (c) 2009 Fair Isaac Corporation
      author: S.Heipcke, May 2009, rev. Mar. 2011
********************************************************/

#include 
#include 
#include 
#include 
#include "xprb.h"
#include "xprs.h"


#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 **SEC;                        /* 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 prob;         
 XPRBctr Risk,Return,Cap,Num;
 XPRBctr *MinReg, *MaxReg, *LimSec, LinkL, LinkU;
 char *MIPSTATUS[] = {"not loaded", "not optimized", "LP optimized",  
                      "unfinished (no solution)", 
                      "unfinished (solution found)", "infeasible", "optimal",
                      "unbounded"};

 readdata(DATAFILE);               /* Data input from file */ 

 prob = XPRBnewprob("FolioMIP3");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 frac = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 buy = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 for(s=0;s0)
   {
    XPRBaddterm(MinReg[r], frac[s], 1); 
    XPRBaddterm(MaxReg[r], frac[s], 1);
   } 
  XPRBaddterm(MinReg[r], NULL, MINREG);
  XPRBaddterm(MaxReg[r], NULL, MAXREG);
 } 

/* Diversification across industry sectors */
 LimSec = (XPRBctr*)malloc(NTYPES*sizeof(XPRBctr));
 for(t=0;t0) XPRBaddterm(LimSec[t], frac[s], 1); 
  XPRBaddterm(LimSec[t], NULL, MAXSEC);
 } 

/* Spend all the capital */
 Cap = XPRBnewctr(prob, "Cap", XPRB_E);
 for(s=0;s0.5)
   printf("  %d : %g%% (%g)\n", s, XPRBgetsol(frac[s])*100, XPRBgetsol(buy[s]));

 XPRBendcb(bprob);
}

foliosolpool.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliosolpool.c
  ```````````````````
  Modeling a MIP problem 
  to perform portfolio optimization.
   
  Same model as in foliomip3.c.
  -- Using the MIP solution pool --
 
  (c) 2009 Fair Isaac Corporation
      author: S.Heipcke, June 2009, rev. Mar. 2011
********************************************************/

#include 
#include 
#include 
#include 
#include "xprb.h"
#include "xprs.h"


#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 **SEC;                        /* Industry sector of shares */

char **SHARES_n;
char **REGIONS_n;
char **TYPES_n;

XPRBprob prob;         
XPRBvar *frac;                     /* Fraction of capital used per share */
XPRBvar *buy;                      /* 1 if asset is in portfolio, 0 otherwise */

#include "readfoliodata.c_"

void print_sol(int num);

int main(int argc, char **argv)
{
 int s,r,t, nSols, nCols, i;
 XPRBctr Risk,Return,Cap,Num;
 XPRBctr *MinReg, *MaxReg, *LimSec, LinkL, LinkU;
 XPRSmipsolpool msp;
 double *xsol;
 int *solIDs;

 readdata(DATAFILE);               /* Data input from file */ 

 prob = XPRBnewprob("FolioMIP3");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 frac = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 buy = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 for(s=0;s0)
   {
    XPRBaddterm(MinReg[r], frac[s], 1); 
    XPRBaddterm(MaxReg[r], frac[s], 1);
   } 
  XPRBaddterm(MinReg[r], NULL, MINREG);
  XPRBaddterm(MaxReg[r], NULL, MAXREG);
 } 

/* Diversification across industry sectors */
 LimSec = (XPRBctr*)malloc(NTYPES*sizeof(XPRBctr));
 for(t=0;t0) XPRBaddterm(LimSec[t], frac[s], 1); 
  XPRBaddterm(LimSec[t], NULL, MAXSEC);
 } 

/* Spend all the capital */
 Cap = XPRBnewctr(prob, "Cap", XPRB_E);
 for(s=0;s0.5)
   printf("  %s : %g%% (%g)\n", SHARES_n[s], XPRBgetsol(frac[s])*100,
          XPRBgetsol(buy[s]));
} 

folioenumsol.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioenumsol.c
  ```````````````````
  Modeling a MIP problem 
  to perform portfolio optimization.
   
  Same model as in foliomip3.c.
  -- Using the solution enumerator --
 
  (c) 2009 Fair Isaac Corporation
      author: S.Heipcke, May 2009, rev. Jul. 2014
********************************************************/

#include 
#include 
#include 
#include 
#include "xprb.h"
#include "xprs.h"
#include "xprs_mse_defaulthandler.h"


#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 **SEC;                        /* Industry sector of shares */

char **SHARES_n;
char **REGIONS_n;
char **TYPES_n;

XPRBprob prob;         
XPRBvar *frac;                     /* Fraction of capital used per share */
XPRBvar *buy;                      /* 1 if asset is in portfolio, 0 otherwise */

#include "readfoliodata.c_"

void print_sol(int num);

int main(int argc, char **argv)
{
 int s,r,t, nMaxSols, nSols, nCols, i;
 XPRBctr Risk,Return,Cap,Num;
 XPRBctr *MinReg, *MaxReg, *LimSec, LinkL, LinkU;
 XPRSmipsolpool msp;
 XPRSmipsolenum mse;
 double *xsol;
 int *solIDs;

 readdata(DATAFILE);               /* Data input from file */ 

 prob = XPRBnewprob("FolioMIP3");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 frac = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 buy = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 for(s=0;s0)
   {
    XPRBaddterm(MinReg[r], frac[s], 1); 
    XPRBaddterm(MaxReg[r], frac[s], 1);
   } 
  XPRBaddterm(MinReg[r], NULL, MINREG);
  XPRBaddterm(MaxReg[r], NULL, MAXREG);
 } 

/* Diversification across industry sectors */
 LimSec = (XPRBctr*)malloc(NTYPES*sizeof(XPRBctr));
 for(t=0;t0) XPRBaddterm(LimSec[t], frac[s], 1); 
  XPRBaddterm(LimSec[t], NULL, MAXSEC);
 } 

/* Spend all the capital */
 Cap = XPRBnewctr(prob, "Cap", XPRB_E);
 for(s=0;s0.5)
   printf("  %s : %g%% (%g)\n", SHARES_n[s], XPRBgetsol(frac[s])*100,
          XPRBgetsol(buy[s]));
} 

folioinfeas.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioinfeas.c
  ``````````````````
  Modeling a MIP problem 
  to perform portfolio optimization.

  Same model as in foliomip3.c.
  -- 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 "xprb.h"


#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 **SEC;                        /* 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;
 double sumf, penalty;
 XPRBprob prob;         
 XPRBctr Risk,Return,Cap,Num;
 XPRBctr *MinReg, *MaxReg, *LimSec, LinkL, LinkU;
 XPRBvar *frac;                   /* Fraction of capital used per share */
 XPRBvar *buy;                    /* 1 if asset is in portfolio, 0 otherwise */
 XPRBvar devRisk, devNum, *devMinReg, *devMaxReg, *devSec;
 char *MIPSTATUS[] = {"not loaded", "not optimized", "LP optimized",  
                      "unfinished (no solution)", 
                      "unfinished (solution found)", "infeasible", "optimal",
                      "unbounded"};

 readdata(DATAFILE);               /* Data input from file */ 

 prob = XPRBnewprob("FolioMIP3inf");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 frac = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 buy = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 for(s=0;s0)
   {
    XPRBaddterm(MinReg[r], frac[s], 1); 
    XPRBaddterm(MaxReg[r], frac[s], 1);
   } 
  XPRBaddterm(MinReg[r], NULL, MINREG);
  XPRBaddterm(MaxReg[r], NULL, MAXREG);
 } 

/* Diversification across industry sectors */
 LimSec = (XPRBctr*)malloc(NTYPES*sizeof(XPRBctr));
 for(t=0;t0) XPRBaddterm(LimSec[t], frac[s], 1); 
  XPRBaddterm(LimSec[t], NULL, MAXSEC);
 } 

/* Spend all the capital */
 Cap = XPRBnewctr(prob, "Cap", XPRB_E);
 for(s=0;s0) sumf+=XPRBgetsol(frac[s]);
    printf("  Region %-11s      %1.2f\t  %1.2f\t  (%g-%g)\n", REGIONS_n[r], sumf,
           XPRBgetsol(devMaxReg[r])-XPRBgetsol(devMinReg[r]), 
           MINREG, MAXREG);
   }
   for(t=0;t0.5)
   printf("  %d : %g%% (%g)\n", s, XPRBgetsol(frac[s])*100, XPRBgetsol(buy[s]));

 return 0;
} 

folioiis.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file folioiis.c
  ```````````````
  Modeling a MIP problem 
  to perform portfolio optimization.

  Same model as in foliomip3.c.
  -- Infeasible model parameter values --
  -- Retrieving IIS --

  (c) 2009 Fair Isaac Corporation
      author: S.Heipcke, June 2009, rev. Mar. 2011
********************************************************/

#include 
#include 
#include 
#include 
#include "xprb.h"


#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 **SEC;                        /* 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,i;
 XPRBprob prob;         
 XPRBctr Risk,Return,Cap,Num;
 XPRBctr *MinReg, *MaxReg, *LimSec, LinkL, LinkU;
 XPRBvar *frac;                   /* Fraction of capital used per share */
 XPRBvar *buy;                    /* 1 if asset is in portfolio, 0 otherwise */
 XPRBctr *iisctr;
 XPRBvar *iisvar;
 int numv, numc, numiis;

 readdata(DATAFILE);               /* Data input from file */ 

 prob = XPRBnewprob("FolioMIP3inf");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 frac = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 buy = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 for(s=0;s0)
   {
    XPRBaddterm(MinReg[r], frac[s], 1); 
    XPRBaddterm(MaxReg[r], frac[s], 1);
   } 
  XPRBaddterm(MinReg[r], NULL, MINREG);
  XPRBaddterm(MaxReg[r], NULL, MAXREG);
 } 

/* Diversification across industry sectors */
 LimSec = (XPRBctr*)malloc(NTYPES*sizeof(XPRBctr));
 for(t=0;t0) XPRBaddterm(LimSec[t], frac[s], 1); 
  XPRBaddterm(LimSec[t], NULL, MAXSEC);
 } 

/* Spend all the capital */
 Cap = XPRBnewctr(prob, "Cap", XPRB_E);
 for(s=0;s0)
   {                              /* Print all variables in the IIS */
    printf("        Variables: ");
    for(i=0;i0)
   {                              /* Print all constraints in the IIS */
    printf("        Constraints: ");
    for(i=0;i

foliomiis.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliomiis.c
  ````````````````
  Modeling a MIP problem 
  to perform portfolio optimization.

  Same model as in foliomip3.c.
  -- Infeasible model parameter values --
  -- Retrieving MIIS --

  (c) 2011 Fair Isaac Corporation
      author: S.Heipcke, Mar. 2011
********************************************************/

#include 
#include 
#include 
#include 
#include "xprb.h"


#define MAXNUM 5                   /* Max. number of different assets */
#define MAXRISK 1.0/4              /* Max. investment into high-risk values */
#define MINREG 0.1                 /* Min. investment per geogr. region */
#define MAXREG 0.25                /* Max. investment per geogr. region */
#define MAXSEC 0.15                /* Max. investment per ind. sector */
#define MAXVAL 0.225               /* Max. investment per share */
#define MINVAL 0.1                 /* Min. investment per share */

#define DATAFILE "folio5.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 **SEC;                        /* 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,i;
 XPRBprob prob;         
 XPRBctr Risk,Return,Cap,Num;
 XPRBctr *MinReg, *MaxReg, *LimSec, LinkL, LinkU;
 XPRBvar *frac;                   /* Fraction of capital used per share */
 XPRBvar *buy;                    /* 1 if asset is in portfolio, 0 otherwise */
 XPRBctr *iisctr;
 XPRBvar *iisvar;
 int numv, numc, numiis;

 readdata(DATAFILE);               /* Data input from file */ 

 prob = XPRBnewprob("FolioMIP3inf");  /* Initialize a new problem in BCL */

/* Create the decision variables (including upper bounds for `frac') */
 frac = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 buy = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
 for(s=0;s0)
   {
    XPRBaddterm(MinReg[r], frac[s], 1); 
    XPRBaddterm(MaxReg[r], frac[s], 1);
   } 
  XPRBaddterm(MinReg[r], NULL, MINREG);
  XPRBaddterm(MaxReg[r], NULL, MAXREG);
 } 

/* Diversification across industry sectors */
 LimSec = (XPRBctr*)malloc(NTYPES*sizeof(XPRBctr));
 for(t=0;t0) XPRBaddterm(LimSec[t], frac[s], 1); 
  XPRBaddterm(LimSec[t], NULL, MAXSEC);
 } 

/* Spend all the capital */
 Cap = XPRBnewctr(prob, "Cap", XPRB_E);
 for(s=0;s0)
   {                              /* Print all variables in the IIS */
    printf("        Variables: ");
    for(i=0;i0)
   {                              /* Print all constraints in the IIS */
    printf("        Constraints: ");
    for(i=0;i

foliorep.c
/********************************************************
  Xpress-BCL C Example Problems
  =============================

  file foliorep.c
  ```````````````
  Modeling a MIP problem
  to perform portfolio optimization.

  Same model as in foliomip3.c.
  -- Infeasible model parameter values --
  -- Repairing infeasibilities --

  (c) 2009 Fair Isaac Corporation
      author: S.Heipcke, June 2009, rev. Oct. 2014
********************************************************/

#include 
#include 
#include 
#include 
#include 
#include "xprb.h"
#include "xprs.h"


#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 **SEC;                        /* Industry sector of shares */

char **SHARES_n;
char **REGIONS_n;
char **TYPES_n;

XPRBprob prob;
XPRBvar *frac;                     /* Fraction of capital used per share */
XPRBvar *buy;                      /* 1 if asset is in portfolio, 0 otherwise */
XPRBctr *allCtr;                   /* array containing all defined contraints */
int allCtrCount = 0;               /* number of constraints in allCtr array */

#include "readfoliodata.c_"

void print_sol_opt(void);
void print_violated(void);

int main(int argc, char **argv)
{
  int s, r, t;
  XPRBctr Return, *Risk, *Cap, *Num;
  XPRBctr *MinReg, *MaxReg, *LimSec, *LinkL, *LinkU;

  readdata(DATAFILE);               /* Data input from file */
  
  prob = XPRBnewprob("FolioMIP3inf");  /* Initialize a new problem in BCL */

  /* Create the decision variables (including upper bounds for `frac') */
  frac = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
  buy = (XPRBvar*)malloc(NSHARES*sizeof(XPRBvar));
  for (s = 0; s0) {
        XPRBaddterm(MinReg[r], frac[s], 1);
        XPRBaddterm(MaxReg[r], frac[s], 1);
      }
    XPRBaddterm(MinReg[r], NULL, MINREG);
    XPRBaddterm(MaxReg[r], NULL, MAXREG);
  }

  /* Diversification across industry sectors */
  for (t = 0; t0) XPRBaddterm(LimSec[t], frac[s], 1);
    XPRBaddterm(LimSec[t], NULL, MAXSEC);
  }

  /* Spend all the capital */
  *Cap = XPRBnewctr(prob, "Cap", XPRB_E);
  for (s = 0; s= rows)
    ax + aux_var  = b
    ax + aux_var >= b
    lbp:
    x_i + aux_var >= l
    ubp:
    x_i - aux_var <= u
    */

    XPRSgetintattrib(op, XPRS_ORIGINALCOLS, &ncol);
    XPRSgetintattrib(op, XPRS_ORIGINALROWS, &nrow);
    lrp = (double *)calloc(nrow, sizeof(double));
    grp = (double *)calloc(nrow, sizeof(double));
    lbp = (double *)calloc(ncol, sizeof(double));
    ubp = (double *)calloc(ncol, sizeof(double));

    lrp[XPRBgetrownum(*Risk)] = 1;
    for (r = 0; r 0.5)
      printf("  %s : %g%% (%g)\n", SHARES_n[s], XPRBgetsol(frac[s])*100, XPRBgetsol(buy[s]));
}

void print_violated(void)
{
  int i;
  char *type=NULL;
  printf(" Violated (relaxed) constraints:\n");
  for (i = 0; i < allCtrCount; i++) {
    double viol, slack = XPRBgetslack(allCtr[i]);
    switch (XPRBgetctrtype(allCtr[i])) {
      case XPRB_E: viol = fabs(slack); type = " ="; break;
      case XPRB_G: viol = slack;       type = ">="; break;
      case XPRB_L: viol = -slack;      type = "<="; break;
      default: printf("  unexpected constraint type\n"); continue;
    }
    if (viol>1e-6) printf("  %s constraint %s by %g\n", type, XPRBgetctrname(allCtr[i]), -slack);
  }
}