jml*_*son 4 python numpy nlopt
当我在 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)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
"ValueError: nlopt invalid argument"
Run Code Online (Sandbox Code Playgroud)
这里参考给出的唯一建议:
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'
Run Code Online (Sandbox Code Playgroud)
通过将函数声明更改为
def myfunc(x, grad):
return -1.0
Run Code Online (Sandbox Code Playgroud)
一切正常。因此 NLopt 无法处理返回 pythoninteger
而不是的目标float
我觉得 NLopt 应该能够将整数目标函数值转换为float
. 如果不是这样,那么至少TypeError
应该提出 a 而不是 a ValueError: nlopt invalid argument
。