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