Test Implementation for selection/crossover/mutation
The current test implementation includes random mutation, single point crossover, and tournament selection. The implementation, in short, is a nested approach. The selection method is the only thing actually called by the GA. Both crossover and mutation occur within the selection method. As long as these three systems all follow a standard input/output system, any implementation we build, as well as any user implementations, will work perfectly. The selection function must take GA as a parameter and output a new population. Crossover takes in GA and outputs a population. Mutation takes a chromosome set and outputs a new chromosome set. Many of the changes in this commit are regarding this test implementation. I have also changed many of the file names from "x_examples" to "x_types" and updated the class names to follow capitalziation standards. I did this because I feel personally like the built-in mutation, crossover, and selection implementations are less "examples" and more just already built implementations to make the code required from the user smaller.
This commit is contained in:
2
src/crossover/__init__.py
Normal file
2
src/crossover/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
# FROM (. means local) file_name IMPORT function_name
|
||||
from .crossover_types import Crossover_Types
|
||||
39
src/crossover/crossover_types.py
Normal file
39
src/crossover/crossover_types.py
Normal file
@ -0,0 +1,39 @@
|
||||
import random
|
||||
from initialization.chromosome_structure.chromosome import Chromosome
|
||||
from initialization.population_structure.population import Population
|
||||
|
||||
class Crossover_Types:
|
||||
""" Crossover explination goes here.
|
||||
|
||||
Points - Defined as sections between the chromosomes genetic makeup
|
||||
"""
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def single_point_crossover(self, ga):
|
||||
"""Single point crossover is when a "point" is selected and the genetic
|
||||
make up of the two parent chromosomes are "Crossed" or better known as swapped"""
|
||||
|
||||
crossover_pool = []
|
||||
for i in range(ga.population_size):
|
||||
if ga.population.get_all_chromosomes()[i].selected:
|
||||
crossover_pool.append(ga.population.get_all_chromosomes()[i])
|
||||
|
||||
new_population = Population()
|
||||
for i in range(len(crossover_pool)):
|
||||
if i + 1 < len(crossover_pool):
|
||||
new_gene_set = []
|
||||
parent_one = crossover_pool[i].get_genes()
|
||||
parent_two = crossover_pool[i+1].get_genes()
|
||||
halfway_point = int(ga.chromosome_length/2)
|
||||
new_gene_set.extend(parent_one[0:halfway_point])
|
||||
new_gene_set.extend(parent_two[halfway_point:])
|
||||
new_chromosome = Chromosome(new_gene_set)
|
||||
new_population.add_chromosome(new_chromosome)
|
||||
|
||||
return new_population
|
||||
|
||||
def multi_point_crossover(self, ga,number_of_points = 2):
|
||||
"""Multi point crossover is when a specific number (More then one) of
|
||||
"points" are created to merge the genetic makup of the chromosomes."""
|
||||
pass
|
||||
Reference in New Issue
Block a user