manbet体育手机客户端


GCPWLFunc.java


/ * 2019年版权,Gurobi优狗万app足彩化,LLC这个例子考虑以下非凸非线性问题最大化2 x + y受制于exp (x) + 4倍根号(y) < = 9 x, y > = 0,我们给你两种方法解决:1)使用分段线性方法来处理一般函数约束(如exp和sqrt)。a)添加两个变量u = exp(x) v = sqrt(y) b)计算u = exp(x)在某个步长(例如x = 0,1e - 3,2e -3,…, xmax)和v = sqrt(y)的点(y, v),例如,y = 0,1e - 3,2e -3,…ymax)。我们需要计算xmax和ymax(这在本例中很简单,但这在一般情况下并不适用)。c)利用点添加两个分段线性型一般约束。2)直接使用Gurobis内置的一般函数约束(EXP和POW)。在这里,我们不需要计算点和可能的最大值,这将由Gurobi内部完成。在这种方法中,我们展示了如何“放大”最优解决方案,并收紧公差以提高解决方案的质量。* /进口gurobi。*;public class GCPWLFunc {private static double f(double u) {return 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) throws GRBException { assert(m.get(GRB.IntAttr.Status) == GRB.OPTIMAL); System.out.println("x = " + x.get(GRB.DoubleAttr.X) + ", u = " + u.get(GRB.DoubleAttr.X)); System.out.println("y = " + y.get(GRB.DoubleAttr.X) + ", v = " + v.get(GRB.DoubleAttr.X)); System.out.println("Obj = " + m.get(GRB.DoubleAttr.ObjVal)); // Calculate violation of exp(x) + 4 sqrt(y) <= 9 double vio = f(x.get(GRB.DoubleAttr.X)) + 4 * g(y.get(GRB.DoubleAttr.X)) - 9; if (vio < 0.0) vio = 0.0; System.out.println("Vio = " + vio); } public static void main(String[] args) { 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 GRBLinExpr obj = new GRBLinExpr(); obj.addTerm(2.0, x); obj.addTerm(1.0, y); m.setObjective(obj, GRB.MAXIMIZE); // Add linear constraint GRBLinExpr expr = new GRBLinExpr(); expr.addTerm(1.0, u); expr.addTerm(4.0, v); m.addConstr(expr, GRB.LESS_EQUAL, 9.0, "l1"); // Approach 1) PWL constraint approach double intv = 1e-3; double xmax = Math.log(9.0); int len = (int) Math.ceil(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.ceil(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", null); GRBGenConstr gcf2 = m.addGenConstrPow(y, v, 0.5, "gcf2", ""); m.set(GRB.DoubleParam.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 double xval = x.get(GRB.DoubleAttr.X); double yval = y.get(GRB.DoubleAttr.X); x.set(GRB.DoubleAttr.LB, Math.max(x.get(GRB.DoubleAttr.LB), xval-0.01)); x.set(GRB.DoubleAttr.UB, Math.min(x.get(GRB.DoubleAttr.UB), xval+0.01)); y.set(GRB.DoubleAttr.LB, Math.max(y.get(GRB.DoubleAttr.LB), yval-0.01)); y.set(GRB.DoubleAttr.UB, Math.min(y.get(GRB.DoubleAttr.UB), yval+0.01)); m.update(); m.reset(); m.set(GRB.DoubleParam.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) { System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage()); } } }