selection changes
This commit is contained in:
@ -23,7 +23,7 @@ class GA:
|
|||||||
# Termination varibles
|
# Termination varibles
|
||||||
self.current_generation = 0
|
self.current_generation = 0
|
||||||
self.current_fitness = 0
|
self.current_fitness = 0
|
||||||
self.generation_goal = 3
|
self.generation_goal = 1
|
||||||
self.fitness_goal = 3
|
self.fitness_goal = 3
|
||||||
# Mutation variables
|
# Mutation variables
|
||||||
self.mutation_rate = 0.03
|
self.mutation_rate = 0.03
|
||||||
@ -84,7 +84,7 @@ class GA:
|
|||||||
# First get the fitness of the population
|
# First get the fitness of the population
|
||||||
self.get_population_fitness(self.population.chromosomes)
|
self.get_population_fitness(self.population.chromosomes)
|
||||||
# Selection - Triggers flags in the chromosome if its been selected
|
# Selection - Triggers flags in the chromosome if its been selected
|
||||||
# self.selection_impl(self)
|
self.selection_impl(self)
|
||||||
# Crossover - Takes the flagged chromosomes and crosses there genetic
|
# Crossover - Takes the flagged chromosomes and crosses there genetic
|
||||||
# makup to make new offsprings.
|
# makup to make new offsprings.
|
||||||
# self.crossover_impl(self)
|
# self.crossover_impl(self)
|
||||||
|
|||||||
@ -39,7 +39,7 @@ class population:
|
|||||||
self.chromosomes[index] = chromosome
|
self.chromosomes[index] = chromosome
|
||||||
|
|
||||||
def set_fitness(self, fitness):
|
def set_fitness(self, fitness):
|
||||||
"""Sets the fitness value of the population"""
|
"""Sets the fitness value of the population"""
|
||||||
self.fitness = fitness
|
self.fitness = fitness
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@ -53,4 +53,5 @@ class population:
|
|||||||
print("Current population:")
|
print("Current population:")
|
||||||
for index in range(len(self.chromosomes)):
|
for index in range(len(self.chromosomes)):
|
||||||
print(f'Chromosome - {index} {self.chromosomes[index]}', end = "")
|
print(f'Chromosome - {index} {self.chromosomes[index]}', end = "")
|
||||||
print(f' / Fitness = {self.chromosomes[index].fitness}')
|
print(f' / Fitness = {self.chromosomes[index].fitness}', end = "")
|
||||||
|
print(f' / Selected = {self.chromosomes[index].selected}')
|
||||||
|
|||||||
@ -1,11 +1,59 @@
|
|||||||
class selection_examples:
|
class selection_examples:
|
||||||
"""Selection defintion here"""
|
"""Selection defintion here"""
|
||||||
|
|
||||||
def tournament_selection():
|
"""Tournament selection involves running several "tournaments" among a
|
||||||
""" """
|
few individuals (or "chromosomes")chosen at random from the population.
|
||||||
|
The winner of each tournament (the one with the best fitness) is selected
|
||||||
|
for crossover.
|
||||||
|
|
||||||
|
Ex
|
||||||
|
Chromsome 1----1 wins ------
|
||||||
|
Chromsome 2---- - --1 wins----
|
||||||
|
- -
|
||||||
|
Chromsome 3----3 wins ------ -- 5 Wins --->Chromosome 5 becomes Parent
|
||||||
|
Chromsome 4---- -
|
||||||
|
-
|
||||||
|
Chromsome 5----5 wins ---------5 wins----
|
||||||
|
Chromsome 6----
|
||||||
|
^--Matchs--^
|
||||||
|
"""
|
||||||
|
|
||||||
|
def match_based_tournament(ga, matchs):
|
||||||
|
# User selects how many matchs they want to start with to create one parent.
|
||||||
|
|
||||||
|
# Select two random parents
|
||||||
|
selected_1_index = random.randint(0, len(ga.population.chromosomes) - 1)]
|
||||||
|
selected_2_index = random.randint(0, len(ga.population.chromosomes) - 1)]
|
||||||
|
|
||||||
|
if selected_1_index <= selected_2_index:
|
||||||
|
return parent1
|
||||||
|
else:
|
||||||
|
return parent2
|
||||||
|
|
||||||
|
def small_tournament(ga):
|
||||||
|
""" Small tournament is only one round of tournament. Beat the other
|
||||||
|
randomly selected chromosome and your are selected as a parent.
|
||||||
|
|
||||||
|
Chromosome 1----
|
||||||
|
- 1 wins -> Becomes selected for crossover.
|
||||||
|
Chromosome 2---- -
|
||||||
|
"""
|
||||||
|
# Select two random parents
|
||||||
|
|
||||||
|
selected_1_index = random.randint(0, len(ga.population.chromosomes) - 1)]
|
||||||
|
selected_2_index = random.randint(0, len(ga.population.chromosomes) - 1)]
|
||||||
|
|
||||||
|
if selected_1 <= fitness2:
|
||||||
|
return parent1
|
||||||
|
else:
|
||||||
|
return parent2
|
||||||
|
|
||||||
|
def population_based_tournament(ga):
|
||||||
|
"""The size of the population dictates how many matchs there will be. The
|
||||||
|
greater the size of the population the more matchs there is."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def roulette_selection():
|
def roulette(ga):
|
||||||
"""Roulette selection works based off of how strong the fitness is of the
|
"""Roulette selection works based off of how strong the fitness is of the
|
||||||
chromosomes in the population. The stronger the fitness the higher the probability
|
chromosomes in the population. The stronger the fitness the higher the probability
|
||||||
that it will be selected. Using the example of a casino roulette wheel.
|
that it will be selected. Using the example of a casino roulette wheel.
|
||||||
|
|||||||
Reference in New Issue
Block a user