static void Main() { try { // Create environment GRBEnv env = new GRBEnv(); // Create a new m GRBModel m = new GRBModel(env); double lb = 0.0, ub = GRB.INFINITY; GRBVar x = m.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = m.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "y"); GRBVar u = m.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "u"); GRBVar v = m.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "v"); // Set objective m.SetObjective(2 * x + y, GRB.MAXIMIZE); // Add linear constraint m.AddConstr(u + 4 * v <= 9, "l1"); // Approach 1) PWL constraint approach double intv = 1e-3; double xmax = Math.Log(9.0); int len = (int)Math.Ceiling(xmax / intv) + 1; double[] xpts = new double[len]; double[] upts = new double[len]; for (int i = 0; i < len; i++) { xpts[i] = i * intv; upts[i] = f(i * intv); } GRBGenConstr gc1 = m.AddGenConstrPWL(x, u, xpts, upts, "gc1"); double ymax = (9.0 / 4.0) * (9.0 / 4.0); len = (int)Math.Ceiling(ymax / intv) + 1; double[] ypts = new double[len]; double[] vpts = new double[len]; for (int i = 0; i < len; i++) { ypts[i] = i * intv; vpts[i] = g(i * intv); } GRBGenConstr gc2 = m.AddGenConstrPWL(y, v, ypts, vpts, "gc2"); // Optimize the model and print solution m.Optimize(); printsol(m, x, y, u, v); // Approach 2) General function constraint approach with auto PWL // translation by Gurobi // restore unsolved state and get rid of PWL constraints m.Reset(); m.Remove(gc1); m.Remove(gc2); m.Update(); GRBGenConstr gcf1 = m.AddGenConstrExp(x, u, "gcf1", ""); GRBGenConstr gcf2 = m.AddGenConstrPow(y, v, 0.5, "gcf2", ""); m.Parameters.FuncPieceLength = 1e-3; // Optimize the model and print solution m.Optimize(); printsol(m, x, y, u, v); // Zoom in, use optimal solution to reduce the ranges and use a smaller // pclen=1e-5 to solve it x.LB = Math.Max(x.LB, x.X - 0.01); x.UB = Math.Min(x.UB, x.X + 0.01); y.LB = Math.Max(y.LB, y.X - 0.01); y.UB = Math.Min(y.UB, y.X + 0.01); m.Update(); m.Reset(); m.Parameters.FuncPieceLength = 1e-5; // Optimize the model and print solution m.Optimize(); printsol(m, x, y, u, v); // Dispose of model and environment m.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } }
public static void Main() { try { int n = 5; int m = 5; double[] c = new double[] { 0.5, 0.8, 0.5, 0.1, -1 }; double[,] A = new double[, ] { { 0, 0, 0, 1, -1 }, { 0, 0, 1, 1, -1 }, { 1, 1, 0, 0, -1 }, { 1, 0, 1, 0, -1 }, { 1, 0, 0, 1, -1 } }; double[] xpts = new double[] { -1, 0, 0, 0, 1 }; double[] ypts = new double[] { 2, 1, 0, 1, 2 }; // Env and model GRBEnv env = new GRBEnv(); GRBModel model = new GRBModel(env); model.ModelName = "gc_pwl_cs"; // Add variables, set bounds and obj coefficients GRBVar[] x = model.AddVars(n, GRB.CONTINUOUS); for (int i = 0; i < n; i++) { x[i].LB = -GRB.INFINITY; x[i].Obj = c[i]; } GRBVar[] y = model.AddVars(n, GRB.CONTINUOUS); // Set objective to maximize model.ModelSense = GRB.MAXIMIZE; // Add linear constraints for (int i = 0; i < m; i++) { GRBLinExpr le = 0.0; for (int j = 0; j < n; j++) { le.AddTerm(A[i, j], x[j]); } model.AddConstr(le, GRB.LESS_EQUAL, 0, "cx" + i); } GRBLinExpr le1 = 0.0; for (int j = 0; j < n; j++) { le1.AddTerm(1.0, y[j]); } model.AddConstr(le1, GRB.LESS_EQUAL, 3, "cy"); // Add piecewise constraints for (int j = 0; j < n; j++) { model.AddGenConstrPWL(x[j], y[j], xpts, ypts, "pwl" + j); } // Optimize model model.Optimize(); for (int j = 0; j < n; j++) { Console.WriteLine("x[" + j + "] = " + x[j].X); } Console.WriteLine("Obj: " + model.ObjVal); // Dispose of model and environment model.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } }