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.math4.genetics;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.math4.exception.util.LocalizedFormats;
023
024
025/**
026 * Chromosome represented by a vector of 0s and 1s.
027 *
028 * @since 2.0
029 */
030public 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<> (length);
071        for (int j=0; j<length; j++) {
072            rList.add(GeneticAlgorithm.getRandomGenerator().nextInt(2));
073        }
074        return rList;
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    protected boolean isSame(Chromosome another) {
080        // type check
081        if (! (another instanceof BinaryChromosome)) {
082            return false;
083        }
084        BinaryChromosome anotherBc = (BinaryChromosome) another;
085        // size check
086        if (getLength() != anotherBc.getLength()) {
087            return false;
088        }
089
090        for (int i=0; i< getRepresentation().size(); i++) {
091            if (!(getRepresentation().get(i).equals(anotherBc.getRepresentation().get(i)))) {
092                return false;
093            }
094        }
095        // all is ok
096        return true;
097    }
098}