ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.89k stars 464 forks source link

ga_instance.best_solution() does not return the solution that minimizes my fitness function #281

Open 24spiders opened 8 months ago

24spiders commented 8 months ago

As mentioned, I have constructed an instance of PyGAD along with a custom fitness function. However, PyGAD does not return the best solution - that is, I have modified my fitness function to print the loss value every time it is called. While it iterates, I can see it return values such as follows:

Solution 8, value [16. 0.24717325 0.4 ], dist 75884.1558043205

Where 'dist' is what should be minimized. These 3 parameters result in a fairly low value of dist. However, once PyGAD completes and exits, the result is output:

Optimized Parameters: [27.          0.24717325  0.4       ]
Loss Value: 5777062.193619523

This loss value is obviously larger than the one it found with solution 8 - I am wondering why it is returning an answer with such a large loss value?

Initialization:

# Execute GA
import pygad
num_generations = 10
num_parents_mating = 4
mutation_rate = 0.1
num_genes = 3  # Number of parameters to optimize
sol_per_pop = 10  # Population size
initial_population = [ [np.random.uniform(low=3, high=40), 
                        np.random.uniform(low=0.1, high=0.4),
                        np.random.uniform(low=0, high=1),] for i in range(num_generations)]

ga_instance = pygad.GA(num_generations=num_generations,
                       num_parents_mating=num_parents_mating,
                       initial_population=initial_population,
                       fitness_func=calc_loss,
                       mutation_percent_genes=mutation_rate,
                       gene_space = [range(3,40), [0.1,0.2,0.3,0.4], [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]],
                        parallel_processing = 16,)
ahmedfgad commented 8 months ago

Can you please share a dummy fitness function so that we can better assist you?

moritz-weber commented 5 months ago

@24spiders pygad tries to maximize fitness values. Does your fitness function result in positive dist values? You could try to multiply the output of your fitness function with -1 and see if pygad minimizes the error then.