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