AbstractListChromosome.java revision dee0849a9704d532af0b550146cbafbaa6ee1d19
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.commons.math.genetics; 18 19import java.util.ArrayList; 20import java.util.Arrays; 21import java.util.Collections; 22import java.util.List; 23 24/** 25 * Chromosome represented by an immutable list of a fixed length. 26 * 27 * @param <T> type of the representation list 28 * @version $Revision: 811685 $ $Date: 2009-09-05 19:36:48 +0200 (sam. 05 sept. 2009) $ 29 * @since 2.0 30 */ 31public abstract class AbstractListChromosome<T> extends Chromosome { 32 33 /** List representing the chromosome */ 34 private final List<T> representation; 35 36 /** 37 * Constructor. 38 * @param representation inner representation of the chromosome 39 */ 40 public AbstractListChromosome(final List<T> representation) { 41 try { 42 checkValidity(representation); 43 } catch (InvalidRepresentationException e) { 44 throw new IllegalArgumentException(String.format("Invalid representation for %s", getClass().getSimpleName()), e); 45 } 46 this.representation = Collections.unmodifiableList(new ArrayList<T> (representation)); 47 } 48 49 /** 50 * Constructor. 51 * @param representation inner representation of the chromosome 52 */ 53 public AbstractListChromosome(final T[] representation) { 54 this(Arrays.asList(representation)); 55 } 56 57 /** 58 * 59 * Asserts that <code>representation</code> can represent a valid chromosome. 60 * @param chromosomeRepresentation representation of the chromosome 61 * @throws InvalidRepresentationException iff the <code>representation</code> can not represent 62 * a valid chromosome 63 */ 64 protected abstract void checkValidity(List<T> chromosomeRepresentation) throws InvalidRepresentationException; 65 66 /** 67 * Returns the (immutable) inner representation of the chromosome. 68 * @return the representation of the chromosome 69 */ 70 protected List<T> getRepresentation() { 71 return representation; 72 } 73 74 /** 75 * Returns the length of the chromosome. 76 * @return the length of the chromosome 77 */ 78 public int getLength() { 79 return getRepresentation().size(); 80 } 81 82 /** 83 * Creates a new instance of the same class as <code>this</code> is, with a 84 * given <code>arrayRepresentation</code>. This is needed in crossover and 85 * mutation operators, where we need a new instance of the same class, but 86 * with different array representation. 87 * 88 * Usually, this method just calls a constructor of the class. 89 * 90 * @param chromosomeRepresentation 91 * the inner array representation of the new chromosome. 92 * @return new instance extended from FixedLengthChromosome with the given 93 * arrayRepresentation 94 */ 95 public abstract AbstractListChromosome<T> newFixedLengthChromosome(final List<T> chromosomeRepresentation); 96 97 /** 98 * {@inheritDoc} 99 */ 100 @Override 101 public String toString() { 102 return String.format("(f=%s %s)", getFitness(), getRepresentation()); 103 } 104} 105