1 /*
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 */
17 package org.apache.commons.math4.legacy.genetics;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
23
24
25 /**
26 * Chromosome represented by a vector of 0s and 1s.
27 *
28 * @since 2.0
29 */
30 public abstract class BinaryChromosome extends AbstractListChromosome<Integer> {
31
32 /**
33 * Constructor.
34 * @param representation list of {0,1} values representing the chromosome
35 * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
36 */
37 public BinaryChromosome(List<Integer> representation) throws InvalidRepresentationException {
38 super(representation);
39 }
40
41 /**
42 * Constructor.
43 * @param representation array of {0,1} values representing the chromosome
44 * @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
45 */
46 public BinaryChromosome(Integer[] representation) throws InvalidRepresentationException {
47 super(representation);
48 }
49
50 /**
51 * {@inheritDoc}
52 */
53 @Override
54 protected void checkValidity(List<Integer> chromosomeRepresentation) throws InvalidRepresentationException {
55 for (int i : chromosomeRepresentation) {
56 if (i < 0 || i >1) {
57 throw new InvalidRepresentationException(LocalizedFormats.INVALID_BINARY_DIGIT,
58 i);
59 }
60 }
61 }
62
63 /**
64 * Returns a representation of a random binary array of length <code>length</code>.
65 * @param length length of the array
66 * @return a random binary array of length <code>length</code>
67 */
68 public static List<Integer> randomBinaryRepresentation(int length) {
69 // random binary list
70 List<Integer> rList= new ArrayList<> (length);
71 for (int j=0; j<length; j++) {
72 rList.add(GeneticAlgorithm.getRandomGenerator().nextInt(2));
73 }
74 return rList;
75 }
76
77 /** {@inheritDoc} */
78 @Override
79 protected boolean isSame(Chromosome another) {
80 // type check
81 if (! (another instanceof BinaryChromosome)) {
82 return false;
83 }
84 BinaryChromosome anotherBc = (BinaryChromosome) another;
85 // size check
86 if (getLength() != anotherBc.getLength()) {
87 return false;
88 }
89
90 for (int i=0; i< getRepresentation().size(); i++) {
91 if (!(getRepresentation().get(i).equals(anotherBc.getRepresentation().get(i)))) {
92 return false;
93 }
94 }
95 // all is ok
96 return true;
97 }
98 }