001 /*
002 * Java Genetic Algorithm Library (jenetics-6.0.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;
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 Double.compare(_allele, _min) >= 0 &&
148 Double.compare(_allele, _max) <= 0;
149 }
150
151 @Override
152 public int compareTo(final DoubleGene other) {
153 return Double.compare(_allele, other._allele);
154 }
155
156 @Override
157 public DoubleGene mean(final DoubleGene that) {
158 return of(_allele + (that._allele - _allele)/2.0, _min, _max);
159 }
160
161 /**
162 * Create a new gene from the given {@code value} and the gene context.
163 *
164 * @since 5.0
165 * @param allele the value of the new gene.
166 * @return a new gene with the given value.
167 */
168 public DoubleGene newInstance(final double allele) {
169 return DoubleGene.of(allele, _min, _max);
170 }
171
172 @Override
173 public DoubleGene newInstance(final Double allele) {
174 return of(allele, _min, _max);
175 }
176
177 @Override
178 public DoubleGene newInstance(final Number allele) {
179 return of(allele.doubleValue(), _min, _max);
180 }
181
182 @Override
183 public DoubleGene newInstance() {
184 return of(nextDouble(_min, _max, random()), _min, _max);
185 }
186
187 @Override
188 public int hashCode() {
189 return hash(_allele, hash(_min, hash(_max)));
190 }
191
192 @Override
193 public boolean equals(final Object obj) {
194 return obj == this ||
195 obj instanceof DoubleGene &&
196 Double.compare(((DoubleGene)obj)._allele, _allele) == 0 &&
197 Double.compare(((DoubleGene)obj)._min, _min) == 0 &&
198 Double.compare(((DoubleGene)obj)._max, _max) == 0;
199 }
200
201 @Override
202 public String toString() {
203 return String.format("[%s]", _allele);
204 }
205
206
207 /* *************************************************************************
208 * Static factory methods.
209 * ************************************************************************/
210
211 /**
212 * Create a new random {@code DoubleGene} with the given value and the
213 * given range. If the {@code value} isn't within the interval [min, max),
214 * no exception is thrown. In this case the method
215 * {@link DoubleGene#isValid()} returns {@code false}.
216 *
217 * @param allele the value of the gene.
218 * @param min the minimal valid value of this gene (inclusively).
219 * @param max the maximal valid value of this gene (exclusively).
220 * @return a new {@code DoubleGene} with the given parameter
221 */
222 public static DoubleGene of(
223 final double allele,
224 final double min,
225 final double max
226 ) {
227 return new DoubleGene(allele, min, max);
228 }
229
230 /**
231 * Create a new random {@code DoubleGene} with the given value and the
232 * given range. If the {@code value} isn't within the interval [min, max),
233 * no exception is thrown. In this case the method
234 * {@link DoubleGene#isValid()} returns {@code false}.
235 *
236 * @since 3.2
237 *
238 * @param allele the value of the gene.
239 * @param range the double range to use
240 * @return a new random {@code DoubleGene}
241 * @throws NullPointerException if the given {@code range} is {@code null}.
242 */
243 public static DoubleGene of(final double allele, final DoubleRange range) {
244 return of(allele, range.min(), range.max());
245 }
246
247 /**
248 * Create a new random {@code DoubleGene}. It is guaranteed that the value
249 * of the {@code DoubleGene} lies in the interval [min, max).
250 *
251 * @param min the minimal valid value of this gene (inclusively).
252 * @param max the maximal valid value of this gene (exclusively).
253 * @return a new {@code DoubleGene} with the given parameter
254 */
255 public static DoubleGene of(final double min, final double max) {
256 return of(nextDouble(min, max, random()), min, max);
257 }
258
259 /**
260 * Create a new random {@code DoubleGene}. It is guaranteed that the value
261 * of the {@code DoubleGene} lies in the interval [min, max).
262 *
263 * @since 3.2
264 *
265 * @param range the double range to use
266 * @return a new {@code DoubleGene} with the given parameter
267 * @throws NullPointerException if the given {@code range} is {@code null}.
268 */
269 public static DoubleGene of(final DoubleRange range) {
270 return of(nextDouble(range.min(), range.max(), random()), range);
271 }
272
273 static ISeq<DoubleGene> seq(
274 final double min,
275 final double max,
276 final IntRange lengthRange
277 ) {
278 final Random r = random();
279 return MSeq.<DoubleGene>ofLength(Randoms.nextInt(lengthRange, r))
280 .fill(() -> new DoubleGene(nextDouble(min, max, r), min, max))
281 .toISeq();
282 }
283
284
285 /* *************************************************************************
286 * Java object serialization
287 * ************************************************************************/
288
289 private Object writeReplace() {
290 return new Serial(Serial.DOUBLE_GENE, this);
291 }
292
293 private void readObject(final ObjectInputStream stream)
294 throws InvalidObjectException
295 {
296 throw new InvalidObjectException("Serialization proxy required.");
297 }
298
299 void write(final DataOutput out) throws IOException {
300 out.writeDouble(_allele);
301 out.writeDouble(_min);
302 out.writeDouble(_max);
303 }
304
305 static DoubleGene read(final DataInput in) throws IOException {
306 return of(in.readDouble(), in.readDouble(), in.readDouble());
307 }
308
309 }
|