Search code examples
matlabgenetic-algorithmgenetic-programming

in genetic algorithm, How to deal with binary representation of constraints of functions?


For example, 0<=x<=31, the length of binary form of 31 is 5, since 31=11111 in base 2. However, how to deal with, say, 0<=x<=25, if I keep length 5, numbers like 11110(30) may be generated, which exceeds 11001(25). I wonder if there is a mapping which could solve this. Thanks a lot!


Solution

  • If I understand you correctly, you are asking how to deal with automatically generated solutions that fall outside the constraint you have. In this case you have several options, firstly you could simply kill these invalid solutions and generate more until one fits within your constraint. The better option is to normalise all of your values within a specified range e.g. 0 to 31 or 0 to 64 etc.

    I have an example of this type of normalisation in the Evaluate Fitness function of this example.

    http://johnnewcombe.net/blog/gaf-part-2/

    The code is based around the Genetic Algorithm Framework for .Net but the technique can be applied to any library or home grown algorithm.