Python 有啥启发式算法的包吗

就推荐一个DEAP https://github.com/DEAP/deap
Python 有啥启发式算法的包吗

在vitu可以在线跑

■网友
安利一下自己写的遗传算法框架
https://github.com/PytLab/gaft
■网友
Pyvolution: 非常模块化且非常可扩展的进化算法框架,具有完整的文档, Apache License 2.0deap: Python中的分布式进化算法, GNU Lesser GPLpySTEP: Python类型的遗传算法, MIT LicensePyevolvePyRobot: 开源的演化算法(GA + GP)模块PonyGEa 是GE的一个小型,单一源文件实现,具有交互式图形演示应用程序, GNU GPL v3inspyred: 生物启发式计算,包括进化计算,群体智能和神经网络, GNU GPL v3DRP: 定向Ruby编程,遗传算法和语法进化库, GNU GPLgplearn :遗传规划方面的一个通用包,支持自定义适应度函数其他语言:遗传编程
BigQuant官方网址:bigquant.com

■网友
集成了六种启发式算法,并且都做了很好的封装
guofei9987/scikit-opt安装
$pip install scikit-opt目标函数
def demo_func(x): x1, x2, x3 = x return x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2
用遗传算法求解
from ga import GAga = GA(func=demo_func, lb=, ub=, max_iter=500)best_x, best_y = ga.run()
还提供一个每代数据的借口,可以拿来画各个维度的图
import pandas as pdimport matplotlib.pyplot as pltFitV_history = pd.DataFrame(ga.FitV_history)fig, ax = plt.subplots(2, 1)ax.plot(FitV_history.index, FitV_history.values, \u0026#39;.\u0026#39;, color=\u0026#39;red\u0026#39;)plt_max = FitV_history.max(axis=1)ax.plot(plt_max.index, plt_max, label=\u0026#39;max\u0026#39;)ax.plot(plt_max.index, plt_max.cummax())plt.show()Python 有啥启发式算法的包吗


2. 用遗传算法解决TSP问题(Travelling Salesman Problem)加载距离矩阵的数据,这里用随机数生成数据
from GA import GA_TSPimport numpy as npnum_points = 8points = range(num_points)points_coordinate = np.random.rand(num_points, 2)distance_matrix = np.zeros(shape=(num_points, num_points))for i in range(num_points): for j in range(num_points): distance_matrix = np.linalg.norm(points_coordinate - points_coordinate, ord=2)print(\u0026#39;distance_matrix is: \\u0026#39;, distance_matrix)def cal_total_distance(points): num_points, = points.shape total_distance = 0 for i in range(num_points - 1): total_distance += distance_matrix, points] total_distance += distance_matrix, points] return total_distance用遗传算法求解:
ga_tsp = GA_TSP(func=cal_total_distance, points=points, pop=50, max_iter=200, Pm=0.001)best_points, best_distance = ga_tsp.fit()画图:
fig, ax = plt.subplots(1, 1)best_points_ = np.concatenate(]])best_points_coordinate = points_coordinateax.plot(best_points_coordinate, best_points_coordinate,\u0026#39;o-r\u0026#39;)plt.show


推荐阅读