Scipy优化 - 不正确的参数

发布于 2025-02-08 05:34:56 字数 1287 浏览 1 评论 0原文

简单的问题/问题

def my_func(x,args):
  a = x[0]
  b = x[1]
  return a*args[0] + a*args[1] + b*args[2]

import scipy.optimize as optimize

params = [1,2,3]
guess = [3,4]
bounds = [(1,5),(2,7)]

result = optimize.minimize(my_func, x0=guess, args=(params), method='SLSQP', bounds = bounds)
print(result)
print('done')
result = optimize.brute(my_func, args = tuple(params), ranges = bounds)
print(result)

第一个优化运行良好。但是,第二个优化似乎与输入参数的数量有问题吗?不确定为什么:

    fun: 9.0
     jac: array([3., 3.])
 message: 'Optimization terminated successfully'
    nfev: 6
     nit: 2
    njev: 2
  status: 0
 success: True
       x: array([1., 2.])
done
Traceback (most recent call last):
  File "c:\Users\bob\Desktop\optimiser.py", line 15, in <module>
    result = optimize.brute(my_func, args = tuple(params), ranges = bounds)
  File "C:\Users\bob\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 3328, in brute
    Jout = np.array(list(mapper(wrapped_func, grid)))
  File "C:\Users\bob\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 3400, in __call__
    return self.f(np.asarray(x).flatten(), *self.args)
TypeError: my_func() takes 2 positional arguments but 4 were given

我不明白为什么它认为4个论点是对my_func的。有什么想法吗?

干杯

simple question/issue

def my_func(x,args):
  a = x[0]
  b = x[1]
  return a*args[0] + a*args[1] + b*args[2]

import scipy.optimize as optimize

params = [1,2,3]
guess = [3,4]
bounds = [(1,5),(2,7)]

result = optimize.minimize(my_func, x0=guess, args=(params), method='SLSQP', bounds = bounds)
print(result)
print('done')
result = optimize.brute(my_func, args = tuple(params), ranges = bounds)
print(result)

The first optimisation runs fine. However, the second optimisation seems to have an issue with the number of input arguments? Not sure why:

    fun: 9.0
     jac: array([3., 3.])
 message: 'Optimization terminated successfully'
    nfev: 6
     nit: 2
    njev: 2
  status: 0
 success: True
       x: array([1., 2.])
done
Traceback (most recent call last):
  File "c:\Users\bob\Desktop\optimiser.py", line 15, in <module>
    result = optimize.brute(my_func, args = tuple(params), ranges = bounds)
  File "C:\Users\bob\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 3328, in brute
    Jout = np.array(list(mapper(wrapped_func, grid)))
  File "C:\Users\bob\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 3400, in __call__
    return self.f(np.asarray(x).flatten(), *self.args)
TypeError: my_func() takes 2 positional arguments but 4 were given

I don't understand why it thinks 4 arguments are being given to my_func. Any ideas?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2025-02-15 05:34:56

因为emotizize.brute将其传递给可呼叫函数时扩展args参数。 Optimize.minimize执行同样的事情,但是该功能首先将args变成元组,如果还没有:

if not isinstance(args, tuple):
    args = (args,)

请参阅在这里以获取详细信息。 a>不执行此操作。因此,您得到的是列表的元组([1,2,3],) nath emotimize.minimize.minimize[1,2,2, 3]呼叫Optimize.Brute时。前者扩展到一个论点,后者为三个论点。

您可以通过播放来解决这个问题。将您的函数定义为采用可变数量的参数:

def my_func(x, *args):
    a = x[0]
    b = x[1]
    return a * args[0] + a * args[1] + b * args[2]

并将其他参数指定为元组:

params = (1, 2, 3)

Because optimize.brute expands the args argument when it passes these to the callable function. optimize.minimize does the same thing, but that function first turns args into a tuple if it is not already:

if not isinstance(args, tuple):
    args = (args,)

See here for details. optimize.brute does not do this. Thus, what you get is a tuple of a list ([1, 2, 3],) when calling optimize.minimize, and [1, 2, 3] when calling optimize.brute. The former expands to a single argument, the latter to three arguments.

You can fix this by playing nice. Define your function as taking a variadic number of arguments:

def my_func(x, *args):
    a = x[0]
    b = x[1]
    return a * args[0] + a * args[1] + b * args[2]

And specify the additional arguments as a tuple:

params = (1, 2, 3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文