001 /*
002 * Java Genetic Algorithm Library (jenetics-4.2.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.util.RandomRegistry.getRandom;
024
025 import java.io.Serializable;
026 import java.util.Random;
027
028 import io.jenetics.internal.math.random;
029 import io.jenetics.util.DoubleRange;
030 import io.jenetics.util.ISeq;
031 import io.jenetics.util.IntRange;
032 import io.jenetics.util.MSeq;
033 import io.jenetics.util.Mean;
034
035 /**
036 * Implementation of the NumericGene which holds a 64 bit floating point number.
037 *
038 * <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">
039 * value-based</a> class; use of identity-sensitive operations (including
040 * reference equality ({@code ==}), identity hash code, or synchronization) on
041 * instances of {@code DoubleGene} may have unpredictable results and should
042 * be avoided.
043 *
044 * @see DoubleChromosome
045 *
046 * @implNote
047 * This class is immutable and thread-safe.
048 *
049 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
050 * @since 1.6
051 * @version 4.0
052 */
053 public final class DoubleGene
054 extends AbstractNumericGene<Double, DoubleGene>
055 implements
056 NumericGene<Double, DoubleGene>,
057 Mean<DoubleGene>,
058 Comparable<DoubleGene>,
059 Serializable
060 {
061
062 private static final long serialVersionUID = 1L;
063
064 /**
065 * Create a new random {@code DoubleGene} with the given value and the
066 * given range. If the {@code value} isn't within the interval [min, max),
067 * no exception is thrown. In this case the method
068 * {@link DoubleGene#isValid()} returns {@code false}.
069 *
070 * @param value the value of the gene.
071 * @param min the minimal valid value of this gene (inclusively).
072 * @param max the maximal valid value of this gene (exclusively).
073 * @throws NullPointerException if one of the arguments is {@code null}.
074 */
075 DoubleGene(final Double value, final Double min, final Double max) {
076 super(value, min, max);
077 }
078
079 @Override
080 public int compareTo(final DoubleGene other) {
081 return _value.compareTo(other._value);
082 }
083
084 /**
085 * Create a new random {@code DoubleGene} with the given value and the
086 * given range. If the {@code value} isn't within the interval [min, max),
087 * no exception is thrown. In this case the method
088 * {@link DoubleGene#isValid()} returns {@code false}.
089 *
090 * @param value the value of the gene.
091 * @param min the minimal valid value of this gene (inclusively).
092 * @param max the maximal valid value of this gene (exclusively).
093 * @return a new {@code DoubleGene} with the given parameter
094 */
095 public static DoubleGene of(
096 final double value,
097 final double min,
098 final double max
099 ) {
100 return new DoubleGene(value, min, max);
101 }
102
103 /**
104 * Create a new random {@code DoubleGene} with the given value and the
105 * given range. If the {@code value} isn't within the interval [min, max),
106 * no exception is thrown. In this case the method
107 * {@link DoubleGene#isValid()} returns {@code false}.
108 *
109 * @since 3.2
110 *
111 * @param value the value of the gene.
112 * @param range the double range to use
113 * @return a new random {@code DoubleGene}
114 * @throws NullPointerException if the given {@code range} is {@code null}.
115 */
116 public static DoubleGene of(final double value, final DoubleRange range) {
117 return new DoubleGene(value, range.getMin(), range.getMax());
118 }
119
120 /**
121 * Create a new random {@code DoubleGene}. It is guaranteed that the value
122 * of the {@code DoubleGene} lies in the interval [min, max).
123 *
124 * @param min the minimal valid value of this gene (inclusively).
125 * @param max the maximal valid value of this gene (exclusively).
126 * @return a new {@code DoubleGene} with the given parameter
127 */
128 public static DoubleGene of(final double min, final double max) {
129 return of(nextDouble(min, max, getRandom()), min, max);
130 }
131
132 /**
133 * Create a new random {@code DoubleGene}. It is guaranteed that the value
134 * of the {@code DoubleGene} lies in the interval [min, max).
135 *
136 * @since 3.2
137 *
138 * @param range the double range to use
139 * @return a new {@code DoubleGene} with the given parameter
140 * @throws NullPointerException if the given {@code range} is {@code null}.
141 */
142 public static DoubleGene of(final DoubleRange range) {
143 return of(nextDouble(range.getMin(), range.getMax(), getRandom()), range);
144 }
145
146 static ISeq<DoubleGene> seq(
147 final Double minimum,
148 final Double maximum,
149 final IntRange lengthRange
150 ) {
151 final double min = minimum;
152 final double max = maximum;
153 final Random r = getRandom();
154
155 return MSeq.<DoubleGene>ofLength(random.nextInt(lengthRange, r))
156 .fill(() -> new DoubleGene(nextDouble(min, max, r), minimum, maximum))
157 .toISeq();
158 }
159
160 @Override
161 public DoubleGene newInstance(final Number number) {
162 return new DoubleGene(number.doubleValue(), _min, _max);
163 }
164
165 @Override
166 public DoubleGene newInstance() {
167 return new DoubleGene(
168 nextDouble(_min, _max, getRandom()), _min, _max
169 );
170 }
171
172 @Override
173 public DoubleGene mean(final DoubleGene that) {
174 return new DoubleGene(_value + (that._value - _value)/2.0, _min, _max);
175 }
176
177 }
|