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