Skip to content

Commit 14cc3cc

Browse files
authored
PyGAD 2.6.0
1 parent 5be4751 commit 14cc3cc

File tree

2 files changed

+262
-23
lines changed

2 files changed

+262
-23
lines changed

README.md

+85
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,91 @@ If you built a project that uses PyGAD, then please drop an e-mail to ahmed.f.ga
6060

6161
Please check the **Contact Us** section for more contact details.
6262

63+
# Life Cycle of PyGAD
64+
65+
The next figure lists the different stages in the lifecycle of an instance of the `pygad.GA` class. Note that PyGAD stops when either all generations are completed or when the function passed to the `on_generation` parameter returns the string `stop`.
66+
67+
![PyGAD Lifecycle](https://user-images.githubusercontent.com/16560492/89446279-9c6f8380-d754-11ea-83fd-a60ea2f53b85.jpg)
68+
69+
The next code implements all the callback functions to trace the execution of the genetic algorithm. Each callback function prints its name.
70+
71+
```python
72+
import pygad
73+
import numpy
74+
75+
function_inputs = [4,-2,3.5,5,-11,-4.7]
76+
desired_output = 44
77+
78+
def fitness_func(solution, solution_idx):
79+
output = numpy.sum(solution*function_inputs)
80+
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
81+
return fitness
82+
83+
fitness_function = fitness_func
84+
85+
def on_start(ga_instance):
86+
print("on_start()")
87+
88+
def on_fitness(ga_instance, population_fitness):
89+
print("on_fitness()")
90+
91+
def on_parents(ga_instance, selected_parents):
92+
print("on_parents()")
93+
94+
def on_crossover(ga_instance, offspring_crossover):
95+
print("on_crossover()")
96+
97+
def on_mutation(ga_instance, offspring_mutation):
98+
print("on_mutation()")
99+
100+
def on_generation(ga_instance):
101+
print("on_generation()")
102+
103+
def on_stop(ga_instance, last_population_fitness):
104+
print("on_stop()")
105+
106+
ga_instance = pygad.GA(num_generations=3,
107+
num_parents_mating=5,
108+
fitness_func=fitness_function,
109+
sol_per_pop=10,
110+
num_genes=len(function_inputs),
111+
on_start=on_start,
112+
on_fitness=on_fitness,
113+
on_parents=on_parents,
114+
on_crossover=on_crossover,
115+
on_mutation=on_mutation,
116+
on_generation=on_generation,
117+
on_stop=on_stop)
118+
119+
ga_instance.run()
120+
```
121+
122+
Based on the used 3 generations as assigned to the `num_generations` argument, here is the output.
123+
124+
```
125+
on_start()
126+
127+
on_fitness()
128+
on_parents()
129+
on_crossover()
130+
on_mutation()
131+
on_generation()
132+
133+
on_fitness()
134+
on_parents()
135+
on_crossover()
136+
on_mutation()
137+
on_generation()
138+
139+
on_fitness()
140+
on_parents()
141+
on_crossover()
142+
on_mutation()
143+
on_generation()
144+
145+
on_stop()
146+
```
147+
63148
# Example
64149

65150
Check the [PyGAD's documentation](https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html) for information about the implementation of this example.

0 commit comments

Comments
 (0)