Added save_data attribute to be able to turn off saving the population to the database.
This commit is contained in:
@ -84,7 +84,8 @@ class GA(Attributes):
|
|||||||
self.sort_by_best_fitness()
|
self.sort_by_best_fitness()
|
||||||
|
|
||||||
# Save the population to the database
|
# Save the population to the database
|
||||||
self.save_population()
|
if self.save_data == True:
|
||||||
|
self.save_population()
|
||||||
|
|
||||||
# Adapt the ga if the generation times the adapt rate
|
# Adapt the ga if the generation times the adapt rate
|
||||||
# passes through an integer value.
|
# passes through an integer value.
|
||||||
|
|||||||
@ -99,6 +99,7 @@ class Attributes:
|
|||||||
population = None,
|
population = None,
|
||||||
target_fitness_type = 'max',
|
target_fitness_type = 'max',
|
||||||
update_fitness = False,
|
update_fitness = False,
|
||||||
|
save_data = True,
|
||||||
|
|
||||||
parent_ratio = 0.10,
|
parent_ratio = 0.10,
|
||||||
selection_probability = 0.50,
|
selection_probability = 0.50,
|
||||||
@ -151,6 +152,7 @@ class Attributes:
|
|||||||
self.population = population
|
self.population = population
|
||||||
self.target_fitness_type = target_fitness_type
|
self.target_fitness_type = target_fitness_type
|
||||||
self.update_fitness = update_fitness
|
self.update_fitness = update_fitness
|
||||||
|
self.save_data = save_data
|
||||||
|
|
||||||
# Selection variables
|
# Selection variables
|
||||||
self.parent_ratio = parent_ratio
|
self.parent_ratio = parent_ratio
|
||||||
|
|||||||
@ -1,18 +1,33 @@
|
|||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
import EasyGA
|
import EasyGA
|
||||||
|
import random
|
||||||
|
|
||||||
for _ in range(2):
|
# Create the Genetic algorithm
|
||||||
# Create a new genetic algorithm each.
|
ga = EasyGA.GA()
|
||||||
ga = EasyGA.GA()
|
|
||||||
ga.evolve()
|
|
||||||
ga.print_population()
|
|
||||||
|
|
||||||
# Graph the average of the two runs
|
ga.save_data = False
|
||||||
plt.subplot(1, 2, 1)
|
|
||||||
ga.graph.highest_value_chromosome("average")
|
|
||||||
|
|
||||||
plt.subplot(1, 2, 1)
|
def is_it_5(chromosome):
|
||||||
ga.graph.highest_value_chromosome("all")
|
"""A very simple case test function - If the chromosomes gene value is a 5 add one
|
||||||
|
to the chromosomes overall fitness value."""
|
||||||
|
# Overall fitness value
|
||||||
|
fitness = 0
|
||||||
|
# For each gene in the chromosome
|
||||||
|
for gene in chromosome.gene_list:
|
||||||
|
# Check if its value = 5
|
||||||
|
if(gene.value == 5):
|
||||||
|
# If its value is 5 then add one to
|
||||||
|
# the overal fitness of the chromosome.
|
||||||
|
fitness += 1
|
||||||
|
|
||||||
ga.graph.show()
|
return fitness
|
||||||
|
|
||||||
|
ga.fitness_function_impl = is_it_5
|
||||||
|
|
||||||
|
# Create random genes from 0 to 10
|
||||||
|
ga.gene_impl = lambda: random.randint(0, 10)
|
||||||
|
|
||||||
|
ga.evolve()
|
||||||
|
|
||||||
|
# Print your default genetic algorithm
|
||||||
|
ga.print_generation()
|
||||||
|
ga.print_population()
|
||||||
|
|||||||
Reference in New Issue
Block a user