View Javadoc
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  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import 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   * @since 2.0
29   */
30  public abstract class AbstractListChromosome<T> extends Chromosome {
31  
32      /** List representing the chromosome. */
33      private final List<T> representation;
34  
35      /**
36       * Constructor, copying the input representation.
37       * @param representation inner representation of the chromosome
38       * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
39       */
40      public AbstractListChromosome(final List<T> representation) throws InvalidRepresentationException {
41          this(representation, true);
42      }
43  
44      /**
45       * Constructor, copying the input representation.
46       * @param representation inner representation of the chromosome
47       * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
48       */
49      public AbstractListChromosome(final T[] representation) throws InvalidRepresentationException {
50          this(Arrays.asList(representation));
51      }
52  
53      /**
54       * Constructor.
55       * @param representation inner representation of the chromosome
56       * @param copyList if {@code true}, the representation will be copied, otherwise it will be referenced.
57       * @since 3.3
58       */
59      public AbstractListChromosome(final List<T> representation, final boolean copyList) {
60          checkValidity(representation);
61          this.representation =
62                  Collections.unmodifiableList(copyList ? new ArrayList<>(representation) : representation);
63      }
64  
65      /**
66       * Asserts that <code>representation</code> can represent a valid chromosome.
67       *
68       * @param chromosomeRepresentation representation of the chromosome
69       * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
70       */
71      protected abstract void checkValidity(List<T> chromosomeRepresentation) throws InvalidRepresentationException;
72  
73      /**
74       * Returns the (immutable) inner representation of the chromosome.
75       * @return the representation of the chromosome
76       */
77      protected List<T> getRepresentation() {
78          return representation;
79      }
80  
81      /**
82       * Returns the length of the chromosome.
83       * @return the length of the chromosome
84       */
85      public int getLength() {
86          return getRepresentation().size();
87      }
88  
89      /**
90       * Creates a new instance of the same class as <code>this</code> is, with a given <code>arrayRepresentation</code>.
91       * This is needed in crossover and mutation operators, where we need a new instance of the same class, but with
92       * different array representation.
93       * <p>
94       * Usually, this method just calls a constructor of the class.
95       *
96       * @param chromosomeRepresentation the inner array representation of the new chromosome.
97       * @return new instance extended from FixedLengthChromosome with the given arrayRepresentation
98       */
99      public abstract AbstractListChromosome<T> newFixedLengthChromosome(List<T> chromosomeRepresentation);
100 
101     /** {@inheritDoc} */
102     @Override
103     public String toString() {
104         return String.format("(f=%s %s)", getFitness(), getRepresentation());
105     }
106 }