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