Skip to content
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

The genes are always integers even if the gene_space attribute has float values. #27

Closed
ahmedfgad opened this issue Feb 20, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@ahmedfgad
Copy link
Owner

In the next code, the initial_population parameter is used to feed an initial population that has 8 solutions with 2 genes each. The gene_space parameter is used which has the half values starting from 0.5 to 15.5.

When the mutation is applied, it is expected that some genes have floating-point values like 3.5, 6.5, 0.5, 2.5, etc. But all genes are integers.

import pygad
import numpy

init_pop = ((1, 1), (3, 3), (5, 5), (7, 7), (9, 9), (11, 11), (13, 13), (15, 15))

def fitness_func(solution, solution_idx):
    fitness = numpy.sum(solution)
    return fitness

gene_space = numpy.arange(0.5, 15.5, 1)

ga_instance = pygad.GA(initial_population=init_pop,
                       num_generations=4,
                       num_parents_mating=8,
                       fitness_func=fitness_func,
                       gene_space=gene_space)

ga_instance.run()
@ahmedfgad ahmedfgad added the bug Something isn't working label Feb 20, 2021
@ahmedfgad ahmedfgad self-assigned this Feb 20, 2021
@ahmedfgad
Copy link
Owner Author

After inspecting the code, then the problem happened due to mismatch between the types of the initial_population and gene_type. Let me clarify.

The initial population is created as follows:

init_pop = ((1, 1), (3, 3), (5, 5), (7, 7), (9, 9), (11, 11), (13, 13), (15, 15))

Then, init_pop is assigned to initial_population in the constructor of the pygad.GA class:

initial_population = init_pop

Inside PyGAD, a NumPy array is created out of initial_population:

self.initial_population = numpy.array(initial_population)

The data type of this array is deduced automatically from the values inside initial_population. Because all the values used in init_pop are integers, then the type of initial_population is set to integer. As a result, when a value like 4.5 is assigned to this array, the value will be automatically casted into an integer to be 4.

To make things work, here is the solution:
Rather than using this line:

init_pop = ((1, 1), (3, 3), (5, 5), (7, 7), (9, 9), (11, 11), (13, 13), (15, 15))

Use this line:

init_pop = np.array(((1, 1), (3, 3), (5, 5), (7, 7), (9, 9), (11, 11), (13, 13), (15, 15)), dtype=float)

Or just write at least a single value as float to make the code things the array is float. In the next line, the first value is now 1.0 rather than 1:

init_pop = ((1.0, 1), (3, 3), (5, 5), (7, 7), (9, 9), (11, 11), (13, 13), (15, 15))

@ahmedfgad
Copy link
Owner Author

ahmedfgad commented Feb 20, 2021

Thanks to Marios Giouvanakis, a PhD candidate in Electrical & Computer Engineer, Aristotle University of Thessaloniki (Αριστοτέλειο Πανεπιστήμιο Θεσσαλονίκης), Greece, for emailing me about this bug.

ahmedfgad added a commit that referenced this issue Feb 20, 2021
## PyGAD 2.12.0

Release Date: 20 February 2021

1. 4 new instance attributes are added to hold temporary results after each generation: `last_generation_fitness` holds the fitness values of the solutions in the last generation, `last_generation_parents` holds the parents selected from the last generation, `last_generation_offspring_crossover` holds the offspring generated after applying the crossover in the last generation, and `last_generation_offspring_mutation` holds the offspring generated after applying the mutation in the last generation. You can access these attributes inside the `on_generation()` method for example.
2. A bug fixed when the `initial_population` parameter is used. The bug occurred due to a mismatch between the data type of the array assigned to `initial_population` and the gene type in the `gene_type` attribute. Assuming that the array assigned to the `initial_population` parameter is `((1, 1), (3, 3), (5, 5), (7, 7))` which has type `int`. When `gene_type` is set to `float`, then the genes will not be float but casted to `int` because the defined array has `int` type. The bug is fixed by forcing the array assigned to `initial_population` to have the data type in the `gene_type` attribute. Check the [issue at GitHub](#27): #27

Thanks to [Marios Giouvanakis](https://www.researchgate.net/profile/Marios-Giouvanakis), a PhD candidate in Electrical & Computer Engineer, [Aristotle University of Thessaloniki (Αριστοτέλειο Πανεπιστήμιο Θεσσαλονίκης), Greece](https://www.auth.gr/en), for emailing me about these issues.
ahmedfgad added a commit that referenced this issue Feb 20, 2021
## PyGAD 2.12.0

Release Date: 20 February 2021

1. 4 new instance attributes are added to hold temporary results after each generation: `last_generation_fitness` holds the fitness values of the solutions in the last generation, `last_generation_parents` holds the parents selected from the last generation, `last_generation_offspring_crossover` holds the offspring generated after applying the crossover in the last generation, and `last_generation_offspring_mutation` holds the offspring generated after applying the mutation in the last generation. You can access these attributes inside the `on_generation()` method for example.
2. A bug fixed when the `initial_population` parameter is used. The bug occurred due to a mismatch between the data type of the array assigned to `initial_population` and the gene type in the `gene_type` attribute. Assuming that the array assigned to the `initial_population` parameter is `((1, 1), (3, 3), (5, 5), (7, 7))` which has type `int`. When `gene_type` is set to `float`, then the genes will not be float but casted to `int` because the defined array has `int` type. The bug is fixed by forcing the array assigned to `initial_population` to have the data type in the `gene_type` attribute. Check the [issue at GitHub](#27): #27

Thanks to [Marios Giouvanakis](https://www.researchgate.net/profile/Marios-Giouvanakis), a PhD candidate in Electrical & Computer Engineer, [Aristotle University of Thessaloniki (Αριστοτέλειο Πανεπιστήμιο Θεσσαλονίκης), Greece](https://www.auth.gr/en), for emailing me about these issues.
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

1 participant