jpayne@68: package fun; jpayne@68: jpayne@68: import java.util.Arrays; jpayne@68: import java.util.Random; jpayne@68: jpayne@68: public class Genetic { jpayne@68: jpayne@68: public static void main(String[] args){ jpayne@68: Genetic g=new Genetic(args); jpayne@68: long answer=g.solve(); jpayne@68: System.out.println(Long.toBinaryString(answer)+" \t-> "+f(answer)); jpayne@68: } jpayne@68: jpayne@68: public Genetic(String[] args){ jpayne@68: pop=(args.length>0 ? Integer.parseInt(args[0]) : 20); jpayne@68: bits=(args.length>1 ? Integer.parseInt(args[1]) : 8); jpayne@68: iters=(args.length>2 ? Integer.parseInt(args[2]) : 20); jpayne@68: mutProb=(args.length>3 ? Double.parseDouble(args[3]) : 0.01); jpayne@68: mask=(bits>63 ? -1L : ~((-1L)<63 ? -1L : ~((-1L)<0 && randy.nextDouble()0; i--){ jpayne@68: if(prob[i-1] { jpayne@68: jpayne@68: public Bug(long dna_){ jpayne@68: dna=dna_; jpayne@68: fitness=f(dna); jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public int compareTo(Bug b){ jpayne@68: return (fitnessb.fitness ? 1 : 0); jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public String toString(){ jpayne@68: return Long.toBinaryString(dna)+" \t-> "+fitness; jpayne@68: } jpayne@68: jpayne@68: final long dna; jpayne@68: final double fitness; jpayne@68: } jpayne@68: jpayne@68: public static final Random randy=new Random(); jpayne@68: jpayne@68: final int pop; jpayne@68: final int bits; jpayne@68: final int iters; jpayne@68: final double mutProb; jpayne@68: jpayne@68: final long mask; jpayne@68: jpayne@68: }