gc_pwl_func_vb.vb


gc_pwl_func_vb.vb


Gurobi优化版权2021年,LL狗万app足彩C的这个例子中考虑以下非凸非线性问题的“最大化2 x + y”受到exp (x) + 4倍根号(y) < = 9“x, y > = 0”“我们向你展示两种方法来解决这个问题:1)使用分段线性方法来处理通用函数的约束(如exp和sqrt)。' a)添加两个变量'' v =√(y)b)计算某步长(例如,x ' = 0,1e - 3,2e -3,…)下u = exp(x)的点(x, u)。(例如,y = 0,1e - 3,2e -3,…)和点(y, v) (v =√(y))ymax)。我们需要“计算xmax和ymax(这在这个例子中很简单,但在一般情况下并不适用)”。c)使用这些点添加两个类型为“分段线性”的一般约束。2)直接使用Gurobis内置的通用函数约束(EXP和POW)。在这里,我们不需要计算点和最大的可能值,这将由Gurobi内部完成。在这种方法中,我们展示了如何“放大”最优的解决方案,并“收紧公差以提高解决方案的质量”。系统导入Gurobi类gc_pwl_func_vb共享函数f(u As Double) As Double Return Math.Exp(u) End Function Shared Function g(u As Double) As Double Return Math.Sqrt(u) End Function Shared Sub printsol(m As GRBModel, x As GRBVar, _ y As GRBVar, u As GRBVar, v As GRBVar)控制台。控制台:"x = " & x.X & ", u = " & u.X "。/ /将y = " & y.X & ", v = " & v.X "写入控制台。WriteLine("Obj = " &。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