BigIntegerGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.2.0).
003  * Copyright (c) 2007-2020 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * 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  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019  */
020 package io.jenetics.ext;
021 
022 import static java.util.Objects.requireNonNull;
023 import static io.jenetics.internal.util.Hashes.hash;
024 import static io.jenetics.util.RandomRegistry.random;
025 
026 import java.io.Serializable;
027 import java.math.BigInteger;
028 import java.util.Objects;
029 import java.util.Random;
030 
031 import io.jenetics.NumericGene;
032 import io.jenetics.internal.util.Requires;
033 import io.jenetics.util.ISeq;
034 import io.jenetics.util.MSeq;
035 import io.jenetics.util.Mean;
036 import io.jenetics.util.RandomRegistry;
037 
038 import io.jenetics.ext.internal.random;
039 
040 /**
041  * Numeric chromosome implementation which holds an arbitrary sized integer
042  * number.
043  *
044  <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">
045  * value-based</a> class; use of identity-sensitive operations (including
046  * reference equality ({@code ==}), identity hash code, or synchronization) on
047  * instances of {@code IntegerGene} may have unpredictable results and should
048  * be avoided.
049  *
050  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
051  @since 3.5
052  @version 5.2
053  */
054 public final class BigIntegerGene
055     implements
056         NumericGene<BigInteger, BigIntegerGene>,
057         Mean<BigIntegerGene>,
058         Serializable
059 {
060     private static final long serialVersionUID = 1L;
061 
062     private static final BigInteger TWO = BigInteger.valueOf(2);
063 
064     private final BigInteger _value;
065     private final BigInteger _min;
066     private final BigInteger _max;
067 
068     private BigIntegerGene(
069         final BigInteger value,
070         final BigInteger min,
071         final BigInteger max
072     ) {
073         _value = requireNonNull(value);
074         _min = requireNonNull(min);
075         _max = requireNonNull(max);
076     }
077 
078     @Deprecated
079     @Override
080     public BigInteger getAllele() {
081         return _value;
082     }
083 
084     @Deprecated
085     @Override
086     public BigInteger getMin() {
087         return _min;
088     }
089 
090     @Deprecated
091     @Override
092     public BigInteger getMax() {
093         return _max;
094     }
095 
096     @Override
097     public BigIntegerGene mean(final BigIntegerGene that) {
098         final BigInteger value = _value.add(that._value).divide(TWO);
099         return of(value, _min, _max);
100     }
101 
102     @Override
103     public BigIntegerGene newInstance(final Number number) {
104         return of(BigInteger.valueOf(number.longValue()), _min, _max);
105     }
106 
107     @Override
108     public BigIntegerGene newInstance(final BigInteger value) {
109         return of(value, _min, _max);
110     }
111 
112     @Override
113     public BigIntegerGene newInstance() {
114         return of(_min, _max);
115     }
116 
117     @Override
118     public int hashCode() {
119         return hash(_value, hash(_min, hash(_max, hash(getClass()))));
120     }
121 
122     @Override
123     public boolean equals(final Object obj) {
124         return obj == this ||
125             obj instanceof BigIntegerGene &&
126             Objects.equals(((BigIntegerGene)obj)._value, _value&&
127             Objects.equals(((BigIntegerGene)obj)._min, _min&&
128             Objects.equals(((BigIntegerGene)obj)._max, _max);
129     }
130 
131     @Override
132     public String toString() {
133         return String.format("[%s]", _value);
134     }
135 
136     /* *************************************************************************
137      * Static factory methods.
138      **************************************************************************/
139 
140     static ISeq<BigIntegerGene> seq(
141         final BigInteger minimum,
142         final BigInteger maximum,
143         final int length
144     ) {
145         Requires.positive(length);
146 
147         final Random r = random();
148 
149         return MSeq.<BigIntegerGene>ofLength(length)
150             .fill(() -> new BigIntegerGene(
151                 random.nextBigInteger(minimum, maximum, r), minimum, maximum))
152             .toISeq();
153     }
154 
155     /**
156      * Create a new random {@code BigIntegerGene} with the given value and the
157      * given range. If the {@code value} isn't within the interval [min, max],
158      * no exception is thrown. In this case the method
159      {@link BigIntegerGene#isValid()} returns {@code false}.
160      *
161      @param value the value of the gene.
162      @param min the minimal valid value of this gene (inclusively).
163      @param max the maximal valid value of this gene (inclusively).
164      @return a new random {@code BigIntegerGene}
165      @throws NullPointerException if one of the arguments is {@code null}
166      */
167     public static BigIntegerGene of(
168         final BigInteger value,
169         final BigInteger min,
170         final BigInteger max
171     ) {
172         return new BigIntegerGene(value, min, max);
173     }
174 
175     /**
176      * Create a new random {@code BigIntegerGene}. It is guaranteed that the
177      * value of the {@code BigIntegerGene} lies in the interval [min, max].
178      *
179      @param min the minimal valid value of this gene (inclusively).
180      @param max the maximal valid value of this gene (inclusively).
181      @return a new random {@code BigIntegerGene}
182      @throws NullPointerException if one of the arguments is {@code null}
183      */
184     public static BigIntegerGene of(final BigInteger min, final BigInteger max) {
185         return of(
186             random.nextBigInteger(min, max, RandomRegistry.random()),
187             min,
188             max
189         );
190     }
191 
192 }