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