001 /*
002 * Java Genetic Algorithm Library (jenetics-4.4.0).
003 * Copyright (c) 2007-2019 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.random.nextDouble;
023 import static io.jenetics.internal.util.Hashes.hash;
024 import static io.jenetics.util.RandomRegistry.getRandom;
025
026 import java.io.Serializable;
027 import java.util.Random;
028
029 import io.jenetics.internal.math.random;
030 import io.jenetics.util.DoubleRange;
031 import io.jenetics.util.ISeq;
032 import io.jenetics.util.IntRange;
033 import io.jenetics.util.MSeq;
034 import io.jenetics.util.Mean;
035
036 /**
037 * Implementation of the NumericGene which holds a 64 bit floating point number.
038 *
039 * <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">
040 * value-based</a> class; use of identity-sensitive operations (including
041 * reference equality ({@code ==}), identity hash code, or synchronization) on
042 * instances of {@code DoubleGene} may have unpredictable results and should
043 * be avoided.
044 *
045 * @see DoubleChromosome
046 *
047 * @implNote
048 * This class is immutable and thread-safe.
049 *
050 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
051 * @since 1.6
052 * @version 4.3
053 */
054 public final class DoubleGene
055 implements
056 NumericGene<Double, DoubleGene>,
057 Mean<DoubleGene>,
058 Comparable<DoubleGene>,
059 Serializable
060 {
061
062 private static final long serialVersionUID = 2L;
063
064 private final double _value;
065 private final double _min;
066 private final double _max;
067
068 /**
069 * Create a new random {@code DoubleGene} with the given value and the
070 * given range. If the {@code value} isn't within the interval [min, max),
071 * no exception is thrown. In this case the method
072 * {@link DoubleGene#isValid()} returns {@code false}.
073 *
074 * @param value the value of the gene.
075 * @param min the minimal valid value of this gene (inclusively).
076 * @param max the maximal valid value of this gene (exclusively).
077 */
078 private DoubleGene(final double value, final double min, final double max) {
079 _value = value;
080 _min = min;
081 _max = max;
082 }
083
084 @Override
085 public Double getAllele() {
086 return _value;
087 }
088
089 @Override
090 public Double getMin() {
091 return _min;
092 }
093
094 @Override
095 public Double getMax() {
096 return _max;
097 }
098
099 /**
100 * Return the range of {@code this} gene.
101 *
102 * @since 4.4
103 *
104 * @return the range of {@code this} gene
105 */
106 public DoubleRange range() {
107 return DoubleRange.of(_min, _max);
108 }
109
110 @Override
111 public byte byteValue() {
112 return (byte)_value;
113 }
114
115 @Override
116 public short shortValue() {
117 return (short)_value;
118 }
119
120 @Override
121 public int intValue() {
122 return (int)_value;
123 }
124
125 @Override
126 public long longValue() {
127 return (long)_value;
128 }
129
130 @Override
131 public float floatValue() {
132 return (float)_value;
133 }
134
135 @Override
136 public double doubleValue() {
137 return _value;
138 }
139
140 @Override
141 public boolean isValid() {
142 return Double.compare(_value, _min) >= 0 &&
143 Double.compare(_value, _max) <= 0;
144 }
145
146 @Override
147 public int compareTo(final DoubleGene other) {
148 return Double.compare(_value, other._value);
149 }
150
151 @Override
152 public DoubleGene mean(final DoubleGene that) {
153 return of(_value + (that._value - _value)/2.0, _min, _max);
154 }
155
156 @Override
157 public DoubleGene newInstance(final Double value) {
158 return of(value, _min, _max);
159 }
160
161 @Override
162 public DoubleGene newInstance(final Number number) {
163 return of(number.doubleValue(), _min, _max);
164 }
165
166 @Override
167 public DoubleGene newInstance() {
168 return of(nextDouble(_min, _max, getRandom()), _min, _max);
169 }
170
171 @Override
172 public int hashCode() {
173 return hash(_value, hash(_min, hash(_max, hash(getClass()))));
174 }
175
176 @Override
177 public boolean equals(final Object obj) {
178 return obj == this ||
179 obj instanceof DoubleGene &&
180 Double.compare(((DoubleGene)obj)._value, _value) == 0 &&
181 Double.compare(((DoubleGene)obj)._min, _min) == 0 &&
182 Double.compare(((DoubleGene)obj)._max, _max) == 0;
183 }
184
185 @Override
186 public String toString() {
187 return String.format("[%s]", _value);
188 }
189
190
191 /* *************************************************************************
192 * Static factory methods.
193 * ************************************************************************/
194
195 /**
196 * Create a new random {@code DoubleGene} with the given value and the
197 * given range. If the {@code value} isn't within the interval [min, max),
198 * no exception is thrown. In this case the method
199 * {@link DoubleGene#isValid()} returns {@code false}.
200 *
201 * @param value the value of the gene.
202 * @param min the minimal valid value of this gene (inclusively).
203 * @param max the maximal valid value of this gene (exclusively).
204 * @return a new {@code DoubleGene} with the given parameter
205 */
206 public static DoubleGene of(
207 final double value,
208 final double min,
209 final double max
210 ) {
211 return new DoubleGene(value, min, max);
212 }
213
214 /**
215 * Create a new random {@code DoubleGene} with the given value and the
216 * given range. If the {@code value} isn't within the interval [min, max),
217 * no exception is thrown. In this case the method
218 * {@link DoubleGene#isValid()} returns {@code false}.
219 *
220 * @since 3.2
221 *
222 * @param value the value of the gene.
223 * @param range the double range to use
224 * @return a new random {@code DoubleGene}
225 * @throws NullPointerException if the given {@code range} is {@code null}.
226 */
227 public static DoubleGene of(final double value, final DoubleRange range) {
228 return of(value, range.getMin(), range.getMax());
229 }
230
231 /**
232 * Create a new random {@code DoubleGene}. It is guaranteed that the value
233 * of the {@code DoubleGene} lies in the interval [min, max).
234 *
235 * @param min the minimal valid value of this gene (inclusively).
236 * @param max the maximal valid value of this gene (exclusively).
237 * @return a new {@code DoubleGene} with the given parameter
238 */
239 public static DoubleGene of(final double min, final double max) {
240 return of(nextDouble(min, max, getRandom()), min, max);
241 }
242
243 /**
244 * Create a new random {@code DoubleGene}. It is guaranteed that the value
245 * of the {@code DoubleGene} lies in the interval [min, max).
246 *
247 * @since 3.2
248 *
249 * @param range the double range to use
250 * @return a new {@code DoubleGene} with the given parameter
251 * @throws NullPointerException if the given {@code range} is {@code null}.
252 */
253 public static DoubleGene of(final DoubleRange range) {
254 return of(nextDouble(range.getMin(), range.getMax(), getRandom()), range);
255 }
256
257 static ISeq<DoubleGene> seq(
258 final double min,
259 final double max,
260 final IntRange lengthRange
261 ) {
262 final Random r = getRandom();
263
264 return MSeq.<DoubleGene>ofLength(random.nextInt(lengthRange, r))
265 .fill(() -> of(nextDouble(min, max, r), min, max))
266 .toISeq();
267 }
268
269 }
|