当前位置: 首页 > news >正文

影响网站排名的因素/新浪舆情通官网

影响网站排名的因素,新浪舆情通官网,谷德设计网室内设计案例,ftp服务器文章目录一.基础介绍二.分布实现三.完整代码四.结果截图一.基础介绍 遗传算法的来源、定义、特点见之前的文章【遗传算法GA】–计算函数最值(Python)。 下面我们先来看本次需要实现的内容:我们随机生成一些城市的坐标,然后找一条最…

文章目录

    • 一.基础介绍
    • 二.分布实现
    • 三.完整代码
    • 四.结果截图

一.基础介绍

遗传算法的来源、定义、特点见之前的文章【遗传算法GA】–计算函数最值(Python)。


下面我们先来看本次需要实现的内容:我们随机生成一些城市的坐标,然后找一条最短路径通过所有城市。

最重要的还是对染色体DNA的编码以及适应度函数的确定。对于本题来说可以先将所以城市进行编号,然后对这些编号进行排序,排好的顺序就是旅行的路线。对于适应度函数来说就是将路程加起来,总路程最小,适应度越高。

参数:

参数名称含义
citys城市个数
pc交叉概率
pm变异概率
popsize种群规模
iternum迭代次数
pop种群
city_position城市坐标

二.分布实现

∙\bullet 参数

citys = 20 #染色体DNA长度
pc = 0.1 #交叉概率
pm = 0.02 #变异概率
popsize = 500 #种群规模
iternum = 100 #迭代次数

GA类:

∙\bullet 将种群中排好的序列横纵坐标分别提取出来transltaeDNA函数:参数DNA为种群pop,参数city_position为所有城市坐标

def translateDNA(self,DNA,city_position):#生成等长的空列表lineX = np.empty_like(DNA,dtype=np.float64)lineY = np.empty_like(DNA,dtype=np.float64)#将下标和值同时提取出来for i,d in enumerate(DNA):city_coord = city_position[d]lineX[i,:] = city_coord[:,0]lineY[i,:] = city_coord[:,1]return lineX,lineY

∙\bullet 求适应度函数getFiness:参数lineX、lineY分别为城市坐标,返回fitness为每个个体的适应度,totalDis为每个个体的总路程。

def getFitness(self,lineX,lineY):totalDis = np.empty((lineX.shape[0],),dtype=np.float64)for i,(xval,yval) in enumerate(zip(lineX,lineY)):totalDis[i]=np.sum(np.sqrt(np.square(np.diff(xval)) + np.square(np.diff(yval))))fitness = np.exp(self.citys*2/totalDis)return fitness,totalDis

∙\bullet 选择函数selection:参数fitness为适应度,选择适应度更高的个体。

def selection(self,fitness):idx = np.random.choice(np.arange(self.popsize),size=self.popsize,replace=True,p=fitness/fitness.sum())return self.pop[idx]

∙\bullet 交叉函数selection:参数parent为父本中一个个体,pop为种群。

交叉规则:在交叉概率内随机选择种群中一个体,随机选择一些位置,将这些位置的数提取出来放到数组前面,然后将母本中除这些数之外的数按顺序放入数组后面,组成新个体。

def crossover(self,parent,pop):if np.random.rand() < self.pc:i = np.random.randint(0, self.popsize, size=1)  #随机选取一个个体进行交换                      cross_points = np.random.randint(0, 2, self.citys).astype(np.bool)   #随机选择个体中的一些位置keep_city = parent[~cross_points]     #将parent中False的位置返给keep_city                                  swap_city = pop[i, np.isin(pop[i].ravel(), keep_city, invert=True)]  #将keep_city中没有出现的数赋给swap_cityparent[:] = np.concatenate((keep_city, swap_city)) #拼接形成新个体return parent

∙\bullet 变异函数mutation:在变异范围内随机选取一个位置与下标位置的数进行互换。

def mutation(self,child):for point in range(self.citys):if np.random.rand()<self.pm:swap_point = np.random.randint(0,self.citys)swapa,swapb = child[point],child[swap_point]child[point],child[swap_point] = swapb,swapareturn child

∙\bullet 进化函数evolve:调用交叉函数和变异函数。

def evolve(self,fitness):pop = self.selection(fitness)pop_copy = pop.copy()for parent in pop: child = self.crossover(parent,pop_copy)child = self.mutation(child)parent[:] = childself.pop = pop

TSP类

∙\bullet 构造函数:随机生成城市坐标

def __init__(self,citys):#生成每个城市的横纵坐标self.city_position = np.random.rand(citys,2)plt.ion()

∙\bullet 绘图函数plotting:参数lx、ly为城市横纵坐标,total_d最优路线。

def plotting(self,lx,ly,total_d):plt.cla()plt.scatter(self.city_position[:, 0].T, self.city_position[:, 1].T, s=100, c='k')  #画散点图plt.plot(lx.T, ly.T, 'r-') #连线plt.text(-0.05, -0.05, "Total distance=%.2f" % total_d, fontdict={'size': 20, 'color': 'red'})plt.xlim((-0.1, 1.1))plt.ylim((-0.1, 1.1))plt.pause(0.01)

∙\bullet 主函数

if __name__=='__main__':ga = GA(citys=citys,pc=pc,pm=pm,popsize=popsize)env = TSP(citys=citys)for gen in range(iternum):lx,ly = ga.translateDNA(ga.pop,env.city_position)fitness,total_distance = ga.getFitness(lx,ly)ga.evolve(fitness)best_idx = np.argmax(fitness) #最优解的下标print("Gen:", gen," | best fit: %.2f"%fitness[best_idx],)env.plotting(lx[best_idx],ly[best_idx],total_distance[best_idx])plt.ioff()plt.show()

三.完整代码

import numpy as np
import matplotlib.pyplot as plt#参数
citys = 20 #染色体DNA长度
pc = 0.1 #交叉概率
pm = 0.02 #变异概率
popsize = 500 #种群规模
iternum = 100 #迭代次数class GA(object):def __init__(self,citys,pc,pm,popsize,):self.citys = citysself.pc = pcself.pm = pmself.popsize = popsize#vstck纵向拼接数组,permutaion将数字0-(city-1)进行随机排序#生成种群,dna序列为0到city的随机序列self.pop = np.vstack([np.random.permutation(citys) for _ in range(popsize)])#将种群中排好的序列横纵坐标分别提取出来def translateDNA(self,DNA,city_position):#生成等长的空列表lineX = np.empty_like(DNA,dtype=np.float64)lineY = np.empty_like(DNA,dtype=np.float64)#将下标和值同时提取出来for i,d in enumerate(DNA):city_coord = city_position[d]lineX[i,:] = city_coord[:,0]lineY[i,:] = city_coord[:,1]return lineX,lineYdef getFitness(self,lineX,lineY):totalDis = np.empty((lineX.shape[0],),dtype=np.float64)for i,(xval,yval) in enumerate(zip(lineX,lineY)):totalDis[i]=np.sum(np.sqrt(np.square(np.diff(xval)) + np.square(np.diff(yval))))fitness = np.exp(self.citys*2/totalDis)return fitness,totalDisdef selection(self,fitness):idx = np.random.choice(np.arange(self.popsize),size=self.popsize,replace=True,p=fitness/fitness.sum())return self.pop[idx]def crossover(self,parent,pop):if np.random.rand() < self.pc:i = np.random.randint(0, self.popsize, size=1)  #随机选取一个个体进行交换                      cross_points = np.random.randint(0, 2, self.citys).astype(np.bool)   #随机选择个体中的一些位置keep_city = parent[~cross_points]     #将parent中False的位置返给keep_city                                  swap_city = pop[i, np.isin(pop[i].ravel(), keep_city, invert=True)]  #将keep_city中没有出现的数赋给swap_cityparent[:] = np.concatenate((keep_city, swap_city)) #拼接形成新个体return parentdef mutation(self,child):for point in range(self.citys):if np.random.rand()<self.pm:swap_point = np.random.randint(0,self.citys)swapa,swapb = child[point],child[swap_point]child[point],child[swap_point] = swapb,swapareturn childdef evolve(self,fitness):pop = self.selection(fitness)pop_copy = pop.copy()for parent in pop: child = self.crossover(parent,pop_copy)child = self.mutation(child)parent[:] = childself.pop = popclass TSP(object):def __init__(self,citys):#生成每个城市的横纵坐标self.city_position = np.random.rand(citys,2)plt.ion()def plotting(self,lx,ly,total_d):plt.cla()plt.scatter(self.city_position[:, 0].T, self.city_position[:, 1].T, s=100, c='k')  #画散点图plt.plot(lx.T, ly.T, 'r-') #连线plt.text(-0.05, -0.05, "Total distance=%.2f" % total_d, fontdict={'size': 20, 'color': 'red'})plt.xlim((-0.1, 1.1))plt.ylim((-0.1, 1.1))plt.pause(0.01)if __name__=='__main__':ga = GA(citys=citys,pc=pc,pm=pm,popsize=popsize)env = TSP(citys=citys)for gen in range(iternum):lx,ly = ga.translateDNA(ga.pop,env.city_position)fitness,total_distance = ga.getFitness(lx,ly)ga.evolve(fitness)best_idx = np.argmax(fitness) #最优解的下标print("Gen:", gen," | best fit: %.2f"%fitness[best_idx],)env.plotting(lx[best_idx],ly[best_idx],total_distance[best_idx])plt.ioff()plt.show()

四.结果截图

初始情况(进行100次迭代):在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://www.lbrq.cn/news/789841.html

相关文章:

  • 网站建设酷隆/媒体广告投放平台
  • 网站域名怎么购买吗/网站推广的概念
  • 合肥网络推广专员/南京网站seo
  • 常德优化公司/公司优化是什么意思
  • 江西做网站的公司有哪些/怎么给自己的公司做网站
  • 百度网站提交了多久收录/口碑营销的步骤
  • 12306网站 谁做的/最近实时热点事件
  • 什么平台可以做网站推广/一站式推广平台
  • 网站子域名什么意思/网站安全检测在线
  • 金昌市建设局网站/电话营销
  • 百度地图导航手机版免费下载/seo是什么味
  • 珠海澳门网站建设/免费网站优化排名
  • 买服务器做网站/免费b2b信息发布网站
  • 不要营业执照的做网站/网络游戏排行榜百度风云榜
  • 自己做的视频网站如何赚钱/中文域名注册管理中心
  • 三门峡做网站/友情链接推广
  • 如何查询网站建设时间/软件开发培训机构去哪个学校
  • 北京管理咨询公司/燃灯seo
  • 重庆便宜做网站的/买域名要多少钱一个
  • 课程网站如何建设/网站编辑怎么做
  • 南昌做网站的公司/内存优化大师
  • 一级A视网站 一级做爰片/360推广联盟
  • 做程序界面的网站/商丘网络推广外包
  • 怎么可以建网站/教育培训网站官网
  • 什么公司可以做网站等级保护/域名注册 万网
  • 做网站怎样投放广告/百度的网站
  • 做网站后的总结/长沙市最新疫情
  • 黄冈论坛遗爱网购/百度seo关键词工具
  • 红色色系做网站的配色/seo方法培训
  • 网站精品案例/武汉seo全网营销
  • 使用 Vuepress + GitHub Pages 搭建项目文档
  • Android 之 MVC架构
  • ulimit参数使用详细总结
  • Linux 摄像头实时抓取:V4L2、FFmpeg 与 GStreamer 全面讲解
  • chrome的数据采集插件chat4data的使用
  • 【机器学习】“回归“算法模型的三个评估指标:MAE(衡量预测准确性)、MSE(放大大误差)、R²(说明模型解释能力)