001 /*
002 * Java Genetic Algorithm Library (jenetics-4.3.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;
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 @Override
100 public byte byteValue() {
101 return (byte)_value;
102 }
103
104 @Override
105 public short shortValue() {
106 return (short)_value;
107 }
108
109 @Override
110 public int intValue() {
111 return (int)_value;
112 }
113
114 @Override
115 public long longValue() {
116 return (long)_value;
117 }
118
119 @Override
120 public float floatValue() {
121 return (float)_value;
122 }
123
124 @Override
125 public double doubleValue() {
126 return _value;
127 }
128
129 @Override
130 public boolean isValid() {
131 return Double.compare(_value, _min) >= 0 &&
132 Double.compare(_value, _max) <= 0;
133 }
134
135 @Override
136 public int compareTo(final DoubleGene other) {
137 return Double.compare(_value, other._value);
138 }
139
140 @Override
141 public DoubleGene mean(final DoubleGene that) {
142 return of(_value + (that._value - _value)/2.0, _min, _max);
143 }
144
145 @Override
146 public DoubleGene newInstance(final Double value) {
147 return of(value, _min, _max);
148 }
149
150 @Override
151 public DoubleGene newInstance(final Number number) {
152 return of(number.doubleValue(), _min, _max);
153 }
154
155 @Override
156 public DoubleGene newInstance() {
157 return of(nextDouble(_min, _max, getRandom()), _min, _max);
158 }
159
160 @Override
161 public int hashCode() {
162 return hash(_value, hash(_min, hash(_max, hash(getClass()))));
163 }
164
165 @Override
166 public boolean equals(final Object obj) {
167 return obj == this ||
168 obj instanceof DoubleGene &&
169 Double.compare(((DoubleGene)obj)._value, _value) == 0 &&
170 Double.compare(((DoubleGene)obj)._min, _min) == 0 &&
171 Double.compare(((DoubleGene)obj)._max, _max) == 0;
172 }
173
174 @Override
175 public String toString() {
176 return String.format("[%s]", _value);
177 }
178
179
180 /* *************************************************************************
181 * Static factory methods.
182 * ************************************************************************/
183
184 /**
185 * Create a new random {@code DoubleGene} with the given value and the
186 * given range. If the {@code value} isn't within the interval [min, max),
187 * no exception is thrown. In this case the method
188 * {@link DoubleGene#isValid()} returns {@code false}.
189 *
190 * @param value the value of the gene.
191 * @param min the minimal valid value of this gene (inclusively).
192 * @param max the maximal valid value of this gene (exclusively).
193 * @return a new {@code DoubleGene} with the given parameter
194 */
195 public static DoubleGene of(
196 final double value,
197 final double min,
198 final double max
199 ) {
200 return new DoubleGene(value, min, max);
201 }
202
203 /**
204 * Create a new random {@code DoubleGene} with the given value and the
205 * given range. If the {@code value} isn't within the interval [min, max),
206 * no exception is thrown. In this case the method
207 * {@link DoubleGene#isValid()} returns {@code false}.
208 *
209 * @since 3.2
210 *
211 * @param value the value of the gene.
212 * @param range the double range to use
213 * @return a new random {@code DoubleGene}
214 * @throws NullPointerException if the given {@code range} is {@code null}.
215 */
216 public static DoubleGene of(final double value, final DoubleRange range) {
217 return of(value, range.getMin(), range.getMax());
218 }
219
220 /**
221 * Create a new random {@code DoubleGene}. It is guaranteed that the value
222 * of the {@code DoubleGene} lies in the interval [min, max).
223 *
224 * @param min the minimal valid value of this gene (inclusively).
225 * @param max the maximal valid value of this gene (exclusively).
226 * @return a new {@code DoubleGene} with the given parameter
227 */
228 public static DoubleGene of(final double min, final double max) {
229 return of(nextDouble(min, max, getRandom()), min, max);
230 }
231
232 /**
233 * Create a new random {@code DoubleGene}. It is guaranteed that the value
234 * of the {@code DoubleGene} lies in the interval [min, max).
235 *
236 * @since 3.2
237 *
238 * @param range the double range to use
239 * @return a new {@code DoubleGene} with the given parameter
240 * @throws NullPointerException if the given {@code range} is {@code null}.
241 */
242 public static DoubleGene of(final DoubleRange range) {
243 return of(nextDouble(range.getMin(), range.getMax(), getRandom()), range);
244 }
245
246 static ISeq<DoubleGene> seq(
247 final double min,
248 final double max,
249 final IntRange lengthRange
250 ) {
251 final Random r = getRandom();
252
253 return MSeq.<DoubleGene>ofLength(random.nextInt(lengthRange, r))
254 .fill(() -> of(nextDouble(min, max, r), min, max))
255 .toISeq();
256 }
257
258 }
|