基于遗传算法的TSP和MTSP问题求解(python)

TSP问题

SCORE_NONE = -1
 
class Life(object):
      """个体类"""
      def __init__(self, aGene = None):
            self.gene = aGene
            self.score = SCORE_NONE
# -*- coding: utf-8 -*-
 
import random
 
class GA(object):
      """遗传算法类"""
      def __init__(self, aCrossRate, aMutationRate, aLifeCount, aGeneLength, aMatchFun = lambda life : 1):
            self.crossRate = aCrossRate               #交叉概率
            self.mutationRate = aMutationRate         #突变概率
            self.lifeCount = aLifeCount               #种群数量,就是每次我们在多少个城市序列里筛选,这里初始化为100
            self.geneLength = aGeneLength             #其实就是城市数量
            self.matchFun = aMatchFun                 #适配函数
            self.lives = []                           #种群
            self.best = None                          #保存这一代中最好的个体
            self.generation = 1                       #一开始的是第一代
            self.crossCount = 0                       #一开始还没交叉过,所以交叉次数是0
            self.mutationCount = 0                    #一开始还没变异过,所以变异次数是0
            self.bounds = 0.0                         #适配值之和,用于选择时计算概率
 
            self.initPopulation()                     #初始化种群
 
 
      def initPopulation(self):
            """初始化种群"""
            self.lives = []
            for i in range(self.lifeCount):
                  #gene = [0,1,…… ,self.geneLength-1]
                  #事实就是0到33
                  gene =list(range(self.geneLength))
                  #将0到33序列的所有元素随机排序得到一个新的序列
                  random.shuffle(gene)
                  #Life这个类就是一个基因序列,初始化life的时候,两个参数,一个是序列gene,一个是这个序列的初始适应度值
                  # 因为适应度值越大,越可能被选择,所以一开始种群里的所有基因都被初始化为-1
                  life =Life(gene)
                  #把生成的这个基因序列life填进种群集合里
                  self.lives.append(life)
 
 
      def judge(self):
            """评估,计算每一个个体的适配值"""
            # 适配值之和,用于选择时计算概率
            self.bounds = 0.0
            #假设种群中的第一个基因被选中
            self.best = self.lives[0]
            for life in self.lives:
                  life.score = self.matchFun(life)
                  self.bounds += life.score
                  #如果新基因的适配值大于原先的best基因,就更新best基因
                  if self.best.score < life.score:
                        self.best = life
 
 
      def cross(self, parent1, parent2):
            """交叉"""
            index1 = random.randint(0, self.geneLength - 1)
            index2 = random.randint(index1, self.geneLength - 1)
            tempGene = parent2.gene[index1:index2]                      #交叉的基因片段
            newGene = []
            p1len = 0
            for g in parent1.gene:
                  if p1len == index1:
                        newGene.extend(tempGene)                               #插入基因片段
                        p1len += 1
                  if g not in tempGene:
                        newGene.append(g)
                        p1len += 1
            self.crossCount += 1
            return newGene
 
 
      def  mutation(self, gene):
            """突变"""
            #相当于取得0到self.geneLength - 1之间的一个数,包括0和self.geneLength - 1
            index1 = random.randint(0, self.geneLength - 1)
            index2 = random.randint(0, self.geneLength - 1)
            #把这两个位置的城市互换
            gene[index1], gene[index2] = gene[index2], gene[index1]
            #突变次数加1
            self.mutationCount += 1
            return gene
 
 
      def getOne(self):
            """选择一个个体"""
            #产生0到(适配值之和)之间的任何一个实数
            r = random.uniform(0, self.bounds)
            for life in self.lives:
                  r -= life.score
                  if r <= 0:
                        return life
 
            raise Exception("选择错误", self.bounds)
 
 
      def newChild(self):
            """产生新后的"""
            parent1 = self
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值