ValueError:函数中矩阵的scipy.optimize.minimize形状不均匀

r7xajy2e  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(227)

我正尝试使用pythons scipy.optimize.minimize来查找函数中x的值,其中函数会趋向于0。然而,尽管函数的最终结果是单个值,但函数确实包含了矩阵运算,这给了我一个错误:

File "...\Anaconda3\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "...\test.py", line 41, in <module>
    result = minimize(func, x0 = 15, bounds = [ (0 , 10000 ) ])

  File "...\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py", line 699, in minimize
    res = _minimize_lbfgsb(fun, x0, args, jac, bounds,

  File "...\Anaconda3\lib\site-packages\scipy\optimize\_lbfgsb_py.py", line 306, in _minimize_lbfgsb
    sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,

  File "...\Anaconda3\lib\site-packages\scipy\optimize\_optimize.py", line 263, in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,

  File "...\Anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 158, in __init__
    self._update_fun()

  File "...\Anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 251, in _update_fun
    self._update_fun_impl()

  File "...\Anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 155, in update_fun
    self.f = fun_wrapped(self.x)

  File "...\Anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 137, in fun_wrapped
    fx = fun(np.copy(x), *args)

  File "...\test.py", line 40, in <lambda>
    func = lambda x: example(x)

  File "...\test.py", line 18, in example
    A = np.matrix([[0, 0, 0, -1, 1, -1],

  File "...\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py", line 145, in __new__
    arr = N.array(data, dtype=dtype, copy=copy)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (6,) + inhomogeneous part.

下面我给出了一个函数的例子,它给出了上面的错误。有没有一种方法可以最小化这个函数?
(note这是一个示例函数,我不知道这个函数是否真的有解。我的主要问题是关于我得到的错误是否有解)。
我用的是python 3.10.8版。

import numpy as np
from scipy.optimize import minimize
from scipy.linalg import solve

def example(x: float) -> float:
    a = 4
    b = 4*x
    c = 18*np.sin(x)
    d = 14*np.sqrt(1/x)

    A = np.matrix([[0, 0, 0, -1, 1, -1],
                   [0 - 1, 4, 2*x, 0, 0],
                   [1, 3*x-4, 4, 18, 2, 0],
                   [0, 0, 1, 0, 0, 0],
                   [3*x, 2*a, b, d, 0, 0],
                   [0, 0, d, d-c, 0, 0]
                   ])

    b = np.matrix([[0],
                   [0],
                   [b-c],
                   [c/d],
                   [4*a],
                   [b*a-c]
                   ])

    C1, C2, C3, C4, C5, C6 = np.squeeze(solve(A, b))

    val = C1 * np.sin(a * 2) + C2 * c**2 + 4*C3*x**2 + d*C4 + C5*x + C6

    return val

func = lambda x: example(x)
result = minimize(func, x0 = 15, bounds = [ (0 , 10000 ) ])

一开始我试着用symy来最小化函数。然而,这花了很长时间,因此不是一个选择。理论上我可以(可能)通过手工求解矩阵来解决这个问题,单独写出每个函数。然而,我宁愿只作为最后的手段,它使代码变得非常不可读,以及使一切不再类似于我使用的引用。

jvlzgdj9

jvlzgdj91#

scipy.optimize.minimize没有向函数传递浮点参数,而是传递了一个1D数组,这会导致矩阵不正确(例如[[0, 0, array(30), 0, 0]])。转换值可以解决问题:

def example(x_in) -> float:
  x = float(x_in)

相关问题