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 /** {@inheritDoc} */ 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}