View Javadoc

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