我正在尝试给我的最小化问题添加一些等式和不等式约束。我使用的是nlopt Python API。
特别是,我想添加一些vector-valued constraints。我的代码如下所示:
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发布于 2014-01-28 16:54:50
确保您的函数eqcontr具有与目标函数function相同的形式。也可以把它贴出来,这样就很容易理解了。而且,我看不到condition是在哪里定义的。
https://stackoverflow.com/questions/17791139
复制相似问题