001 /*
002 * Java Genetic Algorithm Library (jenetics-1.6.0).
003 * Copyright (c) 2007-2014 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.util.math.random.nextDouble;
023
024 import java.util.Random;
025
026 import javax.xml.bind.annotation.XmlAccessType;
027 import javax.xml.bind.annotation.XmlAccessorType;
028 import javax.xml.bind.annotation.XmlAttribute;
029 import javax.xml.bind.annotation.XmlRootElement;
030 import javax.xml.bind.annotation.XmlType;
031 import javax.xml.bind.annotation.XmlValue;
032 import javax.xml.bind.annotation.adapters.XmlAdapter;
033 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
034
035 import org.jenetics.internal.util.model.ModelType;
036 import org.jenetics.internal.util.model.ValueType;
037
038 import org.jenetics.util.Array;
039 import org.jenetics.util.ISeq;
040 import org.jenetics.util.Mean;
041 import org.jenetics.util.RandomRegistry;
042
043 /**
044 * Implementation of the NumericGene which holds a 64 bit floating point number.
045 *
046 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
047 * @version 1.6 — <em>$Date: 2014-03-05 $</em>
048 * @since 1.6
049 */
050 @XmlJavaTypeAdapter(DoubleGene.Model.Adapter.class)
051 public final class DoubleGene
052 extends AbstractNumericGene<Double, DoubleGene>
053 implements
054 NumericGene<Double, DoubleGene>,
055 Mean<DoubleGene>
056 {
057
058 private static final long serialVersionUID = 1L;
059
060 /**
061 * Create a new random {@code DoubleGene} with the given value and the
062 * given range. If the {@code value} isn't within the interval [min, max),
063 * no exception is thrown. In this case the method
064 * {@link DoubleGene#isValid()} returns {@code false}.
065 *
066 * @param value the value of the gene.
067 * @param min the minimal valid value of this gene (inclusively).
068 * @param max the maximal valid value of this gene (exclusively).
069 * @throws NullPointerException if one of the arguments is {@code null}.
070 */
071 public DoubleGene(final Double value, final Double min, final Double max) {
072 super(value, min, max);
073 }
074
075 /**
076 * Create a new random {@code DoubleGene} with the given value and the
077 * given range. If the {@code value} isn't within the interval [min, max),
078 * no exception is thrown. In this case the method
079 * {@link DoubleGene#isValid()} returns {@code false}.
080 *
081 * @param value the value of the gene.
082 * @param min the minimal valid value of this gene (inclusively).
083 * @param max the maximal valid value of this gene (exclusively).
084 */
085 public static DoubleGene of(
086 final double value,
087 final double min,
088 final double max
089 ) {
090 return new DoubleGene(value, min, max);
091 }
092
093 /**
094 * Create a new random {@code DoubleGene}. It is guaranteed that the value
095 * of the {@code DoubleGene} lies in the interval [min, max).
096 *
097 * @param min the minimal valid value of this gene (inclusively).
098 * @param max the maximal valid value of this gene (exclusively).
099 */
100 public static DoubleGene of(final double min, final double max) {
101 return of(nextDouble(RandomRegistry.getRandom(), min, max), min, max);
102 }
103
104 static ISeq<DoubleGene> seq(
105 final Double minimum,
106 final Double maximum,
107 final int length
108 ) {
109 final double min = minimum;
110 final double max = maximum;
111 final Random r = RandomRegistry.getRandom();
112
113 final Array<DoubleGene> genes = new Array<>(length);
114 for (int i = 0; i < length; ++i) {
115 genes.set(i, new DoubleGene(nextDouble(r, min, max), minimum, maximum));
116 }
117 return genes.toISeq();
118 }
119
120 @Override
121 public DoubleGene newInstance(final Number number) {
122 return new DoubleGene(number.doubleValue(), _min, _max);
123 }
124
125 @Override
126 public DoubleGene newInstance() {
127 return new DoubleGene(
128 nextDouble(RandomRegistry.getRandom(), _min, _max), _min, _max
129 );
130 }
131
132 @Override
133 public DoubleGene mean(final DoubleGene that) {
134 return new DoubleGene(_value + (that._value - _value) / 2.0, _min, _max);
135 }
136
137 /* *************************************************************************
138 * JAXB object serialization
139 * ************************************************************************/
140
141 @XmlRootElement(name = "org.jenetics.DoubleGene")
142 @XmlType(name = "org.jenetics.DoubleGene")
143 @XmlAccessorType(XmlAccessType.FIELD)
144 final static class Model {
145
146 @XmlAttribute
147 public double min;
148
149 @XmlAttribute
150 public double max;
151
152 @XmlValue
153 public double value;
154
155 @ValueType(DoubleGene.class)
156 @ModelType(Model.class)
157 public final static class Adapter
158 extends XmlAdapter<Model, DoubleGene>
159 {
160 @Override
161 public Model marshal(final DoubleGene value) {
162 final Model m = new Model();
163 m.min = value.getMin();
164 m.max = value.getMax();
165 m.value = value.getAllele();
166 return m;
167 }
168
169 @Override
170 public DoubleGene unmarshal(final Model m) {
171 return DoubleGene.of(m.value, m.min, m.max);
172 }
173 }
174 }
175
176 }
|