如何给differential_evolution添加几个约束?
How to add several constraints to differential_evolution?
我遇到了与 中相同的问题,但不想只添加一个而是多个约束来优化问题。
例如我想在 x1
和 x2
的总和小于 5
且 x2
小于 3
的约束下最大化 x1 + 5 * x2
(不用说,实际问题要复杂得多,不能像这个问题一样直接扔进 scipy.optimize.minimize
;它只是用来说明问题...)。
我可以像这样进行丑陋的破解:
from scipy.optimize import differential_evolution
import numpy as np
def simple_test(x, more_constraints):
# check wether all constraints evaluate to True
if all(map(eval, more_constraints)):
return -1 * (x[0] + 5 * x[1])
# if not all constraints evaluate to True, return a positive number
return 10
bounds = [(0., 5.), (0., 5.)]
additional_constraints = ['x[0] + x[1] <= 5.', 'x[1] <= 3']
result = differential_evolution(simple_test, bounds, args=(additional_constraints, ), tol=1e-6)
print(result.x, result.fun, sum(result.x))
这将打印
[ 1.99999986 3. ] -16.9999998396 4.99999985882
正如人们所期望的那样。
有没有比使用 'dangerous' eval
更好/更直接的方法来添加多个约束?
一个例子是这样的::
additional_constraints = [lambda(x): x[0] + x[1] <= 5., lambda(x):x[1] <= 3]
def simple_test(x, more_constraints):
# check wether all constraints evaluate to True
if all(constraint(x) for constraint in more_constraints):
return -1 * (x[0] + 5 * x[1])
# if not all constraints evaluate to True, return a positive number
return 10
问题中描述的问题有一个正确的解决方案,可以用 scipy.optimize.differential_evolution 强制执行多个非线性约束。
正确的方法是使用 scipy.optimize.NonlinearConstraint 函数。
下面我给出了一个 non-trivial 优化经典 Rosenbrock function 的例子,在一个由两个圆的交集定义的区域内。
import numpy as np
from scipy import optimize
# Rosenbrock function
def fun(x):
return 100*(x[1] - x[0]**2)**2 + (1 - x[0])**2
# Function defining the nonlinear constraints:
# 1) x^2 + (y - 3)^2 < 4
# 2) (x - 1)^2 + (y + 1)^2 < 13
def constr_fun(x):
r1 = x[0]**2 + (x[1] - 3)**2
r2 = (x[0] - 1)**2 + (x[1] + 1)**2
return r1, r2
# No lower limit on constr_fun
lb = [-np.inf, -np.inf]
# Upper limit on constr_fun
ub = [4, 13]
# Bounds are irrelevant for this problem, but are needed
# for differential_evolution to compute the starting points
bounds = [[-2.2, 1.5], [-0.5, 2.2]]
nlc = optimize.NonlinearConstraint(constr_fun, lb, ub)
sol = optimize.differential_evolution(fun, bounds, constraints=nlc)
# Accurate solution by Mathematica
true = [1.174907377273171, 1.381484428610871]
print(f"nfev = {sol.nfev}")
print(f"x = {sol.x}")
print(f"err = {sol.x - true}\n")
这将使用默认参数打印以下内容:
nfev = 636
x = [1.17490808 1.38148613]
err = [7.06260962e-07 1.70116282e-06]
这是函数(等高线)和由非线性约束定义的可行区域(绿线内的阴影)的可视化。受约束的 global 最小值由黄点表示,而洋红色表示不受约束的 global 最小值。
这个约束问题在可行区域的边界 (x, y) ~ (-1.2, 1.4)
处有一个明显的 local 最小值,这将使局部优化器无法收敛到全局最小值起始位置。但是,differential_evolution
始终按预期找到 全局 最小值。
我遇到了与
例如我想在 x1
和 x2
的总和小于 5
且 x2
小于 3
的约束下最大化 x1 + 5 * x2
(不用说,实际问题要复杂得多,不能像这个问题一样直接扔进 scipy.optimize.minimize
;它只是用来说明问题...)。
我可以像这样进行丑陋的破解:
from scipy.optimize import differential_evolution
import numpy as np
def simple_test(x, more_constraints):
# check wether all constraints evaluate to True
if all(map(eval, more_constraints)):
return -1 * (x[0] + 5 * x[1])
# if not all constraints evaluate to True, return a positive number
return 10
bounds = [(0., 5.), (0., 5.)]
additional_constraints = ['x[0] + x[1] <= 5.', 'x[1] <= 3']
result = differential_evolution(simple_test, bounds, args=(additional_constraints, ), tol=1e-6)
print(result.x, result.fun, sum(result.x))
这将打印
[ 1.99999986 3. ] -16.9999998396 4.99999985882
正如人们所期望的那样。
有没有比使用 'dangerous' eval
更好/更直接的方法来添加多个约束?
一个例子是这样的::
additional_constraints = [lambda(x): x[0] + x[1] <= 5., lambda(x):x[1] <= 3]
def simple_test(x, more_constraints):
# check wether all constraints evaluate to True
if all(constraint(x) for constraint in more_constraints):
return -1 * (x[0] + 5 * x[1])
# if not all constraints evaluate to True, return a positive number
return 10
问题中描述的问题有一个正确的解决方案,可以用 scipy.optimize.differential_evolution 强制执行多个非线性约束。
正确的方法是使用 scipy.optimize.NonlinearConstraint 函数。
下面我给出了一个 non-trivial 优化经典 Rosenbrock function 的例子,在一个由两个圆的交集定义的区域内。
import numpy as np
from scipy import optimize
# Rosenbrock function
def fun(x):
return 100*(x[1] - x[0]**2)**2 + (1 - x[0])**2
# Function defining the nonlinear constraints:
# 1) x^2 + (y - 3)^2 < 4
# 2) (x - 1)^2 + (y + 1)^2 < 13
def constr_fun(x):
r1 = x[0]**2 + (x[1] - 3)**2
r2 = (x[0] - 1)**2 + (x[1] + 1)**2
return r1, r2
# No lower limit on constr_fun
lb = [-np.inf, -np.inf]
# Upper limit on constr_fun
ub = [4, 13]
# Bounds are irrelevant for this problem, but are needed
# for differential_evolution to compute the starting points
bounds = [[-2.2, 1.5], [-0.5, 2.2]]
nlc = optimize.NonlinearConstraint(constr_fun, lb, ub)
sol = optimize.differential_evolution(fun, bounds, constraints=nlc)
# Accurate solution by Mathematica
true = [1.174907377273171, 1.381484428610871]
print(f"nfev = {sol.nfev}")
print(f"x = {sol.x}")
print(f"err = {sol.x - true}\n")
这将使用默认参数打印以下内容:
nfev = 636
x = [1.17490808 1.38148613]
err = [7.06260962e-07 1.70116282e-06]
这是函数(等高线)和由非线性约束定义的可行区域(绿线内的阴影)的可视化。受约束的 global 最小值由黄点表示,而洋红色表示不受约束的 global 最小值。
这个约束问题在可行区域的边界 (x, y) ~ (-1.2, 1.4)
处有一个明显的 local 最小值,这将使局部优化器无法收敛到全局最小值起始位置。但是,differential_evolution
始终按预期找到 全局 最小值。