Added Default for gene impl and setter example in Easy GA
This commit is contained in:
@ -27,7 +27,7 @@ class GA:
|
|||||||
self.chromosome_length = 10
|
self.chromosome_length = 10
|
||||||
self.population_size = 10
|
self.population_size = 10
|
||||||
self.chromosome_impl = None
|
self.chromosome_impl = None
|
||||||
self.gene_impl = None
|
self.gene_impl = lambda: random.randint(1, 10)
|
||||||
self.population = None
|
self.population = None
|
||||||
self.target_fitness_type = 'maximum'
|
self.target_fitness_type = 'maximum'
|
||||||
self.update_fitness = True
|
self.update_fitness = True
|
||||||
@ -136,3 +136,14 @@ class GA:
|
|||||||
return sorted(chromosome_set, # list to be sorted
|
return sorted(chromosome_set, # list to be sorted
|
||||||
key = lambda chromosome: chromosome.get_fitness(), # by fitness
|
key = lambda chromosome: chromosome.get_fitness(), # by fitness
|
||||||
reverse = True) # from highest to lowest fitness
|
reverse = True) # from highest to lowest fitness
|
||||||
|
|
||||||
|
# Example of how the setter error checking will look like
|
||||||
|
@property
|
||||||
|
def chromosome_length(self):
|
||||||
|
return self._chromosome_length
|
||||||
|
|
||||||
|
@chromosome_length.setter
|
||||||
|
def chromosome_length(self, value_input):
|
||||||
|
if(value_input == 0):
|
||||||
|
raise ValueError("Sorry your chromosome length must be greater then 0")
|
||||||
|
self._chromosome_length = value_input
|
||||||
|
|||||||
@ -3,19 +3,10 @@ import EasyGA
|
|||||||
|
|
||||||
# Create the Genetic algorithm
|
# Create the Genetic algorithm
|
||||||
ga = EasyGA.GA()
|
ga = EasyGA.GA()
|
||||||
ga.population_size = 25
|
|
||||||
ga.generation_goal = 100
|
ga.chromosome_length = 0
|
||||||
ga.gene_impl = lambda: random.randint(1, 10)
|
|
||||||
ga.selection_probability = 0.5
|
|
||||||
ga.fitness_function_impl = EasyGA.Fitness_Examples.near_5
|
|
||||||
ga.parent_selection_impl = EasyGA.Parent_Selection.Roulette.stochastic_selection
|
|
||||||
ga.crossover_population_impl = EasyGA.Crossover_Methods.Population.sequential_selection
|
|
||||||
ga.crossover_individual_impl = EasyGA.Crossover_Methods.Individual.Arithmetic.int_random
|
|
||||||
ga.survivor_selection_impl = EasyGA.Survivor_Selection.fill_in_best
|
|
||||||
|
|
||||||
ga.evolve()
|
ga.evolve()
|
||||||
ga.set_all_fitness()
|
|
||||||
ga.population.sort_by_best_fitness(ga)
|
|
||||||
|
|
||||||
print(f"Current Generation: {ga.current_generation}")
|
print(f"Current Generation: {ga.current_generation}")
|
||||||
ga.population.print_all()
|
ga.population.print_all()
|
||||||
|
|||||||
@ -5,12 +5,13 @@ import EasyGA
|
|||||||
# python3 -m pytest
|
# python3 -m pytest
|
||||||
|
|
||||||
def test_chromosome_length():
|
def test_chromosome_length():
|
||||||
ga = EasyGA.GA()
|
for i in range(0,100):
|
||||||
ga.chromosome_length = 100
|
ga = EasyGA.GA()
|
||||||
ga.evolve()
|
ga.chromosome_length = 100
|
||||||
|
ga.evolve()
|
||||||
|
|
||||||
""" Test to see if the actual chromosome length is the same as defined."""
|
""" Test to see if the actual chromosome length is the same as defined."""
|
||||||
assert ga.population.chromosome_list[0].size() == ga.chromosome_length
|
assert ga.population.chromosome_list[0].size() == ga.chromosome_length
|
||||||
|
|
||||||
def test_second():
|
def test_second():
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user