FICO Xpress Optimization Examples Repository
 FICO Optimization Community FICO Xpress Optimization Home

Portfolio - Quadratic Programming with discrete variables

Description

Source Files

Data Files

xbportf.java

```/********************************************************
Xpress-BCL Java Example Problems
================================

file xbportf.java
`````````````````

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

/* In this model, a choice has to be made which values are taken   *
* into a portfolio in order to minimize the total cost. The costs *
* for some values are interrelated, introducing a quadratic part  *
* to the objective function. Upper bounds are given on the total  *
* number of values and the share of each value that may be taken. */

import java.io.*;
import com.dashoptimization.*;

public class xbportf
{
static final int NVal = 30;      /* Total number of values */
static final int LIMIT = 20;     /* Maximum number to be chosen */

static final String QFILE = System.getProperty("XPRBDATA") +
"/portf/pfqcost.dat";          /* Quadratic cost coefficients */
static final String BFILE = System.getProperty("XPRBDATA") +
"/portf/pfubds.dat";           /* Upper bounds on percentages */
static final String CFILE = System.getProperty("XPRBDATA") +
"/portf/pflcost.dat";          /* Linear cost coefficients */

/**** DATA ****/
static double Cost[];            /* Coeff. of lin. part of the obj. */
static double QCost[][];         /* Coeff. of quad. part of the obj. */
static double UBnd[];            /* Upper bound values */

static XPRB bcl;
static XPRBprob p;

/***********************************************************************/

static void modFolio() throws IOException
{
XPRBctr c;
XPRBexpr le, qobj;
XPRBvar[] x;                    /* Amount of a value taken into
the portfolio */
XPRBvar[] y;                    /* 1 if value i is chosen, else 0 */
int i,j;

/**** VARIABLES ****/
x = new XPRBvar[NVal];
y = new XPRBvar[NVal];
for(i=0;i<NVal;i++)
{
x[i] = p.newVar("x_"+(i+1), XPRB.PL, 0, UBnd[i]);
y[i] = p.newVar("y_"+(i+1), XPRB.BV);
}

/****OBJECTIVE****/
qobj = new XPRBexpr();          /* Define the objective: total cost */
for(i=0;i<NVal;i++)
{
for(j=i+1;j<NVal;j++)
}
p.setObj(qobj);

/**** CONSTRAINTS ****/
/* Amounts of values chosen must add up to 100% */
le = new XPRBexpr();
p.newCtr("C1", le .eql(100) );

for(i=0;i<NVal;i++)             /* Upper limits */
p.newCtr("UL", x[i] .lEql(y[i].mul(UBnd[i])) );

le = new XPRBexpr();          /* Limit on total number of values */
p.newCtr("Card", le .lEql(LIMIT) );

/****SOLVING + OUTPUT****/
/*  p.print(); */		  /* Print out the problem definition */
p.exportProb(XPRB.MPS,"Portf"); /* Output the matrix in MPS format */
p.exportProb(XPRB.LP,"Portf");  /* Output the matrix in LP format */

p.setSense(XPRB.MINIM);         /* Choose the sense of the  optimization */
p.mipOptimize("");              /* Solve the MIQP-problem, use 'lpOptimize'
to solve QP */

System.out.println("Objective function value: " + p.getObjVal());
for(i=0;i<NVal;i++)
System.out.print(x[i].getName() + ":" +  x[i].getSol() + ", ");
System.out.println();
}

/***********************************************************************/

/**** Initialize the stream tokenizer ****/
{
StreamTokenizer st=null;

st= new StreamTokenizer(file);   /* Initialize the stream tokenizer */
st.commentChar('!');             /* Use the character '!' for comments */
st.eolIsSignificant(true);       /* Return end-of-line character */
st.ordinaryChar(',');            /* Use ',' as separator */
st.parseNumbers();               /* Read numbers as numbers (not strings)*/
return st;
}

/**** Read data from files ****/
{
int i,j;
double value;
StreamTokenizer st;

QCost = new double[NVal][NVal];
Cost = new double[NVal];
UBnd = new double[NVal];

for(i=0;i<NVal;i++)
for(j=0;j<NVal;j++)
QCost[i][j]=0;                 /* Initialize Q to 0 */
st = initST(datafile);
do
{
do
{
st.nextToken();
} while(st.ttype==st.TT_EOL);    /* Skip empty lines */
if(st.ttype != st.TT_NUMBER) break;
i=(int)st.nval;
if(st.nextToken() != ',') break;
if(st.nextToken() != st.TT_NUMBER) break;
j = (int)st.nval;
if(st.nextToken() != ',') break;
if(st.nextToken() != st.TT_NUMBER) break;
QCost[i-1][j-1] = st.nval;
} while( st.nextToken() == st.TT_EOL );
datafile.close();

/* Read the linear cost data file */
st = initST(datafile);
do
{
do
{
st.nextToken();
} while(st.ttype==st.TT_EOL);    /* Skip empty lines */
if(st.ttype != st.TT_NUMBER) break;
i=(int)st.nval;
if(st.nextToken() != ',') break;
if(st.nextToken() != st.TT_NUMBER) break;
Cost[i-1] = st.nval;
} while( st.nextToken() == st.TT_EOL );
datafile.close();

/* Read the bounds data file */
st = initST(datafile);
do
{
do
{
st.nextToken();
} while(st.ttype==st.TT_EOL);    /* Skip empty lines */
if(st.ttype != st.TT_NUMBER) break;
i=(int)st.nval;
if(st.nextToken() != ',') break;
if(st.nextToken() != st.TT_NUMBER) break;
UBnd[i-1] = st.nval;
} while( st.nextToken() == st.TT_EOL );
datafile.close();
}

/***********************************************************************/

public static void main(String[] args)
{
bcl = new XPRB();                /* Initialize BCL */
p = bcl.newProb("Portfolio");    /* Create a new problem in BCL */
try
{
readData();                     /* Data input from file */
modFolio();                     /* Formulate and solve the problem */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
}
}

```