SciPy: Issue passing arguments to optimize.shgo function

1.3k Views Asked by At

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.
Image presenting the mathematical formulation of the problem implemented in the Python code

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.
2

There are 2 best solutions below

6
Bob On

The problem is that sum is a method, not a property of the array.

It should work if you make your objective function

def fobj(x, y, z):
    return (x+y+z).sum()

Additionally, there is the bug on the development version (updates on this soon).

The method seems not very robust as well, for sum(x) = 14, it has only one solution this could be a problem because the valid search space is 0-dimensional, but even if I relax it to things like sum(x) = 12 the method can't give the solution.

Here you have a more complete working example

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], [3,4,5,6]))
cons = {'type': 'eq', 'fun': lambda x: x.sum() - 9}
min_kwargs = {'method': 'SLSQP', 'options': {'maxiter': 10000, 'disp': True}}
ret = opt.shgo(func=lambda x: fobj(x, y, z), bounds=bnds, args=(), constraints=cons, 
               minimizer_kwargs=min_kwargs, options={'disp': True})
2
Warren Weckesser On

As you discovered, the problem is a bug in shgo.

You can avoid the bug in shgo by avoiding the use of the args parameter. Instead of using args, use a wrapper of fobj that captures y and z in a closure. A simple way to do that is with a lambda expression: change the first argument of shgo from func=fobj to func=lambda x, y=y, z=z: fobj(x, y, z), and remove the args parameter from the call.