使用jacobian尝试使用scipy最小化功能时出现错误消息 [英] error message when trying to minimize a function with scipy using jacobian

查看:185
本文介绍了使用jacobian尝试使用scipy最小化功能时出现错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.6,尝试使用scipy.optimize.minimize最小化一个函数.我的最小化问题有两个约束,我可以找到一个解决方案.到目前为止,我有以下内容:

Using Python 3.6, I am trying to minimize a function using scipy.optimize.minimize. My minimization problem as two constraints, and I can find a solution. So far, I have the following:

import numpy as np
from scipy.optimize import minimize

array = np.array([[3.0, 0.25, 0.75],
                  [0.1, 0.65, 2.50],
                  [0.80, 2.5, 1.20],
                  [0.0, 0.25, 0.15],
                  [1.2, 2.40, 3.60]])

matrix = np.array([[1.0, 1.5, -2.],
                   [0.5, 3.0, 2.5],
                   [1.0, 0.25, 0.75]])


def fct1(x):
    return -sum(x.dot(array.T))


def fct2(x):
    return x.dot(matrix).dot(x)

x0 = np.ones(3) / 3
cons = ({'type': 'eq', 'fun': lambda x: x.sum() - 1.0},
        {'type': 'eq', 'fun': lambda x: fct2(x) - tgt})

tgt = 0.15

w = minimize(fct1, x0, method='SLSQP', constraints=cons)['x']
res1 = fct1(w)
res2 = fct2(w)

我现在正在尝试使我的优化器更快地运行,因为这只是一个简化的问题.最后,我的数组和矩阵要大得多.在上一个问题中,有人提出了定义要优化的函数的雅可比的想法,因此我添加了以下内容:

I am now trying to get my optimizer to run faster as this is only a simplified problem. In the end, my arrays and matrices are way bigger. In a previous question, somebody came up with the idea of defining the jacobian of my function to optimize, so I added the following:

def fct1_deriv(x):
    return -sum(np.ones_like(x).dot(array.T))

w = minimize(fct1, x0, method='SLSQP', jac=fct1_deriv, constraints=cons)['x']

问题是尝试运行时出现以下错误消息:

Problem is I get the following error message when trying to run:

0-th dimension must be fixed to 4 but got 2
Traceback (most recent call last):
  File "C:\Anaconda2\envs\py36\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-111-d1b854178c13>", line 1, in <module>
    w = minimize(fct1, x0, method='SLSQP', jac=fct1_deriv, constraints=cons)['x']
  File "C:\Anaconda2\envs\py36\lib\site-packages\scipy\optimize\_minimize.py", line 458, in minimize
    constraints, callback=callback, **options)
  File "C:\Anaconda2\envs\py36\lib\site-packages\scipy\optimize\slsqp.py", line 410, in _minimize_slsqp
    slsqp(m, meq, x, xl, xu, fx, c, g, a, acc, majiter, mode, w, jw)
_slsqp.error: failed in converting 8th argument `g' of _slsqp.slsqp to C/Fortran array

关于可能是什么问题的任何想法?我以前的答案的链接在这里: 最小化函数的最快方法是什么Python?

Any ideas on what the problem could be? The link to my previous answer is here: What is the fastest way to minimize a function in python?

推荐答案

我想我终于找到了答案,并将在这里发布,以便人们可以使用它或纠正我:

I think I finally found the answer, and will post here so people can use it or correct me:

在形式为y = x^2的优化问题中,可以通过将y相对于x进行微分来找到解决方案,并通过将导数设置为0来求解.因此,可以通过2x = 0.0找到解决方案(求解对于x=0.0).因此,我觉得传递函数的Jacobian(一阶导数)进行优化有助于优化器找到解决方案.

In an optimization problem of the form y = x^2, the solution can be found by differentiating y with respect to x, and solving by setting the derivative equal to 0. Therefore, the solution can be found with 2x = 0.0 (solving for x=0.0). Therefore, I have the feeling that passing the Jacobian (first derivative) of a function to optimize helps the optimizer in finding a solution.

在我要优化的问题中,我的函数的形式为y = x.不能通过相对于x区分y来优化此功能(除了给出约束之外).这将导致以下等式:1.0 = 0.0.因此,由于上述原因,将我函数的雅可比行列式赋予优化器可能会导致问题.

In the problem I am trying to optimize, my function is of the form y = x. This function can't be optimize (besides giving it constraints) by differentiating y with respect to x. That would lead to the following equation : 1.0 = 0.0. Therefore giving the Jacobian of my function to my optimizer is probably causing the problem because of the above.

这篇关于使用jacobian尝试使用scipy最小化功能时出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆