当我在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无法处理返回pythoninteger
而不是python的目标float
我觉得NLopt应该可以将整数目标函数值转换为float
。如果不是这样,则至少TypeError
应引发a而不是a ValueError: nlopt invalid argument
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句