Advertisement
Guest User

Untitled

a guest
Feb 25th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. public class Darwin {
  5.  
  6.     private static final String CHAR_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +
  7.         "0123456789!\"£$%^&*()_+-={}[];'#:@~\\|,./<>? ";
  8.     private static final int NUM_CHARS = CHAR_LIST.length();
  9.     private static final int LITTER_SIZE = 10;
  10.     private static final int MAX_MUTATION = 5;
  11.     private static final double MUTATION_RATE = 0.01;
  12.     private static final int LEN_ERROR_PENALTY = NUM_CHARS / 2;
  13.     private static final int PAUSE_BTW_GENS = 10;
  14.    
  15.     private String target, current;
  16.     private String[] litter;
  17.     private Random rgen;
  18.     private int generation;
  19.    
  20.     public Darwin() {
  21.         rgen = new Random();
  22.         litter = new String[LITTER_SIZE];
  23.     }
  24.        
  25.     public void evolve(String targetString) {
  26.         target = filter(targetString);
  27.         current = "" + getRandomChar();
  28.         generation = 1;
  29.         while (!current.equals(target)) {
  30.             nextGeneration();
  31.             try {Thread.sleep(PAUSE_BTW_GENS);}
  32.             catch (InterruptedException ex) {}
  33.         }
  34.         System.out.println("---Finished---");
  35.     }
  36.    
  37.     private String filter(String text) {
  38.         StringBuilder filtered = new StringBuilder();
  39.         for (char ch : text.toCharArray()) {
  40.             if (CHAR_LIST.indexOf(ch) != -1) {
  41.                 filtered.append(ch);
  42.             }
  43.         }
  44.         return filtered.toString();
  45.     }
  46.        
  47.     private char getRandomChar() {
  48.         return CHAR_LIST.charAt(rgen.nextInt(NUM_CHARS));
  49.     }
  50.    
  51.     private void nextGeneration() {
  52.         System.out.println("Generation " + generation + ":");
  53.         for (int i = 0; i < LITTER_SIZE; i++) {
  54.             litter[i] = getChild();
  55.             System.out.println("Child" + i + ": " + litter[i]);
  56.         }
  57.         current = getWinner();
  58.         System.out.println("Target: " + target);
  59.         System.out.println("Winner: " + current);
  60.         System.out.println();
  61.         generation++;
  62.     }
  63.    
  64.     private String getChild() {
  65.         int newLen = current.length();
  66.         if (rgen.nextDouble() <= MUTATION_RATE) {
  67.             int tmp = newLen + randomMutation();
  68.             newLen = tmp < 1 ? 1 : tmp;
  69.         }
  70.         StringBuilder child = new StringBuilder();
  71.         for (int i = 0; i < newLen; i++) {
  72.             char ch;
  73.             if (i < current.length()) {
  74.                 ch = current.charAt(i);
  75.                 if (rgen.nextDouble() <= MUTATION_RATE) {
  76.                     ch = mutate(ch);
  77.                 }
  78.             } else {
  79.                 ch = getRandomChar();
  80.             }
  81.             child.append(ch);
  82.         }
  83.         return child.toString();
  84.     }
  85.    
  86.     private char mutate(char ch) {
  87.         int posInCharList = CHAR_LIST.indexOf(ch);
  88.         posInCharList += randomMutation();
  89.         while (posInCharList < 0) {
  90.             posInCharList += NUM_CHARS;
  91.         }
  92.         while (posInCharList >= NUM_CHARS) {
  93.             posInCharList -= NUM_CHARS;
  94.         }
  95.         return CHAR_LIST.charAt(posInCharList);
  96.     }
  97.    
  98.     private int randomMutation() {
  99.         int mutationSize = rgen.nextInt(MAX_MUTATION) + 1;
  100.         return rgen.nextBoolean() ? mutationSize : -mutationSize;
  101.     }
  102.    
  103.     private String getWinner() {
  104.         int indexOfLowest = 0;
  105.         int lowestScore = compareToTarget(litter[0]);
  106.         for (int i = 1; i < litter.length; i++) {
  107.             int score = compareToTarget(litter[i]);
  108.             if (score < lowestScore) {
  109.                 indexOfLowest = i;
  110.                 lowestScore = score;
  111.             }
  112.         }
  113.         return litter[indexOfLowest];
  114.     }
  115.    
  116.     private int compareToTarget(String text) {
  117.         int textLen = text.length();
  118.         int targetLen = target.length();
  119.         int score = 0;
  120.         for (int i = 0; i < textLen && i < targetLen; i++) {
  121.             int targetCharPos = CHAR_LIST.indexOf(target.charAt(i));
  122.             int textCharPos = CHAR_LIST.indexOf(text.charAt(i));
  123.             score += Math.abs(targetCharPos - textCharPos);
  124.         }
  125.         return score + (LEN_ERROR_PENALTY * Math.abs(textLen - targetLen));
  126.     }
  127.    
  128.     public static void main(String[] args) {
  129.         Darwin darwin = new Darwin();
  130.         darwin.evolve("Methinks it is like a weasel.");
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement