Skip to content

Solution_FItness array and solutions arrays are in different length. #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
javid-b opened this issue Sep 13, 2021 · 4 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@javid-b
Copy link

javid-b commented Sep 13, 2021

I am using pygad, for GA, to find combination of solutions which would satisfy conditions. I have got a code, which runs 15 generations with 40 populations. When GA stops running, the size of array is 640 where as array is 600. I am looking for a single array which would have solutions for all trials with fitness array next to it. However, i was expecting them to be equal. May be i am doing something wrong?

@urowietu
Copy link

urowietu commented Sep 13, 2021 via email

@javid-b
Copy link
Author

javid-b commented Sep 16, 2021

Hi Keith,
Thanks for response. I did not quite understood, why 640/40 would give 15?
Yes, you are right i have got 15 genes and 40 competing algos in a generation.
Regardless if it gives 600 or 640, my expectation would be that number of iterated solutions would be equal to number of fitness values. Sorry, if i am saying something non-sensible.

Regards,
Javid

@urowietu
Copy link

urowietu commented Sep 19, 2021 via email

@ahmedfgad ahmedfgad self-assigned this Sep 28, 2021
@ahmedfgad ahmedfgad added the bug Something isn't working label Sep 28, 2021
@ahmedfgad
Copy link
Owner

Hi @javid-b,

Thanks for opening this issue. You are right as the fitness of the last population was not saved in the solutions_fitness list. This is solved by adding the next code at the end of the run() method.

if self.save_solutions:
    self.solutions_fitness.extend(self.last_generation_fitness)

The project will be updated soon and a new release of PyGAD will be published too.

Please let me know if you have any bugs or enhancements.

ahmedfgad added a commit that referenced this issue Sep 29, 2021
1. Reuse the fitness of previously explored solutions rather than recalculating them. This feature only works if `save_solutions=True`.
2. The user can use the `tqdm` library to show a progress bar. #50

```python
import pygad
import numpy
import tqdm

equation_inputs = [4,-2,3.5]
desired_output = 44

def fitness_func(solution, solution_idx):
    output = numpy.sum(solution * equation_inputs)
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
    return fitness

num_generations = 10000
with tqdm.tqdm(total=num_generations) as pbar:
    ga_instance = pygad.GA(num_generations=num_generations,
                           sol_per_pop=5,
                           num_parents_mating=2,
                           num_genes=len(equation_inputs),
                           fitness_func=fitness_func,
                           on_generation=lambda _: pbar.update(1))
    
    ga_instance.run()

ga_instance.plot_result()
```

3. Solved the issue of unequal length between the `solutions` and `solutions_fitness` when the `save_solutions` parameter is set to `True`. Now, the fitness of the last population is appended to the `solutions_fitness` array. #64
4. There was an issue of getting the length of these 4 variables (`solutions`, `solutions_fitness`, `best_solutions`, and `best_solutions_fitness`) doubled after each call of the `run()` method. This is solved by resetting these variables at the beginning of the `run()` method. #62
5. Bug fixes when adaptive mutation is used (`mutation_type="adaptive"`). #65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants