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