Python scipy.optimize.minimize:ValueError:所有输入数组的维数必须相同

Python scipy.optimize.minimize:ValueError:所有输入数组的维数必须相同,python,optimization,numpy,scipy,minimize,Python,Optimization,Numpy,Scipy,Minimize,下面是我的代码。我得到了标题中提到的ValueError(并附在最后),我无法想象为什么。我的函数是R^2->R,我严格遵循()中的步骤(格式,而不是实际值)。这就是为什么我不理解维度的问题,所有的东西都非常类似 我的代码: 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 =

下面是我的代码。我得到了标题中提到的ValueError(并附在最后),我无法想象为什么。我的函数是R^2->R,我严格遵循()中的步骤(格式,而不是实际值)。这就是为什么我不理解维度的问题,所有的东西都非常类似

我的代码:

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])
            },
        {'type':'ineq',
            'fun' : lambda x: array([x[1]]),
            'jac' : lambda x: array([1])
            },
        {'type':'ineq',
            'fun' : lambda x: array([1 - x[1]]),
            'jac' : lambda x: array([-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
%run.“/solve\u maxim.py”
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
C:\Program Files\Enthught\Canopy\App\appdata\Canopy-1.1.0.1371.win-x86\U 64\lib\site packages\IPython\utils\py3compat.pyc在execfile(fname,glob,loc)中
174.其他:
175 filename=fname
-->176 exec编译(脚本文本,文件名,'exec'),在glob,loc中
177其他:
178 def execfile(fname,*其中):
在()中求解_max.py
61参数=(参数θ,-1,),
62 jac=func_deriv,约束=cons,
--->63 method='SLSQP',options={'disp':True})
最小化中的AppData\Local\enthught\Canopy\User\lib\site packages\scipy\optimize\\u minimize.pyc(fun、x0、args、method、jac、hess、hessp、bounds、constraints、tol、callback、options)
362 elif meth==“slsqp”:
363返回_最小化_slsqp(fun、x0、args、jac、bounds、,
-->364限制条件,**选项)
365其他:
366 raise VALUERROR('未知的解算器%s'%1!''方法)
\AppData\Local\Enthught\Canopy\User\lib\site packages\scipy\optimize\slsqp中的slsqp.pyc最小化(func、x0、args、jac、bounds、constraints、maxiter、ftol、iprint、disp、eps、**未知选项)
366
367#现在将c#u eq和c#ieq组合成一个矩阵
-->368 c=串联((c_eq,c_ieq))
369
370如果模式==0或模式==-1:#需要梯度评估
ValueError:所有输入数组的维数必须相同

您的
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])
        });

不等式函数的导数不应该是长度为2的数组吗?例如,对于第一个不等式,导数不是
数组([1,0])
?您能填写您的代码使其可运行吗?包括所需的导入,并定义所有变量(例如,
k
)。您正暗示着困扰我的问题:k[0]仍然具有更高的维度。