# Life Cycle of PyGAD The next figure shows the main steps in the life cycle of a `pygad.GA` instance. The genetic algorithm starts from an initial population and repeats the same steps once per generation. It measures the fitness of every solution, selects the parents, applies crossover and mutation to make offspring, and then builds the next generation. PyGAD stops when all generations are done or when the function passed to the `on_generation` parameter returns the string `stop`. :::{figure} images/ga_lifecycle.* :alt: The PyGAD genetic algorithm life cycle :width: 480px :align: center The main steps of the genetic algorithm in PyGAD. ::: The next figure shows the same life cycle in more detail, including the callback functions that PyGAD calls at each stage. ![PyGAD Lifecycle](https://user-images.githubusercontent.com/16560492/220486073-c5b6089d-81e4-44d9-a53c-385f479a7273.jpg) The next code implements all the callback functions to trace the execution of the genetic algorithm. Each callback function prints its name. ```python import pygad import numpy function_inputs = [4,-2,3.5,5,-11,-4.7] desired_output = 44 def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution*function_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness fitness_function = fitness_func def on_start(ga_instance): print("on_start()") def on_fitness(ga_instance, population_fitness): print("on_fitness()") def on_parents(ga_instance, selected_parents): print("on_parents()") def on_crossover(ga_instance, offspring_crossover): print("on_crossover()") def on_mutation(ga_instance, offspring_mutation): print("on_mutation()") def on_generation(ga_instance): print("on_generation()") def on_stop(ga_instance, last_population_fitness): print("on_stop()") ga_instance = pygad.GA(num_generations=3, num_parents_mating=5, fitness_func=fitness_function, sol_per_pop=10, num_genes=len(function_inputs), on_start=on_start, on_fitness=on_fitness, on_parents=on_parents, on_crossover=on_crossover, on_mutation=on_mutation, on_generation=on_generation, on_stop=on_stop) ga_instance.run() ``` Based on the used 3 generations as assigned to the `num_generations` argument, here is the output. ``` on_start() on_fitness() on_parents() on_crossover() on_mutation() on_generation() on_fitness() on_parents() on_crossover() on_mutation() on_generation() on_fitness() on_parents() on_crossover() on_mutation() on_generation() on_stop() ```