Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:46:14+00:00 2026-06-17T16:46:14+00:00

I am trying to run a genetic algorithm that i wrote in python. Unfortunately

  • 0

I am trying to run a genetic algorithm that i wrote in python. Unfortunately when there is a mutation the fittest solution can be worse that the fittest solution of the previous generation despite using elitism to pass the fittest solution from the previous generation to the new one. Like so:

There are 86825 generations left
Invalid count is:  0
The fittest individual has a fitness of 16.9094. 
The least fit individual has a fitness  of 36.6535
*******************************************************************************
mutation on 107
There are 86824 generations left
Invalid count is:  3
The fittest individual has a fitness of 19.8637. 
The least fit individual has a fitness of 1.1618e+09

I have tried to implement elitism which I thought would avoid this happening but it still happens. My algorithm executes in this order:

NUM_GEN = 100000
print "Running Genetic Algorithm for %d generations." % NUM_GEN
gen_count = 0
while gen_count < NUM_GEN:
    #genetic.add_fitness_key()
    genetic.add_fitness_fast()
    fittest_list = np.append(fittest_list, genetic.fittest_fitness)
    least_fit_list = np.append(least_fit_list, genetic.least_fitness)
    genetic.sort_pop()
    genetic.make_new_pop()
    genetic.elitism()
    genetic.population = genetic.new_pop
    print "There are %g generations left" %(NUM_GEN-gen_count) 
    gen_count+=1

And the functions that are called are these:

def select_parent_from_tournament(self):
    x = random.randint(0, 19)
    player1 = self.population[x]
    y = random.randint(0, 19)
    player2 = self.population[y]
    if player1['fitness'] <= player2['fitness']:
        parent = player1['chrom_list']
    else:
        parent = player2['chrom_list']
    return parent

def crossover(self):
    crossover_point = random.randint(0, self.chromosome_size)*(self.string_length)
    parent1 = self.select_parent_from_tournament()
    parent2 = self.select_parent_from_tournament()
    parent1 = self.mutate(parent1)
    parent2 = self.mutate(parent2)
    child1 = parent1[:crossover_point] + parent2[crossover_point:]
    child2 = parent1[crossover_point:] + parent2[:crossover_point]
    return child1, child2

def mutate(self, chromosome):
    for i in range(len(chromosome)):
        if random.random() < self.mutation_rate:
            print 'mutation on %i' % i
            if chromosome[i] =='0':
                chromosome[i] = '1'
            else:
                chromosome[i] = '0'
    return chromosome

def make_new_pop(self):
    self.new_pop = []
    for i in range(10):
        dictionary1= {}
        dictionary2 = {}
        dictionary1['chrom_list'], dictionary2['chrom_list'] = \
        self.crossover()
        self.new_pop = np.append(self.new_pop, [dictionary1, dictionary2])

def elitism(self):
    r = random.randint(0, 19)
    self.new_pop[r] = self.population[0]

So I can’t understand why the fittest solution from the old population isn’t passed to the new population if there is a mutation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:46:15+00:00Added an answer on June 17, 2026 at 4:46 pm

    In your crossover method, you perform the parent1 = self.select_parent_from_tournament() which returns the reference to the list of a chromosome from the original population. After that, you do a mutation which modifies the list (in the original population). Only after mutation you copy the content in a child via child1 = parent1[:crossover_point] + parent2[crossover_point:]. As @Xavier was suggesting, you need to create a physical copy of your element from the original population in mutation().

    In effect, your mutation() method changed the original population.

    As a side note, generally crossover and mutation are two different operations. Also, there are references in the literature suggesting that mutation probability should be kept very low. Otherwise it will keep you from converging.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run the code in there selected answer but can't figure
I'm trying to run multiple functions that connect to a remote site (by network)
Trying to run down an error in my EF datacontext implementation that is yielding
I am trying to run a simple application that uses drool.net. Whenever I execute,
I am trying to profile a ann algorithm written in Java that is implemented
Basically, I'm trying to run the batch file that was copied on the remote
I'm trying run this procedure with generic parameters. If I can't delete because some
I am trying to graph the results of a genetic test that comes in
My problem is that I am trying to run a problem that I coded
I've made a c# class that I'm trying to run in PowerShell with add-Type.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.