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); }
internal static INumVar[] populateByRow(IMPModeler model, IRange[] row) { double[] lb = { 0.0, 0.0, 0.0 }; double[] ub = { 40.0, System.Double.MaxValue, System.Double.MaxValue }; INumVar[] x = model.NumVarArray(3, lb, ub); // - x0 + x1 + x2 <= 20 // x0 - 3*x1 + x2 <= 30 double[][] val = { new double[] { -1.0, 1.0, 1.0 }, new double[] { 1.0, -3.0, 1.0 } }; row[0] = model.AddLe(model.ScalProd(val[0], x), 20.0); row[1] = model.AddLe(model.ScalProd(val[1], x), 30.0); // x0*x0 + x1*x1 + x2*x2 <= 1.0 row[2] = model.AddLe(model.Sum(model.Prod(x[0], x[0]), model.Prod(x[1], x[1]), model.Prod(x[2], x[2])), 1.0); // Q = 0.5 ( 33*x0*x0 + 22*x1*x1 + 11*x2*x2 - 12*x0*x1 - 23*x1*x2 ) INumExpr x00 = model.Prod(33.0, x[0], x[0]); INumExpr x11 = model.Prod(22.0, x[1], x[1]); INumExpr x22 = model.Prod(11.0, x[2], x[2]); INumExpr x01 = model.Prod(-12.0, x[0], x[1]); INumExpr x12 = model.Prod(-23.0, 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(x); }
// 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); }
internal static void PopulateByNonzero(IMPModeler model, INumVar[][] var, IRange[][] rng) { double[] lb = { 0.0, 0.0, 0.0 }; double[] ub = { 40.0, System.Double.MaxValue, System.Double.MaxValue }; INumVar[] x = model.NumVarArray(3, lb, ub); var[0] = x; double[] objvals = { 1.0, 2.0, 3.0 }; model.Add(model.Maximize(model.ScalProd(x, objvals))); rng[0] = new IRange[2]; rng[0][0] = model.AddRange(-System.Double.MaxValue, 20.0); rng[0][1] = model.AddRange(-System.Double.MaxValue, 30.0); rng[0][0].Expr = model.Sum(model.Prod(-1.0, x[0]), model.Prod(1.0, x[1]), model.Prod(1.0, x[2])); rng[0][1].Expr = model.Sum(model.Prod(1.0, x[0]), model.Prod(-3.0, x[1]), model.Prod(1.0, x[2])); x[0].Name = "x1"; x[1].Name = "x2"; x[2].Name = "x3"; rng[0][0].Name = "c1"; rng[0][0].Name = "c2"; }
// 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); }
// 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; }
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); }
// 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); }
internal static void PopulateByNonzero(IMPModeler model, INumVar[][] var, IRange[][] rng) { double[] lb = {0.0, 0.0, 0.0}; double[] ub = {40.0, System.Double.MaxValue, System.Double.MaxValue}; INumVar[] x = model.NumVarArray(3, lb, ub); var[0] = x; double[] objvals = {1.0, 2.0, 3.0}; model.Add(model.Maximize(model.ScalProd(x, objvals))); rng[0] = new IRange[2]; rng[0][0] = model.AddRange(-System.Double.MaxValue, 20.0); rng[0][1] = model.AddRange(-System.Double.MaxValue, 30.0); rng[0][0].Expr = model.Sum(model.Prod(-1.0, x[0]), model.Prod( 1.0, x[1]), model.Prod( 1.0, x[2])); rng[0][1].Expr = model.Sum(model.Prod( 1.0, x[0]), model.Prod(-3.0, x[1]), model.Prod( 1.0, x[2])); x[0].Name = "x1"; x[1].Name = "x2"; x[2].Name = "x3"; rng[0][0].Name = "c1"; rng[0][0].Name = "c2"; }
internal static INumVar[] populateByRow(IMPModeler model, IRange[] row) { double[] lb = {0.0, 0.0, 0.0}; double[] ub = {40.0, System.Double.MaxValue, System.Double.MaxValue}; INumVar[] x = model.NumVarArray(3, lb, ub); // - x0 + x1 + x2 <= 20 // x0 - 3*x1 + x2 <= 30 double[][] val = {new double[]{-1.0, 1.0, 1.0}, new double[]{ 1.0, -3.0, 1.0}}; row[0] = model.AddLe(model.ScalProd(val[0], x), 20.0); row[1] = model.AddLe(model.ScalProd(val[1], x), 30.0); // x0*x0 + x1*x1 + x2*x2 <= 1.0 row[2] = model.AddLe(model.Sum(model.Prod(x[0], x[0]), model.Prod(x[1], x[1]), model.Prod(x[2], x[2])), 1.0); // Q = 0.5 ( 33*x0*x0 + 22*x1*x1 + 11*x2*x2 - 12*x0*x1 - 23*x1*x2 ) INumExpr x00 = model.Prod( 33.0, x[0], x[0]); INumExpr x11 = model.Prod( 22.0, x[1], x[1]); INumExpr x22 = model.Prod( 11.0, x[2], x[2]); INumExpr x01 = model.Prod(-12.0, x[0], x[1]); INumExpr x12 = model.Prod(-23.0, 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 x; }