Python NLOpt中的向量值约束
我试图在最小化问题中加入一些等式和不等式约束。 我正在使用Python API 我特别想补充一些。 我的代码如下示例所示:Python NLOpt中的向量值约束,python,nlopt,Python,Nlopt,我试图在最小化问题中加入一些等式和不等式约束。 我正在使用Python API 我特别想补充一些。 我的代码如下示例所示: def eqconstr(result,x,grad): if grad.size > 0: print "gradient to be implemented" for i in range(len(x)): if condition: result[i] = 0. initvect = # some
def eqconstr(result,x,grad):
if grad.size > 0:
print "gradient to be implemented"
for i in range(len(x)):
if condition: result[i] = 0.
initvect = # some initial guess of the parameters
opt = nlopt.opt(nlopt.LN_PRAXIS,len(initvect))
opt.set_min_objective(function)
tol = np.asarray([0.1]*len(initvect))
opt.add_equality_mconstraint(eqconstr, tol) # this is the call of the constraints (!!!)
opt.set_lower_bounds(-1.) # other parameters to set. not important for this question
opt.set_upper_bounds(1.)
opt.set_initial_step([0.1]*len(initvect))
opt.set_xtol_abs(10e-6)
opt.set_ftol_abs(10e-6)
res = opt.optimize(initvect)
这完全遵循nlopt wiki中的说明。
现在,如果我运行此命令,我会得到:
Traceback (most recent call last):
File "main.py", line 158, in <module>
opt.add_equality_mconstraint(eqconstr, tol)
File "/usr/local/lib/python2.7/dist-packages/nlopt.py", line 269, in add_equality_mconstraint
def add_equality_mconstraint(self, *args): return _nlopt.opt_add_equality_mconstraint(self, *args)
ValueError: nlopt invalid argument
回溯(最近一次呼叫最后一次):
文件“main.py”,第158行,在
选择添加平等约束(eqconstr,tol)
文件“/usr/local/lib/python2.7/dist packages/nlopt.py”,第269行,在add\u equality\u mconstraint中
def add_equality_mconstraint(self,*args):返回_nlopt.opt_add_equality_mconstraint(self,*args)
ValueError:nlopt参数无效
确保您的功能eqcontrol
与您的目标功能功能具有相同的形式。也许也可以贴出来,这样就容易理解了。此外,我看不到在哪里定义了条件