foliolp.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliolp.java
`````````````````
Modeling a small LP problem
to perform portfolio optimization.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2003, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class foliolp
{
static final int NSHARES = 10; /* Number of shares */
static final int NRISK = 5; /* Number of high-risk shares */
static final int NNA = 4; /* Number of North-American shares */
static final double[] RET = {5,17,26,12,8,9,7,6,31,21};
/* Estimated return in investment */
static final int[] RISK = {1,2,3,8,9}; /* High-risk values among shares */
static final int[] NA = {0,1,2,3}; /* Shares issued in N.-America */
static final String[] LPSTATUS = {"not loaded", "optimal", "infeasible",
"worse than cutoff", "unfinished", "unbounded", "cutoff in dual",
"unsolved", "nonconvex"};
public static void main(String[] args)
{
int s;
XPRB bcl;
XPRBprob p;
XPRBexpr Risk,Na,Return,Cap;
XPRBvar[] frac; /* Fraction of capital used per share */
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioLP"); /* Create a new problem in BCL */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
for(s=0;s |
|
folioinit.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file folioinit.java
```````````````````
Modeling a small LP problem
to perform portfolio optimization.
Initialization with error handling.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2003, rev. Dec. 2011
********************************************************/
import com.dashoptimization.*;
public class folioinit
{
static final int NSHARES = 10; /* Number of shares */
static final int NRISK = 5; /* Number of high-risk shares */
static final int NNA = 4; /* Number of North-American shares */
static final double[] RET = {5,17,26,12,8,9,7,6,31,21};
/* Estimated return in investment */
static final int[] RISK = {1,2,3,8,9}; /* High-risk values among shares */
static final int[] NA = {0,1,2,3}; /* Shares issued in N.-America */
static final String[] LPSTATUS = {"not loaded", "optimal", "infeasible",
"worse than cutoff", "unfinished", "unbounded", "cutoff in dual",
"unsolved", "nonconvex"};
static XPRB bcl;
private static void solveProb()
{
int s;
XPRBprob p;
XPRBexpr Risk,Na,Return,Cap;
XPRBvar[] frac; /* Fraction of capital used per share */
p = bcl.newProb("FolioLP"); /* Create a new problem in BCL */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
for(s=0;s |
|
folioinit2.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file folioinit2.java
````````````````````
Modeling a small LP problem
to perform portfolio optimization.
Initialization with error handling.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2003, rev. Dec. 2011
********************************************************/
import com.dashoptimization.*;
public class folioinit2
{
static final int NSHARES = 10; /* Number of shares */
static final int NRISK = 5; /* Number of high-risk shares */
static final int NNA = 4; /* Number of North-American shares */
static final double[] RET = {5,17,26,12,8,9,7,6,31,21};
/* Estimated return in investment */
static final int[] RISK = {1,2,3,8,9}; /* High-risk values among shares */
static final int[] NA = {0,1,2,3}; /* Shares issued in N.-America */
static final String[] LPSTATUS = {"not loaded", "optimal", "infeasible",
"worse than cutoff", "unfinished", "unbounded", "cutoff in dual",
"unsolved", "nonconvex"};
public static void main(String[] args)
{
XPRB bcl=null;
int s;
XPRBprob p;
XPRBexpr Risk,Na,Return,Cap;
XPRBvar[] frac; /* Fraction of capital used per share */
try
{
bcl = new XPRB(); /* Initialize BCL */
}
catch(XPRBlicenseError e)
{
System.out.println("Initialization failed (licensing problem).");
System.exit(1);
}
catch(XPRBerror e)
{
System.out.println("Initialization failed.");
System.exit(1);
}
p = bcl.newProb("FolioLP"); /* Create a new problem in BCL */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
for(s=0;s |
|
foliodata.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliodata.java
```````````````````
Modeling a small LP problem
to perform portfolio optimization.
-- Data input from file --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2003, rev. June 2008, rev. Dec. 2011
********************************************************/
import com.dashoptimization.*;
import java.io.*;
import java.lang.*;
public class foliodata
{
static final String DATAFILE = System.getProperty("XPRBDATA") +
"/GS/foliocpplp.dat";
static final int NSHARES = 10; /* Number of shares */
static final int NRISK = 5; /* Number of high-risk shares */
static final int NNA = 4; /* Number of North-American shares */
static double[] RET; /* Estimated return in investment */
static final String[] RISK = {"hardware", "theater", "telecom", "software",
"electronics"}; /* High-risk values among shares */
static final String[] NA = {"treasury", "hardware", "theater", "telecom"};
/* Shares issued in N.-America */
static final String[] LPSTATUS = {"not loaded", "optimal", "infeasible",
"worse than cutoff", "unfinished", "unbounded", "cutoff in dual",
"unsolved", "nonconvex"};
static XPRBindexSet SHARES; /* Set of shares */
static XPRB bcl;
static XPRBprob p;
private static void readData() throws IOException
{
int s;
FileReader datafile=null;
StreamTokenizer st=null;
SHARES=p.newIndexSet("Shares",NSHARES); /* Create the `SHARES' index set */
RET = new double[NSHARES];
/* Read `RET' data from file */
datafile=new FileReader(DATAFILE); /* Open the data file */
st=new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
st.commentChar('!'); /* Use the character '!' for comments */
st.eolIsSignificant(true); /* Return end-of-line character */
st.parseNumbers(); /* Read numbers as numbers (not strings) */
do /* Read data file and fill the index sets */
{
do
{
st.nextToken();
} while(st.ttype==st.TT_EOL); /* Skip empty lines and comment lines */
if(st.ttype != st.TT_WORD && st.ttype != '"') break;
s=SHARES.addElement(st.sval);
if(st.nextToken() != st.TT_NUMBER) break;
RET[s] = st.nval;
} while( st.nextToken() == st.TT_EOL );
datafile.close();
SHARES.print(); /* Print out the set contents */
}
public static void main(String[] args)
{
int s;
XPRBexpr Risk,Na,Return,Cap;
XPRBvar[] frac; /* Fraction of capital used per share */
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioLP"); /* Create a new problem in BCL */
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
for(s=0;s |
|
foliomip1.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliomip1.java
```````````````````
Modeling a small LP problem
to perform portfolio optimization.
-- Limiting the total number of assets --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2003, rev. Dec. 2011
********************************************************/
import com.dashoptimization.*;
public class foliomip1
{
static final int MAXNUM = 4; /* Max. number of different assets */
static final int NSHARES = 10; /* Number of shares */
static final int NRISK = 5; /* Number of high-risk shares */
static final int NNA = 4; /* Number of North-American shares */
static final double[] RET = {5,17,26,12,8,9,7,6,31,21};
/* Estimated return in investment */
static final int[] RISK = {1,2,3,8,9}; /* High-risk values among shares */
static final int[] NA = {0,1,2,3}; /* Shares issued in N.-America */
static final String[] MIPSTATUS = {"not loaded", "not optimized",
"LP optimized", "unfinished (no solution)",
"unfinished (solution found)", "infeasible", "optimal",
"unbounded"};
public static void main(String[] args)
{
int s;
XPRB bcl;
XPRBprob p;
XPRBexpr Risk,Na,Return,Cap,Num;
XPRBvar[] frac; /* Fraction of capital used per share */
XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP1"); /* Create a new problem in BCL */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s |
|
foliomip2.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliomip2.java
```````````````````
Modeling a small LP problem
to perform portfolio optimization.
-- Imposing a minimum investment per share --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2003, rev. Dec. 2011
********************************************************/
import com.dashoptimization.*;
public class foliomip2
{
static final int NSHARES = 10; /* Number of shares */
static final int NRISK = 5; /* Number of high-risk shares */
static final int NNA = 4; /* Number of North-American shares */
static final double[] RET = {5,17,26,12,8,9,7,6,31,21};
/* Estimated return in investment */
static final int[] RISK = {1,2,3,8,9}; /* High-risk values among shares */
static final int[] NA = {0,1,2,3}; /* Shares issued in N.-America */
public static void main(String[] args)
{
int s;
XPRB bcl;
XPRBprob p;
XPRBexpr Risk,Na,Return,Cap,Num;
XPRBvar[] frac; /* Fraction of capital used per share */
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioSC"); /* Create a new problem in BCL */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
for(s=0;s |
|
folioqp.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file folioqp.java
`````````````````
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, 2003, rev. Dec. 2011
********************************************************/
import com.dashoptimization.*;
import java.io.*;
import java.lang.*;
public class folioqp
{
static final String DATAFILE = System.getProperty("XPRBDATA") +
"/GS/foliocppqp.dat";
static final int TARGET = 9; /* Target yield */
static final int MAXNUM = 4; /* Max. number of different assets */
static final int NSHARES = 10; /* Number of shares */
static final int NNA = 4; /* Number of North-American shares */
static final double[] RET = {5,17,26,12,8,9,7,6,31,21};
/* Estimated return in investment */
static final int[] NA = {0,1,2,3}; /* Shares issued in N.-America */
static double[][] VAR; /* Variance/covariance matrix of
estimated returns */
static XPRB bcl;
static XPRBprob p;
private static void readData() throws IOException
{
int s,t;
FileReader datafile=null;
StreamTokenizer st=null;
VAR = new double[NSHARES][NSHARES];
/* Read `VAR' data from file */
datafile=new FileReader(DATAFILE); /* Open the data file */
st=new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
st.commentChar('!'); /* Use the character '!' for comments */
st.eolIsSignificant(true); /* Return end-of-line character */
st.parseNumbers(); /* Read numbers as numbers (not strings) */
for(s=0;s |
|
folioqc.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file folioqc.java
`````````````````
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. Dec. 2011
********************************************************/
import com.dashoptimization.*;
import java.io.*;
import java.lang.*;
public class folioqc
{
static final String DATAFILE = System.getProperty("XPRBDATA") +
"/GS/foliocppqp.dat";
static final double MAXVAR = 0.55; /* Max. allowed variance */
static final int NSHARES = 10; /* Number of shares */
static final int NNA = 4; /* Number of North-American shares */
static final double[] RET = {5,17,26,12,8,9,7,6,31,21};
/* Estimated return in investment */
static final int[] NA = {0,1,2,3}; /* Shares issued in N.-America */
static double[][] VAR; /* Variance/covariance matrix of
estimated returns */
static XPRB bcl;
static XPRBprob p;
private static void readData() throws IOException
{
int s,t;
FileReader datafile=null;
StreamTokenizer st=null;
VAR = new double[NSHARES][NSHARES];
/* Read `VAR' data from file */
datafile=new FileReader(DATAFILE); /* Open the data file */
st=new StreamTokenizer(datafile); /* Initialize the stream tokenizer */
st.commentChar('!'); /* Use the character '!' for comments */
st.eolIsSignificant(true); /* Return end-of-line character */
st.parseNumbers(); /* Read numbers as numbers (not strings) */
for(s=0;s |
|
folioheur.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file folioheur.java
```````````````````
Modeling a small LP problem
to perform portfolio optimization.
-- Heuristic solution --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2003, rev. Dec. 2011
********************************************************/
import com.dashoptimization.*;
public class folioheur
{
static final int MAXNUM = 4; /* Max. number of different assets */
static final int NSHARES = 10; /* Number of shares */
static final int NRISK = 5; /* Number of high-risk shares */
static final int NNA = 4; /* Number of North-American shares */
static final double[] RET = {5,17,26,12,8,9,7,6,31,21};
/* Estimated return in investment */
static final int[] RISK = {1,2,3,8,9}; /* High-risk values among shares */
static final int[] NA = {0,1,2,3}; /* Shares issued in N.-America */
static XPRB bcl;
static XPRBprob p;
static XPRBvar[] frac; /* Fraction of capital used per share */
static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
public static void main(String[] args) throws XPRSprobException, XPRSexception
{
int s;
XPRBexpr Risk,Na,Return,Cap,Num;
bcl = new XPRB(); /* Initialize BCL */
XPRS.init(); /* Initialize Xpress-Optimizer */
p = bcl.newProb("FolioMIPHeur"); /* Create a new problem in BCL */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s 0.2-TOL) buy[s].setLB(1);
}
p.mipOptimize("c"); /* Continue solving the MIP-problem */
ifgsol=0; solval=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 */
System.out.println("Heuristic solution: Total return: " + p.getObjVal());
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 = null; /* No need to store the saved basis any longer */
if(ifgsol==1)
op.setDblControl(XPRS.MIPABSCUTOFF, solval+TOL);
/* Set the cutoff to the best known solution */
}
}
|
|
foliomip3.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliomip3.java
```````````````````
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, Y.Colombani, rev. Dec. 2011
********************************************************/
import java.io.*;
import java.lang.*;
import java.util.*;
import com.dashoptimization.*;
public class foliomip3
{
static final String DATAFILE = "folio10.cdat";
static final int MAXNUM = 7; /* Max. number of different assets */
static final double MAXRISK = 1.0/3; /* Max. investment into high-risk values */
static final double MINREG = 0.2; /* Min. investment per geogr. region */
static final double MAXREG = 0.5; /* Max. investment per geogr. region */
static final double MAXSEC = 0.25; /* Max. investment per ind. sector */
static final double MAXVAL = 0.2; /* Max. investment per share */
static final double MINVAL = 0.1; /* Min. investment per share */
static int NSHARES; /* Number of shares */
static int NRISK; /* Number of high-risk shares */
static int NREGIONS; /* Number of geographical regions */
static int NTYPES; /* Number of share types */
static double[] RET; /* Estimated return in investment */
static int[] RISK; /* High-risk values among shares */
static boolean LOC[][]; /* Geogr. region of shares */
static boolean SEC[][]; /* Industry sector of shares */
static String SHARES_n[];
static String REGIONS_n[];
static String TYPES_n[];
static final String[] MIPSTATUS = {"not loaded", "not optimized",
"LP optimized", "unfinished (no solution)",
"unfinished (solution found)", "infeasible", "optimal",
"unbounded"};
public static void main(String[] args) throws IOException
{
int s,t,r;
XPRB bcl;
XPRBprob p;
XPRBexpr Risk,Return,Cap,Num, LinkL, LinkU;
XPRBexpr[] MinReg, MaxReg, LimSec;
XPRBvar[] frac; /* Fraction of capital used per share */
XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP3"); /* Create a new problem in BCL */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s0.5)
System.out.println(" " + s + ": " + frac[s].getSol()*100 + "% (" +
buy[s].getSol() + ")");
/* Delete the problem */
p.finalize();
p=null;
}
/***********************Data input routines***************************/
/***************************/
/* Input a list of strings */
/***************************/
private static String [] read_str_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_WORD)
{
l.addLast(st.sval);
}
String a[]=new String[l.size()];
l.toArray(a);
return a;
}
/************************/
/* Input a list of ints */
/************************/
private static int [] read_int_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_NUMBER)
{
l.addLast((int)st.nval);
}
int a[]=new int[l.size()];
for(int i=0;i0)
{
RET=new double[NSHARES];
read_dbl_table(st,RET);
}
else
if ( st.sval.equals("LOC") && NSHARES>0 && NREGIONS>0)
LOC=read_bool_table(st,NREGIONS,NSHARES);
else
if ( st.sval.equals("SEC") && NSHARES>0 && NTYPES>0)
SEC=read_bool_table(st,NTYPES,NSHARES);
else
break;
}
/*
for(int i=0;i |
|
foliocb.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliocb.java
`````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.java.
-- Defining an integer solution callback --
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, Y.Colombani, May 2009, rev. Dec. 2011
********************************************************/
import java.io.*;
import java.lang.*;
import java.util.*;
import com.dashoptimization.*;
public class foliocb
{
static final String DATAFILE = "folio10.cdat";
static final int MAXNUM = 15; /* Max. number of different assets */
static final double MAXRISK = 1.0/3; /* Max. investment into high-risk values */
static final double MINREG = 0.2; /* Min. investment per geogr. region */
static final double MAXREG = 0.5; /* Max. investment per geogr. region */
static final double MAXSEC = 0.25; /* Max. investment per ind. sector */
static final double MAXVAL = 0.2; /* Max. investment per share */
static final double MINVAL = 0.1; /* Min. investment per share */
static int NSHARES; /* Number of shares */
static int NRISK; /* Number of high-risk shares */
static int NREGIONS; /* Number of geographical regions */
static int NTYPES; /* Number of share types */
static double[] RET; /* Estimated return in investment */
static int[] RISK; /* High-risk values among shares */
static boolean LOC[][]; /* Geogr. region of shares */
static boolean SEC[][]; /* Industry sector of shares */
static String SHARES_n[];
static String REGIONS_n[];
static String TYPES_n[];
static final String[] MIPSTATUS = {"not loaded", "not optimized",
"LP optimized", "unfinished (no solution)",
"unfinished (solution found)", "infeasible", "optimal",
"unbounded"};
static XPRBvar[] frac; /* Fraction of capital used per share */
static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
public static void main(String[] args) throws XPRSexception
{
int s,t,r;
XPRB bcl;
XPRBprob p;
XPRBexpr Risk,Return,Cap,Num, LinkL, LinkU;
XPRBexpr[] MinReg, MaxReg, LimSec;
IntSolCallback cb;
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP3"); /* Create a new problem in BCL */
XPRS.init(); /* Initialize Xpress-Optimizer */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s0.5)
System.out.println(s + ": " + frac[s].getSol()*100 + "% (" +
buy[s].getSol() + ")");
/* Delete the problem */
p.finalize();
p=null;
}
/**************************Solution printing****************************/
static class IntSolCallback implements XPRSintSolListener
{
public void XPRSintSolEvent(XPRSprob oprob, Object data)
{
int num, s;
XPRBprob bprob;
try {
bprob = (XPRBprob)data;
bprob.beginCB(oprob);
num = oprob.getIntAttrib(XPRS.MIPSOLS); /* Get number of the solution */
bprob.sync(XPRB.XPRS_SOL); /* Update BCL solution values */
System.out.println("Solution " + num + ": Total return: " +
bprob.getObjVal());
for(s=0;s0.5)
System.out.println(" " + s + ": " + frac[s].getSol()*100 + "% (" +
buy[s].getSol() + ")");
bprob.endCB();
}
catch(XPRSprobException e) {
System.out.println("Error " + e.getCode() + ": " + e.getMessage());
}
}
}
/***********************Data input routines***************************/
/***************************/
/* Input a list of strings */
/***************************/
private static String [] read_str_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_WORD)
{
l.addLast(st.sval);
}
String a[]=new String[l.size()];
l.toArray(a);
return a;
}
/************************/
/* Input a list of ints */
/************************/
private static int [] read_int_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_NUMBER)
{
l.addLast((int)st.nval);
}
int a[]=new int[l.size()];
for(int i=0;i0)
{
RET=new double[NSHARES];
read_dbl_table(st,RET);
}
else
if ( st.sval.equals("LOC") && NSHARES>0 && NREGIONS>0)
LOC=read_bool_table(st,NREGIONS,NSHARES);
else
if ( st.sval.equals("SEC") && NSHARES>0 && NTYPES>0)
SEC=read_bool_table(st,NTYPES,NSHARES);
else
break;
}
/*
for(int i=0;i |
|
foliosolpool.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliosolpool.java
``````````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.java.
-- Using the MIP solution pool --
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, Y.Colombani, June 2009, rev. Dec. 2011
********************************************************/
import java.io.*;
import java.lang.*;
import java.util.*;
import com.dashoptimization.*;
public class foliosolpool
{
static final String DATAFILE = "folio10.cdat";
static final int MAXNUM = 7; /* Max. number of different assets */
static final double MAXRISK = 1.0/3; /* Max. investment into high-risk values */
static final double MINREG = 0.2; /* Min. investment per geogr. region */
static final double MAXREG = 0.5; /* Max. investment per geogr. region */
static final double MAXSEC = 0.25; /* Max. investment per ind. sector */
static final double MAXVAL = 0.2; /* Max. investment per share */
static final double MINVAL = 0.1; /* Min. investment per share */
static int NSHARES; /* Number of shares */
static int NRISK; /* Number of high-risk shares */
static int NREGIONS; /* Number of geographical regions */
static int NTYPES; /* Number of share types */
static double[] RET; /* Estimated return in investment */
static int[] RISK; /* High-risk values among shares */
static boolean LOC[][]; /* Geogr. region of shares */
static boolean SEC[][]; /* Industry sector of shares */
static String SHARES_n[];
static String REGIONS_n[];
static String TYPES_n[];
static XPRBvar[] frac; /* Fraction of capital used per share */
static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
static XPRBprob p;
public static void main(String[] args) throws IOException, XPRSexception
{
int s,t,r,i;
XPRB bcl;
XPRBexpr Risk,Return,Cap,Num, LinkL, LinkU;
XPRBexpr[] MinReg, MaxReg, LimSec;
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP3"); /* Create a new problem in BCL */
XPRS.init(); /* Initialize Xpress-Optimizer */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s0.5)
System.out.println(" " + SHARES_n[s] + ": " + frac[s].getSol()*100 +
"% (" + buy[s].getSol() + ")");
}
/***********************Data input routines***************************/
/***************************/
/* Input a list of strings */
/***************************/
private static String [] read_str_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_WORD)
{
l.addLast(st.sval);
}
String a[]=new String[l.size()];
l.toArray(a);
return a;
}
/************************/
/* Input a list of ints */
/************************/
private static int [] read_int_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_NUMBER)
{
l.addLast((int)st.nval);
}
int a[]=new int[l.size()];
for(int i=0;i0)
{
RET=new double[NSHARES];
read_dbl_table(st,RET);
}
else
if ( st.sval.equals("LOC") && NSHARES>0 && NREGIONS>0)
LOC=read_bool_table(st,NREGIONS,NSHARES);
else
if ( st.sval.equals("SEC") && NSHARES>0 && NTYPES>0)
SEC=read_bool_table(st,NTYPES,NSHARES);
else
break;
}
/*
for(int i=0;i |
|
folioenumsol.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file folioenumsol.java
``````````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.java.
-- Using the solution enumerator --
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, Y.Colombani, June 2009, rev. Dec. 2011
********************************************************/
import java.io.*;
import java.lang.*;
import java.util.*;
import com.dashoptimization.*;
public class folioenumsol
{
static final String DATAFILE = "folio10.cdat";
static final int MAXNUM = 7; /* Max. number of different assets */
static final double MAXRISK = 1.0/3; /* Max. investment into high-risk values */
static final double MINREG = 0.2; /* Min. investment per geogr. region */
static final double MAXREG = 0.5; /* Max. investment per geogr. region */
static final double MAXSEC = 0.25; /* Max. investment per ind. sector */
static final double MAXVAL = 0.2; /* Max. investment per share */
static final double MINVAL = 0.1; /* Min. investment per share */
static int NSHARES; /* Number of shares */
static int NRISK; /* Number of high-risk shares */
static int NREGIONS; /* Number of geographical regions */
static int NTYPES; /* Number of share types */
static double[] RET; /* Estimated return in investment */
static int[] RISK; /* High-risk values among shares */
static boolean LOC[][]; /* Geogr. region of shares */
static boolean SEC[][]; /* Industry sector of shares */
static String SHARES_n[];
static String REGIONS_n[];
static String TYPES_n[];
static XPRBvar[] frac; /* Fraction of capital used per share */
static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
static XPRBprob p;
public static void main(String[] args) throws IOException, XPRSexception
{
int s,t,r,i;
XPRB bcl;
XPRBexpr Risk,Return,Cap,Num, LinkL, LinkU;
XPRBexpr[] MinReg, MaxReg, LimSec;
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP3"); /* Create a new problem in BCL */
XPRS.init(); /* Initialize Xpress-Optimizer */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s0.5)
System.out.println(" " + SHARES_n[s] + ": " + frac[s].getSol()*100 +
"% (" + buy[s].getSol() + ")");
}
/***********************Data input routines***************************/
/***************************/
/* Input a list of strings */
/***************************/
private static String [] read_str_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_WORD)
{
l.addLast(st.sval);
}
String a[]=new String[l.size()];
l.toArray(a);
return a;
}
/************************/
/* Input a list of ints */
/************************/
private static int [] read_int_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_NUMBER)
{
l.addLast((int)st.nval);
}
int a[]=new int[l.size()];
for(int i=0;i0)
{
RET=new double[NSHARES];
read_dbl_table(st,RET);
}
else
if ( st.sval.equals("LOC") && NSHARES>0 && NREGIONS>0)
LOC=read_bool_table(st,NREGIONS,NSHARES);
else
if ( st.sval.equals("SEC") && NSHARES>0 && NTYPES>0)
SEC=read_bool_table(st,NTYPES,NSHARES);
else
break;
}
/*
for(int i=0;i |
|
folioinfeas.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file folioinfeas.java
`````````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.java.
-- Infeasible model parameter values --
-- Handling infeasibility through auxiliary variables --
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, June 2009, rev. Dec. 2011
********************************************************/
import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.DecimalFormat;
import com.dashoptimization.*;
public class folioinfeas
{
static final String DATAFILE = "folio10.cdat";
static final int MAXNUM = 4; /* Max. number of different assets */
static final double MAXRISK = 1.0/3; /* Max. investment into high-risk values */
static final double MINREG = 0.3; /* Min. investment per geogr. region */
static final double MAXREG = 0.5; /* Max. investment per geogr. region */
static final double MAXSEC = 0.15; /* Max. investment per ind. sector */
static final double MAXVAL = 0.2; /* Max. investment per share */
static final double MINVAL = 0.1; /* Min. investment per share */
static int NSHARES; /* Number of shares */
static int NRISK; /* Number of high-risk shares */
static int NREGIONS; /* Number of geographical regions */
static int NTYPES; /* Number of share types */
static double[] RET; /* Estimated return in investment */
static int[] RISK; /* High-risk values among shares */
static boolean LOC[][]; /* Geogr. region of shares */
static boolean SEC[][]; /* Industry sector of shares */
static String SHARES_n[];
static String REGIONS_n[];
static String TYPES_n[];
static final String[] MIPSTATUS = {"not loaded", "not optimized",
"LP optimized", "unfinished (no solution)",
"unfinished (solution found)", "infeasible", "optimal",
"unbounded"};
private static DecimalFormat form = new DecimalFormat ("0.00");
public static void main(String[] args) throws IOException
{
int s,t,r;
XPRB bcl;
XPRBprob p;
XPRBexpr LinkL, LinkU, le, le2;
XPRBctr Risk,Return,Cap,Num;
XPRBctr[] MinReg, MaxReg, LimSec;
XPRBvar[] frac; /* Fraction of capital used per share */
XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP3inf"); /* Create a new problem in BCL */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s0.5)
System.out.println(s + ": " + frac[s].getSol()*100 + "% (" +
buy[s].getSol() + ")");
/* Delete the problem */
p.finalize();
p=null;
}
/***********************Data input routines***************************/
/***************************/
/* Input a list of strings */
/***************************/
private static String [] read_str_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_WORD)
{
l.addLast(st.sval);
}
String a[]=new String[l.size()];
l.toArray(a);
return a;
}
/************************/
/* Input a list of ints */
/************************/
private static int [] read_int_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_NUMBER)
{
l.addLast((int)st.nval);
}
int a[]=new int[l.size()];
for(int i=0;i0)
{
RET=new double[NSHARES];
read_dbl_table(st,RET);
}
else
if ( st.sval.equals("LOC") && NSHARES>0 && NREGIONS>0)
LOC=read_bool_table(st,NREGIONS,NSHARES);
else
if ( st.sval.equals("SEC") && NSHARES>0 && NTYPES>0)
SEC=read_bool_table(st,NTYPES,NSHARES);
else
break;
}
/*
for(int i=0;i |
|
folioiis.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file folioiis.java
``````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.java.
-- Infeasible model parameter values --
-- Retrieving IIS --
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, June 2009, rev. Dec. 2011
********************************************************/
import java.io.*;
import java.lang.*;
import java.util.*;
import com.dashoptimization.*;
public class folioiis
{
static final String DATAFILE = "folio10.cdat";
static final int MAXNUM = 5; /* Max. number of different assets */
static final double MAXRISK = 1.0/3; /* Max. investment into high-risk values */
static final double MINREG = 0.1; /* Min. investment per geogr. region */
static final double MAXREG = 0.2; /* Max. investment per geogr. region */
static final double MAXSEC = 0.1; /* Max. investment per ind. sector */
static final double MAXVAL = 0.2; /* Max. investment per share */
static final double MINVAL = 0.1; /* Min. investment per share */
static int NSHARES; /* Number of shares */
static int NRISK; /* Number of high-risk shares */
static int NREGIONS; /* Number of geographical regions */
static int NTYPES; /* Number of share types */
static double[] RET; /* Estimated return in investment */
static int[] RISK; /* High-risk values among shares */
static boolean LOC[][]; /* Geogr. region of shares */
static boolean SEC[][]; /* Industry sector of shares */
static String SHARES_n[];
static String REGIONS_n[];
static String TYPES_n[];
public static void main(String[] args) throws IOException
{
int s,t,r;
XPRB bcl;
XPRBprob p;
XPRBexpr LinkL, LinkU, le, le2;
XPRBctr Risk,Return,Cap,Num;
XPRBctr[] MinReg, MaxReg, LimSec;
XPRBvar[] frac; /* Fraction of capital used per share */
XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP3inf"); /* Create a new problem in BCL */
XPRS.init(); /* Initialize Xpress-Optimizer */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s0)
{ /* Print all variables in the IIS */
System.out.print(" Variables: ");
for(i=0;i0)
{ /* Print all constraints in the IIS */
System.out.print(" Constraints: ");
for(i=0;i l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_WORD)
{
l.addLast(st.sval);
}
String a[]=new String[l.size()];
l.toArray(a);
return a;
}
/************************/
/* Input a list of ints */
/************************/
private static int [] read_int_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_NUMBER)
{
l.addLast((int)st.nval);
}
int a[]=new int[l.size()];
for(int i=0;i0)
{
RET=new double[NSHARES];
read_dbl_table(st,RET);
}
else
if ( st.sval.equals("LOC") && NSHARES>0 && NREGIONS>0)
LOC=read_bool_table(st,NREGIONS,NSHARES);
else
if ( st.sval.equals("SEC") && NSHARES>0 && NTYPES>0)
SEC=read_bool_table(st,NTYPES,NSHARES);
else
break;
}
/*
for(int i=0;i |
|
foliomiis.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliomiis.java
```````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.java.
-- Infeasible model parameter values --
-- Retrieving MIIS --
(c) 2011 Fair Isaac Corporation
author: S.Heipcke, Mar. 2011, rev. Dec. 2011
********************************************************/
import java.io.*;
import java.lang.*;
import java.util.*;
import com.dashoptimization.*;
public class foliomiis
{
static final String DATAFILE = "folio5.cdat";
static final int MAXNUM = 5; /* Max. number of different assets */
static final double MAXRISK = 1.0/4; /* Max. investment into high-risk values */
static final double MINREG = 0.1; /* Min. investment per geogr. region */
static final double MAXREG = 0.25; /* Max. investment per geogr. region */
static final double MAXSEC = 0.15; /* Max. investment per ind. sector */
static final double MAXVAL = 0.225; /* Max. investment per share */
static final double MINVAL = 0.1; /* Min. investment per share */
static int NSHARES; /* Number of shares */
static int NRISK; /* Number of high-risk shares */
static int NREGIONS; /* Number of geographical regions */
static int NTYPES; /* Number of share types */
static double[] RET; /* Estimated return in investment */
static int[] RISK; /* High-risk values among shares */
static boolean LOC[][]; /* Geogr. region of shares */
static boolean SEC[][]; /* Industry sector of shares */
static String SHARES_n[];
static String REGIONS_n[];
static String TYPES_n[];
public static void main(String[] args) throws IOException
{
int s,t,r;
XPRB bcl;
XPRBprob p;
XPRBexpr LinkL, LinkU, le, le2;
XPRBctr Risk,Return,Cap,Num;
XPRBctr[] MinReg, MaxReg, LimSec;
XPRBvar[] frac; /* Fraction of capital used per share */
XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP3inf"); /* Create a new problem in BCL */
XPRS.init(); /* Initialize Xpress-Optimizer */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
for(s=0;s0)
{ /* Print all variables in the IIS */
System.out.print(" Variables: ");
for(i=0;i0)
{ /* Print all constraints in the IIS */
System.out.print(" Constraints: ");
for(i=0;i l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_WORD)
{
l.addLast(st.sval);
}
String a[]=new String[l.size()];
l.toArray(a);
return a;
}
/************************/
/* Input a list of ints */
/************************/
private static int [] read_int_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_NUMBER)
{
l.addLast((int)st.nval);
}
int a[]=new int[l.size()];
for(int i=0;i0)
{
RET=new double[NSHARES];
read_dbl_table(st,RET);
}
else
if ( st.sval.equals("LOC") && NSHARES>0 && NREGIONS>0)
LOC=read_bool_table(st,NREGIONS,NSHARES);
else
if ( st.sval.equals("SEC") && NSHARES>0 && NTYPES>0)
SEC=read_bool_table(st,NTYPES,NSHARES);
else
break;
}
/*
for(int i=0;i |
|
foliorep.java |
/********************************************************
Xpress-BCL Java Example Problems
================================
file foliorep.java
``````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.java.
-- Infeasible model parameter values --
-- Retrieving IIS --
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, June 2009, rev. Nov. 2014
********************************************************/
import java.io.*;
import java.lang.*;
import java.util.*;
import com.dashoptimization.*;
public class foliorep
{
static final String DATAFILE = "folio10.cdat";
static final int MAXNUM = 4; /* Max. number of different assets */
static final double MAXRISK = 1.0/3; /* Max. investment into high-risk values */
static final double MINREG = 0.3; /* Min. investment per geogr. region */
static final double MAXREG = 0.5; /* Max. investment per geogr. region */
static final double MAXSEC = 0.15; /* Max. investment per ind. sector */
static final double MAXVAL = 0.2; /* Max. investment per share */
static final double MINVAL = 0.1; /* Min. investment per share */
static int NSHARES; /* Number of shares */
static int NRISK; /* Number of high-risk shares */
static int NREGIONS; /* Number of geographical regions */
static int NTYPES; /* Number of share types */
static double[] RET; /* Estimated return in investment */
static int[] RISK; /* High-risk values among shares */
static boolean LOC[][]; /* Geogr. region of shares */
static boolean SEC[][]; /* Industry sector of shares */
static String SHARES_n[];
static String REGIONS_n[];
static String TYPES_n[];
static XPRBvar[] frac; /* Fraction of capital used per share */
static XPRBvar[] buy; /* 1 if asset is in portfolio, 0 otherwise */
public static void main(String[] args) throws IOException
{
int s,t,r;
XPRB bcl;
XPRBprob p;
XPRBexpr LinkL, LinkU, le, le2;
XPRBctr Risk,Return,Cap,Num;
XPRBctr[] MinReg, MaxReg, LimSec;
ArrayList AllCtrs = new ArrayList(); /* Array of ctrs that will be allowed to be relaxed */
try
{
readData(); /* Read data from file */
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
bcl = new XPRB(); /* Initialize BCL */
p = bcl.newProb("FolioMIP3inf"); /* Create a new problem in BCL */
XPRS.init(); /* Initialize Xpress-Optimizer */
/* Create the decision variables */
frac = new XPRBvar[NSHARES];
buy = new XPRBvar[NSHARES];
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
*/
int nrow, ncol;
double[] lrp, grp, lbp, ubp;
ncol=op.getIntAttrib(XPRS.ORIGINALCOLS);
nrow=op.getIntAttrib(XPRS.ORIGINALROWS);
lrp = new double[nrow];
grp = new double[nrow];
lbp = new double[ncol];
ubp = new double[ncol];
lrp[Risk.getRowNum()] = 1;
for(r=0;r0.5)
System.out.printf(" %s: %g%% (%g)\n", SHARES_n[s], frac[s].getSol()*100, buy[s].getSol());
}
static void print_violated(ArrayList ctrs)
{
System.out.println(" Violated (relaxed) constraints:");
for (XPRBctr c: ctrs) {
String type;
double viol, slack = c.getSlack();
switch (c.getType()) {
case XPRB.E: viol = Math.abs(slack); type = " ="; break;
case XPRB.G: viol = slack; type = ">="; break;
case XPRB.L: viol = -slack; type = "<="; break;
default: System.out.println(" unexpected constraint type"); continue;
}
if (viol > 1e-6) System.out.printf(" %s constraint %s by %g\n", type, c.getName(), -slack);
}
}
/***********************Data input routines***************************/
/***************************/
/* Input a list of strings */
/***************************/
private static String [] read_str_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_WORD)
{
l.addLast(st.sval);
}
String a[]=new String[l.size()];
l.toArray(a);
return a;
}
/************************/
/* Input a list of ints */
/************************/
private static int [] read_int_list(StreamTokenizer st) throws IOException
{
LinkedList l=new LinkedList();
st.nextToken(); /* Skip ':' */
while(st.nextToken()==st.TT_NUMBER)
{
l.addLast((int)st.nval);
}
int a[]=new int[l.size()];
for(int i=0;i0)
{
RET=new double[NSHARES];
read_dbl_table(st,RET);
}
else
if ( st.sval.equals("LOC") && NSHARES>0 && NREGIONS>0)
LOC=read_bool_table(st,NREGIONS,NSHARES);
else
if ( st.sval.equals("SEC") && NSHARES>0 && NTYPES>0)
SEC=read_bool_table(st,NTYPES,NSHARES);
else
break;
}
/*
for(int i=0;i |
|