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

Develop mutation in the Generation class #25

Open
gnypit opened this issue Jan 22, 2025 · 1 comment
Open

Develop mutation in the Generation class #25

gnypit opened this issue Jan 22, 2025 · 1 comment
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@gnypit
Copy link
Owner

gnypit commented Jan 22, 2025

Based on the probability of reseting a member of the generation (prob=10% means we randomly choose 10% of the members and use the genome generator to create new genomes for selected members), an efficient way to apply mutation is required.

IMPORTANT: it shouldn't be done in a loop, as computing probabilities, even pseudo-random ones, takes up a lot of time. It's better to have a tool to create a "mask" with indexes of the members to be mutated and then, e.g., to use the mutate_member method in the Generation class.

@gnypit gnypit added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Jan 22, 2025
@gnypit gnypit removed the good first issue Good for newcomers label Jan 22, 2025
@gnypit gnypit self-assigned this Jan 24, 2025
@gnypit
Copy link
Owner Author

gnypit commented Jan 24, 2025

At this point a corresponding method is in the GeneticAlgorithm class, so it's probably too high-level than it should in the future for parallelisation purposes: https://github.com/gnypit/pyqkd/blob/af543841315080e49b7135e06a19019255745f62/genal/genetic_algorithm.py

def mutate(self):
        """Mutation probability is the probability of 'resetting' a member of the current generation, i.e. changing
        it genome randomly. For optimisation purposes instead of a loop over the whole generation, I calculate the
        number of members to be mutated and then generate pseudo-randomly a list of member indexes in the current
        generation to be mutated.
        """
        number_of_mutations = np.floor(self.mutation_prob * self.current_generation.size)

        """Size of generation is a constant, it has to be adjusted to the lack of elite; the elite Members are not
        supposed to be mutated. Additionally, number of mutations has to be an integer, e.g., 
        half of a mutation cannot be performed.
        """
        indexes = random.sample(
            range(self.current_generation.size - self.elite_size),
            int(number_of_mutations)  # has to be an integer, e.g. you can't make half of a mutation
        )

        """For new (mutated) genome creation I use the generator passed to the superclass in it's initialisation:"""
        for index in indexes:
            self.current_generation.members[index].change_genes(
                self.genome_generator(self.genome_generator_args)
            )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant