想要学习启发式算法?推荐你看看这个价值极高的开源项目

文章插图
许多学习算法的开发者在刷题或者练习的过程中都会遇到启发式算法,如果你恰好也正在学习算法,那么今天 Gitee 介绍的这款开源项目一定能对你的学习过程有所帮助,帮你更好的理解启发式算法 。
启发式算法(heuristic algorithm)是相对于最优化算法提出的 。一个问题的最优算法求得该问题每个实例的 最优解。启发式算法可以这样定义:一个基于直观或经验构造的算法,在可接受的花费(指计算时间和空间)下给出待解决组合优化问题每一个实例的一个可行解,该可行解与最优解的偏离程度一般不能被预计 。项目名称:scikit-opt
项目作者:guofei9987
开源许可协议:MIT
项目地址:https://gitee.com/guofei9987/scikit-opt
项目简介该项目是一个封装了7种启发式算法的 Python 代码库,包含了差分进化算法、遗传算法、粒子群算法、模拟退火算法、蚁群算法、鱼群算法、免疫优化算法 。
特性
- UDF(用户自定义算子)
- GPU 加速
- 断点继续运行
'''min f(x1, x2, x3) = x1^2 + x2^2 + x3^2s.t.x1*x2 >= 1x1*x2 <= 5x2 + x3 = 10 <= x1, x2, x3 <= 5'''def obj_func(p):x1, x2, x3 = preturn x1 ** 2 + x2 ** 2 + x3 ** 2constraint_eq = [lambda x: 1 - x[1] - x[2]]constraint_ueq = [lambda x: 1 - x[0] * x[1],lambda x: x[0] * x[1] -
2.做差分进化算法from sko.DE import DEde = DE(func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5],constraint_eq=constraint_eq, constraint_ueq=constraint_ueq)best_x, best_y = de.run()print('best_x:', best_x, 'n', 'best_y:', best_y
遗传算法1.定义你的问题import numpy as npdef schaffer(p):'''This function has plenty of local minimum, with strong shocksglobal minimum at (0,0) with value 0'''x1, x2 = px = np.square(x1) + np.square(x2)return 0.5 + (np.sin(x) - 0.5) / np.square(1 + 0.001 * x
2.运行遗传算法from sko.GA import GAga = 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)
3.用 matplotlib 画出结果import pandas as pdimport matplotlib.pyplot as pltY_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()

文章插图
粒子群算法1.定义问题
def demo_func(x):x1, x2, x3 = xreturn x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2
2.做粒子群算法from sko.PSO import PSOpso = PSO(func=demo_func, dim=3, pop=40, max_iter=150, lb=[0, -1, 0.5], ub=[1, 1, 1], w=0.8, c1=0.5, c2=0.5)pso.run()print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)
3.画出结果import matplotlib.pyplot as pltplt.plot(pso.gbest_y_hist)plt.show()

文章插图

文章插图
模拟退火算法模拟退火算法用于多元函数优化
1.定义问题
demo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2
2.运行模拟退火算法from sko.SA import SAsa = SA(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, L=300, max_stay_counter=150)best_x, best_y = sa.run()print('best_x:', best_x, 'best_y', best_y)
3.画出结果import matplotlib.pyplot as pltimport pandas as pdplt.plot(pd.DataFrame(sa.best_y_history).cummin(axis=0))plt.show()

文章插图
蚁群算法蚁群算法(ACA, Ant Colony Algorithm)解决TSP问题
from sko.ACA import ACA_TSPaca = ACA_TSP(func=cal_total_distance, n_dim=num_points,size_pop=50, max_iter=200,distance_matrix=distance_matrix)best_x, best_y = aca.run(

文章插图
免疫优化算法
from sko.IA import IA_TSPia_tsp = IA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=500, max_iter=800, prob_mut=0.2,T=0.7, alpha=0.95)best_points, best_distance = ia_tsp.run()print('best routine:', best_points, 'best_distance:', best_distance)
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- SpringBoot整合JPA案例
- 简介机器学习中的特征工程
- 2020年顶级深度学习框架:PyTorch与TensorFlow
- C#中的高级语法
- 如何用Python构建机器学习推荐系统?网易云、爱奇艺也用这种方法
- 小孩学习舞蹈好吗
- 学习散打能不能减肥呢
- 学习散打有哪些好处
- 想要增高如何运到?
- 新手手风琴教程 如何学习手风琴