-2

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!

law_light
  • 59
  • 3
  • 3
    Your question is very unclear. Could you please expand it to explain in clear terms what you wish to achieve, and what your problem is? – Andras Deak -- Слава Україні Jan 08 '16 at 20:48
  • @AndrasDeak Sorry for confusing you. My problem is: if x belongs to [0,25], normally, since 25=11001 in base 2, and 11001 has length 5, I will randomly generate a few vectors of length 5 using 1s and 0s. to represent x binarily in the domain[0,25]; However, since 25 is not in the form 2^k-1,(unlike 31=11111), the number generated may fall outside the domain, say 26=11010. My question is: is there a one-to-one mapping that could solve this dilemma :) – law_light Jan 09 '16 at 02:25
  • Is there a reason why you don't generate integers up to 25, *then* convert that to binary? – Andras Deak -- Слава Україні Jan 09 '16 at 03:02

1 Answers1

0

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.

John Newcombe
  • 364
  • 2
  • 7
  • To help clarify the 'normalisation' approach I have added a post on the subject here: [http://johnnewcombe.net/blog/gaf-part-9/](http://johnnewcombe.net/blog/gaf-part-9/). – John Newcombe Feb 11 '16 at 22:50