    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.math3.genetics;
    018
    019    import java.util.ArrayList;
    020    import java.util.List;
    021    import org.apache.commons.math3.exception.util.LocalizedFormats;
    022
    023
    024    /**
    025     * Chromosome represented by a vector of 0s and 1s.
    026     *
    027     * @version $Id: BinaryChromosome.java 1416643 2012-12-03 19:37:14Z tn $
    028     * @since 2.0
    029     */
    030    public abstract class BinaryChromosome extends AbstractListChromosome<Integer> {
    031
    032        /**
    033         * Constructor.
    034         * @param representation list of {0,1} values representing the chromosome
    035         * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
    036         */
    037        public BinaryChromosome(List<Integer> representation) throws InvalidRepresentationException {
    038            super(representation);
    039        }
    040
    041        /**
    042         * Constructor.
    043         * @param representation array of {0,1} values representing the chromosome
    044         * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
    045         */
    046        public BinaryChromosome(Integer[] representation) throws InvalidRepresentationException {
    047            super(representation);
    048        }
    049
    050        /**
    051         * {@inheritDoc}
    052         */
    053        @Override
    054        protected void checkValidity(List<Integer> chromosomeRepresentation) throws InvalidRepresentationException {
    055            for (int i : chromosomeRepresentation) {
    056                if (i < 0 || i >1) {
    057                    throw new InvalidRepresentationException(LocalizedFormats.INVALID_BINARY_DIGIT,
    058                                                             i);
    059                }
    060            }
    061        }
    062
    063        /**
    064         * Returns a representation of a random binary array of length <code>length</code>.
    065         * @param length length of the array
    066         * @return a random binary array of length <code>length</code>
    067         */
    068        public static List<Integer> randomBinaryRepresentation(int length) {
    069            // random binary list
    070            List<Integer> rList= new ArrayList<Integer> (length);
    071            for (int j=0; j<length; j++) {
    072                rList.add(GeneticAlgorithm.getRandomGenerator().nextInt(2));
    073            }
    074            return rList;
    075        }
    076
    077        @Override
    078        protected boolean isSame(Chromosome another) {
    079            // type check
    080            if (! (another instanceof BinaryChromosome)) {
    081                return false;
    082            }
    083            BinaryChromosome anotherBc = (BinaryChromosome) another;
    084            // size check
    085            if (getLength() != anotherBc.getLength()) {
    086                return false;
    087            }
    088
    089            for (int i=0; i< getRepresentation().size(); i++) {
    090                if (!(getRepresentation().get(i).equals(anotherBc.getRepresentation().get(i)))) {
    091                    return false;
    092                }
    093            }
    094            // all is ok
    095            return true;
    096        }
    097    }