Home | History | Annotate | Download | only in genetics
      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 import org.apache.commons.math.util.FastMath;
     23 
     24 /**
     25  * Population of chromosomes which uses elitism (certain percentace of the best
     26  * chromosomes is directly copied to the next generation).
     27  *
     28  * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 aot 2010) $
     29  * @since 2.0
     30  */
     31 public class ElitisticListPopulation extends ListPopulation {
     32 
     33     /** percentage of chromosomes copied to the next generation */
     34     private double elitismRate = 0.9;
     35 
     36     /**
     37      * Creates a new ElitisticListPopulation instance.
     38      *
     39      * @param chromosomes
     40      *            list of chromosomes in the population
     41      * @param populationLimit
     42      *            maximal size of the population
     43      * @param elitismRate
     44      *            how many best chromosomes will be directly transferred to the
     45      *            next generation [in %]
     46      */
     47     public ElitisticListPopulation(List<Chromosome> chromosomes, int populationLimit, double elitismRate) {
     48         super(chromosomes, populationLimit);
     49         this.elitismRate = elitismRate;
     50     }
     51 
     52     /**
     53      * Creates a new ListPopulation instance and initializes its inner
     54      * chromosome list.
     55      *
     56      * @param populationLimit maximal size of the population
     57      * @param elitismRate
     58      *            how many best chromosomes will be directly transferred to the
     59      *            next generation [in %]
     60      */
     61     public ElitisticListPopulation(int populationLimit, double elitismRate) {
     62         super(populationLimit);
     63         this.elitismRate = elitismRate;
     64     }
     65 
     66     /**
     67      * Start the population for the next generation. The
     68      * <code>{@link #elitismRate}<code> percents of the best
     69      * chromosomes are directly copied to the next generation.
     70      *
     71      * @return the beginnings of the next generation.
     72      */
     73     public Population nextGeneration() {
     74         // initialize a new generation with the same parameters
     75         ElitisticListPopulation nextGeneration = new ElitisticListPopulation(this.getPopulationLimit(), this.getElitismRate());
     76 
     77         List<Chromosome> oldChromosomes = this.getChromosomes();
     78         Collections.sort(oldChromosomes);
     79 
     80         // index of the last "not good enough" chromosome
     81         int boundIndex = (int) FastMath.ceil((1.0 - this.getElitismRate()) * oldChromosomes.size());
     82         for (int i=boundIndex; i<oldChromosomes.size(); i++) {
     83             nextGeneration.addChromosome(oldChromosomes.get(i));
     84         }
     85         return nextGeneration;
     86     }
     87 
     88     /**
     89      * Sets the elitism rate, i.e. how many best chromosomes will be directly
     90      * transferred to the next generation [in %].
     91      *
     92      * @param elitismRate
     93      *            how many best chromosomes will be directly transferred to the
     94      *            next generation [in %]
     95      */
     96     public void setElitismRate(double elitismRate) {
     97         if (elitismRate < 0 || elitismRate > 1)
     98             throw new IllegalArgumentException("Elitism rate has to be in [0,1]");
     99         this.elitismRate = elitismRate;
    100     }
    101 
    102     /**
    103      * Access the elitism rate.
    104      * @return the elitism rate
    105      */
    106     public double getElitismRate() {
    107         return this.elitismRate;
    108     }
    109 
    110 }
    111