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