Skip to content

when set mutation_type="adaptive", it ran into error. #65

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
DailiZhang2010 opened this issue Sep 22, 2021 · 7 comments
Open

when set mutation_type="adaptive", it ran into error. #65

DailiZhang2010 opened this issue Sep 22, 2021 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@DailiZhang2010
Copy link

code
error_message

@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 @DailiZhang2010,

The problem is related to the fitness array. I cannot figure out the problem from the screenshot. Is it possible to share the code? You are welcome to send it by email ([email protected]) if this is better for you.

@DailiZhang2010
Copy link
Author

DailiZhang2010 commented Sep 28, 2021 via email

@ahmedfgad
Copy link
Owner

I already tested the sample code posted in the documentation with the following 2 lines and everything works well.

mutation_type = "adaptive"
mutation_num_genes=(3, 1)

The problem exists in the fitness function. For the error case, could you check if the fitness function returns a single number?

@DailiZhang2010
Copy link
Author

DailiZhang2010 commented Sep 28, 2021 via email

@DailiZhang2010
Copy link
Author

DailiZhang2010 commented Sep 28, 2021 via email

@ahmedfgad
Copy link
Owner

ahmedfgad commented Sep 29, 2021

@DailiZhang2010,

Thanks for opening this issue. I figured out where is the problem which is related to the keep_parents parameter. When all parents are used in the next population (i.e. keep_parents=-1), things work well. When the number of kept parents is less than the number of mating parents, the problem arises.

It is solved now.

The project will be updated soon and a new release of PyGAD will be published. Please have a try when the new release is published and get back if you found any bugs.

@DailiZhang2010
Copy link
Author

DailiZhang2010 commented Sep 29, 2021 via email

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

2 participants