0

最小化問題に等式と不等式の制約を追加しようとしています。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
4

1 に答える 1

0

eqcontr関数が目的関数と同じ形式であることを確認してくださいfunction。分かりやすいように掲載することもあります。conditionまた、どこに定義されているかわかりません。

于 2014-01-28T08:54:50.410 に答える