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

Applying different "mutation strategies" to specific chromosomes in Jenetics #900

Open
m4r0lt opened this issue Apr 26, 2024 · 3 comments
Open
Labels

Comments

@m4r0lt
Copy link

m4r0lt commented Apr 26, 2024

I'm currently working with the Jenetics library to implement a genetic algorithm and I've run into a specific need that I'm not sure how to address. I need to apply different mutation strategies to different chromosomes within the same genotype. Specifically, I want to use a Swap Mutator on the first and third chromosomes, and a Gaussian Mutator on the second and fourth chromosomes.

Here's a basic outline of my genotype structure:
Genotype genotype = Genotype.of(
IntegerChromosome.of(1, 10, 10), // First chromosome
IntegerChromosome.of(1, 3, 10), // Second chromosome
IntegerChromosome.of(1, 10, 10), // Third chromosome
IntegerChromosome.of(1, 5, 10) // Fourth chromosome
);

Could anyone share insights on how to implement this or point me towards any resources or examples that might help?

Thank you in advance for your help!

@jenetics
Copy link
Owner

jenetics commented Apr 28, 2024

The PartialAlterer will do this trick.

final Genotype<IntegerGene> genotype = Genotype.of(
    IntegerChromosome.of(1, 10, 10), // First chromosome
    IntegerChromosome.of(1, 3, 10), // Second chromosome
    IntegerChromosome.of(1, 10, 10), // Third chromosome
    IntegerChromosome.of(1, 5, 10) // Fourth chromosome
);

final Engine<IntegerGene, Double> engine = Engine
    .builder(gt -> gt.gene().doubleValue(), genotype)
    .alterers(
        PartialAlterer.of(new SwapMutator<IntegerGene, Double>(), 0, 2),
        PartialAlterer.of(new GaussianMutator<IntegerGene, Double>(), 1, 3)
    )
    .build();

Regards, Franz

@m4r0lt
Copy link
Author

m4r0lt commented Apr 29, 2024

Thank you!

@m4r0lt m4r0lt closed this as completed Apr 29, 2024
@m4r0lt m4r0lt reopened this Apr 29, 2024
@m4r0lt
Copy link
Author

m4r0lt commented Apr 29, 2024

Hello Franz,

Thank you for your previous response, it was quite helpful. After diving deeper into the Jenetics documentation, I understand that a Genotype is expected to have chromosomes of the same type. I initially wanted to combine PermutationChromosome and IntegerChromosome within the same Genotype, but it seems that's not feasible.

My follow-up question is: Is there a way to use IntegerChromosome to simulate the behaviour of PermutationChromosome?

To elaborate, I would like an IntegerChromosome with values ranging from 1 to 10 where the SwapMutator doesn't alter the sequence, thus maintaining a permutation of the original sequence.

For example:
IntegerGene[] genes = new IntegerGene[10];
for (int i = 0; i < 10; i++) {
genes[i] = IntegerGene.of(i + 1, 1, 10);
}
IntegerChromosome chromosome = IntegerChromosome.of(genes);

However, I believe the IntegerChromosome is initialized with random values within the specified range despite setting the allele values manually, which is not the behaviour I expected.

With the PermutationChromosome, I wanted to represent the priorities of tasks/jobs, and thus, the repetition of numbers would not yield the desired 'ideal' representation. Is there a way to configure the IntegerChromosome so it behaves like a PermutationChromosome, or is there a workaround using the SwapMutator that could help me achieve this?

Thank you for your time and assistance.

Best regards, m4r0lt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants