Skip to content

Commit 74536c7

Browse files
committed
Support passing classes
1 parent 19250a9 commit 74536c7

File tree

4 files changed

+381
-85
lines changed

4 files changed

+381
-85
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import pygad
2+
import numpy
3+
4+
"""
5+
Use a method to build the lifecycle.
6+
"""
7+
8+
class Fitness:
9+
def __call__(self, ga_instance, solution, solution_idx):
10+
fitness = numpy.sum(solution)
11+
return fitness
12+
13+
class Crossover:
14+
def __call__(self, parents, offspring_size, ga_instance):
15+
return numpy.random.rand(offspring_size[0], offspring_size[1])
16+
17+
class Mutation:
18+
def __call__(self, offspring, ga_instance):
19+
return offspring
20+
21+
class OnStart:
22+
def __call__(self, ga_instance):
23+
print("on_start")
24+
25+
class OnFitness:
26+
def __call__(self, ga_instance, fitness):
27+
print("on_fitness")
28+
29+
class OnCrossover:
30+
def __call__(self, ga_instance, offspring):
31+
print("on_crossover")
32+
33+
class OnMutation:
34+
def __call__(self, ga_instance, offspring):
35+
print("on_mutation")
36+
37+
class OnParents:
38+
def __call__(self, ga_instance, parents):
39+
print("on_parents")
40+
41+
class OnGeneration:
42+
def __call__(self, ga_instance):
43+
print("on_generation")
44+
45+
class OnStop:
46+
def __call__(self, ga_instance, fitness):
47+
print("on_stop")
48+
49+
num_generations = 10 # Number of generations.
50+
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
51+
52+
sol_per_pop = 10 # Number of solutions in the population.
53+
num_genes = 5
54+
55+
ga_instance = pygad.GA(num_generations=num_generations,
56+
num_parents_mating=num_parents_mating,
57+
sol_per_pop=sol_per_pop,
58+
num_genes=num_genes,
59+
60+
fitness_func=Fitness(),
61+
62+
crossover_type=Crossover(),
63+
mutation_type=Mutation(),
64+
65+
on_start=OnStart(),
66+
on_fitness=OnFitness(),
67+
on_crossover=OnCrossover(),
68+
on_mutation=OnMutation(),
69+
on_parents=OnParents(),
70+
on_generation=OnGeneration(),
71+
on_stop=OnStop(),
72+
73+
suppress_warnings=True)
74+
75+
# Running the GA to optimize the parameters of the function.
76+
ga_instance.run()
77+
78+
ga_instance.plot_fitness()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import pygad
2+
import numpy
3+
4+
"""
5+
Use a method to build the lifecycle.
6+
"""
7+
8+
class GAOperations:
9+
def fitness_func(self, ga_instance, solution, solution_idx):
10+
fitness = numpy.sum(solution)
11+
return fitness
12+
13+
def crossover(self, parents, offspring_size, ga_instance):
14+
return numpy.random.rand(offspring_size[0], offspring_size[1])
15+
16+
def mutation(self, offspring, ga_instance):
17+
return offspring
18+
19+
class Lifecycle:
20+
def on_start(self, ga_instance):
21+
print("on_start")
22+
23+
def on_fitness(self, ga_instance, fitness):
24+
print("on_fitness")
25+
26+
def on_crossover(self, ga_instance, offspring):
27+
print("on_crossover")
28+
29+
def on_mutation(self, ga_instance, offspring):
30+
print("on_mutation")
31+
32+
def on_parents(self, ga_instance, parents):
33+
print("on_parents")
34+
35+
def on_generation(self, ga_instance):
36+
print("on_generation")
37+
38+
def on_stop(self, ga_instance, fitness):
39+
print("on_stop")
40+
41+
ga_obj = GAOperations()
42+
lifecycle_obj = Lifecycle()
43+
44+
num_generations = 10 # Number of generations.
45+
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
46+
47+
sol_per_pop = 10 # Number of solutions in the population.
48+
num_genes = 5
49+
50+
ga_instance = pygad.GA(num_generations=num_generations,
51+
num_parents_mating=num_parents_mating,
52+
sol_per_pop=sol_per_pop,
53+
num_genes=num_genes,
54+
55+
fitness_func=ga_obj.fitness_func,
56+
57+
crossover_type=ga_obj.crossover,
58+
mutation_type=ga_obj.mutation,
59+
60+
on_start=lifecycle_obj.on_start,
61+
on_fitness=lifecycle_obj.on_fitness,
62+
on_crossover=lifecycle_obj.on_crossover,
63+
on_mutation=lifecycle_obj.on_mutation,
64+
on_parents=lifecycle_obj.on_parents,
65+
on_generation=lifecycle_obj.on_generation,
66+
on_stop=lifecycle_obj.on_stop,
67+
68+
suppress_warnings=True)
69+
70+
# Running the GA to optimize the parameters of the function.
71+
ga_instance.run()
72+
73+
ga_instance.plot_fitness()

examples/pygad_lifecycle.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ def on_stop(ga_instance, last_population_fitness):
3737
fitness_func=fitness_function,
3838
sol_per_pop=10,
3939
num_genes=len(function_inputs),
40+
4041
on_start=on_start,
4142
on_fitness=on_fitness,
4243
on_parents=on_parents,
4344
on_crossover=on_crossover,
4445
on_mutation=on_mutation,
4546
on_generation=on_generation,
46-
on_stop=on_stop)
47+
on_stop=on_stop,
48+
49+
suppress_warnings=True)
4750

4851
ga_instance.run()

0 commit comments

Comments
 (0)