|
| 1 | +package net.dervism.genericalgorithms; |
| 2 | + |
| 3 | +/** |
| 4 | + * A 64-bit immutable Chromosome that contains several genes. |
| 5 | + * |
| 6 | + * Implementation is based on the encoding proposed at the Chess Programming Wiki: |
| 7 | + * https://chessprogramming.wikispaces.com/Encoding+Moves |
| 8 | + * |
| 9 | + * Created by dervism on 27.12.13. |
| 10 | + */ |
| 11 | +public class Chromosome { |
| 12 | + |
| 13 | + public long genes; |
| 14 | + |
| 15 | + public Chromosome() { |
| 16 | + this(0L); |
| 17 | + } |
| 18 | + |
| 19 | + public Chromosome(long genes) { |
| 20 | + this.genes = genes; |
| 21 | + } |
| 22 | + |
| 23 | + public Chromosome(Chromosome c) { |
| 24 | + this.genes |= c.genes; |
| 25 | + } |
| 26 | + |
| 27 | + public Chromosome setNthBit(long n) { |
| 28 | + Chromosome c = new Chromosome(genes | 1L << n); |
| 29 | + return new Chromosome(c); |
| 30 | + } |
| 31 | + |
| 32 | + // ^ = exclusive or, xor |
| 33 | + public Chromosome flipNthBit(long n) { |
| 34 | + Chromosome c = new Chromosome(genes ^ (1L << n)); |
| 35 | + return new Chromosome(c); |
| 36 | + } |
| 37 | + |
| 38 | + // if n'th bit is 1, then return true, otherwise false |
| 39 | + public boolean getNthBit(long n) { |
| 40 | + return !((genes & (1L << n)) == 0); |
| 41 | + } |
| 42 | + |
| 43 | + public Chromosome clearNthBit(long n) { |
| 44 | + Chromosome c = new Chromosome(genes & ~(1L << n)); |
| 45 | + return new Chromosome(c); |
| 46 | + } |
| 47 | + |
| 48 | + public byte[] toBytes() { |
| 49 | + return longToByte(genes); |
| 50 | + } |
| 51 | + |
| 52 | + public static byte[] longToByte(long l) { |
| 53 | + // a long occupies 8 bytes |
| 54 | + byte[] ret = new byte[8]; |
| 55 | + |
| 56 | + // do a unsigned right shift |
| 57 | + // - push everything to the right and insert 0's from the left |
| 58 | + ret[0] = (byte)(l >>> 56); |
| 59 | + ret[1] = (byte)(l >>> 48); |
| 60 | + ret[2] = (byte)(l >>> 40); |
| 61 | + ret[3] = (byte)(l >>> 32); |
| 62 | + ret[4] = (byte)(l >>> 24); |
| 63 | + ret[5] = (byte)(l >>> 16); |
| 64 | + ret[6] = (byte)(l >>> 8); |
| 65 | + ret[7] = (byte)(l >>> 0); |
| 66 | + |
| 67 | + return ret; |
| 68 | + } |
| 69 | + |
| 70 | + public static long byteToLong(byte[] bytes) { |
| 71 | + return (bytes[0] << 56) |
| 72 | + + ((bytes[1] & 0xFF) << 48) |
| 73 | + + ((bytes[2] & 0xFF) << 40) |
| 74 | + + ((bytes[3] & 0xFF) << 32) |
| 75 | + + ((bytes[4] & 0xFF) << 24) |
| 76 | + + ((bytes[5] & 0xFF) << 16) |
| 77 | + + ((bytes[6] & 0xFF) << 8) |
| 78 | + + (bytes[7] & 0xFF); |
| 79 | + } |
| 80 | + |
| 81 | + // Chromosome class is immutable, however, if you want to allow |
| 82 | + // mutability then use this setter. |
| 83 | + private void setGenes(long genes) { |
| 84 | + this.genes = genes; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Prints the binary string representation of this Chromosome's bits |
| 89 | + * @return |
| 90 | + */ |
| 91 | + @Override |
| 92 | + public String toString() { |
| 93 | + String ret = ""; |
| 94 | + String group = ""; |
| 95 | + for(int i = 0; i < Long.numberOfLeadingZeros(genes); i++) ret += '0'; |
| 96 | + ret += Long.toBinaryString(genes); |
| 97 | + for (int i = 1; i <= ret.length(); i++) { |
| 98 | + if ((i % 4) == 0) { |
| 99 | + group += ret.charAt(i-1) + " "; |
| 100 | + } |
| 101 | + else group += ret.charAt(i-1); |
| 102 | + } |
| 103 | + ret = group; |
| 104 | + return ret; |
| 105 | + } |
| 106 | +} |
0 commit comments