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 */
017package org.apache.commons.math3.genetics;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collections;
022import 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 * @since 2.0
029 */
030public abstract class AbstractListChromosome<T> extends Chromosome {
031
032    /** List representing the chromosome */
033    private final List<T> representation;
034
035    /**
036     * Constructor, copying the input representation.
037     * @param representation inner representation of the chromosome
038     * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
039     */
040    public AbstractListChromosome(final List<T> representation) throws InvalidRepresentationException {
041        this(representation, true);
042    }
043
044    /**
045     * Constructor, copying the input representation.
046     * @param representation inner representation of the chromosome
047     * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
048     */
049    public AbstractListChromosome(final T[] representation) throws InvalidRepresentationException {
050        this(Arrays.asList(representation));
051    }
052
053    /**
054     * Constructor.
055     * @param representation inner representation of the chromosome
056     * @param copyList if {@code true}, the representation will be copied, otherwise it will be referenced.
057     * @since 3.3
058     */
059    public AbstractListChromosome(final List<T> representation, final boolean copyList) {
060        checkValidity(representation);
061        this.representation =
062                Collections.unmodifiableList(copyList ? new ArrayList<T>(representation) : representation);
063    }
064
065    /**
066     * Asserts that <code>representation</code> can represent a valid chromosome.
067     *
068     * @param chromosomeRepresentation representation of the chromosome
069     * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
070     */
071    protected abstract void checkValidity(List<T> chromosomeRepresentation) throws InvalidRepresentationException;
072
073    /**
074     * Returns the (immutable) inner representation of the chromosome.
075     * @return the representation of the chromosome
076     */
077    protected List<T> getRepresentation() {
078        return representation;
079    }
080
081    /**
082     * Returns the length of the chromosome.
083     * @return the length of the chromosome
084     */
085    public int getLength() {
086        return getRepresentation().size();
087    }
088
089    /**
090     * Creates a new instance of the same class as <code>this</code> is, with a given <code>arrayRepresentation</code>.
091     * This is needed in crossover and mutation operators, where we need a new instance of the same class, but with
092     * different array representation.
093     * <p>
094     * Usually, this method just calls a constructor of the class.
095     *
096     * @param chromosomeRepresentation the inner array representation of the new chromosome.
097     * @return new instance extended from FixedLengthChromosome with the given arrayRepresentation
098     */
099    public abstract AbstractListChromosome<T> newFixedLengthChromosome(final List<T> chromosomeRepresentation);
100
101    /** {@inheritDoc} */
102    @Override
103    public String toString() {
104        return String.format("(f=%s %s)", getFitness(), getRepresentation());
105    }
106}