user3046679
user3046679

Reputation: 91

Restricted Tournament Selection

I need to implement a restricted tournament selection. This method consists of comparing each offspring individual with a random group of individuals. Select the one that most resembles the offspring individual and choose the best of the two to be inserted into the new population.

I have all the operators implemented but I don't know how to do that:

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 = actual population

self.levenshtein = is distance between two strings with different length

Upvotes: 0

Views: 606

Answers (1)

seaotternerd
seaotternerd

Reputation: 6419

So, distancia gives you the distance between hijo and a given member of the population. The individual most similar to the offspring will be the one with the lowest distance. The simplest way to do that is to initialize variables before the inner for-loop to keep track of the most similar seen so far. Then you'll just need to compare the fitness of the most similar individual to the fitness of hijo and put the most fit into the next generation. Here's some psuedo-ish code for doing that:

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

Upvotes: 0

Related Questions