Comments (5)
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.
@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.
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.
Thanks @ahmedfgad , this indeed fixes the issue.
from geneticalgorithmpython.
Thanks @ahmedfgad
from geneticalgorithmpython.
Related Issues (20)
- multi objective optimization - metric of quality HOT 1
- Force recalculation of fitness values for each new generation ? HOT 7
- The on_generation callback is not called at the end of generation 0 HOT 2
- GA
- Convergence issue: good solution appears to be forgotten, resulting best solution is not even valid HOT 1
- Error when running 3.2.0 vs. 3.1.0 for ga_instance.best_solutions HOT 1
- ga_instance.save will not work with tqdm example HOT 1
- fitness function is being saved? HOT 2
- Cannot modify the attributes of the ga_instance when running multiple processes in parallel HOT 1
- Manipulate solution before saving it as parent HOT 5
- `pareto_fronts` is from previous generation HOT 1
- `initial_population` not effectively used/retained for multiobjective problems? HOT 4
- Training traffic sign recognition with faster rcnn using ga HOT 1
- ga_instance.best_solution() does not return the solution that minimizes my fitness function HOT 1
- delay_after_gen warning HOT 2
- Is NSGA-III supported? HOT 1
- Caching of Fitness Values for Elites/Kept Parents
- pygad.kerasga
- ga_instance.best_solution() only returning best parameters and fitness of previous Generation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geneticalgorithmpython.