/******************************************************** Xpress-BCL C# Example Problems ============================== file folioqp.cs ``````````````` Modeling a small QP problem to perform portfolio optimization. -- 1. QP: minimize variance 2. MIQP: limited number of assets --- (c) 2008 Fair Isaac Corporation authors: S.Heipcke, D.Brett. ********************************************************/ using System; using System.Text; using System.IO; using BCL; namespace Examples { public class TestUGFolioQp { //Define XPRBDATAPATH to wherever you have placed the data folder; here we expect it to be same directory as compiled example. static string XPRBDATAPATH = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName + "/Data"; static string DATAFILE = XPRBDATAPATH + "/GS/foliocppqp.dat"; const int TARGET = 9; // Target yield const int MAXNUM = 4; // Max. number of different assets const int NSHARES = 10; // Number of shares const int 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 = new double[NSHARES,NSHARES]; // Variance/covariance matrix of // estimated returns public static void Main() { XPRB.init(); int s,t; XPRBprob p = new XPRBprob("FolioQP"); // Initialize a new problem in BCL XPRBexpr Na,Return,Cap,Num; XPRBexpr Variance; XPRBvar[] frac = new XPRBvar[NSHARES]; // Fraction of capital used per share XPRBvar[] buy = new XPRBvar[NSHARES]; // 1 if asset is in portfolio, 0 otherwise FileStream file; StreamReader fileStreamIn; TestUGFolioQp TestInstance = new TestUGFolioQp(); // Read `VAR' data from file file = new FileStream(DATAFILE, FileMode.Open, FileAccess.Read); fileStreamIn = new StreamReader(file); object[] outdata = new object[NSHARES]; for (s = 0; s < NSHARES; s++) { p.XPRBreadarrline(fileStreamIn, 200, "{g} ", out outdata, NSHARES); for(t=0; t= 0,5); // Spend all the capital Cap = new XPRBexpr(); for(s=0;s= TARGET); // Solve the problem p.setSense(BCLconstant.XPRB_MINIM); p.lpOptimize(); /* Solve the LP-problem */ // Solution printing System.Console.WriteLine("With a target of " + TARGET + " minimum variance is " + p.getObjVal()); for(s=0;s