遗传算法python版

本文实例为大家分享了python遗传算法的具体代码,供大家参考,具体内容如下

1、基本概念

遗传算法(GA)是最早由美国Holland教授提出的一种基于自然界的“适者生存,优胜劣汰”基本法则的智能搜索算法。该法则很好地诠释了生物进化的自然选择过程。遗传算法也是借鉴该基本法则,通过基于种群的思想,将问题的解通过编码的方式转化为种群中的个体,并让这些个体不断地通过选择、交叉和变异算子模拟生物的进化过程,然后利用“优胜劣汰”法则选择种群中适应性较强的个体构成子种群,然后让子种群重复类似的进化过程,直到找到问题的最优解或者到达一定的进化(运算)时间。

Ga算法中的几个重要名词概念。

个体(染色体):自然界中一个个体(染色体)代表一个生物,在GA算法中,个体(染色体)代表了具体问题的一个解。

基因:在GA算法中,基因代表了具体问题解的一个决策变量,问题解和染色体中基因的对应关系如下所示:

种群:多个个体即组成一个种群。GA算法中,一个问题的多组解即构成了问题的解的种群。

2、主要步骤

GA算法的基本步骤如下:

Step 1. 种群初始化。选择一种编码方案然后在解空间内通过随机生成的方式初始化一定数量的个体构成GA的种群。

Step 2. 评估种群。利用启发式算法对种群中的个体(矩形件的排入顺序)生成排样图并依此计算个体的适应函数值(利用率),然后保存当前种群中的最优个体作为搜索到的最优解。

Step 3. 选择操作。根据种群中个体的适应度的大小,通过轮盘赌或者期望值方法,将适应度高的个体从当前种群中选择出来。

Step 4. 交叉操作。将上一步骤选择的个体,用一定的概率阀值Pc控制是否利用单点交叉、多点交叉或者其他交叉方式生成新的交叉个体。

Step 5. 变异操作。用一定的概率阀值Pm控制是否对个体的部分基因执行单点变异或多点变异。

Step 6. 终止判断。若满足终止条件,则终止算法,否则返回Step 2。

流程图如下所示:

3、主要操作介绍

3.1 种群初始化

种群的初始化和具体的问题有关。比如一个问题有n个决策变量{x1,x2,…,xn}。每个决策变量有取值范围:下界{L1,L2,…,Ln}和上界{U1,U2,…,Un},则种群中个体的初始化即随机地在决策变量的取值范围内生成各个决策变量的值:Xj={x1,x2,...,xn},其中xi属于范围(Li,Ui)内。所有的个体即构成种群。当每个个体都初始化后,即种群完成初始化。

3.2 评价种群

种群的评价即计算种群中个体的适应度值。假设种群populationpopsize个个体。依次计算每个个体的适应度值及评价种群。

3.3 选择操作

GA算法中常见的选择操作有轮盘赌方式:种群中适应度值更优的个体被选择的概率越大。假设popsize=4,按照如下表达式计算各个个体的被选择概率的大小,然后用圆饼图表示如下。

P(Xj) = fit(Xj)/(fit(X1)+fit(X2)+fit(X3)+fit(X4)),j=1,2,3,4

当依据轮盘赌方式进行选择时,则概率越大的越容易被选择到。

3.4 交叉操作

交叉操作也有许多种:单点交叉,两点交叉等。此处仅讲解一下两点交叉。首先利用选择操作从种群中选择两个父辈个体parent1和parent2,然后随机产生两个位置pos1和pos2,将这两个位置中间的基因位信息进行交换,便得到下图所示的off1和off2两个个体,但是这两个个体中一般会存在基因位信息冲突的现象(整数编码时),此时需要对off1和off2个体进行调整:off1中的冲突基因根据parent1中的基因调整为parent2中的相同位置处的基因。如off1中的“1”出现了两次,则第二处的“1”需要调整为parent1中“1”对应的parent2中的“4”,依次类推处理off1中的相冲突的基因。需要注意的是,调整off2,则需要参考parent2。

3.5 变异操作

变异操作的话,根据不同的编码方式有不同的变异操作。

如果是浮点数编码,则变异可以就染色体中间的某一个基因位的信息进行变异(重新生成或者其他调整方案)。

如果是采用整数编码方案,则一般有多种变异方法:位置变异和符号变异。

位置变异:

符号变异:

4、Python代码

#-*- coding:utf-8 -*- 

import random
import math
from operator import itemgetter 

class Gene:
 '''''
 This is a class to represent individual(Gene) in GA algorithom
 each object of this class have two attribute: data, size
 '''
 def __init__(self,**data):
  self.__dict__.update(data)
  self.size = len(data['data'])#length of gene 

class GA:
 '''''
 This is a class of GA algorithm.
 '''
 def __init__(self,parameter):
  '''''
  Initialize the pop of GA algorithom and evaluate the pop by computing its' fitness value .
  The data structure of pop is composed of several individuals which has the form like that: 

  {'Gene':a object of class Gene, 'fitness': 1.02(for example)} 

  Representation of Gene is a list: [b s0 u0 sita0 s1 u1 sita1 s2 u2 sita2] 

  '''
  #parameter = [CXPB, MUTPB, NGEN, popsize, low, up]
  self.parameter = parameter 

  low = self.parameter[4]
  up = self.parameter[5] 

  self.bound = []
  self.bound.append(low)
  self.bound.append(up) 

  pop = []
  for i in range(self.parameter[3]):
   geneinfo = []
   for pos in range(len(low)):
    geneinfo.append(random.uniform(self.bound[0][pos], self.bound[1][pos]))#initialise popluation 

   fitness = evaluate(geneinfo)#evaluate each chromosome
   pop.append({'Gene':Gene(data = geneinfo), 'fitness':fitness})#store the chromosome and its fitness 

  self.pop = pop
  self.bestindividual = self.selectBest(self.pop)#store the best chromosome in the population 

 def selectBest(self, pop):
  '''''
  select the best individual from pop
  '''
  s_inds = sorted(pop, key = itemgetter("fitness"), reverse = False)
  return s_inds[0] 

 def selection(self, individuals, k):
  '''''
  select two individuals from pop
  '''
  s_inds = sorted(individuals, key = itemgetter("fitness"), reverse=True)#sort the pop by the reference of 1/fitness
  sum_fits = sum(1/ind['fitness'] for ind in individuals) #sum up the 1/fitness of the whole pop 

  chosen = []
  for i in xrange(k):
   u = random.random() * sum_fits#randomly produce a num in the range of [0, sum_fits]
   sum_ = 0
   for ind in s_inds:
    sum_ += 1/ind['fitness']#sum up the 1/fitness
    if sum_ > u:
     #when the sum of 1/fitness is bigger than u, choose the one, which means u is in the range of [sum(1,2,...,n-1),sum(1,2,...,n)] and is time to choose the one ,namely n-th individual in the pop
     chosen.append(ind)
     break 

  return chosen  

 def crossoperate(self, offspring):
  '''''
  cross operation
  '''
  dim = len(offspring[0]['Gene'].data) 

  geninfo1 = offspring[0]['Gene'].data#Gene's data of first offspring chosen from the selected pop
  geninfo2 = offspring[1]['Gene'].data#Gene's data of second offspring chosen from the selected pop 

  pos1 = random.randrange(1,dim)#select a position in the range from 0 to dim-1,
  pos2 = random.randrange(1,dim) 

  newoff = Gene(data = [])#offspring produced by cross operation
  temp = []
  for i in range(dim):
   if (i >= min(pos1,pos2) and i <= max(pos1,pos2)):
    temp.append(geninfo2[i])
    #the gene data of offspring produced by cross operation is from the second offspring in the range [min(pos1,pos2),max(pos1,pos2)]
   else:
    temp.append(geninfo1[i])
    #the gene data of offspring produced by cross operation is from the frist offspring in the range [min(pos1,pos2),max(pos1,pos2)]
  newoff.data = temp 

  return newoff 

 def mutation(self, crossoff, bound):
  '''''
  mutation operation
  ''' 

  dim = len(crossoff.data) 

  pos = random.randrange(1,dim)#chose a position in crossoff to perform mutation. 

  crossoff.data[pos] = random.uniform(bound[0][pos],bound[1][pos])
  return crossoff 

 def GA_main(self):
  '''''
  main frame work of GA
  ''' 

  popsize = self.parameter[3] 

  print("Start of evolution") 

  # Begin the evolution
  for g in range(NGEN): 

   print("-- Generation %i --" % g)  

   #Apply selection based on their converted fitness
   selectpop = self.selection(self.pop, popsize)  

   nextoff = []
   while len(nextoff) != popsize:
    # Apply crossover and mutation on the offspring    

    # Select two individuals
    offspring = [random.choice(selectpop) for i in xrange(2)] 

    if random.random() < CXPB: # cross two individuals with probability CXPB
     crossoff = self.crossoperate(offspring)
     fit_crossoff = evaluate(self.xydata, crossoff.data)# Evaluate the individuals    

     if random.random() < MUTPB: # mutate an individual with probability MUTPB
      muteoff = self.mutation(crossoff,self.bound)
      fit_muteoff = evaluate(self.xydata, muteoff.data)# Evaluate the individuals
      nextoff.append({'Gene':muteoff,'fitness':fit_muteoff}) 

   # The population is entirely replaced by the offspring
   self.pop = nextoff 

   # Gather all the fitnesses in one list and print the stats
   fits = [ind['fitness'] for ind in self.pop] 

   length = len(self.pop)
   mean = sum(fits) / length
   sum2 = sum(x*x for x in fits)
   std = abs(sum2 / length - mean**2)**0.5
   best_ind = self.selectBest(self.pop) 

   if best_ind['fitness'] < self.bestindividual['fitness']:
    self.bestindividual = best_ind 

   print("Best individual found is %s, %s" % (self.bestindividual['Gene'].data,self.bestindividual['fitness']))
   print(" Min fitness of current pop: %s" % min(fits))
   print(" Max fitness of current pop: %s" % max(fits))
   print(" Avg fitness of current pop: %s" % mean)
   print(" Std of currrent pop: %s" % std) 

  print("-- End of (successful) evolution --")  

if __name__ == "__main__": 

 CXPB, MUTPB, NGEN, popsize = 0.8, 0.3, 50, 100#control parameters 

 up = [64, 64, 64, 64, 64, 64, 64, 64, 64, 64]#upper range for variables
 low = [-64, -64, -64, -64, -64, -64, -64, -64, -64, -64]#lower range for variables
 parameter = [CXPB, MUTPB, NGEN, popsize, low, up] 

 run = GA(parameter)
 run.GA_main() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

您可能感兴趣的文章:

  • python实现简单遗传算法
  • Python实现简单遗传算法(SGA)
  • Python使用遗传算法解决最大流问题
  • 详解用python实现简单的遗传算法
  • 遗传算法之Python实现代码
(0)

相关推荐

  • Python实现简单遗传算法(SGA)

    本文用Python3完整实现了简单遗传算法(SGA) Simple Genetic Alogrithm是模拟生物进化过程而提出的一种优化算法.SGA采用随机导向搜索全局最优解或者说近似全局最优解.传统的爬山算法(例如梯度下降,牛顿法)一次只优化一个解,并且对于多峰的目标函数很容易陷入局部最优解,而SGA算法一次优化一个种群(即一次优化多个解),SGA比传统的爬山算法更容易收敛到全局最优解或者近似全局最优解. SGA基本流程如下: 1.对问题的解进行二进制编码.编码涉及精度的问题,在本例中精度de

  • 详解用python实现简单的遗传算法

    今天整理之前写的代码,发现在做数模期间写的用python实现的遗传算法,感觉还是挺有意思的,就拿出来分享一下. 首先遗传算法是一种优化算法,通过模拟基因的优胜劣汰,进行计算(具体的算法思路什么的就不赘述了).大致过程分为初始化编码.个体评价.选择,交叉,变异. 遗传算法介绍 遗传算法是通过模拟大自然中生物进化的历程,来解决问题的.大自然中一个种群经历过若干代的自然选择后,剩下的种群必定是适应环境的.把一个问题所有的解看做一个种群,经历过若干次的自然选择以后,剩下的解中是有问题的最优解的.当然,只

  • python实现简单遗传算法

    今天整理之前写的代码,发现在做数模期间写的用python实现的遗传算法,感觉还是挺有意思的,就拿出来分享一下. 首先遗传算法是一种优化算法,通过模拟基因的优胜劣汰,进行计算(具体的算法思路什么的就不赘述了).大致过程分为初始化编码.个体评价.选择,交叉,变异. 以目标式子 y = 10 * sin(5x) + 7 * cos(4x)为例,计算其最大值 首先是初始化,包括具体要计算的式子.种群数量.染色体长度.交配概率.变异概率等.并且要对基因序列进行初始化 pop_size = 500 # 种群

  • 遗传算法之Python实现代码

    写在前面 之前的文章中已经讲过了遗传算法的基本流程,并且用MATLAB实现过一遍了.这一篇文章主要面对的人群是看过了我之前的文章,因此我就不再赘述遗传算法是什么以及基本的内容了,假设大家已经知道我是怎么写遗传算法的了. Python的遗传算法主函数 我的思想是,创建一个染色体的类,其中包括了两个变量:染色体chrom与适应度fitness.因此我们就可以通过直接建立对象来作为种群中的个体. #染色体的类 class Chrom: chrom = [] fitness = 0 def showCh

  • Python使用遗传算法解决最大流问题

    本文为大家分享了Python遗传算法解决最大流问题,供大家参考,具体内容如下 Generate_matrix def Generate_matrix(x,y): import numpy as np import random return np.ceil(np.array([random.random()*10 for i in range(x*y)]).reshape(x,y)) Max_road def Max_road(A,degree,start): import random imp

  • 遗传算法python版

    本文实例为大家分享了python遗传算法的具体代码,供大家参考,具体内容如下 1.基本概念 遗传算法(GA)是最早由美国Holland教授提出的一种基于自然界的"适者生存,优胜劣汰"基本法则的智能搜索算法.该法则很好地诠释了生物进化的自然选择过程.遗传算法也是借鉴该基本法则,通过基于种群的思想,将问题的解通过编码的方式转化为种群中的个体,并让这些个体不断地通过选择.交叉和变异算子模拟生物的进化过程,然后利用"优胜劣汰"法则选择种群中适应性较强的个体构成子种群,然后让

  • 基于ID3决策树算法的实现(Python版)

    实例如下: # -*- coding:utf-8 -*- from numpy import * import numpy as np import pandas as pd from math import log import operator #计算数据集的香农熵 def calcShannonEnt(dataSet): numEntries=len(dataSet) labelCounts={} #给所有可能分类创建字典 for featVec in dataSet: currentLa

  • 名片管理系统python版

    本文实例为大家分享了python名片管理系统的具体代码,供大家参考,具体内容如下 import os list_all = [] def page(): """输出主页面""" print("*" * 30) print("欢迎使用[名片管理系统]v2.0") print() print("1.新建名片") print("2.查看全部") print("3.

  • python版百度语音识别功能

    本文实例为大家分享了python版百度语音识别功能的具体代码,供大家参考,具体内容如下 环境:使用的IDE是Pycharm 1.新建工程 2.配置百度语音识别环境 "File"--"Settings"打开设置面板,"Project"标签下添加Project Interpreter,点击右侧"+" 输入"baidu-aip",进行安装 新建测试文件 from aip import AipSpeech &quo

  • python版opencv摄像头人脸实时检测方法

    OpenCV版本3.3.0,注意模型文件的路径要改成自己所安装的opencv的模型文件的路径,路径不对就会报错,一般在opencv-3.3.0/data/haarcascades 路径下 import numpy as np import cv2 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) while True: ret,img = ca

  • Python版名片管理系统

    本文实例为大家分享了Python版名片管理系统的具体代码,供大家参考,具体内容如下 先建立cards_main的文件 import cards_tools #无限循环,由用户主动决定什么时候退出 while True: #TODO注释,用于标记需要去做的工作 cards_tools.show_menu() action_str = raw_input("请选择希望执行的操作: ") print("你选择的操作是 %s" % action_str) #1,2,3针对名

  • python版大富翁源代码分享

    本文实例为大家分享了python版大富翁游戏的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- # code by: 物网141 王璞劼Khalil # name: 理工大富翁beta2.0 # describe: 基于python的一个2D大富翁游戏 ''' 1.游戏地图为自己使用各种网络素材制作: 各种按钮和选项,小图标等也是使用PS制作. 2.声音效果主要为背景音乐和几种游戏中音效: 3.游戏设定了两个类:玩家和建筑 玩家的参数和方法都在代码中给出: 具体

  • Python版中国省市经纬度

    一级行政区经纬度 一级行政区(省级行政区):34个(23个省.5个自治区.4个直辖市.2个特别行政区) provinces = { '吉林省': [125.326800, 43.896160], '黑龙江省': [126.662850, 45.742080], '辽宁省': [123.429250, 41.835710], '内蒙古自治区': [111.765220, 40.817330], '新疆维吾尔自治区': [87.627100, 43.793430], '青海省': [101.7801

  • Python版实现微信公众号扫码登陆

    基于python 实现公众扫码登陆 前提 申请公众号服务,配置相关信息,并在相关平台进行配置,就这么多东西 实现逻辑,使用临时临时二维码,带参数的二维码扫码登陆 流程,用户已经扫码关注,在登陆页面直接扫码登陆, 用户未关注,则需要点击关注后,直接登录, 我们使用带参数的场景值来区别是哪个用户进行扫码登陆 场景值用户可以自定义,但是必须是唯一的,我用的时间戳 我现在要做的功能,有账户绑定需求,并且是前后端分离的情况下, 流程1 当用户已经关注过,并且绑定账号,直接扫码登陆, 当用户已经关注过,未绑

  • Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)

    selenium操作chrome浏览器需要有ChromeDriver驱动来协助.webdriver中关浏览器关闭有两个方法,一个叫quit,一个叫close. /** * Close the current window, quitting the browser if it's the last window currently open. */ void close(); /** * Quits this driver, closing every associated window. */

随机推荐