Example #1
0
    internal static ILPMatrix PopulateByRow(IMPModeler model)
    {
        ILPMatrix lp = model.AddLPMatrix();

        double[]  lb = { 0.0, 0.0, 0.0 };
        double[]  ub = { 40.0, System.Double.MaxValue, System.Double.MaxValue };
        INumVar[] x  = model.NumVarArray(model.ColumnArray(lp, 3), lb, ub);

        // - x0 +   x1 + x2 <= 20
        //   x0 - 3*x1 + x2 <= 30
        double[]   lhs = { -System.Double.MaxValue, -System.Double.MaxValue };
        double[]   rhs = { 20.0, 30.0 };
        double[][] val = { new double[] { -1.0,  1.0, 1.0 },
                           new double[] {  1.0, -3.0, 1.0 } };
        int[][]    ind = { new int[] { 0, 1, 2 },
                           new int[] { 0, 1, 2 } };
        lp.AddRows(lhs, rhs, ind, val);

        // Q = 0.5 ( 33*x0*x0 + 22*x1*x1 + 11*x2*x2 - 12*x0*x1 - 23*x1*x2 )
        INumExpr x00 = model.Prod(33.0, model.Square(x[0]));
        INumExpr x11 = model.Prod(22.0, model.Square(x[1]));
        INumExpr x22 = model.Prod(11.0, model.Square(x[2]));
        INumExpr x01 = model.Prod(-12.0, model.Prod(x[0], x[1]));
        INumExpr x12 = model.Prod(-23.0, model.Prod(x[1], x[2]));
        INumExpr Q   = model.Prod(0.5, model.Sum(x00, x11, x22, x01, x12));

        // maximize x0 + 2*x1 + 3*x2 + Q
        double[] objvals = { 1.0, 2.0, 3.0 };
        model.Add(model.Maximize(model.Diff(model.ScalProd(x, objvals), Q)));

        return(lp);
    }
Example #2
0
    internal static ILPMatrix PopulateByRow(IMPModeler model)
    {
        ILPMatrix lp = model.AddLPMatrix();

          double[]    lb = {0.0, 0.0, 0.0};
          double[]    ub = {40.0, System.Double.MaxValue, System.Double.MaxValue};
          INumVar[]   x  = model.NumVarArray(model.ColumnArray(lp, 3), lb, ub);

          // - x0 +   x1 + x2 <= 20
          //   x0 - 3*x1 + x2 <= 30
          double[]   lhs = {-System.Double.MaxValue, -System.Double.MaxValue};
          double[]   rhs = {20.0, 30.0};
          double[][] val = { new double[] {-1.0,  1.0,  1.0},
                         new double[] { 1.0, -3.0,  1.0} };
          int[][]    ind = { new int[] {0, 1, 2},
                         new int[] {0, 1, 2} };
          lp.AddRows(lhs, rhs, ind, val);

          // Q = 0.5 ( 33*x0*x0 + 22*x1*x1 + 11*x2*x2 - 12*x0*x1 - 23*x1*x2 )
          INumExpr x00 = model.Prod( 33.0, model.Square(x[0]));
          INumExpr x11 = model.Prod( 22.0, model.Square(x[1]));
          INumExpr x22 = model.Prod( 11.0, model.Square(x[2]));
          INumExpr x01 = model.Prod(-12.0, model.Prod(x[0], x[1]));
          INumExpr x12 = model.Prod(-23.0, model.Prod(x[1], x[2]));
          INumExpr Q   = model.Prod(0.5, model.Sum(x00, x11, x22, x01, x12));

          // maximize x0 + 2*x1 + 3*x2 + Q
          double[] objvals = {1.0, 2.0, 3.0};
          model.Add(model.Maximize(model.Diff(model.ScalProd(x, objvals), Q)));

          return (lp);
    }
Example #3
0
    // To populate by row, we first create the variables, and then use them to
    // create the range constraints and objective.  The model we create is:
    //
    // Minimize
    //  obj:   - 0.5 (-3 * xˆ2 - 3 * yˆ2 - 1 * x * y)
    // Subject To
    //  c1: -x + y >= 0
    //  c2:  x + y >= 0
    // Bounds
    //  -1 <= x <= 1
    //   0 <= y <= 1
    // End
    internal static ILPMatrix PopulateByRow(IMPModeler model)
    {
        ILPMatrix lp = model.AddLPMatrix();

          double[]    lb = {-1.0, 0.0};
          double[]    ub = { 1.0, 1.0};
          INumVar[] x  = model.NumVarArray(model.ColumnArray(lp, 2), lb, ub);

          double[]   lhs = {0.0, 0.0};
          double[]   rhs = {System.Double.MaxValue, System.Double.MaxValue};
          double[][] val = {new double[] {-1.0, 1.0},
                        new double[] { 1.0, 1.0}};
          int[][]    ind = {new int[] {0, 1},
                        new int[] {0, 1}};
          lp.AddRows(lhs, rhs, ind, val);

          INumExpr x00 = model.Prod(-3.0, x[0], x[0]);
          INumExpr x11 = model.Prod(-3.0, x[1], x[1]);
          INumExpr x01 = model.Prod(-1.0, x[0], x[1]);
          INumExpr Q   = model.Prod(0.5, model.Sum(x00, x11, x01));

          model.Add(model.Minimize(Q));

          return (lp);
    }
Example #4
0
    // To populate by row, we first create the variables, and then use them to
    // create the range constraints and objective.  The model we create is:
    //
    // Minimize
    //  obj:   - 0.5 (-3 * xˆ2 - 3 * yˆ2 - 1 * x * y)
    // Subject To
    //  c1: -x + y >= 0
    //  c2:  x + y >= 0
    // Bounds
    //  -1 <= x <= 1
    //   0 <= y <= 1
    // End

    internal static ILPMatrix PopulateByRow(IMPModeler model)
    {
        ILPMatrix lp = model.AddLPMatrix();

        double[]  lb = { -1.0, 0.0 };
        double[]  ub = { 1.0, 1.0 };
        INumVar[] x  = model.NumVarArray(model.ColumnArray(lp, 2), lb, ub);

        double[]   lhs = { 0.0, 0.0 };
        double[]   rhs = { System.Double.MaxValue, System.Double.MaxValue };
        double[][] val = { new double[] { -1.0, 1.0 },
                           new double[] {  1.0, 1.0 } };
        int[][]    ind = { new int[] { 0, 1 },
                           new int[] { 0, 1 } };
        lp.AddRows(lhs, rhs, ind, val);

        INumExpr x00 = model.Prod(-3.0, x[0], x[0]);
        INumExpr x11 = model.Prod(-3.0, x[1], x[1]);
        INumExpr x01 = model.Prod(-1.0, x[0], x[1]);
        INumExpr Q   = model.Prod(0.5, model.Sum(x00, x11, x01));

        model.Add(model.Minimize(Q));

        return(lp);
    }
Example #5
0
    // Creating a simple QP problem
    internal static ILPMatrix CreateQPModel(IMPModeler model)
    {
        ILPMatrix lp = model.AddLPMatrix();

        double[]  lb    = { 0.0, 0.0, 0.0 };
        double[]  ub    = { 40.0, System.Double.MaxValue, System.Double.MaxValue };
        INumVar[] x     = model.NumVarArray(model.ColumnArray(lp, 3), lb, ub);
        int       nvars = x.Length;

        for (int j = 0; j < nvars; ++j)
        {
            x[j].Name = "x" + j;
        }

        // - x0 +   x1 + x2 <= 20
        //   x0 - 3*x1 + x2 <= 30
        double[]   lhs = { -System.Double.MaxValue, -System.Double.MaxValue };
        double[]   rhs = { 20.0, 30.0 };
        double[][] val = { new double[] { -1.0,  1.0, 1.0 },
                           new double[] {  1.0, -3.0, 1.0 } };
        int[][]    ind = { new int[] { 0, 1, 2 },
                           new int[]    { 0, 1, 2 } };
        lp.AddRows(lhs, rhs, ind, val);

        // minimize - x0 - x1 - x2 + x0*x0 + x1*x1 + x0*x1 + x1*x0
        ILQNumExpr objExpr = model.LqNumExpr();

        for (int i = 0; i < nvars; ++i)
        {
            objExpr.AddTerm(-1.0, x[i]);
            for (int j = 0; j < nvars; ++j)
            {
                objExpr.AddTerm(1.0, x[i], x[j]);
            }
        }
        IObjective obj = model.Minimize(objExpr);

        model.Add(obj);

        // Print out the objective function
        PrintObjective(obj);

        return(lp);
    }
Example #6
0
    internal static ILPMatrix PopulateByRow(IMPModeler model)
    {
        ILPMatrix lp = model.AddLPMatrix();

        double[]  lb = { 0.0, 0.0, 0.0 };
        double[]  ub = { 40.0, System.Double.MaxValue, System.Double.MaxValue };
        INumVar[] x  = model.NumVarArray(model.ColumnArray(lp, 3), lb, ub);

        double[]   lhs = { -System.Double.MaxValue, -System.Double.MaxValue };
        double[]   rhs = { 20.0, 30.0 };
        double[][] val = { new double[] { -1.0,  1.0, 1.0 },
                           new double[] {  1.0, -3.0, 1.0 } };
        int[][]    ind = { new int[] { 0, 1, 2 },
                           new int[] { 0, 1, 2 } };
        lp.AddRows(lhs, rhs, ind, val);

        double[] objvals = { 1.0, 2.0, 3.0 };
        model.AddMaximize(model.ScalProd(x, objvals));

        return(lp);
    }
Example #7
0
    internal static ILPMatrix PopulateByRow(IMPModeler model)
    {
        ILPMatrix lp = model.AddLPMatrix();

          double[]  lb = {0.0, 0.0, 0.0};
          double[]  ub = {40.0, System.Double.MaxValue, System.Double.MaxValue};
          INumVar[] x  = model.NumVarArray(model.ColumnArray(lp, 3), lb, ub);

          double[]   lhs = {-System.Double.MaxValue, -System.Double.MaxValue};
          double[]   rhs = {20.0, 30.0};
          double[][] val = {new double[] {-1.0,  1.0,  1.0},
                        new double[] { 1.0, -3.0,  1.0}};
          int[][]    ind = {new int[] {0, 1, 2},
                        new int[] {0, 1, 2}};
          lp.AddRows(lhs, rhs, ind, val);

          double[] objvals = {1.0, 2.0, 3.0};
          model.AddMaximize(model.ScalProd(x, objvals));

          return (lp);
    }
Example #8
0
    // Creating a simple QP problem
    internal static ILPMatrix CreateQPModel(IMPModeler model)
    {
        ILPMatrix lp = model.AddLPMatrix();

          double[]    lb = {0.0, 0.0, 0.0};
          double[]    ub = {40.0, System.Double.MaxValue, System.Double.MaxValue};
          INumVar[] x  = model.NumVarArray(model.ColumnArray(lp, 3), lb, ub);
          int nvars = x.Length;
          for (int j = 0; j < nvars; ++j)
         x[j].Name = "x" +j;

          // - x0 +   x1 + x2 <= 20
          //   x0 - 3*x1 + x2 <= 30
          double[] lhs = { -System.Double.MaxValue, -System.Double.MaxValue };
          double[] rhs = { 20.0, 30.0 };
          double[][] val = { new double[] {-1.0,  1.0,  1.0},
                         new double[] { 1.0, -3.0,  1.0} };
          int[][] ind = { new int[] {0, 1, 2},
                         new int[] {0, 1, 2} };
          lp.AddRows(lhs, rhs, ind, val);

          // minimize - x0 - x1 - x2 + x0*x0 + x1*x1 + x0*x1 + x1*x0
          ILQNumExpr objExpr = model.LqNumExpr();
          for (int i = 0; i < nvars; ++i) {
         objExpr.AddTerm(-1.0, x[i]);
         for (int j = 0; j < nvars; ++j) {
            objExpr.AddTerm(1.0, x[i], x[j]);
         }
          }
          IObjective obj = model.Minimize(objExpr);
          model.Add(obj);

          // Print out the objective function
          PrintObjective(obj);

          return lp;
    }