【问题标题】:Does scipy's minimize function with method "COBYLA" accept bounds?scipy 使用“COBYLA”方法的最小化函数是否接受界限?
【发布时间】:2017-01-20 18:45:03
【问题描述】:

我在 scipy 的 optimize.minimize 函数中使用算法 'COBYLA'(为 cygwin 构建的 v.0.11)。我观察到在这种情况下似乎没有使用参数bounds。比如简单的例子:

from scipy.optimize import minimize

def f(x):
    return -sum(x)

minimize(f, x0=1, method='COBYLA', bounds=(-2,2))

返回:

status: 2.0
nfev: 1000
maxcv: 0.0
success: False
fun: -1000.0
x: array(1000.0)
message: 'Maximum number of function evaluations has been exceeded.'

而不是 x 的预期 2

有没有人察觉到同样的问题?是否存在已知的错误或文档错误?在 scipy 0.11 文档中,COBYLA 算法不排除此选项。事实上函数fmin_cobyla 没有bounds 参数。 感谢您的任何提示。

【问题讨论】:

  • 听起来应该可以,但也许你必须使用bounds=[(-2,2)]。不过,我没有可以尝试使用最小化的新 scipy。

标签: python scipy


【解决方案1】:

您可以以约束的形式制定边界

import scipy
#function to minimize
def f(x):
    return -sum(x)
#initial values
initial_point=[1.,1.,1.]    
#lower and upper bound for variables
bounds=[ [-2,2],[-1,1],[-3,3]   ]

#construct the bounds in the form of constraints
cons = []
for factor in range(len(bounds)):
    lower, upper = bounds[factor]
    l = {'type': 'ineq',
         'fun': lambda x, lb=lower, i=factor: x[i] - lb}
    u = {'type': 'ineq',
         'fun': lambda x, ub=upper, i=factor: ub - x[i]}
    cons.append(l)
    cons.append(u)

#similarly aditional constrains can be added

#run optimization
res = scipy.optimize.minimize(f,initial_point,constraints=cons,method='COBYLA')
#print result
print res

请注意,最小化函数会将设计变量赋予该函数。在这种情况下,3 个输入变量给出了 3 个上限和下限。结果产生:

   fun: -6.0
   maxcv: -0.0
 message: 'Optimization terminated successfully.'
    nfev: 21
  status: 1
 success: True
       x: array([ 2.,  1.,  3.])

【讨论】:

    【解决方案2】:

    原始的COBYLA(2) FORTRAN 算法不明确支持变量边界,您必须在一般约束的上下文中制定边界。

    查看 SciPy minimize 接口 here 的当前源代码,很明显 SciPy 中尚未采取任何措施来处理此限制.

    因此,为了在 SciPy minimize 函数中为 cobyla 算法应用 bounds,您需要制定变量边界作为不等式约束并将它们包含在关联的 constraints 参数中。

    (源代码摘录)

    // bounds set to anything else than None yields warning
    if meth is 'cobyla' and bounds is not None:
        warn('Method %s cannot handle bounds.' % method,
             RuntimeWarning)
    ...
    // No bounds argument in the internal call to the COBYLA function
    elif meth == 'cobyla':
        return _minimize_cobyla(fun, x0, args, constraints, **options)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2015-02-18
      • 1970-01-01
      • 2021-10-25
      • 2020-03-22
      • 2021-06-24
      相关资源
      最近更新 更多