我正在尝试为我的最小化问题添加一些等式和不等式约束。我正在使用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 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