当我在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"
这里提到的唯一建议是:
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'
发布于 2015-02-17 16:26:50
通过将函数声明更改为
def myfunc(x, grad):
return -1.0
一切都正常。因此,NLopt不能处理返回python integer
而不是float
的目标。
我觉得NLopt应该能够将整数目标函数值转换为float
。如果不是这样,那么至少应该引发一个TypeError
而不是一个ValueError: nlopt invalid argument
。
发布于 2018-01-23 15:18:36
由于我的目标函数返回类型是numpy.float128,所以我通过将返回类型更改为numpy.float64来修正这个错误。
https://stackoverflow.com/questions/28564976
复制相似问题