BigIntegerGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.2.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.Objects;
028 import java.util.Random;
029 
030 import io.jenetics.NumericGene;
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         int hash = 17;
116         hash += 31*Objects.hashCode(_value37;
117         hash += 31*Objects.hashCode(_min37;
118         hash += 31*Objects.hashCode(_max37;
119         return hash;
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         require.positive(length);
146 
147         final Random r = getRandom();
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.getRandom()),
187             min,
188             max
189         );
190     }
191 
192 }