-
-
Notifications
You must be signed in to change notification settings - Fork 489
-
My implementation looks like this:
num_generations = 100
num_parents_mating = 2
sol_per_pop = 5000
num_genes = 10
init_range_low = 0
init_range_high = 1
parent_selection_type = "tournament"
keep_elitism = 50
crossover_type = "single_point"
mutation_type = "random"
mutation_percent_genes = 20
continious_gene_space = {'low' : 0, 'high': 1}
discrete_gene_space_1 = np.linspace(0.3, 2.4, 8)
discrete_gene_space_2 = np.linspace(3, 12, 4)
def check_for_termination(ga_instance):
if ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] >= np.float64(2.0):
return "stop"
def genetic_optimizer(fun):
ga_instance = pygad.GA(num_generations = num_generations,
num_parents_mating = num_parents_mating,
fitness_func = fun,
sol_per_pop = sol_per_pop,
num_genes = num_genes,
init_range_low = init_range_low,
init_range_high = init_range_high,
parent_selection_type = parent_selection_type,
keep_elitism = keep_elitism,
crossover_type = crossover_type,
mutation_type = mutation_type,
mutation_percent_genes = mutation_percent_genes,
gene_space = [continious_gene_space, continious_gene_space, continious_gene_space, continious_gene_space, continious_gene_space, continious_gene_space, continious_gene_space, continious_gene_space, discrete_gene_space_1, discrete_gene_space_2],
on_generation = check_for_termination,
mutation_by_replacement = False,
random_mutation_min_val = -1,
random_mutation_max_val = 1,
save_best_solutions = True)
ga_instance.run()
solution = ga_instance.best_solutions[-1]
solution_fitness = ga_instance.best_solutions_fitness[-1]
num_completed_generations = ga_instance.generations_completed
print('Completed Generation: ' + str(num_completed_generations))
return solution_fitness, solution
I was not able to find a statement in the documentation, on how the mutation effects discrete genes.
Are the genes mutated and then rounded to the next discrete value or is there any other mechanism?
Is it furthermore possible, to define an individual mutation range for each gene similar to the gene_space attribute?
Beta Was this translation helpful? Give feedback.
All reactions
When gene_space
is used, then mutation happens by replacing the gene value by a value randomly selected from the gene space. This happens for both int
and float
data types.
Defining a mutation space is a good feature to be supported. Thanks for your suggestion.
Replies: 2 comments
-
When gene_space
is used, then mutation happens by replacing the gene value by a value randomly selected from the gene space. This happens for both int
and float
data types.
Defining a mutation space is a good feature to be supported. Thanks for your suggestion.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Update: Custom mutation range feature is implemented. It will be supported in the next release. 31a3975
Beta Was this translation helpful? Give feedback.