DoubleGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.0.0).
003  * Copyright (c) 2007-2017 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  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
047  @since 1.6
048  @version 4.0
049  */
050 public final class DoubleGene
051     extends AbstractNumericGene<Double, DoubleGene>
052     implements
053         NumericGene<Double, DoubleGene>,
054         Mean<DoubleGene>,
055         Comparable<DoubleGene>,
056         Serializable
057 {
058 
059     private static final long serialVersionUID = 1L;
060 
061     /**
062      * Create a new random {@code DoubleGene} with the given value and the
063      * given range. If the {@code value} isn't within the interval [min, max),
064      * no exception is thrown. In this case the method
065      {@link DoubleGene#isValid()} returns {@code false}.
066      *
067      @param value the value of the gene.
068      @param min the minimal valid value of this gene (inclusively).
069      @param max the maximal valid value of this gene (exclusively).
070      @throws NullPointerException if one of the arguments is {@code null}.
071      */
072     DoubleGene(final Double value, final Double min, final Double max) {
073         super(value, min, max);
074     }
075 
076     @Override
077     public int compareTo(final DoubleGene other) {
078         return _value.compareTo(other._value);
079     }
080 
081     /**
082      * Create a new random {@code DoubleGene} with the given value and the
083      * given range. If the {@code value} isn't within the interval [min, max),
084      * no exception is thrown. In this case the method
085      {@link DoubleGene#isValid()} returns {@code false}.
086      *
087      @param value the value of the gene.
088      @param min the minimal valid value of this gene (inclusively).
089      @param max the maximal valid value of this gene (exclusively).
090      @return a new {@code DoubleGene} with the given parameter
091      */
092     public static DoubleGene of(
093         final double value,
094         final double min,
095         final double max
096     ) {
097         return new DoubleGene(value, min, max);
098     }
099 
100     /**
101      * Create a new random {@code DoubleGene} with the given value and the
102      * given range. If the {@code value} isn't within the interval [min, max),
103      * no exception is thrown. In this case the method
104      {@link DoubleGene#isValid()} returns {@code false}.
105      *
106      @since 3.2
107      *
108      @param value the value of the gene.
109      @param range the double range to use
110      @return a new random {@code DoubleGene}
111      @throws NullPointerException if the given {@code range} is {@code null}.
112      */
113     public static DoubleGene of(final double value, final DoubleRange range) {
114         return new DoubleGene(value, range.getMin(), range.getMax());
115     }
116 
117     /**
118      * Create a new random {@code DoubleGene}. It is guaranteed that the value
119      * of the {@code DoubleGene} lies in the interval [min, max).
120      *
121      @param min the minimal valid value of this gene (inclusively).
122      @param max the maximal valid value of this gene (exclusively).
123      @return a new {@code DoubleGene} with the given parameter
124      */
125     public static DoubleGene of(final double min, final double max) {
126         return of(nextDouble(min, max, getRandom()), min, max);
127     }
128 
129     /**
130      * Create a new random {@code DoubleGene}. It is guaranteed that the value
131      * of the {@code DoubleGene} lies in the interval [min, max).
132      *
133      @since 3.2
134      *
135      @param range the double range to use
136      @return a new {@code DoubleGene} with the given parameter
137      @throws NullPointerException if the given {@code range} is {@code null}.
138      */
139     public static DoubleGene of(final DoubleRange range) {
140         return of(nextDouble(range.getMin(), range.getMax(), getRandom()), range);
141     }
142 
143     static ISeq<DoubleGene> seq(
144         final Double minimum,
145         final Double maximum,
146         final IntRange lengthRange
147     ) {
148         final double min = minimum;
149         final double max = maximum;
150         final Random r = getRandom();
151 
152         return MSeq.<DoubleGene>ofLength(random.nextInt(lengthRange, r))
153             .fill(() -> new DoubleGene(nextDouble(min, max, r), minimum, maximum))
154             .toISeq();
155     }
156 
157     @Override
158     public DoubleGene newInstance(final Number number) {
159         return new DoubleGene(number.doubleValue(), _min, _max);
160     }
161 
162     @Override
163     public DoubleGene newInstance() {
164         return new DoubleGene(
165             nextDouble(_min, _max, getRandom()), _min, _max
166         );
167     }
168 
169     @Override
170     public DoubleGene mean(final DoubleGene that) {
171         return new DoubleGene(_value + (that._value - _value)/2.0, _min, _max);
172     }
173 
174 }