Minor edits
This commit is contained in:
@ -19,6 +19,8 @@ from mutation import Mutation_Methods
|
||||
from crossover import Crossover_Methods
|
||||
|
||||
class attributes:
|
||||
"""SAMPLE TEXT"""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the GA."""
|
||||
|
||||
@ -55,7 +57,7 @@ class attributes:
|
||||
# Methods for accomplishing Parent-Selection -> Crossover -> Survivor_Selection -> Mutation
|
||||
self.parent_selection_impl = Parent_Selection.Rank.tournament
|
||||
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.mutation_individual_impl = Mutation_Methods.Individual.single_gene
|
||||
self.mutation_population_impl = Mutation_Methods.Population.random_selection
|
||||
@ -67,6 +69,7 @@ class attributes:
|
||||
# Getter and setters for all varibles
|
||||
@property
|
||||
def chromosome_length(self):
|
||||
"""SAMPLE TEXT"""
|
||||
return self._chromosome_length
|
||||
|
||||
@chromosome_length.setter
|
||||
|
||||
@ -3,7 +3,7 @@ import random
|
||||
class Crossover_Methods:
|
||||
|
||||
class Population:
|
||||
"""Methods for selecting chromosomes to crossover"""
|
||||
"""Methods for selecting chromosomes to crossover."""
|
||||
|
||||
def sequential_selection(ga):
|
||||
"""Select sequential pairs from the mating pool.
|
||||
@ -41,22 +41,22 @@ class Crossover_Methods:
|
||||
|
||||
|
||||
class Individual:
|
||||
"""Methods for crossing parents"""
|
||||
"""Methods for crossing parents."""
|
||||
|
||||
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)
|
||||
return ga.make_chromosome(parent_one.get_gene_list()[:index] + parent_two.get_gene_list()[index:])
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
random.choice([ # by selecting random genes from
|
||||
@ -66,10 +66,10 @@ class Crossover_Methods:
|
||||
for i in range(parent_one.size())]) # for each gene
|
||||
|
||||
class Arithmetic:
|
||||
"""Crossover methods for numerical genes"""
|
||||
"""Crossover methods for numerical genes."""
|
||||
|
||||
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
|
||||
ga.make_gene( # filled with new genes
|
||||
@ -81,7 +81,7 @@ class Crossover_Methods:
|
||||
|
||||
|
||||
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
|
||||
weight = 0.25
|
||||
@ -95,7 +95,7 @@ class Crossover_Methods:
|
||||
|
||||
|
||||
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
|
||||
ga.make_gene( # filled with new genes
|
||||
@ -108,7 +108,7 @@ class Crossover_Methods:
|
||||
|
||||
|
||||
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
|
||||
weight = 0.25
|
||||
|
||||
@ -55,7 +55,8 @@ class Parent_Selection:
|
||||
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
|
||||
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
|
||||
ga.population.sort_by_best_fitness(ga)
|
||||
|
||||
Reference in New Issue
Block a user