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