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.ArrayList;
     20 import java.util.Iterator;
     21 import java.util.List;
     22 
     23 import org.apache.commons.math.exception.util.LocalizedFormats;
     24 import org.apache.commons.math.exception.NotPositiveException;
     25 import org.apache.commons.math.exception.NumberIsTooLargeException;
     26 
     27 /**
     28  * Population of chromosomes represented by a {@link List}.
     29  *
     30  * @since 2.0
     31  * @version $Revision: 983921 $ $Date: 2010-08-10 12:46:06 +0200 (mar. 10 aot 2010) $
     32  */
     33 public abstract class ListPopulation implements Population {
     34 
     35     /** List of chromosomes */
     36     private List<Chromosome> chromosomes;
     37 
     38     /** maximial size of the population */
     39     private int populationLimit;
     40 
     41 
     42     /**
     43      * Creates a new ListPopulation instance.
     44      *
     45      * @param chromosomes list of chromosomes in the population
     46      * @param populationLimit maximal size of the population
     47      */
     48     public ListPopulation (List<Chromosome> chromosomes, int populationLimit) {
     49         if (chromosomes.size() > populationLimit) {
     50             throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE,
     51                                                 chromosomes.size(), populationLimit, false);
     52         }
     53         if (populationLimit < 0) {
     54             throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
     55         }
     56 
     57         this.chromosomes = chromosomes;
     58         this.populationLimit = populationLimit;
     59     }
     60 
     61     /**
     62      * Creates a new ListPopulation instance and initializes its inner
     63      * chromosome list.
     64      *
     65      * @param populationLimit maximal size of the population
     66      */
     67     public ListPopulation (int populationLimit) {
     68         if (populationLimit < 0) {
     69             throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
     70         }
     71         this.populationLimit = populationLimit;
     72         this.chromosomes = new ArrayList<Chromosome>(populationLimit);
     73     }
     74 
     75     /**
     76      * Sets the list of chromosomes.
     77      * @param chromosomes the list of chromosomes
     78      */
     79     public void setChromosomes(List<Chromosome> chromosomes) {
     80         this.chromosomes = chromosomes;
     81     }
     82 
     83     /**
     84      * Access the list of chromosomes.
     85      * @return the list of chromosomes
     86      */
     87     public List<Chromosome> getChromosomes() {
     88         return chromosomes;
     89     }
     90 
     91     /**
     92      * Add the given chromosome to the population.
     93      * @param chromosome the chromosome to add.
     94      */
     95     public void addChromosome(Chromosome chromosome) {
     96         this.chromosomes.add(chromosome);
     97     }
     98 
     99     /**
    100      * Access the fittest chromosome in this population.
    101      * @return the fittest chromosome.
    102      */
    103     public Chromosome getFittestChromosome() {
    104         // best so far
    105         Chromosome bestChromosome = this.chromosomes.get(0);
    106         for (Chromosome chromosome : this.chromosomes) {
    107             if (chromosome.compareTo(bestChromosome) > 0) {
    108                 // better chromosome found
    109                 bestChromosome = chromosome;
    110             }
    111         }
    112         return bestChromosome;
    113     }
    114 
    115     /**
    116      * Access the maximum population size.
    117      * @return the maximum population size.
    118      */
    119     public int getPopulationLimit() {
    120         return this.populationLimit;
    121     }
    122 
    123     /**
    124      * Sets the maximal population size.
    125      * @param populationLimit maximal population size.
    126      */
    127     public void setPopulationLimit(int populationLimit) {
    128         this.populationLimit = populationLimit;
    129     }
    130 
    131     /**
    132      * Access the current population size.
    133      * @return the current population size.
    134      */
    135     public int getPopulationSize() {
    136         return this.chromosomes.size();
    137     }
    138 
    139     /**
    140      * {@inheritDoc}
    141      */
    142     @Override
    143     public String toString() {
    144         return this.chromosomes.toString();
    145     }
    146 
    147     /**
    148      * Chromosome list iterator
    149      *
    150      * @return chromosome iterator
    151      */
    152     public Iterator<Chromosome> iterator() {
    153         return chromosomes.iterator();
    154     }
    155 }
    156