[英]Nonlinear Optimization of absolute value function using ipopt solver
python - 当目标函数是模(绝对)值函数时,如何在python中使用IPOPT Solver解决非线性优化问题?
Absolute abs(x)
has a point at x=0
that is not continuously differentiable.绝对abs(x)
在x=0
处具有不可连续微分的点。 There are two popular ways to reformulate the abs()
function for continuous derivatives that are discussed in the optimization course .有两种流行的方法可以重新构造优化课程中讨论的连续导数的abs()
函数。
Approach 1: MPCC方法一:MPCC
Use Mathematical Program with Complementarity Constraints (MPCC) as a first approach.使用具有互补约束的数学程序 (MPCC) 作为第一种方法。 This is implemented in Python Gekko with the m.abs2()
function.这是在 Python Gekko 中使用m.abs2()
函数实现的。
from gekko import GEKKO
# define new GEKKO model
m = GEKKO()
# calculate x to minimize objective
x = m.Var(1)
# use abs2 to define y
y = m.abs2(x)
# define objective to minimize
m.Minimize((y+3)**2)
# solve
m.solve()
# print solution
print('x: ' + str(x.value))
print('y: ' + str(y.value))
This is solved with IPOPT: IPOPT 解决了这个问题:
Number of Iterations....: 8
(scaled) (unscaled)
Objective...............: 9.0000075849548888e-01 9.0000075849548882e+00
Dual infeasibility......: 3.3306690738754696e-16 3.3306690738754696e-15
Constraint violation....: 2.8561682212339413e-21 2.8561682212339413e-21
Complementarity.........: 3.8526843291314332e-07 3.8526843291314330e-06
Overall NLP error.......: 3.8526843291314332e-07 3.8526843291314330e-06
Number of objective function evaluations = 9
Number of objective gradient evaluations = 9
Number of equality constraint evaluations = 9
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 9
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 8
Total CPU secs in IPOPT (w/o function evaluations) = 0.005
Total CPU secs in NLP function evaluations = 0.000
EXIT: Optimal Solution Found.
The solution was found.
The final value of the objective function is 9.00000758495489
---------------------------------------------------
Solver : IPOPT (v3.12)
Solution time : 1.110000000335276E-002 sec
Objective : 9.00000758495489
Successful solution
---------------------------------------------------
x: [-2.0813825544e-17]
y: [1.264092301e-06]
The advantage of the MPCC form is that it avoids binary variables that can be challenging to solve with MILP or MINLP solvers. MPCC 形式的优点是它避免了使用 MILP 或 MINLP 求解器可能难以求解的二进制变量。 The disadvantage is that solutions at the switch point have poor numerical properties and can get stuck at the saddle point.缺点是切换点处的解具有较差的数值特性,并且可能会卡在鞍点处。
Approach 2: Mixed Integer Optimization方法 2:混合整数优化
Binary Variables are alternative method to create continuous gradients.二元变量是创建连续梯度的替代方法。 Here is a simple example in Python Gekko:这是 Python Gekko 中的一个简单示例:
from gekko import GEKKO
# define new GEKKO model
m = GEKKO()
# calculate x to minimize objective
x = m.Var(1.0)
# define new binary variable
intb = m.Var(0,lb=0,ub=1,integer=True)
# define y
y = m.Var()
# define equations
m.Equation((1-intb)*x <= 0)
m.Equation(intb * (-x) <= 0)
# output
m.Equation(y==(1-intb)*(-x) + intb*x)
# solve with APOPT (MINLP solver)
m.options.SOLVER=1
m.solve()
# print solution
print('x: ' + str(x.value))
print('intb: ' + str(intb.value))
print('y: ' + str(y.value))
or simplify with the use of the gekko m.abs3()
function to implement these equations.或使用 gekko m.abs3()
函数来简化这些方程。
# with abs3 object
from gekko import GEKKO
# define new GEKKO model
m = GEKKO()
# variable
x = m.Var(-0.5)
# calculate y=abs(x) with abs3
y = m.abs3(x)
# solve with APOPT (MINLP solver)
m.solve()
# print solution
print('x: ' + str(x.value))
print('y: ' + str(y.value))
The disadvantage of this approach is that standard NLP solvers such as IPOPT are not designed to handle binary variables.这种方法的缺点是标准 NLP 求解器(例如 IPOPT)并非旨在处理二进制变量。 This approach uses the APOPT solver that is also in Gekko.这种方法使用了 Gekko 中的 APPT 求解器。
APMonitor, Version 1.0.1
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 5
Intermediates: 0
Connections : 0
Equations : 3
Residuals : 3
Number of state variables: 5
Number of total equations: - 3
Number of slack variables: - 2
---------------------------------------
Degrees of freedom : 0
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 2 Dpth: 0 Lvs: 0 Obj: 0.00E+00 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 5.720000001019798E-002 sec
Objective : 0.000000000000000E+000
Successful solution
---------------------------------------------------
x: [-0.16666666667]
y: [0.16666666667]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.