GeneticAlgorithmPython icon indicating copy to clipboard operation
GeneticAlgorithmPython copied to clipboard

Enhancing stop_criteria for Multi-Objective Optimization

Open Scyneo opened this issue 1 year ago • 1 comments

In the current PyGAD implementation, the stop_criteria parameter references the fitness function's return value to determine when the desired fitness has been achieved. For example:

def fitness_func(ga_instance, solution, solution_idx):
    output = numpy.sum(solution * equation_inputs)

    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)

    return fitness

ga_instance = pygad.GA(
    num_generations=200,
    sol_per_pop=10,
    num_parents_mating=4,
    num_genes=len(equation_inputs),
    fitness_func=fitness_func,
    stop_criteria=["reach_127.4", "saturate_15"])

In this case, the reach_127.4 is pretty clear with what it does. However, the docs do not specify how stop_criteria works in the context of multi-objective optimization, where the fitness function returns multiple values like here:

def fitness_func(ga_instance, solution, solution_idx):
    ...
    return [fitness1, fitness2, ..., fitnessN]

From a quick look at the code I deduced that pygad actually supports it, but it's just not mentioned in the docs. So if I'm correct something like this works just fine for multi-objective optimization:

ga_instance = pygad.GA(
    num_generations=200,
    sol_per_pop=10,
    num_parents_mating=4,
    num_genes=len(equation_inputs),
    fitness_func=fitness_func,
    stop_criteria=["reach_127.4", "reach_130.1", "saturate_15"])

If that's the case, it would be great to have docs updated to reflect this mechanism

Scyneo avatar Jan 23 '25 23:01 Scyneo

Thanks so much!

The documentation is updated and some more bugs are fixed: https://github.com/ahmedfgad/GeneticAlgorithmPython/commit/fd0e18f8fc79b8dd2df49420be8f70ffef1c7483

Will be available in the next release!

ahmedfgad avatar Feb 06 '25 20:02 ahmedfgad