Advertisement
itblanco

evolutionAlgorithm

Jan 30th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. DNA[] population = new DNA[50000];
  4. String target = "Pack my box with five dozen liquor jugs";
  5. DNA winner;
  6.  
  7. void setup() { //this is a comment
  8.   size(900, 400);
  9.  
  10.   for (int i = 0; i < population.length; i++) {
  11.     population[i] = new DNA();
  12.     population[i].fitness();
  13.   }
  14.  
  15.   sortPopulation();
  16. }
  17.  
  18. void draw() {
  19.   background(0);
  20.  
  21.   if (winner.fitness < 1.0) {
  22.     sortPopulation();
  23.  
  24.     ArrayList<DNA> matingPool = new ArrayList<DNA>();
  25.     for (int i=0; i < population.length; i++) {
  26.       int n = int(population[i].fitness * 100);
  27.       for (int j = 0; j < n; j++) {
  28.         matingPool.add(population[i]);
  29.       }
  30.     }
  31.  
  32.     for (int i=0; i < population.length; i++) {
  33.       DNA parentA, parentB;
  34.       while (true) {
  35.         int a = int(random(matingPool.size()));
  36.         int b = int(random(matingPool.size()));
  37.  
  38.         parentA = matingPool.get(a);
  39.         parentB = matingPool.get(b);
  40.  
  41.         if (parentA != parentB) break;
  42.       }
  43.  
  44.       DNA child = parentA.crossover(parentB);
  45.       child.mutate();    
  46.  
  47.       population[i] = child;
  48.       population[i].fitness();
  49.     }
  50.   }
  51.  
  52.   drawText();
  53. }
  54.  
  55. void drawText() {
  56.   textSize(30);
  57.   textAlign(LEFT, TOP);
  58.   text(new String(winner.genes), 10, 10);
  59.  
  60.   textSize(20);
  61.   for (int i = 1; i <= 17; i++) {
  62.     text(new String(population[i].genes), 10, i * 20 + 30);
  63.   }
  64. }
  65. void sortPopulation() {
  66.   Comparator<DNA> c = new Comparator<DNA>() {
  67.     public int compare (DNA a, DNA b) {
  68.       //return int(a.fitness*100 - b.fitness*100);
  69.       if(a.fitness > b.fitness) return 1;
  70.       if (a.fitness < b.fitness) return -1;
  71.       return 0;
  72.     }
  73.   };
  74.  
  75.   Arrays.sort(population, c);
  76.   population = (DNA[])reverse(population);  
  77.  
  78.   winner = population[0];
  79. }
  80.  
  81. class DNA {  
  82.   char[] genes = new char[target.length()];
  83.   float fitness;
  84.   float mutationRate = 0.015;
  85.  
  86.   DNA() {
  87.     for (int i = 0; i < genes.length; i++) {
  88.       genes[i] = (char) random(32, 128);
  89.     }
  90.   }
  91.  
  92.   void fitness() {
  93.     int score = 0;
  94.     for (int i=0; i < genes.length; i++) {
  95.       if (genes[i] == target.charAt(i)) score++;
  96.     }
  97.  
  98.     //fitness = float(score) / target.length();
  99.     fitness = (score == 0) ? 0 : pow(score, 2) / pow(target.length(), 2) + 0.01;
  100.   }
  101.  
  102.   DNA crossover(DNA partner) {
  103.     DNA child = new DNA();
  104.  
  105.     for (int i=0; i < genes.length; i++) {
  106.       float s = random(1);
  107.       if (s < 0.5) child.genes[i] = genes[i];
  108.       else child.genes[i] = partner.genes[i];
  109.     }
  110.  
  111.     return child;
  112.   }
  113.  
  114.   void mutate() {
  115.     for (int i=0; i < genes.length; i++) {
  116.       if (random(1) < mutationRate) {
  117.         genes[i] = (char) random(32, 128);
  118.       }
  119.     }
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement