Is there any package available or some other approach to implement constraints like (x1 < x2) or even more complex relationships provided by some function.

Another desired option would be a possibility to call some custom correction function after crossover and mutation operations or the definition of custom crossover / mutation functions.

The packages I know only support constant constraints (x1 > 10, x2 < 20, etc.).


The canonical GA is an unconstrained global metaheuristic and has no native way of integrating constraints into the optimisation process. Constant constraints or bounds are easy to implement in the form of "filters" but anything more complex than that should be best left out of the "mating" stage. It would not impossible to add but it would be impractical and could significantly slow down training/convergence (plus you would need to code it manually).

The ideal way to go about this would be to integrate your dynamic constraints inside the objective function. Off the top of my head, you could code a constraint as a conditional statement and have the objective function return an extreme value (e.g. -Inf, Inf) every time this constraint is violated. The initial population will likely have a significant portion of "violated" chromosomes but the GA should be powerful enough to handle such noise and filter out all faulty individuals after a certain number of iterations.

  • $\begingroup$ Thanks for your feedback. I also was thinking about integrate the constraint into the fitness function, but this could lead to a high number of invalid chromosomes. I found the gaoptim package which allows custom mutation and crossover operations, where I can assure that I only generate valid chromosomes. $\endgroup$
    – MikeHuber
    Dec 6 '17 at 14:06
  • $\begingroup$ It's good that you have found a library that offers you this flexibility. Still, if I were you I'd try both ways and see what performs best. Depending on the complexity of your constraints, the optimisation process with your method could get really slow (though time may not be an issue for you). With a fairly large population size there is a good chance that invalid chromosomes (or valid chromosomes that lead to invalid chromosomes when mated together) will cease to exist after a couple of iterations. $\endgroup$
    – Digio
    Dec 6 '17 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.