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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.genetics;
19
import java.util.Collections;
20
import java.util.List;
23
* Population of chromosomes which uses elitism (certain percentace of the best
24
* chromosomes is directly copied to the next generation).
26
* @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
29
public class ElitisticListPopulation extends ListPopulation {
31
/** percentage of chromosomes copied to the next generation */
32
private double elitismRate = 0.9;
35
* Creates a new ElitisticListPopulation instance.
38
* list of chromosomes in the population
39
* @param populationLimit
40
* maximal size of the population
42
* how many best chromosomes will be directly transferred to the
43
* next generation [in %]
45
public ElitisticListPopulation(List<Chromosome> chromosomes, int populationLimit, double elitismRate) {
46
super(chromosomes, populationLimit);
47
this.elitismRate = elitismRate;
51
* Creates a new ListPopulation instance and initializes its inner
54
* @param populationLimit maximal size of the population
56
* how many best chromosomes will be directly transferred to the
57
* next generation [in %]
59
public ElitisticListPopulation(int populationLimit, double elitismRate) {
60
super(populationLimit);
61
this.elitismRate = elitismRate;
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.
69
* @return the beginnings of the next generation.
71
public Population nextGeneration() {
72
// initialize a new generation with the same parameters
73
ElitisticListPopulation nextGeneration = new ElitisticListPopulation(this.getPopulationLimit(), this.getElitismRate());
75
List<Chromosome> oldChromosomes = this.getChromosomes();
76
Collections.sort(oldChromosomes);
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));
83
return nextGeneration;
87
* Sets the elitism rate, i.e. how many best chromosomes will be directly
88
* transferred to the next generation [in %].
91
* how many best chromosomes will be directly transferred to the
92
* next generation [in %]
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;
101
* Access the elitism rate.
102
* @return the elitism rate
104
public double getElitismRate() {
105
return this.elitismRate;