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 import org.apache.commons.math.exception.util.LocalizedFormats;
022
023
024 /**
025 * Chromosome represented by a vector of 0s and 1s.
026 *
027 * @version $Id: BinaryChromosome.java 1139906 2011-06-26 18:42:32Z luc $
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 */
036 public BinaryChromosome(List<Integer> representation) {
037 super(representation);
038 }
039
040 /**
041 * Constructor.
042 * @param representation array of {0,1} values representing the chromosome
043 */
044 public BinaryChromosome(Integer[] representation) {
045 super(representation);
046 }
047
048 /**
049 * {@inheritDoc}
050 */
051 @Override
052 protected void checkValidity(List<Integer> chromosomeRepresentation)
053 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 /**
077 * {@inheritDoc}
078 */
079 @Override
080 protected boolean isSame(Chromosome another) {
081 // type check
082 if (! (another instanceof BinaryChromosome)) {
083 return false;
084 }
085 BinaryChromosome anotherBc = (BinaryChromosome) another;
086 // size check
087 if (getLength() != anotherBc.getLength()) {
088 return false;
089 }
090
091 for (int i=0; i< getRepresentation().size(); i++) {
092 if (!(getRepresentation().get(i).equals(anotherBc.getRepresentation().get(i)))) {
093 return false;
094 }
095 }
096 // all is ok
097 return true;
098 }
099 }