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