Advertisement
Guest User

simple evolution

a guest
Oct 26th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. public interface Fitness {
  2.   public float evaluate(Specimen s);
  3. }
  4. public interface Breeder {
  5.   public Specimen breed(Specimen[] parents);
  6. }
  7. class Population {
  8.   Specimen[] generation;
  9.   int size;
  10.   float[] scores;
  11.   float maxScore;
  12.   int genotypeSize;
  13.   Population(int size, int genotypeSize) {
  14.     this.size = size;
  15.     scores = new float[size];
  16.     generation = new Specimen[size];
  17.     this.genotypeSize = genotypeSize;
  18.     for(int i = 0; i < size; i++) {
  19.       scores[i] = 0;
  20.       generation[i] = new Specimen(genotypeSize);
  21.     }
  22.     maxScore = 0;
  23.   }
  24.   void evaluate(Fitness environment) {
  25.     for(int i = 0; i < size; i++) {
  26.       scores[i] = environment.evaluate(generation[i]);
  27.       if(scores[i] > maxScore) maxScore = scores[i];
  28.     }
  29.   }
  30.   float getMaxScore() {
  31.     return maxScore;
  32.   }
  33.   float getAverageScore() {
  34.     float sum = 0;
  35.     for(int i = 0; i < size; i++)
  36.       sum += scores[i];
  37.     return sum / float(size);
  38.   }
  39.   float[] getNormalizedScores() {
  40.     if(maxScore == 0) return scores;
  41.     float[] normalizedScores = new float[size];
  42.     for(int i = 0; i < size; i++)
  43.       normalizedScores[i] = scores[i] / maxScore;
  44.     return normalizedScores;
  45.   }  
  46.   float[] getPowerScores(float evolutionaryPressure) {
  47.     if(evolutionaryPressure == 1) return scores;
  48.     float[] powerScores = new float[size];
  49.     for(int i = 0; i < size; i++)
  50.       powerScores[i] = pow(scores[i], evolutionaryPressure);
  51.     return powerScores;
  52.   }
  53.   void breed(float mutationRate) {
  54.     breed(mutationRate, 1);
  55.   }  
  56.   void breed(float mutationRate, float evolutionaryPressure, Breeder breeder) {
  57.     float[] powerScores = getPowerScores(evolutionaryPressure);
  58.     float maxPowerScore = pow(maxScore, evolutionaryPressure);
  59.    
  60.     Specimen nextGeneration[] = new Specimen[size];
  61.    
  62.     for(int i = 0; i < size; i++) {
  63.       Specimen[] parents = {getRandomSpecimen(powerScores, maxPowerScore),
  64.                             getRandomSpecimen(powerScores, maxPowerScore)};
  65.       Specimen child = breeder.breed(parents);
  66.       child.mutate(mutationRate);
  67.       nextGeneration[i] = child;
  68.     }
  69.     generation = nextGeneration;
  70.   }
  71.   void breed(float mutationRate, float evolutionaryPressure) {
  72.     breed(mutationRate, evolutionaryPressure, new Breeder() {
  73.       public Specimen breed(Specimen[] parents) {
  74.         float[] genotype = new float[genotypeSize];
  75.         for(int g = 0; g < genotypeSize; g++)
  76.           genotype[g] = parents[floor(random(parents.length))].getGene(g);
  77.         return new Specimen(genotype);
  78.       }
  79.     });
  80.   }
  81.   Specimen getRandomSpecimen(float[] scores, float maxScore) {
  82.     while(true) {
  83.       int index = int(random(size));
  84.       if(random(maxScore) >= scores[index]) return generation[index];
  85.     }
  86.   }
  87.   float[][] getGenerationAsArray() {
  88.     float[][] generationArray = new float[size][genotypeSize + 1];
  89.     for(int i = 0; i < size; i++)
  90.       generationArray[i] = append(generation[i].getGenotype(), scores[i]);
  91.     return generationArray;
  92.   }
  93. }
  94. class Specimen {
  95.   float[] genotype;
  96.   Specimen(int genotypeSize) {
  97.     genotype = new float[genotypeSize];
  98.     for(int i = 0; i < genotype.length; i++)
  99.       genotype[i] = random(1);
  100.   }
  101.   Specimen(float[] genotype) {
  102.     this.genotype = genotype;
  103.   }
  104.   float[] getGenotype() {
  105.     return genotype;
  106.   }
  107.   float getGene(int i) {
  108.     return genotype[i];
  109.   }
  110.   int getGenotypeSize() {
  111.     return genotype.length;
  112.   }
  113.   void mutate(float mutationRate) {
  114.     for(int i = 0; i < genotype.length; i++) {
  115.       while(random(1) < mutationRate)
  116.         genotype[i] += random(-0.001, 0.001);
  117.       while(genotype[i] < 0) genotype[i]++;
  118.       while(genotype[i] > 1) genotype[i]--;
  119.     }
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement