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