gc_pwl_func_vb.vb


Gurobi优化版权2023年,LL狗万app足彩C的这个例子中考虑以下非凸非线性问题的“最大化2 x + y”受到exp (x) + 4倍根号(y) < = 9“x, y > = 0”“我们向你展示两种方法来解决这个问题:1)使用分段线性方法来处理通用函数的约束(如exp和sqrt)。)添加两个变量的u = exp (x)' v = sqrt (y)“b)计算点(x, u) u = exp (x)的步长(例如,x ' e - 3 = 0, 1, 2 e - 3,…xmax)和点(y v v =√(y)的一些步长(例如,y = 0、1 e - 3 2 e - 3,…ymax)。我们需要计算xmax和ymax(这是容易的对于这个示例,但这种“不持有)。' c)使用的点添加两个通用类型的约束的分段线性。“2)直接使用Gurobis内置的通用函数约束(EXP”和战俘)。在这里,我们不需要计算的点和最大的可能值,这将由Gurobi内部完成。在这个方法中,我们展示了如何最优解“放大”和“紧公差来提高解决方案的质量。进口系统进口Gurobi类gc_pwl_func_vb共享函数f (u双)作为双返回Math.Exp (u)结束函数共享函数g (u双)作为双返回Math.Sqrt (u)结束函数共享子printsol (GRBVar m GRBModel, x, y _ GRBVar, GRBVar u, v GRBVar)控制台。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 Dim vio As Double = f(x.X) + 4 * g(y.X) - 9 If vio < 0.0 Then vio = 0.0 End If Console.WriteLine("Vio = " & vio) End Sub Shared Sub Main() Try ' Create environment Dim env As New GRBEnv() ' Create a new m Dim m As New GRBModel(env) Dim lb As Double = 0.0 Dim ub As Double = GRB.INFINITY Dim x As GRBVar = m.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "x") Dim y As GRBVar = m.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "y") Dim u As GRBVar = m.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "u") Dim v As GRBVar = 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") ' PWL constraint approach Dim intv As Double = 1e-3 Dim xmax As Double = Math.Log(9.0) Dim npts As Integer = Math.Ceiling(xmax/intv) + 1 Dim xpts As Double() = new Double(npts -1) {} Dim upts As Double() = new Double(npts -1) {} For i As Integer = 0 To npts - 1 xpts(i) = i*intv upts(i) = f(i*intv) Next Dim gc1 As GRBGenConstr = m.AddGenConstrPWL(x, u, xpts, upts, "gc1") Dim ymax As Double = (9.0/4.0)*(9.0/4.0) npts = Math.Ceiling(ymax/intv) + 1 Dim ypts As Double() = new Double(npts -1) {} Dim vpts As Double() = new Double(npts -1) {} For i As Integer = 0 To npts - 1 ypts(i) = i*intv vpts(i) = g(i*intv) Next Dim gc2 As GRBGenConstr = m.AddGenConstrPWL(y, v, ypts, vpts, "gc2") ' Optimize the model and print solution m.Optimize() printsol(m, x, y, u, v) ' General function approach with auto PWL translation by Gurobi m.Reset() m.Remove(gc1) m.Remove(gc2) m.Update() Dim gcf1 As GRBGenConstr = m.AddGenConstrExp(x, u, "gcf1", "") Dim gcf2 As GRBGenConstr = 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) ' Use optimal solution to reduce the ranges and use smaller pclen to solve 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 e As GRBException Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message) End Try End Sub End Class