Advertisement
Guest User

selection algorithm

a guest
Dec 11th, 2011
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. void produceNextGen() {
  2.      LinkedList<Candidate> newPopulation = new LinkedList<Candidate>();
  3.      
  4.      //TODO: improve selection algorithm          
  5.      while(newPopulation.size()<populationSize) {
  6.           int[] pos = getPermutation(4); // pick 4 candidates
  7.          
  8.           Candidate c1 = population.get(pos[0]);
  9.           Candidate c2 = population.get(pos[1]);
  10.           Candidate c3 = population.get(pos[2]);
  11.           Candidate c4 = population.get(pos[3]);
  12.  
  13.           // pick 2 winners
  14.           Candidate w1, w2;
  15.           w1 = (c1.score() < c2.score()) ? c1 : c2;
  16.           w2 = (c3.score() < c4.score()) ? c3 : c4;
  17.          
  18.           // cross-over the winners
  19.           Candidate d1 = newChild(w1, w2, rnd.nextInt(numElements-2)+1);
  20.           Candidate d2 = newChild(w1, w2, rnd.nextInt(numElements-2)+1);
  21.          
  22.           // mutate children
  23.           if(rnd.nextInt(100)<mutationRatePercent) {
  24.                mutate(d1);
  25.           }
  26.           if(rnd.nextInt(100)<mutationRatePercent) {
  27.                mutate(d2);
  28.           }
  29.          
  30.           // add children or the winners, based on the score
  31.           newPopulation.add( (d1.score()<w1.score()) ? d1 : w1  );
  32.           newPopulation.add( (d2.score()<w2.score()) ? d1 : w2  );
  33.          
  34.      }
  35.      
  36.      population = newPopulation;
  37.      //Collections.sort(population, candidateComparator);
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement