python的智能算法_scikit-opt——Python中的群体智能优化算法库
安装
pip install scikit-opt
对于当前的开发者版本:
git clone git@github.com:guofei9987/scikit-opt.git
cd scikit-opt
pipinstall .
Genetic Algorithm
第一步:定义你的问题
importnumpy as npdefschaffer(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.sin(x) - 0.5) / np.square(1 + 0.001 * x)
第二步:运行遗传算法
from sko.GA importGA
#2个变量,每代取50个,800次迭代,上下界及精度
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)
第三步:画出结果
importpandas as pdimportmatplotlib.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()
精度改成1就能视为整数规划。
Genetic Algorithm for TSP(Travelling Salesman Problem)
只需要导入GA_TSP,它重载了crossover, mutation来解决TSP.
第一步:定义你的问题。准备你的点的坐标和距离矩阵。
这里使用随机数据作为Demo.
importnumpy as npfrom scipy importspatialimportmatplotlib.pyplot as plt
num_points= 50points_coordinate= np.random.rand(num_points, 2) #generate coordinate of points
distance_matrix = spatial.distance.cdist(points_coordinate, points_coordinate, metric='euclidean')defcal_total_distance(routine):'''The objective function. input routine, return total distance.
cal_total_distance(np.arange(num_points))'''num_points,=routine.shapereturn sum([distance_matrix[routine[i % num_points], routine[(i + 1) % num_points]] for i in range(num_points)])
第二步:运行GA算法
from sko.GA importGA_TSP
ga_tsp= GA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=500, prob_mut=1)
best_points, best_distance= ga_tsp.run()
第三步:画出结果
fig, ax = plt.subplots(1, 2)
best_points_=np.concatenate([best_points, [best_points[0]]])
best_points_coordinate=points_coordinate[best_points_, :]
ax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:,1], 'o-r')
ax[1].plot(ga_tsp.generation_best_Y)
plt.show()
python的智能算法_scikit-opt——Python中的群体智能优化算法库相关推荐
- 如何用python和pip安装在txt文件中配置好版本的库包
如何用python和pip安装在txt文件中配置好版本的库包 文章目录: 一.建立库包版本的txt配置文件 二.安装txt配置文件中的库包 有时候你自己用python实现一个项目,然后想把它在gith ...
- 机器学习中常用的优化算法:
http://blog.csdn.net/losteng/article/details/50993911 有知识点和代码的实现过程. 机器学习中常用的优化算法: 1.梯度下降法 2.牛顿法和拟牛顿法 ...
- 文献翻译__人工智能时代医学图像重建中的凸优化算法(第4、5、6章)
文章下载–我的Gitee Convex optimization algorithms in medical image reconstruction-in the age of AI 人工智能时代医 ...
- 深度学习中常用的优化算法(SGD, Nesterov,Adagrad,RMSProp,Adam)总结
深度学习中常用的优化算法(SGD, Nesterov,Adagrad,RMSProp,Adam)总结 1. 引言 在深度学习中我们定义了损失函数以后,会采取各种各样的方法来降低损失函数的数值,从而使模 ...
- 【群体智能优化算法】粒子群算法(PSO)入门实践
1.群体智能 群体智能源于对以蚂蚁.蜜蜂等为代表的社会性昆虫的群体行为的研究.最早被用在细胞机器人系统的描述中.它的控制是分布式的,不存在中心控制.群体具有自组织性. 群体智能优化算法主要模拟了昆虫. ...
- 小嘿嘿之群体智能优化算法
常见群体智能优化算法 遗传算法 定义 基本知识点 编码 适应度函数 算法流程 选择 交叉 变异 粒子群算法(Particle Swarm Optimization,PSO) 背景思想 位置和速度迭代公 ...
- 强化学习经典算法笔记(十二):近端策略优化算法(PPO)实现,基于A2C(下)
强化学习经典算法笔记(十二):近端策略优化算法(PPO)实现,基于A2C 本篇实现一个基于A2C框架的PPO算法,应用于连续动作空间任务. import torch import torch.nn a ...
- python算法和数据结构_Python中的数据结构和算法
python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...
- 基于python的智能家居_基于Python的智能家居自动化测试脚本设计及实现
投稿网址 http:www.VideoE.cn|<电视技术>第39 卷第5 期(总第458 期) 117 [本文献信息]沈杰,潘科,刘昕,等. 基于 Python 的智能家居自动化测试 ...
最新文章
- S5PV210开发 -- 烧写/启动模式
- 虚拟机上装的centos7使用xshell连接不上_使用Xshell连接虚拟机Ubuntu
- springboot + vue项目跨域请求解决方案
- 条码生成 SDK - Zint 教程及示例
- vue 引入qunee_Vue页面中js引入的问题
- 鸿蒙系统基础是云计算吗,国内两大系统横空出世,鸿蒙不算啥,这系统才叫牛...
- 一位程序员 8 年的物联网奋斗史
- Mybatis-Plus 自定义 id 生成器
- android 有序map,给HashMap排序,使之成为有序Map
- c语言1117查找数组元素,路雪军 Carl
- P1843 奶牛晒衣服
- Kali防火墙ufw安装与命令
- element ui table显示本地图片
- 地铁怎么坐才不能做反_坐地铁怎样才不会坐反方向
- 记录关于富文本编辑器出现的不能插入图片问题
- 关于DSSD算法相关解析
- golang实现图片颜色反转、图片灰度、缩放、转为字符画
- 英语 作文模板二 图表作文
- 计算机类综合素质测评考什么,综合素质测试考什么内容
- html图片做一个转盘,H5+CSS3+JS制作抽奖转盘