Integer64Chromosome.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 static java.util.Objects.requireNonNull;
023 import static org.jenetics.Integer64Gene.Value;
024 import static org.jenetics.internal.util.model.Integer64Model.Marshaller;
025 import static org.jenetics.internal.util.model.Integer64Model.Unmarshaller;
026 import static org.jenetics.util.functions.compose;
027 
028 import java.io.IOException;
029 import java.io.ObjectInputStream;
030 import java.io.ObjectOutputStream;
031 import java.util.List;
032 
033 import javax.xml.bind.annotation.XmlAccessType;
034 import javax.xml.bind.annotation.XmlAccessorType;
035 import javax.xml.bind.annotation.XmlAttribute;
036 import javax.xml.bind.annotation.XmlElement;
037 import javax.xml.bind.annotation.XmlRootElement;
038 import javax.xml.bind.annotation.XmlType;
039 import javax.xml.bind.annotation.adapters.XmlAdapter;
040 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
041 
042 import javolution.xml.XMLFormat;
043 import javolution.xml.XMLSerializable;
044 import javolution.xml.stream.XMLStreamException;
045 
046 import org.jscience.mathematics.number.Integer64;
047 
048 import org.jenetics.internal.util.HashBuilder;
049 import org.jenetics.internal.util.model.Integer64Model;
050 import org.jenetics.internal.util.model.ModelType;
051 import org.jenetics.internal.util.model.ValueType;
052 
053 import org.jenetics.util.Array;
054 import org.jenetics.util.Factory;
055 import org.jenetics.util.Function;
056 import org.jenetics.util.ISeq;
057 
058 
059 /**
060  * Number chromosome implementation which holds 64 bit integer numbers.
061  *
062  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
063  @since 1.0
064  @version 1.6 &mdash; <em>$Date: 2014-02-27 $</em>
065  *
066  @deprecated Use {@link org.jenetics.LongChromosome} instead. This classes
067  *             uses the <i>JScience</i> library, which will be removed in the
068  *             next major version.
069  */
070 @Deprecated
071 @XmlJavaTypeAdapter(Float64Chromosome.Model.Adapter.class)
072 public class Integer64Chromosome
073     extends NumberChromosome<Integer64, Integer64Gene>
074     implements XMLSerializable
075 {
076     private static final long serialVersionUID = 1L;
077 
078     /**
079      * Create a new chromosome from the given genes array.
080      *
081      @param genes the genes of the new chromosome.
082      @throws IllegalArgumentException if the {@code genes.length()} is smaller
083      *         than one.
084      */
085     protected Integer64Chromosome(final ISeq<Integer64Gene> genes) {
086         super(genes);
087     }
088 
089     private Integer64Chromosome(
090         final ISeq<Integer64Gene> genes,
091         final Integer64 min,
092         final Integer64 max
093     ) {
094         this(genes);
095         _min = requireNonNull(min);
096         _max = requireNonNull(max);
097     }
098 
099     /**
100      * Create a new chromosome from the given genes array.
101      *
102      @param genes the genes of the new chromosome.
103      @throws NullPointerException if the given genes array is {@code null}.
104      @throws IllegalArgumentException if the {@code genes.length} is smaller
105      *         than one.
106      */
107     public Integer64Chromosome(final Integer64Gene... genes) {
108         this(Array.of(genes).toISeq());
109     }
110 
111     /**
112      * Create a new random {@code Integer64Chromosome} of the given
113      * {@code length}.
114      *
115      @param min the minimum value of the {@link Integer64Gene}s (inclusively).
116      @param max the maximum value of the {@link Integer64Gene}s (inclusively).
117      @param length the length of the chromosome.
118      @throws NullPointerException if {@code min} or {@code max} is
119      *         {@code null}.
120      */
121     public Integer64Chromosome(
122         final Integer64 min,
123         final Integer64 max,
124         final int length
125     ) {
126         this(
127             new Array<Integer64Gene>(length).fill(
128                 Integer64Gene.valueOf(min, max)
129             ).toISeq()
130         );
131         _valid = true;
132     }
133 
134     /**
135      * Create a new random {@code Integer64Chromosome} of the given
136      * {@code length}.
137      *
138      @param min the minimum value of the {@link Integer64Gene}s (inclusively).
139      @param max the maximum value of the {@link Integer64Gene}s (inclusively).
140      @param length the length of the chromosome.
141      */
142     public Integer64Chromosome(final long min, final long max, int length) {
143         this(Integer64.valueOf(min), Integer64.valueOf(max), length);
144     }
145 
146     /**
147      * Create a new random {@code Integer64Chromosome} of length one.
148      *
149      @param min the minimum value of the {@link Integer64Gene}s (inclusively).
150      @param max the maximum value of the {@link Integer64Gene}s (inclusively).
151      */
152     public Integer64Chromosome(final long min, final long max) {
153         this(Integer64.valueOf(min), Integer64.valueOf(max));
154     }
155 
156     /**
157      * Create a new random {@code Integer64Chromosome} of length one.
158      *
159      @param min the minimum value of the {@link Integer64Gene}s (inclusively).
160      @param max the maximum value of the {@link Integer64Gene}s (inclusively).
161      @throws NullPointerException if {@code min} or {@code max} is
162      *         {@code null}.
163      */
164     public Integer64Chromosome(final Integer64 min, final Integer64 max) {
165         this(min, max, 1);
166     }
167 
168     @Override
169     public Integer64Chromosome newInstance(final ISeq<Integer64Gene> genes) {
170         return new Integer64Chromosome(genes);
171     }
172 
173     /**
174      * Return a more specific view of this chromosome factory.
175      *
176      @return a more specific view of this chromosome factory.
177      *
178      @deprecated No longer needed after adding new factory methods to the
179      *             {@link Array} class.
180      */
181     @Deprecated
182     @SuppressWarnings("unchecked")
183     public Factory<Integer64Chromosome> asFactory() {
184         return (Factory<Integer64Chromosome>)(Object)this;
185     }
186 
187     /**
188      * Create a new, <em>random</em> chromosome.
189      */
190     @Override
191     public Integer64Chromosome newInstance() {
192         return new Integer64Chromosome(_min, _max, length());
193     }
194 
195     @Override
196     public int hashCode() {
197         return HashBuilder.of(getClass()).and(super.hashCode()).value();
198     }
199 
200     @Override
201     public boolean equals(final Object obj) {
202         return obj == this ||
203                 obj instanceof Integer64Chromosome && super.equals(obj);
204     }
205 
206     /* *************************************************************************
207      *  Property access methods
208      * ************************************************************************/
209 
210     /**
211      * Return a {@link Function} which returns the gene array from this
212      {@link Chromosome}.
213      */
214     public static final Function<AbstractChromosome<Integer64Gene>, ISeq<Integer64Gene>>
215         Genes = AbstractChromosome.genes();
216 
217     /**
218      * Return a {@link Function} which returns the first {@link Gene} from this
219      {@link Chromosome}.
220      */
221     public static final Function<Chromosome<Integer64Gene>, Integer64Gene>
222         Gene = AbstractChromosome.gene();
223 
224     /**
225      * Return a {@link Function} which returns the {@link Gene} with the given
226      * {@code index} from this {@link Chromosome}.
227      */
228     public static final Function<Chromosome<Integer64Gene>, Integer64Gene>
229     Gene(final int index)
230     {
231         return AbstractChromosome.gene(index);
232     }
233 
234     /* *************************************************************************
235      *  Java object serialization
236      * ************************************************************************/
237 
238     private void writeObject(final ObjectOutputStream out)
239         throws IOException
240     {
241         out.defaultWriteObject();
242 
243         out.writeInt(length());
244         out.writeLong(_min.longValue());
245         out.writeLong(_max.longValue());
246 
247         for (Integer64Gene gene : _genes) {
248             out.writeLong(gene.longValue());
249         }
250     }
251 
252     private void readObject(final ObjectInputStream in)
253         throws IOException, ClassNotFoundException
254     {
255         in.defaultReadObject();
256 
257         final int length = in.readInt();
258         Integer64 min = Integer64.valueOf(in.readLong());
259         Integer64 max = Integer64.valueOf(in.readLong());
260 
261         _min = min;
262         _max = max;
263         final Array<Integer64Gene> genes = new Array<>(length);
264         for (int i = 0; i < length; ++i) {
265             genes.set(i, Integer64Gene.valueOf(Integer64.valueOf(in.readLong()), min, max));
266         }
267 
268         _genes = genes.toISeq();
269     }
270 
271     /* *************************************************************************
272      *  XML object serialization
273      * ************************************************************************/
274 
275     static final XMLFormat<Integer64Chromosome>
276         XML = new XMLFormat<Integer64Chromosome>(Integer64Chromosome.class) {
277         private static final String LENGTH = "length";
278         private static final String MIN = "min";
279         private static final String MAX = "max";
280 
281         @Override
282         public Integer64Chromosome newInstance(
283             final Class<Integer64Chromosome> cls, final InputElement xml
284         throws XMLStreamException
285         {
286             final int length = xml.getAttribute(LENGTH, 0);
287             final long min = xml.getAttribute(MIN, 0L);
288             final long max = xml.getAttribute(MAX, 100L);
289             final Array<Integer64Gene> genes = new Array<>(length);
290 
291             for (int i = 0; i < length; ++i) {
292                 final Integer64 value = xml.getNext();
293                 genes.set(i, Integer64Gene.valueOf(value.longValue(), min, max));
294             }
295 
296             final Integer64Chromosome chromosome = new Integer64Chromosome(genes.toISeq());
297             chromosome._min = Integer64.valueOf(min);
298             chromosome._max = Integer64.valueOf(max);
299 
300             return chromosome;
301         }
302         @Override
303         public void write(final Integer64Chromosome chromosome, final OutputElement xml)
304             throws XMLStreamException
305         {
306             xml.setAttribute(LENGTH, chromosome.length());
307             xml.setAttribute(MIN, chromosome._min.longValue());
308             xml.setAttribute(MAX, chromosome._max.longValue());
309             for (Integer64Gene gene : chromosome) {
310                 xml.add(gene.getAllele());
311             }
312         }
313         @Override
314         public void read(final InputElement e, final Integer64Chromosome c) {
315         }
316     };
317 
318     /* *************************************************************************
319      *  JAXB object serialization
320      * ************************************************************************/
321 
322     @XmlRootElement(name = "org.jenetics.Integer64Chromosome")
323     @XmlType(name = "org.jenetics.Integer64Chromosome")
324     @XmlAccessorType(XmlAccessType.FIELD)
325     final static class Model {
326 
327         @XmlAttribute
328         public int length;
329 
330         @XmlAttribute
331         public long min;
332 
333         @XmlAttribute
334         public long max;
335 
336         @XmlElement(name = "org.jscience.mathematics.number.Integer64")
337         public List<Integer64Model> values;
338 
339         @ValueType(Integer64Chromosome.class)
340         @ModelType(Model.class)
341         public final static class Adapter
342             extends XmlAdapter<Model, Integer64Chromosome>
343         {
344             @Override
345             public Model marshal(final Integer64Chromosome c) {
346                 final Model m = new Model();
347                 m.length = c.length();
348                 m.min = c._min.longValue();
349                 m.max = c._max.longValue();
350                 m.values = c.toSeq().map(compose(Value, Marshaller)).asList();
351                 return m;
352             }
353 
354             @Override
355             public Integer64Chromosome unmarshal(final Model model) {
356                 final Integer64 min = Integer64.valueOf(model.min);
357                 final Integer64 max = Integer64.valueOf(model.max);
358                 final ISeq<Integer64Gene> genes = Array.of(model.values)
359                     .map(compose(Unmarshaller, Integer64Gene.Gene(min, max)))
360                     .toISeq();
361 
362                 return new Integer64Chromosome(genes, min, max);
363             }
364         }
365     }
366 
367 }