Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void produceNextGen() {
- LinkedList<Candidate> newPopulation = new LinkedList<Candidate>();
- //TODO: improve selection algorithm
- while(newPopulation.size()<populationSize) {
- int[] pos = getPermutation(4); // pick 4 candidates
- Candidate c1 = population.get(pos[0]);
- Candidate c2 = population.get(pos[1]);
- Candidate c3 = population.get(pos[2]);
- Candidate c4 = population.get(pos[3]);
- // pick 2 winners
- Candidate w1, w2;
- w1 = (c1.score() < c2.score()) ? c1 : c2;
- w2 = (c3.score() < c4.score()) ? c3 : c4;
- // cross-over the winners
- Candidate d1 = newChild(w1, w2, rnd.nextInt(numElements-2)+1);
- Candidate d2 = newChild(w1, w2, rnd.nextInt(numElements-2)+1);
- // mutate children
- if(rnd.nextInt(100)<mutationRatePercent) {
- mutate(d1);
- }
- if(rnd.nextInt(100)<mutationRatePercent) {
- mutate(d2);
- }
- // add children or the winners, based on the score
- newPopulation.add( (d1.score()<w1.score()) ? d1 : w1 );
- newPopulation.add( (d2.score()<w2.score()) ? d1 : w2 );
- }
- population = newPopulation;
- //Collections.sort(population, candidateComparator);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement