Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 3.35 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // BitSet Demonstration.
  2. import java.util.BitSet;
  3. import java.util.HashSet;
  4. import java.util.Random;
  5. import java.util.Arrays;
  6.  
  7. class SearchBitSetSpace {
  8.         public static int bsLen = 16; // number of bits in BitSet
  9.         public static int topLen = 6; // number of States in top pool
  10.        
  11.         // BitSet strongest = top[LAST]
  12.         public static State[] top = new State[topLen]; // top-N states
  13.         public static HashSet<BitSet> all = new HashSet<BitSet>();
  14.         public static Random ran = new Random();
  15.  
  16.         public static void main(String args[]) {
  17.                 initTop();
  18.                 updateTopViaCrossOver();
  19.                 updateTopViaMutate();
  20.                 updateTopViaCrossOver();
  21.                 updateTopViaMutate();
  22.                 updateTopViaCrossOver();
  23.                 updateTopViaMutate();
  24.                        
  25.                 for (int i = 0; i < topLen; i++) {
  26.                         echo(top[i]);
  27.                 }
  28.                
  29.                 echo(all);
  30.         }
  31.        
  32.         public static void updateTopViaMutate() {
  33.                 for (int i = 0; i < topLen; i++) {             
  34.                         // create mutant
  35.                         BitSet bs = mutate(top[i].bs);
  36.                         // add bs to all and check if it was already contained
  37.                         if(all.add(bs) == false) {
  38.                                 continue;
  39.                         }
  40.                         int bsfitness = fitness(bs);
  41.                        
  42.                         // check if mutant is stronger than top[0], which is the current weakest
  43.                         if(bsfitness > top[0].fitness) {
  44.                                 top[0].bs = bs;
  45.                                 top[0].fitness = bsfitness;
  46.                                 sortTop();
  47.                         }
  48.                 }      
  49.         }
  50.        
  51.         public static void updateTopViaCrossOver() {
  52.                 // create crossover from strongest and 1 other top-BitSet
  53.                 BitSet strongest = top[topLen - 1].bs;
  54.  
  55.                 for (int i = 0; i < topLen - 1; i++) {
  56.                         // create crossover, top[i] is the other BitSet
  57.                         BitSet bs = cross(strongest, top[i].bs);
  58.                         //add bs to all and check if it was already contained
  59.                         if(all.add(bs) == false) {
  60.                                 continue;
  61.                         }                      
  62.                         int bsfitness = fitness(bs);
  63.  
  64.                         // check if crossover is stronger than top[0], which is the current weakest
  65.                         if(bsfitness > top[0].fitness) {
  66.                                 top[0].bs = bs;
  67.                                 top[0].fitness = bsfitness;
  68.                                 sortTop();
  69.                         }
  70.                 }
  71.         }
  72.        
  73.         public static void sortTop() {
  74.                 Arrays.sort(top);
  75.         }
  76.  
  77.         // fill the top pool with different BitSets
  78.         public static void initTop() {
  79.                 for (int i = 0; i < topLen; i++) {
  80.                         BitSet a = new BitSet(bsLen);
  81.                         a.set(i);
  82.                         top[i] = new State(a, fitness(a));
  83.                         all.add(a);
  84.                 }
  85.         }
  86.  
  87.         // this is a fake fitness function
  88.         public static int fitness(BitSet a) {
  89.                 return a.cardinality();
  90.         }
  91.  
  92.         // randomly change 1 bit in a BitSet
  93.         public static BitSet mutate(BitSet a) {
  94.                 BitSet copy = (BitSet) a.clone();
  95.                 copy.flip(ran.nextInt(bsLen));
  96.                 return copy;
  97.         }
  98.  
  99.         // randomly inherit bits from BitSet a or b
  100.         public static BitSet cross(BitSet a, BitSet b) {
  101.                 BitSet copy = (BitSet) a.clone();
  102.  
  103.                 for (int i = 0; i < bsLen; i++) {
  104.                         if (ran.nextBoolean() == true) {
  105.                                 copy.set(i, b.get(i));
  106.                         }
  107.                 }
  108.                 return copy;
  109.         }
  110.  
  111.         // print a BitSet BitString
  112.         public static void print(BitSet bs) {
  113.                 for (int i = 0; i < bsLen; i++) {
  114.                         if (bs.get(i) == true) {
  115.                                 System.out.print("1");
  116.                         } else {
  117.                                 System.out.print("0");
  118.                         }
  119.                 }
  120.                 System.out.println();
  121.         }
  122.  
  123.         public static void echo(Object o) {
  124.                 System.out.println(o);
  125.         }
  126. }
  127.  
  128. class State implements Comparable<State> {
  129.         BitSet bs;
  130.         int fitness;
  131.        
  132.         public State(BitSet bs, int fitness) {
  133.                 this.bs = bs;
  134.                 this.fitness = fitness;
  135.         }
  136.        
  137.         public String toString() {
  138.                 return bs.toString() + " :: " + fitness;
  139.         }
  140.        
  141.         public int compareTo(State state) {
  142.                 return this.fitness - state.fitness;
  143.         }
  144. }