Float64Gene.java
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 java.io.IOException;
023 import java.io.ObjectInputStream;
024 import java.io.ObjectOutputStream;
025 import java.util.Random;
026 
027 import javax.xml.bind.annotation.XmlAccessType;
028 import javax.xml.bind.annotation.XmlAccessorType;
029 import javax.xml.bind.annotation.XmlAttribute;
030 import javax.xml.bind.annotation.XmlElement;
031 import javax.xml.bind.annotation.XmlRootElement;
032 import javax.xml.bind.annotation.XmlType;
033 import javax.xml.bind.annotation.adapters.XmlAdapter;
034 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
035 
036 import javolution.context.ObjectFactory;
037 import javolution.xml.XMLFormat;
038 import javolution.xml.stream.XMLStreamException;
039 
040 import org.jscience.mathematics.number.Float64;
041 import org.jscience.mathematics.structure.GroupMultiplicative;
042 
043 import org.jenetics.internal.util.model.DoubleModel;
044 import org.jenetics.internal.util.model.ModelType;
045 import org.jenetics.internal.util.model.ValueType;
046 
047 import org.jenetics.util.Function;
048 import org.jenetics.util.RandomRegistry;
049 import org.jenetics.util.math;
050 
051 /**
052  * Implementation of the NumberGene which holds a 64 bit floating point number.
053  *
054  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
055  @since 1.0
056  @version 1.6 &mdash; <em>$Date: 2014-02-15 $</em>
057  *
058  @deprecated Use {@link org.jenetics.DoubleGene} instead. This classes
059  *             uses the <i>JScience</i> library, which will be removed in the
060  *             next major version.
061  */
062 @Deprecated
063 @XmlJavaTypeAdapter(Float64Gene.Model.Adapter.class)
064 public final class Float64Gene
065     extends NumberGene<Float64, Float64Gene>
066     implements GroupMultiplicative<Float64Gene>
067 {
068     private static final long serialVersionUID = 1L;
069 
070     Float64Gene() {
071     }
072 
073     @Override
074     protected Float64 box(final java.lang.Number value) {
075         return Float64.valueOf(value.doubleValue());
076     }
077 
078     public Float64Gene divide(final Float64Gene gene) {
079         return newInstance(_value.divide(gene._value));
080     }
081 
082     @Override
083     public Float64Gene inverse() {
084         return newInstance(_value.inverse());
085     }
086 
087     @Override
088     public Float64Gene mean(final Float64Gene that) {
089         return newInstance(
090             _value.doubleValue()  +
091             (that._value.doubleValue() - _value.doubleValue())/2.0
092         );
093     }
094 
095 
096     /* *************************************************************************
097      *  Property access methods
098      * ************************************************************************/
099 
100     /**
101      * Converter for accessing the value from a given number gene.
102      */
103     public static final Function<Float64Gene, Float64> Allele =
104         new Function<Float64Gene, Float64>() {
105             @Override public Float64 apply(final Float64Gene value) {
106                 return value._value;
107             }
108         };
109 
110     /**
111      * Converter for accessing the allele from a given number gene.
112      */
113     public static final Function<Float64Gene, Float64> Value = Allele;
114 
115     /**
116      * Converter for accessing the allowed minimum from a given number gene.
117      */
118     public static final Function<Float64Gene, Float64> Min =
119         new Function<Float64Gene, Float64>() {
120             @Override public Float64 apply(final Float64Gene value) {
121                 return value._min;
122             }
123         };
124 
125     /**
126      * Converter for accessing the allowed minimum from a given number gene.
127      */
128     public static final Function<Float64Gene, Float64> Max =
129         new Function<Float64Gene, Float64>() {
130             @Override public Float64 apply(final Float64Gene value) {
131                 return value._max;
132             }
133         };
134 
135     static Function<Float64, Float64Gene> Gene(
136         final Float64 min,
137         final Float64 max
138     ) {
139         return new Function<Float64, Float64Gene>() {
140             @Override
141             public Float64Gene apply(final Float64 value) {
142                 return Float64Gene.valueOf(value, min, max);
143             }
144         };
145     }
146 
147     /* *************************************************************************
148      *  Factory methods
149      * ************************************************************************/
150 
151     /**
152      * Create a new valid, <em>random</em> gene.
153      */
154     @Override
155     public Float64Gene newInstance() {
156         return valueOf(_min, _max);
157     }
158 
159     /**
160      * Create a new Float64Gene with the same limits and the given value.
161      *
162      @param value the value of the new {@code NumberGene}.
163      @return the new {@code NumberGene}.
164      */
165     public Float64Gene newInstance(final double value) {
166         return valueOf(Float64.valueOf(value), _min, _max);
167     }
168 
169     @Override
170     public Float64Gene newInstance(final Float64 value) {
171         return valueOf(value, _min, _max);
172     }
173 
174 
175     /* *************************************************************************
176      *  Static object creation methods
177      * ************************************************************************/
178 
179     private static final ObjectFactory<Float64Gene> FACTORY =
180         new ObjectFactory<Float64Gene>() {
181             @Override protected Float64Gene create() {
182                 return new Float64Gene();
183             }
184         };
185 
186     /**
187      * Create a new random {@code Float64Gene} with the given value and the
188      * given range. If the {@code value} isn't within the interval [min, max),
189      * no exception is thrown. In this case the method
190      {@link Float64Gene#isValid()} returns {@code false}.
191      *
192      @param value the value of the gene.
193      @param min the minimal valid value of this gene (inclusively).
194      @param max the maximal valid value of this gene (exclusively).
195      @return the new created gene with the given {@code value}.
196      */
197     public static Float64Gene valueOf(
198         final double value,
199         final double min,
200         final double max
201     ) {
202         return valueOf(
203             Float64.valueOf(value),
204             Float64.valueOf(min),
205             Float64.valueOf(max)
206         );
207     }
208 
209     /**
210      * Create a new random {@code Float64Gene} with the given value and the
211      * given range. If the {@code value} isn't within the interval [min, max),
212      * no exception is thrown. In this case the method
213      {@link Float64Gene#isValid()} returns {@code false}.
214      *
215      @param value the value of the gene.
216      @param min the minimal valid value of this gene (inclusively).
217      @param max the maximal valid value of this gene (exclusively).
218      @return the new created gene with the given {@code value}.
219      @throws NullPointerException if one of the arguments is {@code null}.
220      */
221     public static Float64Gene valueOf(
222         final Float64 value,
223         final Float64 min,
224         final Float64 max
225     ) {
226         final Float64Gene gene = FACTORY.object();
227         gene.set(value, min, max);
228         return gene;
229     }
230 
231     /**
232      * Create a new random {@code Float64Gene}. It is guaranteed that the value
233      * of the {@code Float64Gene} lies in the interval [min, max).
234      *
235      @param min the minimal valid value of this gene (inclusively).
236      @param max the maximal valid value of this gene (exclusively).
237      @return the new created gene.
238      */
239     public static Float64Gene valueOf(final double min, final double max) {
240         return valueOf(Float64.valueOf(min), Float64.valueOf(max));
241     }
242 
243     /**
244      * Create a new random {@code Float64Gene}. It is guaranteed that the value
245      * of the {@code Float64Gene} lies in the interval [min, max).
246      *
247      @param min the minimal valid value of this gene (inclusively).
248      @param max the maximal valid value of this gene (exclusively).
249      @return the new created gene.
250      @throws NullPointerException if one of the arguments is {@code null}.
251      */
252     public static Float64Gene valueOf(
253         final Float64 min,
254         final Float64 max
255     ) {
256         final Random random = RandomRegistry.getRandom();
257         final Float64 value = Float64.valueOf(
258             math.random.nextDouble(random, min.doubleValue(), max.doubleValue())
259         );
260 
261         return valueOf(value, min, max);
262     }
263 
264     /* *************************************************************************
265      *  Java object serialization
266      * ************************************************************************/
267 
268     private void writeObject(final ObjectOutputStream out)
269         throws IOException
270     {
271         out.defaultWriteObject();
272 
273         out.writeDouble(_value.doubleValue());
274         out.writeDouble(_min.doubleValue());
275         out.writeDouble(_max.doubleValue());
276     }
277 
278     private void readObject(final ObjectInputStream in)
279         throws IOException, ClassNotFoundException
280     {
281         in.defaultReadObject();
282 
283         set(
284             Float64.valueOf(in.readDouble()),
285             Float64.valueOf(in.readDouble()),
286             Float64.valueOf(in.readDouble())
287         );
288     }
289 
290     /* *************************************************************************
291      *  XML object serialization
292      * ************************************************************************/
293 
294     static final XMLFormat<Float64Gene>
295     XML = new XMLFormat<Float64Gene>(Float64Gene.class)
296     {
297         private static final String MIN = "min";
298         private static final String MAX = "max";
299 
300         @Override
301         public Float64Gene newInstance(
302             final Class<Float64Gene> cls, final InputElement element
303         )
304             throws XMLStreamException
305         {
306             final double min = element.getAttribute(MIN, 0.0);
307             final double max = element.getAttribute(MAX, 1.0);
308             final double value = element.<Double>getNext();
309             return Float64Gene.valueOf(value, min, max);
310         }
311         @Override
312         public void write(final Float64Gene gene, final OutputElement element)
313             throws XMLStreamException
314         {
315             element.setAttribute(MIN, gene.getMin().doubleValue());
316             element.setAttribute(MAX, gene.getMax().doubleValue());
317             element.add(gene.getAllele().doubleValue());
318         }
319         @Override
320         public void read(final InputElement element, final Float64Gene gene) {
321         }
322     };
323 
324     /* *************************************************************************
325      *  JAXB object serialization
326      * ************************************************************************/
327 
328     @XmlRootElement(name = "org.jenetics.Float64Gene")
329     @XmlType(name = "org.jenetics.Float64Gene")
330     @XmlAccessorType(XmlAccessType.FIELD)
331     final static class Model {
332 
333         @XmlAttribute
334         public double min;
335 
336         @XmlAttribute
337         public double max;
338 
339         @XmlJavaTypeAdapter(DoubleModel.Adapter.class)
340         @XmlElement(name= "java.lang.Double")
341         public Double value;
342 
343         @ValueType(Float64Gene.class)
344         @ModelType(Model.class)
345         public final static class Adapter
346             extends XmlAdapter<Model, Float64Gene>
347         {
348             @Override
349             public Model marshal(final Float64Gene value) {
350                 final Model m = new Model();
351                 m.min = value.getMin().doubleValue();
352                 m.max = value.getMax().doubleValue();
353                 m.value = value.doubleValue();
354                 return m;
355             }
356 
357             @Override
358             public Float64Gene unmarshal(final Model m) {
359                 return Float64Gene.valueOf(
360                     m.value,
361                     m.min,
362                     m.max
363                 );
364             }
365         }
366     }
367 
368 }