Removed useless import statements

This commit is contained in:
danielwilczak101
2020-10-12 23:13:02 -04:00
parent b8f1de9b52
commit 50e7166ea5
2 changed files with 8 additions and 16 deletions

View File

@ -1,8 +1,4 @@
import random import random
from initialization.chromosome_structure.chromosome import Chromosome as create_chromosome
from initialization.gene_structure.gene import Gene as create_gene
from initialization.population_structure.population import Population
from initialization.chromosome_structure.chromosome import Chromosome
class Parent_Selection: class Parent_Selection:
@ -13,18 +9,18 @@ class Parent_Selection:
Will make tournaments of size tournament_size and choose the winner (best fitness) from the tournament and use it as a parent for the next generation Will make tournaments of size tournament_size and choose the winner (best fitness) from the tournament and use it as a parent for the next generation
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(ga.population.size()*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) < ga.population.size()*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)])
# For each chromosome, add it to the mating pool based on its rank in the tournament. # For each chromosome, add it to the mating pool based on its rank in the tournament.
for index in range(tournament_size): for index in range(tournament_size):
# Probability required is selection_probability * (1-selection_probability) ^ (tournament_size-index+1) # Probability required is selection_probability * (1-selection_probability) ^ (tournament_size-index+1)
@ -45,19 +41,19 @@ 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(ga.population.size())) 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:
if (total_fitness != 0): if (total_fitness != 0):
rel_fitnesses.append(float(chromosome.fitness)/total_fitness) rel_fitnesses.append(float(chromosome.fitness)/total_fitness)
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) < ga.population.size()*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
for i in range(len(probability)): for i in range(len(probability)):
# If the probability is greater than the random_number, then select that chromosome # If the probability is greater than the random_number, then select that chromosome

View File

@ -1,8 +1,4 @@
import random import random
from initialization.chromosome_structure.chromosome import Chromosome as create_chromosome
from initialization.gene_structure.gene import Gene as create_gene
from initialization.population_structure.population import Population
from initialization.chromosome_structure.chromosome import Chromosome
class Survivor_Selection: class Survivor_Selection:
"""Survivor selection determines which individuals should be brought to the next generation""" """Survivor selection determines which individuals should be brought to the next generation"""