很多学习算法的开发者在刷题或者练习的过程中都会遇到启发式算法。如果你恰好是学习算法的,那么今天Gitee介绍的这个开源项目一定会对你的学习过程有所帮助,帮助你更好地理解启发式算法。启发式算法(heuristicalgorithm)是相对于优化算法提出来的。问题的最优算法为问题的每个实例找到最优解。启发式算法可以定义如下:一种基于直觉或经验的算法,以可接受的代价(指计算时间和空间)对待求解的组合优化问题的每个实例给出可行解,以及可行解与最优解的偏差程度一般无法预测。项目名称:scikit-opt项目作者:guofei9987开源许可协议:MIT项目地址:https://gitee.com/guofei9987/scikit-opt项目介绍本项目是一个Python代码库,封装了7种启发式算法,包括差分进化算法、遗传算法、粒子群算法、模拟退火算法、蚁群算法、鱼群算法、免疫优化算法。特点UDF(User-DefinedOperator)GPU加速断点继续算法差分进化算法演示1.定义你的问题'''minf(x1,x2,x3)=x1^2+x2^2+x3^2s.t。x1\*x2>=1x1\*x2<=5x2+x3=10<=x1,x2,x3<=5'''defobj\_func(p):x1,x2,x3=p返回x1\*\*2+x2\*\*2+x3\*\*2constraint\_eq=\[lambdax:1-x\[1\]-x\[2\]\]constraint\_ueq=\[λx:1-x\[0\]\*x\[1\],λx:x\[0\]\*x\[1\]-2。从sko做差分进化算法。DEimportDEde=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.定义你的问题importnumpyasnpdefschaffer(p):'''这个函数有很多局部最小值,在(0,0)处有强烈的冲击全局最小值值为0'''x1,x2=px=np.square(x1)+np.square(x2)返回0.5+(np.sin(x)-0.5)/np.square(1+0.001\*x2。从sk运行遗传算法o.GAimportGAga=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绘制结果importpandasaspdimportmatplotlib.pyplotaspltY\_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.定义问??题defdemo\_func(x):x1,x2,x3=xreturnx1\*\*2+(x2-0.05)\*\*2+x3\*\*22.doparticleswarmoptimizationfromsko.PSOimportPSOpso=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\_xis',pso.gbest\_x,'best\_yis',pso.gbest\_y)3.绘制结果importmatplotlib.pyplotaspltplt.plot(pso.gbest\_y\_hist)plt.show()SimulatedannealingalgorithmSimulatedannealingalgorithmformultivariatefunctionoptimization1.定义问??题demo\_func=λx:x\[0\]\*\*2+(x\[1\]-0.05)\*\*2+x\[2\]\*\*22.运行模拟退火算法fromsko.SAimportSAsa=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.绘制结果importmatplotlib.pyplotaspltimportpandasaspdplt.plot(pd.DataFrame(sa.best\_y\_history).cummin(axis=0))plt.show()蚁群算法蚁群算法(ACA,AntColonyAlgorithm)解决TSP问题fromsko.ACAimportACA\_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(免疫优化算法fromsko.IAimportIA\_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('bestroutine:',best\_points,'best\_distance:',best\_distance)人工鱼群算法deffunc(x):x1,x2=xreturn1/x1\*\*2+x1\*\*2+1/x2\*\*2+x2\*\*2fromsko.AFSAimportAFSAafsa=AFSA(func,n\_dim=2,size\_pop=50,max\_iter=300,max\_try\_num=100,step=0.5,visual=0.3,q=0.98,delta=0.5)best\_x,best\_y=afsa.run()print(best\_x,best\_想了解更多算法开源项目?点击以下链接到Gitee看看:https://gitee.com/explore/mathlibs
