python - scipy.optimize.minimize : ValueError: all the input arrays must have same number of dimensions

标签 python optimization numpy scipy minimize

以下是我的代码。我得到标题中提到的 ValueError(并附在最后),我无法想象为什么。我的函数是 R^2 -> R,我密切关注(格式,而不是实际值)( http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html#constrained-minimization-of-multivariate-scalar-functions-minimize ) 中的步骤。这就是为什么我不明白维度问题,一切都和那里很相似。

我的代码:

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

最佳答案

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

关于python - scipy.optimize.minimize : ValueError: all the input arrays must have same number of dimensions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19569499/

相关文章:

python - 名称错误 : global name 'CV_GUI_NORMAL' is not defined

java - Java 是否对单线程应用程序强制执行 as-if-serial

ios - Xcode 8 Swift 3 应用程序能否在 iOS 7 上成功运行?

python - Django 导入 LDAP 库

python - Scipy,差异进化

c++ - 如果通过引用传递变量,则会出现冗余 mov 操作

python - 比较 numpy 数组中的多列

python - Numpy:根据满足条件的组拆分数组

python - 对数组进行切片以排除单个元素

python - 如何只打印按字母顺序嵌套在列表、字典中的内部字典的键?