Minor edits

This commit is contained in:
SimpleArt
2020-10-25 22:36:54 -04:00
parent 3625453a28
commit ae74ce1766
3 changed files with 16 additions and 12 deletions

View File

@ -19,6 +19,8 @@ from mutation import Mutation_Methods
from crossover import Crossover_Methods from crossover import Crossover_Methods
class attributes: class attributes:
"""SAMPLE TEXT"""
def __init__(self): def __init__(self):
"""Initialize the GA.""" """Initialize the GA."""
@ -55,7 +57,7 @@ class attributes:
# Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation # Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation
self.parent_selection_impl = Parent_Selection.Rank.tournament self.parent_selection_impl = Parent_Selection.Rank.tournament
self.crossover_individual_impl = Crossover_Methods.Individual.single_point self.crossover_individual_impl = Crossover_Methods.Individual.single_point
self.crossover_population_impl = Crossover_Methods.Population.random_selection self.crossover_population_impl = Crossover_Methods.Population.sequential_selection
self.survivor_selection_impl = Survivor_Selection.fill_in_best self.survivor_selection_impl = Survivor_Selection.fill_in_best
self.mutation_individual_impl = Mutation_Methods.Individual.single_gene self.mutation_individual_impl = Mutation_Methods.Individual.single_gene
self.mutation_population_impl = Mutation_Methods.Population.random_selection self.mutation_population_impl = Mutation_Methods.Population.random_selection
@ -67,6 +69,7 @@ class attributes:
# Getter and setters for all varibles # Getter and setters for all varibles
@property @property
def chromosome_length(self): def chromosome_length(self):
"""SAMPLE TEXT"""
return self._chromosome_length return self._chromosome_length
@chromosome_length.setter @chromosome_length.setter

View File

@ -3,7 +3,7 @@ import random
class Crossover_Methods: class Crossover_Methods:
class Population: class Population:
"""Methods for selecting chromosomes to crossover""" """Methods for selecting chromosomes to crossover."""
def sequential_selection(ga): def sequential_selection(ga):
"""Select sequential pairs from the mating pool. """Select sequential pairs from the mating pool.
@ -41,22 +41,22 @@ class Crossover_Methods:
class Individual: class Individual:
"""Methods for crossing parents""" """Methods for crossing parents."""
def single_point(ga, parent_one, parent_two): def single_point(ga, parent_one, parent_two):
"""Cross two parents by swapping genes at one random point""" """Cross two parents by swapping genes at one random point."""
index = random.randint(0, parent_one.size()-1) index = random.randint(0, parent_one.size()-1)
return ga.make_chromosome(parent_one.get_gene_list()[:index] + parent_two.get_gene_list()[index:]) return ga.make_chromosome(parent_one.get_gene_list()[:index] + parent_two.get_gene_list()[index:])
def multi_point(ga, parent_one, parent_two): def multi_point(ga, parent_one, parent_two):
"""Cross two parents by swapping genes at multiple points""" """Cross two parents by swapping genes at multiple points."""
pass pass
def uniform(ga, parent_one, parent_two): def uniform(ga, parent_one, parent_two):
"""Cross two parents by swapping all genes randomly""" """Cross two parents by swapping all genes randomly."""
return ga.make_chromosome([ # Make a new chromosome return ga.make_chromosome([ # Make a new chromosome
random.choice([ # by selecting random genes from random.choice([ # by selecting random genes from
@ -66,10 +66,10 @@ class Crossover_Methods:
for i in range(parent_one.size())]) # for each gene for i in range(parent_one.size())]) # for each gene
class Arithmetic: class Arithmetic:
"""Crossover methods for numerical genes""" """Crossover methods for numerical genes."""
def int_random(ga, parent_one, parent_two): def int_random(ga, parent_one, parent_two):
"""Cross two parents by taking a random integer value between each of the genes""" """Cross two parents by taking a random integer value between each of the genes."""
return ga.make_chromosome([ # Make a new chromosome return ga.make_chromosome([ # Make a new chromosome
ga.make_gene( # filled with new genes ga.make_gene( # filled with new genes
@ -81,7 +81,7 @@ class Crossover_Methods:
def int_weighted(ga, parent_one, parent_two): def int_weighted(ga, parent_one, parent_two):
"""Cross two parents by taking a a weighted average of the genes""" """Cross two parents by taking a a weighted average of the genes."""
# the percentage of genes taken from the first gene # the percentage of genes taken from the first gene
weight = 0.25 weight = 0.25
@ -95,7 +95,7 @@ class Crossover_Methods:
def float_random(ga, parent_one, parent_two): def float_random(ga, parent_one, parent_two):
"""Cross two parents by taking a random numeric value between each of the genes""" """Cross two parents by taking a random numeric value between each of the genes."""
return ga.make_chromosome([ # Make a new chromosome return ga.make_chromosome([ # Make a new chromosome
ga.make_gene( # filled with new genes ga.make_gene( # filled with new genes
@ -108,7 +108,7 @@ class Crossover_Methods:
def float_weighted(ga, parent_one, parent_two): def float_weighted(ga, parent_one, parent_two):
"""Cross two parents by taking a a weighted average of the genes""" """Cross two parents by taking a a weighted average of the genes."""
# the percentage of genes taken from the first gene # the percentage of genes taken from the first gene
weight = 0.25 weight = 0.25

View File

@ -55,7 +55,8 @@ class Parent_Selection:
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.
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.
"""
# Make sure the population is sorted by fitness # Make sure the population is sorted by fitness
ga.population.sort_by_best_fitness(ga) ga.population.sort_by_best_fitness(ga)