    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.Arrays;
    021    import java.util.Collections;
    022    import java.util.List;
    023
    024    /**
    025     * Chromosome represented by an immutable list of a fixed length.
    026     *
    027     * @param <T> type of the representation list
    028     * @version $Revision: 811685 $ $Date: 2009-09-05 19:36:48 +0200 (sam. 05 sept. 2009) $
    029     * @since 2.0
    030     */
    031    public abstract class AbstractListChromosome<T> extends Chromosome {
    032
    033        /** List representing the chromosome */
    034        private final List<T> representation;
    035
    036        /**
    037         * Constructor.
    038         * @param representation inner representation of the chromosome
    039         */
    040        public AbstractListChromosome(final List<T> representation) {
    041            try {
    042                checkValidity(representation);
    043            } catch (InvalidRepresentationException e) {
    044                throw new IllegalArgumentException(String.format("Invalid representation for %s", getClass().getSimpleName()), e);
    045            }
    046            this.representation = Collections.unmodifiableList(new ArrayList<T> (representation));
    047        }
    048
    049        /**
    050         * Constructor.
    051         * @param representation inner representation of the chromosome
    052         */
    053        public AbstractListChromosome(final T[] representation) {
    054            this(Arrays.asList(representation));
    055        }
    056
    057        /**
    058         *
    059         * Asserts that <code>representation</code> can represent a valid chromosome.
    060         * @param chromosomeRepresentation representation of the chromosome
    061         * @throws InvalidRepresentationException iff the <code>representation</code> can not represent
    062         *         a valid chromosome
    063         */
    064        protected abstract void checkValidity(List<T> chromosomeRepresentation) throws InvalidRepresentationException;
    065
    066        /**
    067         * Returns the (immutable) inner representation of the chromosome.
    068         * @return the representation of the chromosome
    069         */
    070        protected List<T> getRepresentation() {
    071            return representation;
    072        }
    073
    074        /**
    075         * Returns the length of the chromosome.
    076         * @return the length of the chromosome
    077         */
    078        public int getLength() {
    079            return getRepresentation().size();
    080        }
    081
    082        /**
    083         * Creates a new instance of the same class as <code>this</code> is, with a
    084         * given <code>arrayRepresentation</code>. This is needed in crossover and
    085         * mutation operators, where we need a new instance of the same class, but
    086         * with different array representation.
    087         *
    088         * Usually, this method just calls a constructor of the class.
    089         *
    090         * @param chromosomeRepresentation
    091         *            the inner array representation of the new chromosome.
    092         * @return new instance extended from FixedLengthChromosome with the given
    093         *         arrayRepresentation
    094         */
    095        public abstract AbstractListChromosome<T> newFixedLengthChromosome(final List<T> chromosomeRepresentation);
    096
    097        /**
    098         * {@inheritDoc}
    099         */
    100        @Override
    101        public String toString() {
    102            return String.format("(f=%s %s)", getFitness(), getRepresentation());
    103        }
    104    }