| 1 | package fun; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.Random; |
| 5 | |
| 6 | public class Genetic { |
| 7 | |
| 8 | public static void main(String[] args){ |
| 9 | Genetic g=new Genetic(args); |
| 10 | long answer=g.solve(); |
| 11 | System.out.println(Long.toBinaryString(answer)+" \t-> "+f(answer)); |
| 12 | } |
| 13 | |
| 14 | public Genetic(String[] args){ |
| 15 | pop=(args.length>0 ? Integer.parseInt(args[0]) : 20); |
| 16 | bits=(args.length>1 ? Integer.parseInt(args[1]) : 8); |
| 17 | iters=(args.length>2 ? Integer.parseInt(args[2]) : 20); |
| 18 | mutProb=(args.length>3 ? Double.parseDouble(args[3]) : 0.01); |
| 19 | mask=(bits>63 ? -1L : ~((-1L)<<bits)); |
| 20 | } |
| 21 | |
| 22 | public long solve(){ |
| 23 | final long mask=(bits>63 ? -1L : ~((-1L)<<bits)); |
| 24 | double[] prob=new double[pop]; |
| 25 | Bug[] current=new Bug[pop]; |
| 26 | for(int i=0; i<pop; i++){ |
| 27 | current[i]=new Bug(randy.nextLong()&mask); |
| 28 | } |
| 29 | Arrays.sort(current); |
| 30 | Bug best=current[current.length-1]; |
| 31 | |
| 32 | for(int i=0; i<iters; i++){ |
| 33 | |
| 34 | if(true){ |
| 35 | System.out.println("Iteration "+i+": "+current[current.length-1]); |
| 36 | } |
| 37 | |
| 38 | current=iterate(current, prob); |
| 39 | Arrays.sort(current); |
| 40 | if(best.compareTo(current[current.length-1])<0){best=current[current.length-1];} |
| 41 | } |
| 42 | |
| 43 | return best.dna; |
| 44 | } |
| 45 | |
| 46 | public Bug[] iterate(Bug[] current, double[] prob){ |
| 47 | Arrays.fill(prob, 0); |
| 48 | double sum=0; |
| 49 | for(int i=0; i<current.length; i++){ |
| 50 | Bug b=current[i]; |
| 51 | sum+=b.fitness; |
| 52 | prob[i]=b.fitness; |
| 53 | } |
| 54 | double mult=1/sum; |
| 55 | prob[0]*=mult; |
| 56 | |
| 57 | for(int i=1; i<prob.length; i++){ |
| 58 | prob[i]=prob[i-1]+prob[i]*mult; |
| 59 | } |
| 60 | |
| 61 | Bug[] next=new Bug[current.length]; |
| 62 | for(int i=0; i<next.length; i++){ |
| 63 | long babyDna=breed(current, prob, mutProb); |
| 64 | next[i]=new Bug(babyDna); |
| 65 | } |
| 66 | return next; |
| 67 | } |
| 68 | |
| 69 | public long breed(Bug[] current, double[] prob, double mutProb){ |
| 70 | double fa=randy.nextDouble(); |
| 71 | double fb=randy.nextDouble(); |
| 72 | Bug a=current[findIndex(fa, prob)]; |
| 73 | Bug b=current[findIndex(fb, prob)]; |
| 74 | long crossover=randy.nextLong(); |
| 75 | long baby=(a.dna&crossover)|(b.dna&~crossover); |
| 76 | if(mutProb>0 && randy.nextDouble()<mutProb){ |
| 77 | long bit=(1L<<randy.nextInt(bits)); |
| 78 | baby^=bit; |
| 79 | } |
| 80 | return baby; |
| 81 | } |
| 82 | |
| 83 | public int findIndex(double f, double[] prob){ |
| 84 | for(int i=pop-1; i>0; i--){ |
| 85 | if(prob[i-1]<f){return i;} |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | public static double f(long x){ |
| 91 | return x*x; |
| 92 | } |
| 93 | |
| 94 | private static class Bug implements Comparable<Bug> { |
| 95 | |
| 96 | public Bug(long dna_){ |
| 97 | dna=dna_; |
| 98 | fitness=f(dna); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int compareTo(Bug b){ |
| 103 | return (fitness<b.fitness ? -1 : fitness>b.fitness ? 1 : 0); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String toString(){ |
| 108 | return Long.toBinaryString(dna)+" \t-> "+fitness; |
| 109 | } |
| 110 | |
| 111 | final long dna; |
| 112 | final double fitness; |
| 113 | } |
| 114 | |
| 115 | public static final Random randy=new Random(); |
| 116 | |
| 117 | final int pop; |
| 118 | final int bits; |
| 119 | final int iters; |
| 120 | final double mutProb; |
| 121 | |
| 122 | final long mask; |
| 123 | |
| 124 | } |