Error when using Scipy's differential_evolution function

136 views Asked by At

I'm trying to do parameterization work for a college activity, but I've been encountering problems with Scipy's differential_evolution function (which is what our professor instructed us to use). I'm following the same steps that the teacher did in class, but I'm receiving the following error message: "RuntimeError: The map-like callable must be of the form f(func, iterable), returning a sequence of numbers the same length as 'iterable'"

Following is my code (I'm using jupyter, but I joined the two cells to make it easier to show here):

x = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
y = [0.0, 1.8, 2.0, 4.0, 4.0, 6.0, 4.0, 3.6, 3.4, 2.8, 0.0]

plt.plot(x, y, 'bo', markersize=3, label='Dados')
plt.legend()
plt.show()

def model_reta(x, c0, c1, c2, c3, c4, c5 ):
  return c0+c1*x+c2*pow(x,2)+c3*pow(x,3)+c4*pow(x,4)+c5*pow(x,5)

def model(params):
  sol = model_reta(x, params[0], params[1], params[2], params[3], params[4], params[5])

  erro = np.linalg.norm(y-sol, 1)/np.linalg.norm(y, 1)

  return [erro, sol, x]

def model_adj(x, args):
  result = model(x)
  return result[0]

bounds = [(-10, 10),(-10, 10),(-10, 10),(-10, 10),(-10, 10),(-10, 10),]

result = differential_evolution(model_adj, bounds, strategy='best1bin', disp=True)

Full error message:

TypeError                                 Traceback (most recent call last)
File c:\Users\filip\AppData\Local\Programs\Python\Python311\Lib\site-packages\scipy\optimize\_differentialevolution.py:1146, in DifferentialEvolutionSolver._calculate_population_energies(self, population)
   1145 try:
-> 1146     calc_energies = list(
   1147         self._mapwrapper(self.func, parameters_pop[0:S])
   1148     )
   1149     calc_energies = np.squeeze(calc_energies)

File c:\Users\filip\AppData\Local\Programs\Python\Python311\Lib\site-packages\scipy\_lib\_util.py:360, in _FunctionWrapper.__call__(self, x)
    359 def __call__(self, x):
--> 360     return self.f(x, *self.args)

TypeError: model_adj() missing 1 required positional argument: 'args'

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
d:\Scripts Python\atividade_integracao_numerica\integracao_numerica.ipynb Cell 9 line 1
     13   return result[0]
     15 bounds = [(-10, 10),(-10, 10),(-10, 10),(-10, 10),(-10, 10),(-10, 10),]
---> 17 result = differential_evolution(model_adj, bounds, strategy='best1bin', disp=True)
     18 '''
     19 print(result.x)
     20 erro, sol, x = model(result.x)
...
   1156     ) from e
   1158 if calc_energies.size != S:
   1159     if self.vectorized:

RuntimeError: The map-like callable must be of the form f(func, iterable), returning a sequence of numbers the same length as 'iterable'

If anyone could help me I would really appreciate it!

0

There are 0 answers