001 /*
002 * Java Genetic Algorithm Library (jenetics-6.3.0).
003 * Copyright (c) 2007-2021 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;
021
022 import static io.jenetics.internal.math.Randoms.nextDouble;
023 import static io.jenetics.internal.util.Hashes.hash;
024 import static io.jenetics.util.RandomRegistry.random;
025
026 import java.io.DataInput;
027 import java.io.DataOutput;
028 import java.io.IOException;
029 import java.io.InvalidObjectException;
030 import java.io.ObjectInputStream;
031 import java.io.Serializable;
032 import java.util.Random;
033
034 import io.jenetics.internal.math.Randoms;
035 import io.jenetics.util.DoubleRange;
036 import io.jenetics.util.ISeq;
037 import io.jenetics.util.IntRange;
038 import io.jenetics.util.MSeq;
039 import io.jenetics.util.Mean;
040
041 /**
042 * Implementation of the NumericGene which holds a 64 bit floating point 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 DoubleGene} may have unpredictable results and should
048 * be avoided.
049 *
050 * @see DoubleChromosome
051 *
052 * @implNote
053 * This class is immutable and thread-safe.
054 *
055 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
056 * @since 1.6
057 * @version 6.0
058 */
059 public final class DoubleGene
060 implements
061 NumericGene<Double, DoubleGene>,
062 Mean<DoubleGene>,
063 Comparable<DoubleGene>,
064 Serializable
065 {
066
067 private static final long serialVersionUID = 2L;
068
069 private final double _allele;
070 private final double _min;
071 private final double _max;
072
073 /**
074 * Create a new random {@code DoubleGene} with the given value and the
075 * given range. If the {@code value} isn't within the interval [min, max),
076 * no exception is thrown. In this case the method
077 * {@link DoubleGene#isValid()} returns {@code false}.
078 *
079 * @param allele the value of the gene.
080 * @param min the minimal valid value of this gene (inclusively).
081 * @param max the maximal valid value of this gene (exclusively).
082 */
083 private DoubleGene(final double allele, final double min, final double max) {
084 _allele = allele;
085 _min = min;
086 _max = max;
087 }
088
089 @Override
090 public Double allele() {
091 return _allele;
092 }
093
094 @Override
095 public Double min() {
096 return _min;
097 }
098
099 @Override
100 public Double max() {
101 return _max;
102 }
103
104 /**
105 * Return the range of {@code this} gene.
106 *
107 * @since 4.4
108 *
109 * @return the range of {@code this} gene
110 */
111 public DoubleRange range() {
112 return DoubleRange.of(_min, _max);
113 }
114
115 @Override
116 public byte byteValue() {
117 return (byte)_allele;
118 }
119
120 @Override
121 public short shortValue() {
122 return (short)_allele;
123 }
124
125 @Override
126 public int intValue() {
127 return (int)_allele;
128 }
129
130 @Override
131 public long longValue() {
132 return (long)_allele;
133 }
134
135 @Override
136 public float floatValue() {
137 return (float)_allele;
138 }
139
140 @Override
141 public double doubleValue() {
142 return _allele;
143 }
144
145 @Override
146 public boolean isValid() {
147 return
148 Double.isFinite(_allele) &&
149 Double.isFinite(_min) &&
150 Double.isFinite(_max) &&
151 Double.compare(_allele, _min) >= 0 &&
152 Double.compare(_allele, _max) < 0;
153 }
154
155 @Override
156 public int compareTo(final DoubleGene other) {
157 return Double.compare(_allele, other._allele);
158 }
159
160 @Override
161 public DoubleGene mean(final DoubleGene that) {
162 return of(_allele + (that._allele - _allele)/2.0, _min, _max);
163 }
164
165 /**
166 * Create a new gene from the given {@code value} and the gene context.
167 *
168 * @since 5.0
169 * @param allele the value of the new gene.
170 * @return a new gene with the given value.
171 */
172 public DoubleGene newInstance(final double allele) {
173 return DoubleGene.of(allele, _min, _max);
174 }
175
176 @Override
177 public DoubleGene newInstance(final Double allele) {
178 return of(allele, _min, _max);
179 }
180
181 @Override
182 public DoubleGene newInstance(final Number allele) {
183 return of(allele.doubleValue(), _min, _max);
184 }
185
186 @Override
187 public DoubleGene newInstance() {
188 return of(nextDouble(_min, _max, random()), _min, _max);
189 }
190
191 @Override
192 public int hashCode() {
193 return hash(_allele, hash(_min, hash(_max)));
194 }
195
196 @Override
197 public boolean equals(final Object obj) {
198 return obj == this ||
199 obj instanceof DoubleGene &&
200 Double.compare(((DoubleGene)obj)._allele, _allele) == 0 &&
201 Double.compare(((DoubleGene)obj)._min, _min) == 0 &&
202 Double.compare(((DoubleGene)obj)._max, _max) == 0;
203 }
204
205 @Override
206 public String toString() {
207 return String.format("[%s]", _allele);
208 }
209
210
211 /* *************************************************************************
212 * Static factory methods.
213 * ************************************************************************/
214
215 /**
216 * Create a new random {@code DoubleGene} with the given value and the
217 * given range. If the {@code value} isn't within the interval [min, max),
218 * no exception is thrown. In this case the method
219 * {@link DoubleGene#isValid()} returns {@code false}.
220 *
221 * @param allele the value of the gene.
222 * @param min the minimal valid value of this gene (inclusively).
223 * @param max the maximal valid value of this gene (exclusively).
224 * @return a new {@code DoubleGene} with the given parameter
225 */
226 public static DoubleGene of(
227 final double allele,
228 final double min,
229 final double max
230 ) {
231 return new DoubleGene(allele, min, max);
232 }
233
234 /**
235 * Create a new random {@code DoubleGene} with the given value and the
236 * given range. If the {@code value} isn't within the interval [min, max),
237 * no exception is thrown. In this case the method
238 * {@link DoubleGene#isValid()} returns {@code false}.
239 *
240 * @since 3.2
241 *
242 * @param allele the value of the gene.
243 * @param range the double range to use
244 * @return a new random {@code DoubleGene}
245 * @throws NullPointerException if the given {@code range} is {@code null}.
246 */
247 public static DoubleGene of(final double allele, final DoubleRange range) {
248 return of(allele, range.min(), range.max());
249 }
250
251 /**
252 * Create a new random {@code DoubleGene}. It is guaranteed that the value
253 * of the {@code DoubleGene} lies in the interval [min, max).
254 *
255 * @param min the minimal valid value of this gene (inclusively).
256 * @param max the maximal valid value of this gene (exclusively).
257 * @return a new {@code DoubleGene} with the given parameter
258 */
259 public static DoubleGene of(final double min, final double max) {
260 return of(nextDouble(min, max, random()), min, max);
261 }
262
263 /**
264 * Create a new random {@code DoubleGene}. It is guaranteed that the value
265 * of the {@code DoubleGene} lies in the interval [min, max).
266 *
267 * @since 3.2
268 *
269 * @param range the double range to use
270 * @return a new {@code DoubleGene} with the given parameter
271 * @throws NullPointerException if the given {@code range} is {@code null}.
272 */
273 public static DoubleGene of(final DoubleRange range) {
274 return of(nextDouble(range.min(), range.max(), random()), range);
275 }
276
277 static ISeq<DoubleGene> seq(
278 final double min,
279 final double max,
280 final IntRange lengthRange
281 ) {
282 final Random r = random();
283 return MSeq.<DoubleGene>ofLength(Randoms.nextInt(lengthRange, r))
284 .fill(() -> new DoubleGene(nextDouble(min, max, r), min, max))
285 .toISeq();
286 }
287
288
289 /* *************************************************************************
290 * Java object serialization
291 * ************************************************************************/
292
293 private Object writeReplace() {
294 return new Serial(Serial.DOUBLE_GENE, this);
295 }
296
297 private void readObject(final ObjectInputStream stream)
298 throws InvalidObjectException
299 {
300 throw new InvalidObjectException("Serialization proxy required.");
301 }
302
303 void write(final DataOutput out) throws IOException {
304 out.writeDouble(_allele);
305 out.writeDouble(_min);
306 out.writeDouble(_max);
307 }
308
309 static DoubleGene read(final DataInput in) throws IOException {
310 return of(in.readDouble(), in.readDouble(), in.readDouble());
311 }
312
313 }
|