DoubleGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.9.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@gmx.at)
019  */
020 package org.jenetics;
021 
022 import static org.jenetics.internal.math.random.nextDouble;
023 import static org.jenetics.util.RandomRegistry.getRandom;
024 
025 import java.io.Serializable;
026 import java.util.Random;
027 
028 import javax.xml.bind.annotation.XmlAccessType;
029 import javax.xml.bind.annotation.XmlAccessorType;
030 import javax.xml.bind.annotation.XmlAttribute;
031 import javax.xml.bind.annotation.XmlRootElement;
032 import javax.xml.bind.annotation.XmlType;
033 import javax.xml.bind.annotation.XmlValue;
034 import javax.xml.bind.annotation.adapters.XmlAdapter;
035 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
036 
037 import org.jenetics.internal.util.require;
038 
039 import org.jenetics.util.DoubleRange;
040 import org.jenetics.util.ISeq;
041 import org.jenetics.util.MSeq;
042 import org.jenetics.util.Mean;
043 
044 /**
045  * Implementation of the NumericGene which holds a 64 bit floating point number.
046  *
047  <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">
048  * value-based</a> class; use of identity-sensitive operations (including
049  * reference equality ({@code ==}), identity hash code, or synchronization) on
050  * instances of {@code DoubleGene} may have unpredictable results and should
051  * be avoided.
052  *
053  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
054  @since 1.6
055  @version 3.2
056  */
057 @XmlJavaTypeAdapter(DoubleGene.Model.Adapter.class)
058 public final class DoubleGene
059     extends AbstractNumericGene<Double, DoubleGene>
060     implements
061         NumericGene<Double, DoubleGene>,
062         Mean<DoubleGene>,
063         Comparable<DoubleGene>,
064         Serializable
065 {
066 
067     private static final long serialVersionUID = 1L;
068 
069     /**
070      * Create a new random {@code DoubleGene} with the given value and the
071      * given range. If the {@code value} isn't within the interval [min, max),
072      * no exception is thrown. In this case the method
073      {@link DoubleGene#isValid()} returns {@code false}.
074      *
075      @param value the value of the gene.
076      @param min the minimal valid value of this gene (inclusively).
077      @param max the maximal valid value of this gene (exclusively).
078      @throws NullPointerException if one of the arguments is {@code null}.
079      */
080     DoubleGene(final Double value, final Double min, final Double max) {
081         super(value, min, max);
082     }
083 
084     @Override
085     public int compareTo(final DoubleGene other) {
086         return _value.compareTo(other._value);
087     }
088 
089     /**
090      * Create a new random {@code DoubleGene} with the given value and the
091      * given range. If the {@code value} isn't within the interval [min, max),
092      * no exception is thrown. In this case the method
093      {@link DoubleGene#isValid()} returns {@code false}.
094      *
095      @param value the value of the gene.
096      @param min the minimal valid value of this gene (inclusively).
097      @param max the maximal valid value of this gene (exclusively).
098      @return a new {@code DoubleGene} with the given parameter
099      */
100     public static DoubleGene of(
101         final double value,
102         final double min,
103         final double max
104     ) {
105         return new DoubleGene(value, min, max);
106     }
107 
108     /**
109      * Create a new random {@code DoubleGene} with the given value and the
110      * given range. If the {@code value} isn't within the interval [min, max),
111      * no exception is thrown. In this case the method
112      {@link DoubleGene#isValid()} returns {@code false}.
113      *
114      @since 3.2
115      *
116      @param value the value of the gene.
117      @param range the double range to use
118      @return a new random {@code DoubleGene}
119      @throws NullPointerException if the given {@code range} is {@code null}.
120      */
121     public static DoubleGene of(final double value, final DoubleRange range) {
122         return new DoubleGene(value, range.getMin(), range.getMax());
123     }
124 
125     /**
126      * Create a new random {@code DoubleGene}. It is guaranteed that the value
127      * of the {@code DoubleGene} lies in the interval [min, max).
128      *
129      @param min the minimal valid value of this gene (inclusively).
130      @param max the maximal valid value of this gene (exclusively).
131      @return a new {@code DoubleGene} with the given parameter
132      */
133     public static DoubleGene of(final double min, final double max) {
134         return of(nextDouble(getRandom(), min, max), min, max);
135     }
136 
137     /**
138      * Create a new random {@code DoubleGene}. It is guaranteed that the value
139      * of the {@code DoubleGene} lies in the interval [min, max).
140      *
141      @since 3.2
142      *
143      @param range the double range to use
144      @return a new {@code DoubleGene} with the given parameter
145      @throws NullPointerException if the given {@code range} is {@code null}.
146      */
147     public static DoubleGene of(final DoubleRange range) {
148         return of(nextDouble(getRandom(), range.getMin(), range.getMax()), range);
149     }
150 
151     static ISeq<DoubleGene> seq(
152         final Double minimum,
153         final Double maximum,
154         final int length
155     ) {
156         require.positive(length);
157 
158         final double min = minimum;
159         final double max = maximum;
160         final Random r = getRandom();
161 
162         return MSeq.<DoubleGene>ofLength(length)
163             .fill(() -> new DoubleGene(nextDouble(r, min, max), minimum, maximum))
164             .toISeq();
165     }
166 
167     @Override
168     public DoubleGene newInstance(final Number number) {
169         return new DoubleGene(number.doubleValue(), _min, _max);
170     }
171 
172     @Override
173     public DoubleGene newInstance() {
174         return new DoubleGene(
175             nextDouble(getRandom(), _min, _max), _min, _max
176         );
177     }
178 
179     @Override
180     public DoubleGene mean(final DoubleGene that) {
181         return new DoubleGene(_value + (that._value - _value)/2.0, _min, _max);
182     }
183 
184     /* *************************************************************************
185      *  JAXB object serialization
186      * ************************************************************************/
187 
188     @XmlRootElement(name = "double-gene")
189     @XmlType(name = "org.jenetics.DoubleGene")
190     @XmlAccessorType(XmlAccessType.FIELD)
191     final static class Model {
192 
193         @XmlAttribute(name = "min", required = true)
194         public double min;
195 
196         @XmlAttribute(name = "max", required = true)
197         public double max;
198 
199         @XmlValue
200         public double value;
201 
202         public final static class Adapter
203             extends XmlAdapter<Model, DoubleGene>
204         {
205             @Override
206             public Model marshal(final DoubleGene value) {
207                 final Model m = new Model();
208                 m.min = value.getMin();
209                 m.max = value.getMax();
210                 m.value = value.getAllele();
211                 return m;
212             }
213 
214             @Override
215             public DoubleGene unmarshal(final Model m) {
216                 return DoubleGene.of(m.value, m.min, m.max);
217             }
218         }
219     }
220 
221 }