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.math3.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 $Id: AbstractListChromosome.java 1416643 2012-12-03 19:37:14Z tn $
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         * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
040         */
041        public AbstractListChromosome(final List<T> representation) throws InvalidRepresentationException {
042            checkValidity(representation);
043            this.representation = Collections.unmodifiableList(new ArrayList<T> (representation));
044        }
045    
046        /**
047         * Constructor.
048         * @param representation inner representation of the chromosome
049         * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
050         */
051        public AbstractListChromosome(final T[] representation) throws InvalidRepresentationException {
052            this(Arrays.asList(representation));
053        }
054    
055        /**
056         * Asserts that <code>representation</code> can represent a valid chromosome.
057         *
058         * @param chromosomeRepresentation representation of the chromosome
059         * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
060         */
061        protected abstract void checkValidity(List<T> chromosomeRepresentation) throws InvalidRepresentationException;
062    
063        /**
064         * Returns the (immutable) inner representation of the chromosome.
065         * @return the representation of the chromosome
066         */
067        protected List<T> getRepresentation() {
068            return representation;
069        }
070    
071        /**
072         * Returns the length of the chromosome.
073         * @return the length of the chromosome
074         */
075        public int getLength() {
076            return getRepresentation().size();
077        }
078    
079        /**
080         * Creates a new instance of the same class as <code>this</code> is, with a given <code>arrayRepresentation</code>.
081         * This is needed in crossover and mutation operators, where we need a new instance of the same class, but with
082         * different array representation.
083         * <p>
084         * Usually, this method just calls a constructor of the class.
085         *
086         * @param chromosomeRepresentation the inner array representation of the new chromosome.
087         * @return new instance extended from FixedLengthChromosome with the given arrayRepresentation
088         */
089        public abstract AbstractListChromosome<T> newFixedLengthChromosome(final List<T> chromosomeRepresentation);
090    
091        @Override
092        public String toString() {
093            return String.format("(f=%s %s)", getFitness(), getRepresentation());
094        }
095    }