blob: 19dab38364fd3d66dfb33a185fb5c4c57169eee4 [file] [log] [blame]
Raymonddee08492015-04-02 10:43:13 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.apache.commons.math.genetics;
18
19import java.util.ArrayList;
20import java.util.List;
21
22
23/**
24 * Chromosome represented by a vector of 0s and 1s.
25 *
26 * @version $Revision: 811685 $ $Date: 2009-09-05 19:36:48 +0200 (sam. 05 sept. 2009) $
27 * @since 2.0
28 */
29public abstract class BinaryChromosome extends AbstractListChromosome<Integer> {
30
31 /**
32 * Constructor.
33 * @param representation list of {0,1} values representing the chromosome
34 */
35 public BinaryChromosome(List<Integer> representation) {
36 super(representation);
37 }
38
39 /**
40 * Constructor.
41 * @param representation array of {0,1} values representing the chromosome
42 */
43 public BinaryChromosome(Integer[] representation) {
44 super(representation);
45 }
46
47 /**
48 * {@inheritDoc}
49 */
50 @Override
51 protected void checkValidity(List<Integer> chromosomeRepresentation) throws InvalidRepresentationException {
52 for (int i : chromosomeRepresentation) {
53 if (i < 0 || i >1)
54 throw new InvalidRepresentationException("Elements can be only 0 or 1.");
55 }
56 }
57
58 /**
59 * Returns a representation of a random binary array of length <code>length</code>.
60 * @param length length of the array
61 * @return a random binary array of length <code>length</code>
62 */
63 public static List<Integer> randomBinaryRepresentation(int length) {
64 // random binary list
65 List<Integer> rList= new ArrayList<Integer> (length);
66 for (int j=0; j<length; j++) {
67 rList.add(GeneticAlgorithm.getRandomGenerator().nextInt(2));
68 }
69 return rList;
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
76 protected boolean isSame(Chromosome another) {
77 // type check
78 if (! (another instanceof BinaryChromosome))
79 return false;
80 BinaryChromosome anotherBc = (BinaryChromosome) another;
81 // size check
82 if (getLength() != anotherBc.getLength())
83 return false;
84
85 for (int i=0; i< getRepresentation().size(); i++) {
86 if (!(getRepresentation().get(i).equals(anotherBc.getRepresentation().get(i))))
87 return false;
88 }
89 // all is ok
90 return true;
91 }
92}