2013-10-24 93 views
2

以下是我的代碼。我得到了標題中提到的ValueError(並在最後添加),我無法想象爲什麼。我的函數是R^2 - > R,並且我遵循(http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html#constrained-minimization-of-multivariate-scalar-functions-minimize)中的步驟(格式,而不是實際值)。這就是爲什麼我不瞭解維度問題,一切都與那裏非常相似。scipy.optimize.minimize:ValueError:所有輸入數組必須具有相同的維數

我的代碼:

def func(x, theta, sign=1.0): 
    return sign*(math.log(x[0]) + theta*math.log(1-x[1])) 


def func_deriv (x, theta, sign=1.0): 
    dfdc = (1/x[0]) 
    dfdn = theta*1/(1-x[1])*(-1) 
    return sign*array([ dfdc, dfdn]) 



cons = (
    {'type':'eq', 
      'fun' : lambda x: array([ 
       exp(e)*k**alpha*x[1]**(1-alpha) - (kPrime - k*(1-delta)) 
        - phi/2*(kPrime/k - delta)**2 - x[0] ]), 
      'jac' : lambda x: array([ 
       -1, (1-alpha)*exp(e)*k**alpha*x[1]**(-alpha)    
      ]) 
      }, 
    {'type':'ineq', 
     'fun' : lambda x: array([x[0]]), 
     'jac' : lambda x: array([1, 0]) 
     }, 
    {'type':'ineq', 
     'fun' : lambda x: array([x[1]]), 
     'jac' : lambda x: array([0, 1]) 
     }, 
    {'type':'ineq', 
     'fun' : lambda x: array([1 - x[1]]), 
     'jac' : lambda x: array([0, -1]) 
     }); 


res = scipy.optimize.minimize(
    func, [3, 0.5], 
    args=(param.theta,-1,), 
    jac=func_deriv, constraints=cons, 
    method='SLSQP', options={'disp': True}) 

完全回溯:

%run "./solve_maxim.py" 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.1.0.1371.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc) 
    174    else: 
    175     filename = fname 
--> 176    exec compile(scripttext, filename, 'exec') in glob, loc 
    177  else: 
    178   def execfile(fname, *where): 

solve_maxim.py in <module>() 
    61  args=(param.theta,-1,), 
    62  jac=func_deriv, constraints=cons, 
---> 63  method='SLSQP', options={'disp': True}) 

AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\_minimize.pyc in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options) 
    362  elif meth == 'slsqp': 
    363   return _minimize_slsqp(fun, x0, args, jac, bounds, 
--> 364        constraints, **options) 
    365  else: 
    366   raise ValueError('Unknown solver %s' % method) 

\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\slsqp.pyc in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, **unknown_options) 
    366 
    367    # Now combine c_eq and c_ieq into a single matrix 
--> 368    c = concatenate((c_eq, c_ieq)) 
    369 
    370   if mode == 0 or mode == -1: # gradient evaluation required 

ValueError: all the input arrays must have same number of dimensions 
+0

不等式函數的導數不應該是長度爲2的數組嗎?例如。對於第一個不等式,衍生物是不是'array([1,0])'? –

+0

你可以填寫你的代碼,以便它可以運行嗎?包括所需的導入,並定義所有變量(例如'k')。 –

+0

你正在暗示着什麼讓我困擾:k [0]仍然具有更高的維度。 – FooBar

回答

2

的不平等您jac值不正確。它們應該是長度爲2的數組,包含相對於x[0]x[1]的衍生物。例如。

... 
    {'type':'ineq', 
     'fun' : lambda x: array([x[0]]), 
     'jac' : lambda x: array([1, 0]) 
     }, 
    {'type':'ineq', 
     'fun' : lambda x: array([x[1]]), 
     'jac' : lambda x: array([0, 1]) 
     }, 
    {'type':'ineq', 
     'fun' : lambda x: array([1 - x[1]]), 
     'jac' : lambda x: array([0, -1]) 
     }); 
相關問題