Example #1
0
    public static void Main(string[] args)
    {
      double[][] A = 
        { new double[] { 50.0, 31.0 },
          new double[] { 3.0,  -2.0 } };
      double[] c = { 1.0, 0.64 };
      using (Model M = new Model("milo1"))
      {
        Variable x = M.Variable("x", 2, Domain.GreaterThan(0.0), Domain.IsInteger());

        // Create the constraints
        //      50.0 x[0] + 31.0 x[1] <= 250.0
        //       3.0 x[0] -  2.0 x[1] >= -4.0
        M.Constraint("c1", Expr.Dot(A[0], x), Domain.LessThan(250.0));
        M.Constraint("c2", Expr.Dot(A[1], x), Domain.GreaterThan(-4.0));

        // Set max solution time
        M.SetSolverParam("mioMaxTime", 60.0);
        // Set max relative gap (to its default value)
        M.SetSolverParam("mioTolRelGap", 1e-4);
        // Set max absolute gap (to its default value)
        M.SetSolverParam("mioTolAbsGap", 0.0);

        // Set the objective function to (c^T * x)
        M.Objective("obj", ObjectiveSense.Maximize, Expr.Dot(c, x));

        // Solve the problem
        M.Solve();

        // Get the solution values
        double[] sol = x.Level();
        Console.WriteLine("x1,x2 = {0}, {1}", sol[0], sol[1]);
        Console.WriteLine("MIP rel gap = {0} ({0})",M.GetSolverDoubleInfo("mioObjRelGap"),M.GetSolverDoubleInfo("mioObjAbsGap"));
      }
    }
Example #2
0
        /*
            Description:
                Extends the basic Markowitz model with a market cost term.

            Input:
                n: Number of assets
                mu: An n dimmensional vector of expected returns
                GT: A matrix with n columns so (GT")*GT  = covariance matrix"
                x0: Initial holdings 
                w: Initial cash holding
                gamma: Maximum risk (=std. dev) accepted
                f: If asset j is traded then a fixed cost f_j must be paid
                g: If asset j is traded then a cost g_j must be paid for each unit traded

            Output:
               Optimal expected return and the optimal portfolio     

        */
        public static double[] MarkowitzWithTransactionsCost
          ( int n, 
            double[] mu,
            DenseMatrix GT,
            double[] x0,
            double   w,
            double   gamma,
            double[] f,
            double[] g)
        {

          // Upper bound on the traded amount
          double[] u = new double[n];
          {
            double v = w+sum(x0);
            for (int i = 0; i < n; ++i) u[i] = v;
          }

          using( Model M = new Model("Markowitz portfolio with transaction costs") )
          {
            Console.WriteLine("\n-------------------------------------------------------------------------");
            Console.WriteLine("Markowitz portfolio optimization with transaction cost\n");
            Console.WriteLine("------------------------------------------------------------------------\n");
          
            //M.SetLogHandler(Console.Out);

            // Defines the variables. No shortselling is allowed.
            Variable x = M.Variable("x", n, Domain.GreaterThan(0.0));

            // Addtional "helper" variables 
            Variable z = M.Variable("z", n, Domain.Unbounded());
            // Binary varables
            Variable y = M.Variable("y", n, Domain.InRange(0.0,1.0), Domain.IsInteger());

            //  Maximize expected return
            M.Objective("obj", ObjectiveSense.Maximize, Expr.Dot(mu,x));

            // Invest amount + transactions costs = initial wealth
            M.Constraint("budget", Expr.Add(Expr.Add(Expr.Sum(x),Expr.Dot(f,y)),Expr.Dot(g,z)),
                         Domain.EqualsTo(w+sum(x0)));

            // Imposes a bound on the risk
            M.Constraint("risk", Expr.Vstack(Expr.ConstTerm(new double[] {gamma}),Expr.Mul(GT,x)), Domain.InQCone());

            // z >= |x-x0| 
            M.Constraint("buy",  Expr.Sub(z,Expr.Sub(x,x0)),Domain.GreaterThan(0.0));
            M.Constraint("sell", Expr.Sub(z,Expr.Sub(x0,x)),Domain.GreaterThan(0.0));

            //M.constraint("trade", Expr.hstack(z.asExpr(),Expr.sub(x,x0)), Domain.inQcone())"

            // Consraints for turning y off and on. z-diag(u)*y<=0 i.e. z_j <= u_j*y_j
            M.Constraint("y_on_off", Expr.Sub(z,Expr.Mul(Matrix.Diag(u),y)), Domain.LessThan(0.0));

            // Integer optimization problems can be very hard to solve so limiting the 
            // maximum amount of time is a valuable safe guard
            M.SetSolverParam("mioMaxTime", 180.0);
            M.Solve();
            Console.WriteLine("Expected return: {0:e4} Std. deviation: {1:e4} Transactions cost: {2:e4}",
                   dot(mu, x.Level()), gamma, dot(f, y.Level()) + dot(g, z.Level()));
            return x.Level();
          }
        }