2013-07-22 22 views
0

我正在嘗試爲最小化問題添加一些等式和不等式約束。 我正在使用nlopt Python API。NLOpt中的向量值約束

特別是我想補充一些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維基的說明。 現在,如果我運行此我得到:

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 

回答

0

確保你的函數eqcontr具有相同的形式作爲目標函數function。也許發佈它,所以它會很容易理解。另外,我看不到condition的定義。