I am trying to use the SHGO algorithm implemented in SciPy, but I have trouble when the objective function takes more than one argument. If I understand the error correctly, I am not passing the additional arguments (parameters) to the objective function as expected, however, I don't see where my syntax is wrong. Can someone explain what the root cause of the error is and how to fix it?
Below is a reproducible example of the issue I am facing.
import numpy as np
import scipy.optimize as opt
def fobj(x, y, z):
return (x+y+z).sum()
x0 = np.array([0.5, 0.5, 0.5, 0.5])
y = np.array([1, 3, 5, 7])
z = np.array([10, 20, 30, 40])
bnds = list(zip([0, 1, 2, 3], [2, 3, 4, 5]))
cons = {'type': 'eq', 'fun': lambda x: x.sum() - 14}
min_kwargs = {'method': 'SLSQP', 'options': {'maxiter': 100, 'disp': True}}
ret = opt.shgo(func=fobj, bounds=bnds, args=(y, z), constraints=cons, minimizer_kwargs=min_kwargs, options={'disp': True})
When run the following traceback is shown.
Splitting first generation
Traceback (most recent call last):
File "C:\path\lib\site-packages\scipy\optimize\_shgo_lib\triangulation.py", line 630, in __getitem__
return self.cache[x]
KeyError: (0, 0, 0, 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\path\lib\site-packages\scipy\optimize\_shgo.py", line 420, in shgo
shc.construct_complex()
File "C:\path\lib\site-packages\scipy\optimize\_shgo.py", line 733, in construct_complex
self.iterate()
File "C:\path\lib\site-packages\scipy\optimize\_shgo.py", line 876, in iterate
self.iterate_complex()
File "C:\path\lib\site-packages\scipy\optimize\_shgo.py", line 895, in iterate_hypercube
self.HC = Complex(self.dim, self.func, self.args,
File "C:\path\lib\site-packages\scipy\optimize\_shgo_lib\triangulation.py", line 25, in __init__
self.n_cube(dim, symmetry=symmetry)
File "C:\path\lib\site-packages\scipy\optimize\_shgo_lib\triangulation.py", line 76, in n_cube
self.C0.add_vertex(self.V[origintuple])
File "C:\path\lib\site-packages\scipy\optimize\_shgo_lib\triangulation.py", line 634, in __getitem__
xval = Vertex(x, bounds=self.bounds,
File "C:\path\lib\site-packages\scipy\optimize\_shgo_lib\triangulation.py", line 557, in __init__
self.f = func(x_a, *func_args)
File "C:\path\lib\site-packages\scipy\optimize\_optimize.py", line 466, in function_wrapper
fx = function(np.copy(x), *(wrapper_args + args))
TypeError: fobj() takes 3 positional arguments but 5 were given
I don't get why the TypeError is raised. It's saying that 5 arguments rather than 3 were passed to the objective function fobj
, but as far as I can understand I am only passing (y, z)
, so I can't see how they are 5!
Note that I tried also to re-write the local minimizer dictionary as min_kwargs = {'method': 'SLSQP', 'args': (x0), 'options': {'maxiter': 100, 'disp': True}}
, but I kept facing the same error. I am sure I am passing the arguments incorrectly, but I can't understand how to do it right. Any help will be greatly appreciated.
I am using: Python 3.10.5, Numpy 1.22.4, and SciPy 1.8.1.