1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.math.genetics;
18
19 import java.util.Collections;
20 import java.util.List;
21
22 /**
23 * Population of chromosomes which uses elitism (certain percentace of the best
24 * chromosomes is directly copied to the next generation).
25 *
26 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
27 * @since 2.0
28 */
29 public class ElitisticListPopulation extends ListPopulation {
30
31 /** percentage of chromosomes copied to the next generation */
32 private double elitismRate = 0.9;
33
34 /**
35 * Creates a new ElitisticListPopulation instance.
36 *
37 * @param chromosomes
38 * list of chromosomes in the population
39 * @param populationLimit
40 * maximal size of the population
41 * @param elitismRate
42 * how many best chromosomes will be directly transferred to the
43 * next generation [in %]
44 */
45 public ElitisticListPopulation(List<Chromosome> chromosomes, int populationLimit, double elitismRate) {
46 super(chromosomes, populationLimit);
47 this.elitismRate = elitismRate;
48 }
49
50 /**
51 * Creates a new ListPopulation instance and initializes its inner
52 * chromosome list.
53 *
54 * @param populationLimit maximal size of the population
55 * @param elitismRate
56 * how many best chromosomes will be directly transferred to the
57 * next generation [in %]
58 */
59 public ElitisticListPopulation(int populationLimit, double elitismRate) {
60 super(populationLimit);
61 this.elitismRate = elitismRate;
62 }
63
64 /**
65 * Start the population for the next generation. The
66 * <code>{@link #elitismRate}<code> percents of the best
67 * chromosomes are directly copied to the next generation.
68 *
69 * @return the beginnings of the next generation.
70 */
71 public Population nextGeneration() {
72 // initialize a new generation with the same parameters
73 ElitisticListPopulation nextGeneration = new ElitisticListPopulation(this.getPopulationLimit(), this.getElitismRate());
74
75 List<Chromosome> oldChromosomes = this.getChromosomes();
76 Collections.sort(oldChromosomes);
77
78 // index of the last "not good enough" chromosome
79 int boundIndex = (int) Math.ceil((1.0 - this.getElitismRate()) * oldChromosomes.size());
80 for (int i=boundIndex; i<oldChromosomes.size(); i++) {
81 nextGeneration.addChromosome(oldChromosomes.get(i));
82 }
83 return nextGeneration;
84 }
85
86 /**
87 * Sets the elitism rate, i.e. how many best chromosomes will be directly
88 * transferred to the next generation [in %].
89 *
90 * @param elitismRate
91 * how many best chromosomes will be directly transferred to the
92 * next generation [in %]
93 */
94 public void setElitismRate(double elitismRate) {
95 if (elitismRate < 0 || elitismRate > 1)
96 throw new IllegalArgumentException("Elitism rate has to be in [0,1]");
97 this.elitismRate = elitismRate;
98 }
99
100 /**
101 * Access the elitism rate.
102 * @return the elitism rate
103 */
104 public double getElitismRate() {
105 return this.elitismRate;
106 }
107
108 }