Below is a very stupid example which is basically a dumbing-down of my real world use case
import pandas as pd
from scipy.optimize import differential_evolution
import time
def optimizer_function(x, cost_name):
print(cost_name)
a = df_generator(x)
return cost_function(a,cost_name)
def df_generator(x):
return pd.DataFrame({"a":[x**2+3*x+8 for i in range(-int(x),int(x))]})
def cost_function(df, name):
cost = min(df["a"])
if name == "distance":
cost = 2*cost
return cost
def optimiser(criteria):
print("start optimization")
start = time.process_time()
print(start)
result = differential_evolution(func=optimizer_function, bounds=[(-100,100)],
args=(criteria))
end = time.process_time()
print(end)
print(end - start)
print("end optimization")
if __name__ == '__main__':
optimiser("distance")
print("all good")
I cannot get it to work though.
I would expect it to give me the value of x
that minimizes the cost_function
but instead I get 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'
It sounds like I am not setting the problem correctly or that my functions do not return the right type of data for differential_evolution
to interpret correctly but I have no idea where I should correct.
Any help welcome
here is the stack
File "H:/projects/decay-optimizer/test_diff_evo.py", line 33, in <module>
optimiser("distance")
File "H:/projects/decay-optimizer/test_diff_evo.py", line 25, in optimiser
args=(criteria))
File "H:\projects\decay-optimizer\venv\lib\site-packages\scipy\optimize\_differentialevolution.py", line 276, in differential_evolution
ret = solver.solve()
File "H:\projects\decay-optimizer\venv\lib\site-packages\scipy\optimize\_differentialevolution.py", line 688, in solve
self.population)
File "H:\projects\decay-optimizer\venv\lib\site-packages\scipy\optimize\_differentialevolution.py", line 794, in _calculate_population_energies
raise RuntimeError("The map-like callable must be of the"
RuntimeError: The map-like callable must be of the form f(func, iterable), returning a sequence of numbers the same length as 'iterable'
args=(criteria,)
from @hpaulj answer.