Integer64Gene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.5.0).
003  * Copyright (c) 2007-2013 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 javolution.context.ObjectFactory;
028 import javolution.xml.XMLFormat;
029 import javolution.xml.stream.XMLStreamException;
030 
031 import org.jscience.mathematics.number.Integer64;
032 
033 import org.jenetics.util.Function;
034 import org.jenetics.util.RandomRegistry;
035 import org.jenetics.util.math;
036 
037 /**
038  * NumberGene implementation which holds a 64 bit integer number.
039  *
040  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
041  @since 1.0
042  @version 1.2 &mdash; <em>$Date: 2013-12-05 $</em>
043  */
044 public final class Integer64Gene
045     extends NumberGene<Integer64, Integer64Gene>
046 {
047     private static final long serialVersionUID = 1L;
048 
049     Integer64Gene() {
050     }
051 
052     @Override
053     protected Integer64 box(final java.lang.Number value) {
054         return Integer64.valueOf(value.longValue());
055     }
056 
057     public Integer64Gene divide(final Integer64Gene gene) {
058         return newInstance(_value.divide(gene._value));
059     }
060 
061     @Override
062     public Integer64Gene mean(final Integer64Gene that) {
063         return newInstance(
064             _value.longValue()  +
065             (that._value.longValue() - _value.longValue())/2L
066         );
067     }
068 
069     /* *************************************************************************
070      *  Property access methods.
071      * ************************************************************************/
072 
073     /**
074      * Converter for accessing the value from a given number gene.
075      */
076     public static final Function<Integer64Gene, Integer64> Allele =
077         new Function<Integer64Gene, Integer64>() {
078             @Override public Integer64 apply(final Integer64Gene value) {
079                 return value._value;
080             }
081         };
082 
083     /**
084      * Converter for accessing the allele from a given number gene.
085      */
086     public static final Function<Integer64Gene, Integer64> Value = Allele;
087 
088     /**
089      * Converter for accessing the allowed minimum from a given number gene.
090      */
091     public static final Function<Integer64Gene, Integer64> Min =
092         new Function<Integer64Gene, Integer64>() {
093             @Override public Integer64 apply(final Integer64Gene value) {
094                 return value._min;
095             }
096         };
097 
098     /**
099      * Converter for accessing the allowed minimum from a given number gene.
100      */
101     public static final Function<Integer64Gene, Integer64> Max =
102         new Function<Integer64Gene, Integer64>() {
103             @Override public Integer64 apply(final Integer64Gene value) {
104                 return value._value;
105             }
106         };
107 
108     /* *************************************************************************
109      *  Factory methods
110      * ************************************************************************/
111 
112     /**
113      * Create a new valid, <em>random</em> gene.
114      */
115     @Override
116     public Integer64Gene newInstance() {
117         return valueOf(_min, _max);
118     }
119 
120     /**
121      * Create a new {@code Integer64Gene} with the same limits and the given
122      * value.
123      *
124      @param value the value of the new {@code NumberGene}.
125      @return the new {@code NumberGene}.
126      */
127     public Integer64Gene newInstance(final long value) {
128         return valueOf(Integer64.valueOf(value), _min, _max);
129     }
130 
131     @Override
132     public Integer64Gene newInstance(final Integer64 value) {
133         return valueOf(value, _min, _max);
134     }
135 
136     /* *************************************************************************
137      *  Static object creation methods
138      * ************************************************************************/
139 
140     private static final ObjectFactory<Integer64Gene> FACTORY =
141         new ObjectFactory<Integer64Gene>() {
142             @Override protected Integer64Gene create() {
143                 return new Integer64Gene();
144             }
145         };
146 
147     /**
148      * Create a new random {@code Integer64Gene} with the given value and the
149      * given range. If the {@code value} isn't within the closed interval
150      * [min, max], no exception is thrown. In this case the method
151      {@link Integer64Gene#isValid()} returns {@code false}.
152      *
153      @param value the value of the gene.
154      @param min the minimal valid value of this gene (inclusively).
155      @param max the maximal valid value of this gene (inclusively).
156      @return the new created gene with the given {@code value}.
157      */
158     public static Integer64Gene valueOf(
159         final long value,
160         final long min,
161         final long max
162     ) {
163         return valueOf(
164             Integer64.valueOf(value),
165             Integer64.valueOf(min),
166             Integer64.valueOf(max)
167         );
168     }
169 
170     /**
171      * Create a new random {@code Integer64Gene} with the given value and the
172      * given range. If the {@code value} isn't within the closed interval
173      * [min, max], no exception is thrown. In this case the method
174      {@link Integer64Gene#isValid()} returns {@code false}.
175      *
176      @param value the value of the gene.
177      @param min the minimal valid value of this gene (inclusively).
178      @param max the maximal valid value of this gene (inclusively).
179      @return the new created gene with the given {@code value}.
180      @throws NullPointerException if one of the arguments is {@code null}.
181      */
182     public static Integer64Gene valueOf(
183         final Integer64 value,
184         final Integer64 min,
185         final Integer64 max
186     ) {
187         final Integer64Gene gene = FACTORY.object();
188         gene.set(value, min, max);
189         return gene;
190     }
191 
192     /**
193      * Create a new random {@code Integer64Gene}. It is guaranteed that the
194      * value of the {@code Integer64Gene} lies in the closed interval [min, max].
195      *
196      @param min the minimal value of the {@code Integer64Gene} to create
197      *        (inclusively).
198      @param max the maximal value of the {@code Integer64Gene} to create
199      *        (inclusively).
200      @return the new created gene.
201      */
202     public static Integer64Gene valueOf(final long min, final long max) {
203         return valueOf(Integer64.valueOf(min), Integer64.valueOf(max));
204     }
205 
206     /**
207      * Create a new random {@code Integer64Gene}. It is guaranteed that the
208      * value of the {@code Integer64Gene} lies in the closed interval [min, max].
209      *
210      @param min the minimal value of the {@code Integer64Gene} to create
211      *        (inclusively).
212      @param max the maximal value of the {@code Integer64Gene} to create
213      *        (inclusively).
214      @return the new created gene.
215      @throws NullPointerException if one of the arguments is {@code null}.
216      */
217     public static Integer64Gene valueOf(
218         final Integer64 min,
219         final Integer64 max
220     ) {
221         final Random random = RandomRegistry.getRandom();
222         final Integer64 value = Integer64.valueOf(
223             math.random.nextLong(random, min.longValue(), max.longValue())
224         );
225 
226         return valueOf(value, min, max);
227     }
228 
229 
230     /* *************************************************************************
231      *  XML object serialization
232      * ************************************************************************/
233 
234     static final XMLFormat<Integer64Gene>
235     XML = new XMLFormat<Integer64Gene>(Integer64Gene.class)
236     {
237         private static final String MIN = "min";
238         private static final String MAX = "max";
239 
240         @Override
241         public Integer64Gene newInstance(
242             final Class<Integer64Gene> cls, final InputElement element
243         )
244             throws XMLStreamException
245         {
246             final long min = element.getAttribute(MIN, 0L);
247             final long max = element.getAttribute(MAX, 100L);
248             final long value = element.<Long>getNext();
249             return Integer64Gene.valueOf(value, min, max);
250         }
251         @Override
252         public void write(final Integer64Gene gene, final OutputElement element)
253             throws XMLStreamException
254         {
255             element.setAttribute(MIN, gene.getMin().longValue());
256             element.setAttribute(MAX, gene.getMax().longValue());
257             element.add(gene.getAllele().longValue());
258         }
259         @Override
260         public void read(final InputElement e, final Integer64Gene g) {
261         }
262     };
263 
264 
265     /* *************************************************************************
266      *  Java object serialization
267      * ************************************************************************/
268 
269     private void writeObject(final ObjectOutputStream out)
270         throws IOException
271     {
272         out.defaultWriteObject();
273 
274         out.writeLong(_value.longValue());
275         out.writeLong(_min.longValue());
276         out.writeLong(_max.longValue());
277     }
278 
279     private void readObject(final ObjectInputStream in)
280         throws IOException, ClassNotFoundException
281     {
282         in.defaultReadObject();
283 
284         set(
285             Integer64.valueOf(in.readLong()),
286             Integer64.valueOf(in.readLong()),
287             Integer64.valueOf(in.readLong())
288         );
289     }
290 
291 
292 }
293 
294 
295 
296 
297 
298