NLOPT 无效参数 Python

NLOPT Invalid argument Python

当我 运行 在 python 中使用以下简单的 NLOPT 示例时:

import numpy as np
import nlopt 

n = 2
localopt_feval_max = 10
lb = np.array([-1, -1])
ub = np.array([1, 1])


def myfunc(x, grad):
    return -1

opt = nlopt.opt(nlopt.LN_NELDERMEAD, n)

opt.set_lower_bounds(lb)
opt.set_upper_bounds(ub)
opt.set_maxeval(localopt_feval_max)

opt.set_min_objective(myfunc)
opt.set_xtol_rel(1e-8)
x0 = np.array([0,0])

x = opt.optimize(x0)

我收到一个错误:

"ValueError: nlopt invalid argument"

此处引用给出的唯一建议:

http://ab-initio.mit.edu/wiki/index.php/NLopt_Python_Reference

是下限可能比上限大,或者有一个未知的算法(这里都不是这种情况)。我正在 运行 使用以下版本的 Python、NLOPT 和 NumPy

>>> sys.version
'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'
>>> nlopt.__version__
'2.4.2'
>>> np.__version__
'1.8.2'

通过将函数声明更改为

def myfunc(x, grad):
    return -1.0

一切正常。所以 NLopt 不能处理 objectives that return python integer 而不是 float

我觉得 NLopt 应该能够将整数 objective 函数值转换为 float。如果不是这样,那么至少应该提出 TypeError 而不是 ValueError: nlopt invalid argument.

我得到这个错误是因为我的 objective 函数 return 类型是 numpy.float128 所以我通过将 return 类型更改为 numpy 来修复错误。 float64