gc_pwl_func_cs.cs


/ * 2023年版权,Gurobi优狗万app足彩化,LLC这个例子考虑以下非凸非线性问题最大化2 x + y受制于exp (x) + 4倍根号(y) < = 9 x, y > = 0,我们给你两种方法解决:1)使用分段线性方法来处理一般函数约束(如exp和sqrt)。)添加两个变量u = v exp (x) = sqrt (y) b)计算点(x, u) u = exp (x)的步长(如x = 0、1 e - 3, 2 e - 3,…xmax)和点(y, v) v = sqrt (y)的步长(例如,y = 0、1 e - 3 2 e - 3,…ymax)。我们需要计算xmax和ymax(这是容易的对于这个示例,但这并不持有)。c)使用点添加两个通用类型分段线性的约束。2)使用Gurobis内置的通用函数约束直接(EXP和战俘)。在这里,我们不需要计算点和最大可能的值,这将被Gurobi内部完成。在这种方法中,我们展示了如何“放大”最优解和收紧公差,提高解决方案的质量。* /使用系统;使用Gurobi;类gc_pwl_func_cs{私有静态双f(双u){返回Math.Exp (u); } private static double g(double u) { return Math.Sqrt(u); } private static void printsol(GRBModel m, GRBVar x, GRBVar y, GRBVar u, GRBVar v) { Console.WriteLine("x = " + x.X + ", u = " + u.X); Console.WriteLine("y = " + y.X + ", v = " + v.X); Console.WriteLine("Obj = " + m.ObjVal); // Calculate violation of exp(x) + 4 sqrt(y) <= 9 double vio = f(x.X) + 4 * g(y.X) - 9; if (vio < 0.0) vio = 0.0; Console.WriteLine("Vio = " + vio); } 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); } } }