001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.genetics;
018
019 import java.util.ArrayList;
020 import java.util.Iterator;
021 import java.util.List;
022
023 /**
024 * Population of chromosomes represented by a {@link List}.
025 *
026 * @since 2.0
027 * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
028 */
029 public abstract class ListPopulation implements Population {
030
031 /** List of chromosomes */
032 private List<Chromosome> chromosomes;
033
034 /** maximial size of the population */
035 private int populationLimit;
036
037
038 /**
039 * Creates a new ListPopulation instance.
040 *
041 * @param chromosomes list of chromosomes in the population
042 * @param populationLimit maximal size of the population
043 */
044 public ListPopulation (List<Chromosome> chromosomes, int populationLimit) {
045 if (chromosomes.size() > populationLimit) {
046 throw new IllegalArgumentException("List of chromosomes bigger than maxPopulationSize.");
047 }
048 if (populationLimit < 0) {
049 throw new IllegalArgumentException("Population limit has to be >= 0");
050 }
051
052 this.chromosomes = chromosomes;
053 this.populationLimit = populationLimit;
054 }
055
056 /**
057 * Creates a new ListPopulation instance and initializes its inner
058 * chromosome list.
059 *
060 * @param populationLimit maximal size of the population
061 */
062 public ListPopulation (int populationLimit) {
063 if (populationLimit < 0) {
064 throw new IllegalArgumentException("Population limit has to be >= 0");
065 }
066 this.populationLimit = populationLimit;
067 this.chromosomes = new ArrayList<Chromosome>(populationLimit);
068 }
069
070 /**
071 * Sets the list of chromosomes.
072 * @param chromosomes the list of chromosomes
073 */
074 public void setChromosomes(List<Chromosome> chromosomes) {
075 this.chromosomes = chromosomes;
076 }
077
078 /**
079 * Access the list of chromosomes.
080 * @return the list of chromosomes
081 */
082 public List<Chromosome> getChromosomes() {
083 return chromosomes;
084 }
085
086 /**
087 * Add the given chromosome to the population.
088 * @param chromosome the chromosome to add.
089 */
090 public void addChromosome(Chromosome chromosome) {
091 this.chromosomes.add(chromosome);
092 }
093
094 /**
095 * Access the fittest chromosome in this population.
096 * @return the fittest chromosome.
097 */
098 public Chromosome getFittestChromosome() {
099 // 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 }