Gray code, also called reflected binary code, represents adjacent numerical values with exactly one differing bit. This locality reduces the Hamming-cliff effect in binary-coded genetic algorithms, where flipping a significant bit can abruptly change the decoded solution and fitness. section 8.1
A four-bit sequence
The table maps decimal values to binary and Gray representations: 0 is 0000, 1 is 0001, 2 is 0011, and 3 is 0010. Each successive Gray representation changes one bit, unlike ordinary binary around transitions such as 3 to 4. section 8.1
Table 8.1 Decimal, binary, and Gray coding (view table figure)
| Decimal number |
Binary code |
Gray code |
|---|---|---|
| 0 |
0000 |
0000 |
| 1 |
0001 |
0001 |
| 2 |
0010 |
0011 |
| 3 |
0011 |
0010 |
| 4 |
0100 |
0110 |
| 5 |
0101 |
0111 |
| 6 |
0110 |
0101 |
| 7 |
0111 |
0100 |
| 8 |
1000 |
1100 |
| 9 |
1001 |
1101 |
| 10 |
1010 |
1111 |
| 11 |
1011 |
1110 |
| 12 |
1100 |
1010 |
| 13 |
1101 |
1011 |
| 14 |
1110 |
1001 |
| 15 |
1111 |
1000 |
Compare decimal values with their four-bit binary and Gray-code representations from 0 through 15. section 8.1
Gray coding is useful when representation changes should produce gentler solution changes:
- Use it in binary-coded genetic algorithms to smooth the genotype-to-phenotype mapping and reduce disruptive crossover or mutation effects. section 8.1
- Convert four-bit binary values with XOR gates; XOR returns 1 for different inputs and 0 for matching inputs. section 8.1
- Apply the representation in rotary encoders and digital-to-analog converters, where minimizing conversion errors matters. section 8.1
- Treat Gray code as an alternative to binary or real-valued encoding when binary representation creates poor convergence or inefficient exploration and exploitation. section 8.9
