工具包
在GitHub上找到一个开源的项目,并对其进行了更新,提交了一些小bug,它实现了数学建模中常用的算法包括:
- 差分进化算法
- 遗传算法
- 粒子群算法
- 模拟退火算法
- 蚁群算法
- 免疫优化算法
- 人工鱼群算法
项目链接:
https://github.com/zhangxiao123qqq/scikit-opt
原作者链接:
https://github.com/guofei9987/scikit-opt
Github有时候会无法访问,为方便国内小伙伴访问仓库,创建Gitee仓库https://gitee.com/zhangxiaodong123qqq/scikit-opt
黑盒实现,即拿即用!不要太爽!!祝大家建模愉快。
提示:项目中使用等help()命令可直接获得参数说明。
#遗传算法帮助
help(GA)
#模拟退火算法帮助
help(SA)
算法使用方式见:
Demo演示:
import numpy as np
def schaffer(p):
'''
This function has plenty of local minimum, with strong shocks
global minimum at (0,0) with value 0
'''
x1, x2 = p
x = np.square(x1) + np.square(x2)
return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x)
# %%
from sko.GA import GA
ga = GA(func=schaffer, n_dim=2, size_pop=50, max_iter=800, lb=[-1, -1], ub=[1, 1], precision=1e-7)
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)
# %% Plot the result
import pandas as pd
import matplotlib.pyplot as plt
Y_history = pd.DataFrame(ga.all_history_Y)
fig, ax = plt.subplots(2, 1)
ax[0].plot(Y_history.index, Y_history.values, '.', color='red')
Y_history.min(axis=1).cummin().plot(kind='line')
plt.show()