BinaryChromosome.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.List;

  20. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;


  21. /**
  22.  * Chromosome represented by a vector of 0s and 1s.
  23.  *
  24.  * @since 2.0
  25.  */
  26. public abstract class BinaryChromosome extends AbstractListChromosome<Integer> {

  27.     /**
  28.      * Constructor.
  29.      * @param representation list of {0,1} values representing the chromosome
  30.      * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
  31.      */
  32.     public BinaryChromosome(List<Integer> representation) throws InvalidRepresentationException {
  33.         super(representation);
  34.     }

  35.     /**
  36.      * Constructor.
  37.      * @param representation array of {0,1} values representing the chromosome
  38.      * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
  39.      */
  40.     public BinaryChromosome(Integer[] representation) throws InvalidRepresentationException {
  41.         super(representation);
  42.     }

  43.     /**
  44.      * {@inheritDoc}
  45.      */
  46.     @Override
  47.     protected void checkValidity(List<Integer> chromosomeRepresentation) throws InvalidRepresentationException {
  48.         for (int i : chromosomeRepresentation) {
  49.             if (i < 0 || i >1) {
  50.                 throw new InvalidRepresentationException(LocalizedFormats.INVALID_BINARY_DIGIT,
  51.                                                          i);
  52.             }
  53.         }
  54.     }

  55.     /**
  56.      * Returns a representation of a random binary array of length <code>length</code>.
  57.      * @param length length of the array
  58.      * @return a random binary array of length <code>length</code>
  59.      */
  60.     public static List<Integer> randomBinaryRepresentation(int length) {
  61.         // random binary list
  62.         List<Integer> rList= new ArrayList<> (length);
  63.         for (int j=0; j<length; j++) {
  64.             rList.add(GeneticAlgorithm.getRandomGenerator().nextInt(2));
  65.         }
  66.         return rList;
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     protected boolean isSame(Chromosome another) {
  71.         // type check
  72.         if (! (another instanceof BinaryChromosome)) {
  73.             return false;
  74.         }
  75.         BinaryChromosome anotherBc = (BinaryChromosome) another;
  76.         // size check
  77.         if (getLength() != anotherBc.getLength()) {
  78.             return false;
  79.         }

  80.         for (int i=0; i< getRepresentation().size(); i++) {
  81.             if (!(getRepresentation().get(i).equals(anotherBc.getRepresentation().get(i)))) {
  82.                 return false;
  83.             }
  84.         }
  85.         // all is ok
  86.         return true;
  87.     }
  88. }