Guest User

Untitled

a guest
Jan 31st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. // Fitness function
  2.     public long getFitness() {
  3.         long fitness = getLength();
  4.  
  5.         return fitness;
  6.     }
  7.  
  8. // Selection method
  9.     public void selection(Network[] population, Network[] newGeneration) {
  10.         long eFitness = 0;
  11.         for(int i = 0; i < population.length; i++) {
  12.             eFitness += population[i].getFitness();
  13.         }
  14.        
  15.         for(int i = 0; i < Info.POPULATION; i++) {
  16.             Network child = Network.breed(getByRoulette(eFitness, population), getByRoulette(eFitness, population));
  17.             newGeneration[i] = child;
  18.         }
  19.        
  20.     }
  21.  
  22. // Roulette method
  23.     public Network getByRoulette(long eFitness, Network[] population) {
  24.         long rand = (long) (rnd.nextDouble() * eFitness);
  25.         long runningSum = 0;
  26.         for(int i = 0; i < population.length; i++) {
  27.             runningSum += population[i].getFitness();
  28.             if(runningSum > rand) {
  29.                 return population[i];
  30.             }
  31.         }
  32.  
  33.         return null;
  34.     }
  35.  
  36. // new generation method
  37.     private Network[] newGeneration(int species, Network[] population) {
  38.         Network[] newGeneration = new Network[Info.POPULATION];
  39.  
  40.         // Average the total fitnesses
  41.         for(int i = 0; i < Info.POPULATION; i++) {
  42.             population[i].setFitness(population[i].getFitness() / Info.REPLAY_NETWORKS);
  43.         }
  44.  
  45.         Arrays.sort(population, Maths.SORT_FITNESS);
  46.         if(population[0].getFitness() > mech.highestFitnessEver) {
  47.             mech.highestFitnessEver = population[0].getFitness();
  48.         }
  49.         totalFitnessOfBest[species] += population[0].getFitness();
  50.  
  51.         selection(population, newGeneration);
  52.  
  53.         if(Info.GENERATION % Info.GRAPH_SCALE == 0) {
  54.             mech.averageFitnessOfBest[species] = totalFitnessOfBest[species] / Info.GRAPH_SCALE;
  55.             if(mech.averageFitnessOfBest[species] > highestAverageFitness) {
  56.                 highestAverageFitness = mech.averageFitnessOfBest[species];
  57.             }
  58.             totalFitnessOfBest[species] = 0;
  59.             mech.addScore(species, (long) mech.averageFitnessOfBest[species]);
  60.         }
  61.  
  62.         if(Info.GENERATION % 5 == 0) {
  63.             try {
  64.                 population[0].save(saveFile, false, Info.GENERATION, (int) mech.averageFitnessOfBest[species], species);
  65.             }
  66.             catch(IOException e) {
  67.                 e.printStackTrace();
  68.             }
  69.         }
  70.        
  71.         return newGeneration;
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment