    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    import org.apache.commons.math.exception.util.LocalizedFormats;
    024    import org.apache.commons.math.exception.NotPositiveException;
    025    import org.apache.commons.math.exception.NumberIsTooLargeException;
    026
    027    /**
    028     * Population of chromosomes represented by a {@link List}.
    029     *
    030     * @since 2.0
    031     * @version $Revision: 983921 $ $Date: 2010-08-10 12:46:06 +0200 (mar. 10 ao??t 2010) $
    032     */
    033    public abstract class ListPopulation implements Population {
    034
    035        /** List of chromosomes */
    036        private List<Chromosome> chromosomes;
    037
    038        /** maximial size of the population */
    039        private int populationLimit;
    040
    041
    042        /**
    043         * Creates a new ListPopulation instance.
    044         *
    045         * @param chromosomes list of chromosomes in the population
    046         * @param populationLimit maximal size of the population
    047         */
    048        public ListPopulation (List<Chromosome> chromosomes, int populationLimit) {
    049            if (chromosomes.size() > populationLimit) {
    050                throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE,
    051                                                    chromosomes.size(), populationLimit, false);
    052            }
    053            if (populationLimit < 0) {
    054                throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
    055            }
    056
    057            this.chromosomes = chromosomes;
    058            this.populationLimit = populationLimit;
    059        }
    060
    061        /**
    062         * Creates a new ListPopulation instance and initializes its inner
    063         * chromosome list.
    064         *
    065         * @param populationLimit maximal size of the population
    066         */
    067        public ListPopulation (int populationLimit) {
    068            if (populationLimit < 0) {
    069                throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
    070            }
    071            this.populationLimit = populationLimit;
    072            this.chromosomes = new ArrayList<Chromosome>(populationLimit);
    073        }
    074
    075        /**
    076         * Sets the list of chromosomes.
    077         * @param chromosomes the list of chromosomes
    078         */
    079        public void setChromosomes(List<Chromosome> chromosomes) {
    080            this.chromosomes = chromosomes;
    081        }
    082
    083        /**
    084         * Access the list of chromosomes.
    085         * @return the list of chromosomes
    086         */
    087        public List<Chromosome> getChromosomes() {
    088            return chromosomes;
    089        }
    090
    091        /**
    092         * Add the given chromosome to the population.
    093         * @param chromosome the chromosome to add.
    094         */
    095        public void addChromosome(Chromosome chromosome) {
    096            this.chromosomes.add(chromosome);
    097        }
    098
    099        /**
    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    }