- // BitSet Demonstration.
- import java.util.BitSet;
- import java.util.HashSet;
- import java.util.Random;
- import java.util.Arrays;
- class SearchBitSetSpace {
- public static int bsLen = 16; // number of bits in BitSet
- public static int topLen = 6; // number of States in top pool
- // BitSet strongest = top[LAST]
- public static State[] top = new State[topLen]; // top-N states
- public static HashSet<BitSet> all = new HashSet<BitSet>();
- public static Random ran = new Random();
- public static void main(String args[]) {
- initTop();
- updateTopViaCrossOver();
- updateTopViaMutate();
- updateTopViaCrossOver();
- updateTopViaMutate();
- updateTopViaCrossOver();
- updateTopViaMutate();
- for (int i = 0; i < topLen; i++) {
- echo(top[i]);
- }
- echo(all);
- }
- public static void updateTopViaMutate() {
- for (int i = 0; i < topLen; i++) {
- // create mutant
- BitSet bs = mutate(top[i].bs);
- // add bs to all and check if it was already contained
- if(all.add(bs) == false) {
- continue;
- }
- int bsfitness = fitness(bs);
- // check if mutant is stronger than top[0], which is the current weakest
- if(bsfitness > top[0].fitness) {
- top[0].bs = bs;
- top[0].fitness = bsfitness;
- sortTop();
- }
- }
- }
- public static void updateTopViaCrossOver() {
- // create crossover from strongest and 1 other top-BitSet
- BitSet strongest = top[topLen - 1].bs;
- for (int i = 0; i < topLen - 1; i++) {
- // create crossover, top[i] is the other BitSet
- BitSet bs = cross(strongest, top[i].bs);
- //add bs to all and check if it was already contained
- if(all.add(bs) == false) {
- continue;
- }
- int bsfitness = fitness(bs);
- // check if crossover is stronger than top[0], which is the current weakest
- if(bsfitness > top[0].fitness) {
- top[0].bs = bs;
- top[0].fitness = bsfitness;
- sortTop();
- }
- }
- }
- public static void sortTop() {
- Arrays.sort(top);
- }
- // fill the top pool with different BitSets
- public static void initTop() {
- for (int i = 0; i < topLen; i++) {
- BitSet a = new BitSet(bsLen);
- a.set(i);
- top[i] = new State(a, fitness(a));
- all.add(a);
- }
- }
- // this is a fake fitness function
- public static int fitness(BitSet a) {
- return a.cardinality();
- }
- // randomly change 1 bit in a BitSet
- public static BitSet mutate(BitSet a) {
- BitSet copy = (BitSet) a.clone();
- copy.flip(ran.nextInt(bsLen));
- return copy;
- }
- // randomly inherit bits from BitSet a or b
- public static BitSet cross(BitSet a, BitSet b) {
- BitSet copy = (BitSet) a.clone();
- for (int i = 0; i < bsLen; i++) {
- if (ran.nextBoolean() == true) {
- copy.set(i, b.get(i));
- }
- }
- return copy;
- }
- // print a BitSet BitString
- public static void print(BitSet bs) {
- for (int i = 0; i < bsLen; i++) {
- if (bs.get(i) == true) {
- System.out.print("1");
- } else {
- System.out.print("0");
- }
- }
- System.out.println();
- }
- public static void echo(Object o) {
- System.out.println(o);
- }
- }
- class State implements Comparable<State> {
- BitSet bs;
- int fitness;
- public State(BitSet bs, int fitness) {
- this.bs = bs;
- this.fitness = fitness;
- }
- public String toString() {
- return bs.toString() + " :: " + fitness;
- }
- public int compareTo(State state) {
- return this.fitness - state.fitness;
- }
- }