GeneticAlgorithmPython
GeneticAlgorithmPython copied to clipboard
Add inequality constraint of two different genes
Hello, may I know how to add inequality constraint of two different genes. For example, I have a gene space gene_space=[{'low': 0, 'high': 16}, {'low': 0, 'high': 24}]. I would like to add the constraint of gene1 < gene2. May I know if there is a way to do it in PyGAD. Thank you!
@terrancelu92, this is definitely a good question and I am also thinking of adding constraints across the genes.
Definitely this can be supported in PyGAD. It is about controlling the values of these genes when:
- Creating the initial population.
- Applying crossover.
- Applying mutation.
Even if it is easy to add constraints like gene1 < or > gene2, I am looking to supporting a broad way to add conditions between the different genes. For example: $$gene_1*4+2 <= 2^{\dfrac{\sqrt{gene_2}}{5}}$$
I hope we can find a solution soon.
@ahmedfgad ,Thank you for the reply! Could you please advise on where I need to revise based on the latest source code to implement the simple constraint e.g. gene1 < gene2?
I am now applying a large penalty on the objective function if the constraint is violated. However, by doing so I fear that the optimizer will focus more on avoiding the large penalty instead of optimizing the rest of the objective function.
Assuming you are going to use random mutation, then you need to edit this function: https://github.com/ahmedfgad/GeneticAlgorithmPython/blob/7f2b1f45cf2d4a4345613ede8a6d6ff3cd3505b2/pygad.py#L2060
While generating the new value for the gene, make sure that its value meets your condition.