NLOPT无效参数Python

2024-09-30 10:28:52 发布

您现在位置: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)

我得到一个错误:

^{pr2}$

参考文献给出的唯一建议是:

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'

Tags: importversionnpmyfuncarraymaxoptset
2条回答

我得到这个错误是因为我的目标函数返回类型是numpy.float128所以我通过将返回类型改为数字浮点数64

通过将函数声明更改为

def myfunc(x, grad):
    return -1.0

一切正常。因此NLopt不能处理返回pythoninteger而不是{}的目标

我觉得NLopt应该能够将整数目标函数值转换为float。如果不是这样,那么至少应该引发一个TypeError,而不是ValueError: nlopt invalid argument。在

相关问题 更多 >

    热门问题