updated all code to use .size() methods
This commit is contained in:
@ -19,7 +19,7 @@ class Fitness_Examples:
|
|||||||
"""Test of the GA's ability to improve fitness when the value is index-dependent"""
|
"""Test of the GA's ability to improve fitness when the value is index-dependent"""
|
||||||
"""If a gene is equal to its index in the chromosome + 1, fitness is incremented"""
|
"""If a gene is equal to its index in the chromosome + 1, fitness is incremented"""
|
||||||
fitness = 0
|
fitness = 0
|
||||||
for i in range(len(chromosome.gene_list)):
|
for i in range(chromosome.size()):
|
||||||
if (chromosome.gene_list[i].value == i+1):
|
if (chromosome.gene_list[i].value == i+1):
|
||||||
fitness += 1
|
fitness += 1
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,10 @@ class Chromosome:
|
|||||||
# If the chromosome has been selected then the flag would switch to true
|
# If the chromosome has been selected then the flag would switch to true
|
||||||
self.selected = False
|
self.selected = False
|
||||||
|
|
||||||
|
def size(self):
|
||||||
|
"""Returns the number of genes in the chromosome"""
|
||||||
|
return len(self.gene_list)
|
||||||
|
|
||||||
def add_gene(self, gene, index = -1):
|
def add_gene(self, gene, index = -1):
|
||||||
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
|
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
|
||||||
if index == -1:
|
if index == -1:
|
||||||
|
|||||||
@ -1,13 +0,0 @@
|
|||||||
# Imported library
|
|
||||||
import random
|
|
||||||
|
|
||||||
def check_values(low,high):
|
|
||||||
#Check to make sure its not less then zero
|
|
||||||
assert low > 0 , "The random gene low can not be less then zero"
|
|
||||||
# Check to make sure the high value is not
|
|
||||||
# lower than or equal to low and not 0.
|
|
||||||
assert high > low , "High value can not be smaller then low value"
|
|
||||||
assert high != 0, "High value can not be zero"
|
|
||||||
|
|
||||||
def random_gene():
|
|
||||||
return random.randint(1,100)
|
|
||||||
@ -12,14 +12,14 @@ class Parent_Selection:
|
|||||||
The total number of parents selected is determined by parent_ratio, an attribute to the GA object.
|
The total number of parents selected is determined by parent_ratio, an attribute to the GA object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tournament_size = int(len(ga.population.get_all_chromosomes())*ga.tournament_size_ratio)
|
tournament_size = int(ga.population.size()*ga.tournament_size_ratio)
|
||||||
if tournament_size < 5:
|
if tournament_size < 5:
|
||||||
tournament_size = 5
|
tournament_size = 5
|
||||||
# Probability used for determining if a chromosome should enter the mating pool.
|
# Probability used for determining if a chromosome should enter the mating pool.
|
||||||
selection_probability = ga.selection_probability
|
selection_probability = ga.selection_probability
|
||||||
|
|
||||||
# Repeat tournaments until the mating pool is large enough.
|
# Repeat tournaments until the mating pool is large enough.
|
||||||
while (len(ga.population.mating_pool) < len(ga.population.get_all_chromosomes())*ga.parent_ratio):
|
while (len(ga.population.mating_pool) < ga.population.size()*ga.parent_ratio):
|
||||||
# Generate a random tournament group and sort by fitness.
|
# Generate a random tournament group and sort by fitness.
|
||||||
tournament_group = ga.sort_by_best_fitness([random.choice(ga.population.get_all_chromosomes()) for n in range(tournament_size)])
|
tournament_group = ga.sort_by_best_fitness([random.choice(ga.population.get_all_chromosomes()) for n in range(tournament_size)])
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ class Parent_Selection:
|
|||||||
Where the chromosomes are the numbers to be selected and the board size for
|
Where the chromosomes are the numbers to be selected and the board size for
|
||||||
those numbers are directly proportional to the chromosome's current fitness. Where
|
those numbers are directly proportional to the chromosome's current fitness. Where
|
||||||
the ball falls is a randomly generated number between 0 and 1"""
|
the ball falls is a randomly generated number between 0 and 1"""
|
||||||
total_fitness = sum(ga.population.chromosome_list[i].get_fitness() for i in range(len(ga.population.chromosome_list)))
|
total_fitness = sum(ga.population.chromosome_list[i].get_fitness() for i in range(ga.population.size()))
|
||||||
rel_fitnesses = []
|
rel_fitnesses = []
|
||||||
|
|
||||||
for chromosome in ga.population.chromosome_list:
|
for chromosome in ga.population.chromosome_list:
|
||||||
@ -50,7 +50,7 @@ class Parent_Selection:
|
|||||||
|
|
||||||
probability = [sum(rel_fitnesses[:i+1]) for i in range(len(rel_fitnesses))]
|
probability = [sum(rel_fitnesses[:i+1]) for i in range(len(rel_fitnesses))]
|
||||||
|
|
||||||
while (len(ga.population.mating_pool) < len(ga.population.get_all_chromosomes())*ga.parent_ratio):
|
while (len(ga.population.mating_pool) < ga.population.size()*ga.parent_ratio):
|
||||||
rand_number = random.random()
|
rand_number = random.random()
|
||||||
|
|
||||||
# Loop through the list of probabilities
|
# Loop through the list of probabilities
|
||||||
|
|||||||
@ -3,19 +3,15 @@ class Termination_Methods:
|
|||||||
|
|
||||||
def fitness_based(ga):
|
def fitness_based(ga):
|
||||||
"""Fitness based approach to terminate when the goal fitness has been reached"""
|
"""Fitness based approach to terminate when the goal fitness has been reached"""
|
||||||
|
|
||||||
status = True
|
|
||||||
if ga.population == None:
|
if ga.population == None:
|
||||||
return status
|
return True
|
||||||
for i in range(len(ga.population.get_all_chromosomes())):
|
for i in range(ga.population.size()):
|
||||||
if(ga.population.get_all_chromosomes()[i].fitness >= ga.fitness_goal):
|
if(ga.population.get_all_chromosomes()[i].fitness >= ga.fitness_goal):
|
||||||
status = False
|
return False
|
||||||
break
|
return True
|
||||||
return status
|
|
||||||
|
|
||||||
def generation_based(ga):
|
def generation_based(ga):
|
||||||
"""Generation based approach to terminate when the goal generation has been reached"""
|
"""Generation based approach to terminate when the goal generation has been reached"""
|
||||||
status = True
|
|
||||||
if(ga.current_generation > ga.generation_goal):
|
return ga.current_generation < ga.generation_goal
|
||||||
status = False
|
|
||||||
return status
|
|
||||||
|
|||||||
Reference in New Issue
Block a user