Scipy:最小化违反给定界限

Noi*_*lef 7 python scipy

我正在尝试使用scipy.optimize.minimize简单的a <= x <= b边界。但是,经常发生我的目标函数在边界之外进行评估。据我了解,当minimize试图确定边界处目标函数的梯度时会发生这种情况。


最小的例子:

import math
import numpy as np
from scipy.optimize import Bounds, minimize

constraint = Bounds([-1, -1], [1, 1], True)
def fun(x):
    print(x)
    return -math.exp(-np.dot(x,x))
result = minimize(fun, [-1, -1], bounds=constraint)
Run Code Online (Sandbox Code Playgroud)

输出显示最小化器跳转到该点[1,1],然后尝试在 处求值[1.00000001, 1]

[-1. -1.]
[-0.99999999 -1.        ]
[-1.         -0.99999999]
[-0.72932943 -0.72932943]
[-0.72932942 -0.72932943]
[-0.72932943 -0.72932942]
[-0.22590689 -0.22590689]
[-0.22590688 -0.22590689]
[-0.22590689 -0.22590688]
[1. 1.]
[1.00000001 1.        ]
[1.         1.00000001]
[-0.03437328 -0.03437328]
...
Run Code Online (Sandbox Code Playgroud)

当然,在这个例子中没有问题,因为fun也可以在那里评估。但情况可能并非总是如此......


在我的实际问题中,最小值不能在边界上,我有一个简单的解决方法,即向边界添加一个 epsilon。但是人们会期望应该有一个简单的解决方案来解决这个问题,如果最小值可以在边界上,它也可以工作?

PS:如果我是第一个遇到这个问题的人会很奇怪 - 如果这个问题之前在某个地方被问过,很抱歉,但我没有在任何地方找到它。

Noi*_*lef 5

正如这里所讨论的(感谢@“欢迎使用堆栈溢出”的评论,指导我到那里),问题确实是梯度例程不尊重边界。我写了一个新的来完成这项工作:

import math
import numpy as np
from scipy.optimize import minimize

def gradient_respecting_bounds(bounds, fun, eps=1e-8):
    """bounds: list of tuples (lower, upper)"""
    def gradient(x):
        fx = fun(x)
        grad = np.zeros(len(x))
        for k in range(len(x)):
            d = np.zeros(len(x))
            d[k] = eps if x[k] + eps <= bounds[k][1] else -eps
            grad[k] = (fun(x + d) - fx) / d[k]
        return grad
    return gradient

bounds = ((-1, 1), (-1, 1))
def fun(x):
    print(x)
    return -math.exp(-np.dot(x,x))
result = minimize(fun, [-1, -1], bounds=bounds,
                  jac=gradient_respecting_bounds(bounds, fun))
Run Code Online (Sandbox Code Playgroud)

请注意,这可能会降低效率,因为fun(x)现在每个点都会被评估两次。这似乎是_minimize_lbfgsblbfgsb.py 中不可避免的相关片段:

if jac is None:
    def func_and_grad(x):
        f = fun(x, *args)
        g = _approx_fprime_helper(x, fun, epsilon, args=args, f0=f)
        return f, g
else:
    def func_and_grad(x):
        f = fun(x, *args)
        g = jac(x, *args)
        return f, g
Run Code Online (Sandbox Code Playgroud)

可以看到, 的值f只能被内部_approx_fprime_helper函数重用。