下面是一个非常愚蠢的例子,它基本上是我真实世界用例的简化
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")
我无法让它工作。我希望它能给我x
最小化的值,cost_function
但我收到以下错误消息:
RuntimeError:类似地图的可调用对象必须是 f(func, iterable) 的形式,返回与“可迭代”长度相同的数字序列
听起来我没有正确设置问题,或者我的函数没有返回正确类型的数据differential_evolution
以正确解释,但我不知道应该在哪里更正。
欢迎任何帮助
这是堆栈
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'