8000 Update example.py · Kaoschuks/GeneticAlgorithmPython@3820b0d · GitHub
[go: up one dir, main page]

Skip to content

Commit 3820b0d

Browse files
authored
Update example.py
1 parent 33e92b9 commit 3820b0d

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

example.py

Lines changed: 39 additions & 36 deletions
A61D
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
import pygad
22
import numpy
33

4-
num_generations = 50 # Number of generations.
5-
sol_per_pop = 8 # Number of solutions in the population.
6-
num_parents_mating = 4 # Number of solutions to be selected as parents in the mating pool.
7-
8-
# Parameters of the mutation operation.
9-
mutation_percent_genes = 10 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
10-
mutation_num_genes = None # Number of genes to mutate. If the parameter mutation_num_genes exists, then no need for the parameter mutation_percent_genes.
11-
12-
parent_selection_type = "tournament" # Type of parent selection.
13-
14-
crossover_type = "two_points" # Type of the crossover operator.
15-
16-
mutation_type = "scramble" # Type of the mutation operator.
17-
18-
keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
19-
20-
init_range_low = -2
21-
init_range_high = 5
22-
234
"""
245
Given the following function:
256
y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6
@@ -30,30 +11,52 @@
3011
function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs.
3112
desired_output = 44 # Function output.
3213

33-
num_genes = len(function_inputs)
34-
35-
def fitness_func(solution):
14+
def fitness_func(solution, solution_idx):
3615
# Calculating the fitness value of each solution in the current population.
3716
# The fitness function calulates the sum of products between each input and its corresponding weight.
3817
output = numpy.sum(solution*function_inputs)
3918
fitness = 1.0 / numpy.abs(output - desired_output)
4019
return fitness
4120

21+
fitness_function = fitness_func
22+
23+
num_generations = 50 # Number of generations.
24+
num_parents_mating = 4 # Number of solutions to be selected as parents in the mating pool.
25+
26+
sol_per_pop = 8 # Number of solutions in the population.
27+
num_genes = len(function_inputs)
28+
29+
init_range_low = -2
30+
init_range_high = 5
31+
32+
33+
parent_selection_type = "sss" # Type of parent selection.
34+
keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
35+
36+
crossover_type = "single_point" # Type of the crossover operator.
37+
38+
# Parameters of the mutation operation.
39+
mutation_type = "random" # Type of the mutation operator.
40+
mutation_percent_genes = 10 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
41+
42+
def callback_generation(ga_instance):
43+
print("Generation :", ga_instance.generations_completed)
44+
print("Fitness of the best solution :", ga_instance.best_solution()[1])
45+
4246
# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
43-
ga_instance = pygad.GA(num_generations=num_generations,
44-
sol_per_pop=sol_per_pop,
45-
num_parents_mating=num_parents_mating,
46-
num_genes=num_genes,
47-
fitness_func=fitness_func,
48-
mutation_percent_genes=mutation_percent_genes,
49-
mutation_num_genes=mutation_num_genes,
50-
init_range_low=init_range_low,
51-
init_range_high=init_range_high,
52-
parent_selection_type=parent_selection_type,
53-
crossover_type=crossover_type,
54-
mutation_type=mutation_type,
55-
keep_parents=keep_parents,
56-
K_tournament=3)
47+
ga_instance = pygad.GA(num_generations=num_generations,
48+
num_parents_mating=num_parents_mating,
49+
fitness_func=fitness_function,
50+
sol_per_pop=sol_per_pop,
51+
num_genes=num_genes,
52+
init_range_low=init_range_low,
53+
init_range_high=init_range_high,
54+
parent_selection_type=parent_selection_type,
55+
keep_parents=keep_parents,
56+
crossover_type=crossover_type,
57+
mutation_type=mutation_type,
58+
mutation_percent_genes=mutation_percent_genes,
59+
callback_generation=callback_generation)
5760

5861
# Running the GA to optimize the parameters of the function.
5962
ga_instance.run()

0 commit comments

Comments
 (0)
0