AbstractListChromosome.java

  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.  */
  17. package org.apache.commons.math4.legacy.genetics;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.List;

  22. /**
  23.  * Chromosome represented by an immutable list of a fixed length.
  24.  *
  25.  * @param <T> type of the representation list
  26.  * @since 2.0
  27.  */
  28. public abstract class AbstractListChromosome<T> extends Chromosome {

  29.     /** List representing the chromosome. */
  30.     private final List<T> representation;

  31.     /**
  32.      * Constructor, copying the input representation.
  33.      * @param representation inner representation of the chromosome
  34.      * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
  35.      */
  36.     public AbstractListChromosome(final List<T> representation) throws InvalidRepresentationException {
  37.         this(representation, true);
  38.     }

  39.     /**
  40.      * Constructor, copying the input representation.
  41.      * @param representation inner representation of the chromosome
  42.      * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
  43.      */
  44.     public AbstractListChromosome(final T[] representation) throws InvalidRepresentationException {
  45.         this(Arrays.asList(representation));
  46.     }

  47.     /**
  48.      * Constructor.
  49.      * @param representation inner representation of the chromosome
  50.      * @param copyList if {@code true}, the representation will be copied, otherwise it will be referenced.
  51.      * @since 3.3
  52.      */
  53.     public AbstractListChromosome(final List<T> representation, final boolean copyList) {
  54.         checkValidity(representation);
  55.         this.representation =
  56.                 Collections.unmodifiableList(copyList ? new ArrayList<>(representation) : representation);
  57.     }

  58.     /**
  59.      * Asserts that <code>representation</code> can represent a valid chromosome.
  60.      *
  61.      * @param chromosomeRepresentation representation of the chromosome
  62.      * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
  63.      */
  64.     protected abstract void checkValidity(List<T> chromosomeRepresentation) throws InvalidRepresentationException;

  65.     /**
  66.      * Returns the (immutable) inner representation of the chromosome.
  67.      * @return the representation of the chromosome
  68.      */
  69.     protected List<T> getRepresentation() {
  70.         return representation;
  71.     }

  72.     /**
  73.      * Returns the length of the chromosome.
  74.      * @return the length of the chromosome
  75.      */
  76.     public int getLength() {
  77.         return getRepresentation().size();
  78.     }

  79.     /**
  80.      * Creates a new instance of the same class as <code>this</code> is, with a given <code>arrayRepresentation</code>.
  81.      * This is needed in crossover and mutation operators, where we need a new instance of the same class, but with
  82.      * different array representation.
  83.      * <p>
  84.      * Usually, this method just calls a constructor of the class.
  85.      *
  86.      * @param chromosomeRepresentation the inner array representation of the new chromosome.
  87.      * @return new instance extended from FixedLengthChromosome with the given arrayRepresentation
  88.      */
  89.     public abstract AbstractListChromosome<T> newFixedLengthChromosome(List<T> chromosomeRepresentation);

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public String toString() {
  93.         return String.format("(f=%s %s)", getFitness(), getRepresentation());
  94.     }
  95. }