Code Monkey home page Code Monkey logo

Comments (5)

sunderved avatar sunderved commented on June 11, 2024 1

A full example to reproduce the issue:

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)
    solution_fitness = 1.0 / numpy.abs(output - desired_output)
    print(ga_instance.generations_completed, solution_idx, solution_fitness)    
    return solution_fitness

def callback_generation(ga_instance):
    solution, solution_fitness, solution_idx = ga_instance.best_solution()
    print(' ---> End of generation', ga_instance.generations_completed, ":", solution_fitness)

num_generations = 2
num_parents_mating = 4
sol_per_pop = 10
mutation_percent_genes = 10
keep_parents = 1
num_genes = len(function_inputs)
init_range_low = -2
init_range_high = 5
parent_selection_type = "sss"
crossover_type = "single_point"
mutation_type = "random"

ga_instance = pygad.GA(num_generations=num_generations,
                       num_parents_mating=num_parents_mating,
                       fitness_func=fitness_func,
                       sol_per_pop=sol_per_pop,
                       num_genes=num_genes,
                       init_range_low=init_range_low,
                       init_range_high=init_range_high,
                       parent_selection_type=parent_selection_type,
                       keep_parents=keep_parents,
                       crossover_type=crossover_type,
                       mutation_type=mutation_type,
                       mutation_percent_genes=mutation_percent_genes,
                       on_generation=callback_generation)

ga_instance.run()

solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Parameters of the best solution : {solution}".format(solution=solution))
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))

prediction = numpy.sum(numpy.array(function_inputs)*solution)
print("Predicted output based on the best solution : {prediction}".format(prediction=prediction))

from geneticalgorithmpython.

xino1010 avatar xino1010 commented on June 11, 2024 1

@sunderved how did you resolve this issue? I have a fitness function that takes a lot of time to execute it... it's a lost of time re-execute the same solution

from geneticalgorithmpython.

ahmedfgad avatar ahmedfgad commented on June 11, 2024 1

The reason for this behavior is calling the best_solution() method.

When it is called this way, then it forces the fitness function to be called against the latest generation's population.
solution, solution_fitness, solution_idx = ga_instance.best_solution()

To re-use the fitness instead of calling the fitness function, pass the pop_fitness parameter. It should be assigned to the fitness of the latest generation's population.
solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)

This new code does not call the fitness function again for the same population.

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)
    solution_fitness = 1.0 / numpy.abs(output - desired_output)
    print(ga_instance.generations_completed, solution_idx, solution_fitness)    
    return solution_fitness

def callback_generation(ga_instance):
    solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)
    print(' ---> End of generation', ga_instance.generations_completed, ":", solution_fitness)

num_generations = 2
num_parents_mating = 4
sol_per_pop = 10
mutation_percent_genes = 10
keep_parents = 1
num_genes = len(function_inputs)
init_range_low = -2
init_range_high = 5
parent_selection_type = "sss"
crossover_type = "single_point"
mutation_type = "random"

ga_instance = pygad.GA(num_generations=num_generations,
                       num_parents_mating=num_parents_mating,
                       fitness_func=fitness_func,
                       sol_per_pop=sol_per_pop,
                       num_genes=num_genes,
                       init_range_low=init_range_low,
                       init_range_high=init_range_high,
                       parent_selection_type=parent_selection_type,
                       keep_parents=keep_parents,
                       crossover_type=crossover_type,
                       mutation_type=mutation_type,
                       mutation_percent_genes=mutation_percent_genes,
                       on_generation=callback_generation)

ga_instance.run()

solution, solution_fitness, solution_idx = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)
print("Parameters of the best solution : {solution}".format(solution=solution))
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))

prediction = numpy.sum(numpy.array(function_inputs)*solution)
print("Predicted output based on the best solution : {prediction}".format(prediction=prediction))

from geneticalgorithmpython.

sunderved avatar sunderved commented on June 11, 2024

Thanks @ahmedfgad , this indeed fixes the issue.

from geneticalgorithmpython.

xino1010 avatar xino1010 commented on June 11, 2024

Thanks @ahmedfgad

from geneticalgorithmpython.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.