2013-11-28 69 views
0

我需要实施限制锦标赛选择。这种方法包括比较每个后代个体与随机组个体。选择最相似于后代个体的那个,并选择插入到新种群中的最好的两个。受限制的锦标赛选择

我所有的运营商实现,但我不知道该怎么做:

def reduccion(self, hijos): 

    for hijo in hijos: 
     torneo = random.sample(self.generacion, 5) 
     for i in range(0,len(torneo)): 
      distancia=self.levenshtein(hijo.fenotipo, torneo[i].fenotipo) 
      print(distancia) 

self.generacion =实际人口

self.levenshtein =是两个字符串不同长度之间的距离

回答

0

因此,distancia为您提供了hijo和给定的人口成员之间的距离。与后代最相似的人是距离最近的人。最简单的方法是在内部for-loop之前初始化变量,以跟踪迄今为止所看到的最类似的变量。那么你只需要比较最相似个体的适应度与hijo的适应度,并将其最适合下一代。这里有一些伪代码:

def reduccion(self, hijos): 

    for hijo in hijos: 
     torneo = random.sample(self.generacion, 5) 

     mostSimilar = torneo[0] #Initialize this to be the 
           #first population member in the tournament 
     lowestDistance = self.levenshtein(hijo.fenotipo, mostSimilar.fenotipo) 

     for i in range(1,len(torneo)): #now we've already looked at #1 
      distancia=self.levenshtein(hijo.fenotipo, torneo[i].fenotipo) 

      if distancia < lowestDistance: #see if this distance is lower 
       lowestDistance = distancia #than what we've seen before 
       mostSimilar = torneo[i] 

     if mostSimilar.fitness < hijo.fitness: #compare the fitnesses 
      add hijo to next generation 
     else: 
      add mostSimilar to next generation