Integer64Chromosome.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 static org.jenetics.util.object.hashCodeOf;
023 
024 import java.io.IOException;
025 import java.io.ObjectInputStream;
026 import java.io.ObjectOutputStream;
027 
028 import javolution.xml.XMLFormat;
029 import javolution.xml.XMLSerializable;
030 import javolution.xml.stream.XMLStreamException;
031 
032 import org.jscience.mathematics.number.Integer64;
033 
034 import org.jenetics.util.Array;
035 import org.jenetics.util.Factory;
036 import org.jenetics.util.Function;
037 import org.jenetics.util.ISeq;
038 
039 
040 /**
041  * Number chromosome implementation which holds 64 bit integer numbers.
042  *
043  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044  @since 1.0
045  @version 1.0 &mdash; <em>$Date: 2013-12-05 $</em>
046  */
047 public class Integer64Chromosome
048     extends NumberChromosome<Integer64, Integer64Gene>
049     implements XMLSerializable
050 {
051     private static final long serialVersionUID = 1L;
052 
053     /**
054      * Create a new chromosome from the given genes array.
055      *
056      @param genes the genes of the new chromosome.
057      @throws IllegalArgumentException if the {@code genes.length()} is smaller
058      *         than one.
059      */
060     protected Integer64Chromosome(final ISeq<Integer64Gene> genes) {
061         super(genes);
062     }
063 
064     /**
065      * Create a new chromosome from the given genes array.
066      *
067      @param genes the genes of the new chromosome.
068      @throws NullPointerException if the given genes array is {@code null}.
069      @throws IllegalArgumentException if the {@code genes.length} is smaller
070      *         than one.
071      */
072     public Integer64Chromosome(final Integer64Gene... genes) {
073         this(Array.valueOf(genes).toISeq());
074     }
075 
076     /**
077      * Create a new random {@code Integer64Chromosome} of the given
078      * {@code length}.
079      *
080      @param min the minimum value of the {@link Integer64Gene}s (inclusively).
081      @param max the maximum value of the {@link Integer64Gene}s (inclusively).
082      @param length the length of the chromosome.
083      @throws NullPointerException if {@code min} or {@code max} is
084      *         {@code null}.
085      */
086     public Integer64Chromosome(
087         final Integer64 min,
088         final Integer64 max,
089         final int length
090     ) {
091         this(
092             new Array<Integer64Gene>(length).fill(
093                 Integer64Gene.valueOf(min, max)
094             ).toISeq()
095         );
096         _valid = true;
097     }
098 
099     /**
100      * Create a new random {@code Integer64Chromosome} of the given
101      * {@code length}.
102      *
103      @param min the minimum value of the {@link Integer64Gene}s (inclusively).
104      @param max the maximum value of the {@link Integer64Gene}s (inclusively).
105      @param length the length of the chromosome.
106      */
107     public Integer64Chromosome(final long min, final long max, int length) {
108         this(Integer64.valueOf(min), Integer64.valueOf(max), length);
109     }
110 
111     /**
112      * Create a new random {@code Integer64Chromosome} of length one.
113      *
114      @param min the minimum value of the {@link Integer64Gene}s (inclusively).
115      @param max the maximum value of the {@link Integer64Gene}s (inclusively).
116      */
117     public Integer64Chromosome(final long min, final long max) {
118         this(Integer64.valueOf(min), Integer64.valueOf(max));
119     }
120 
121     /**
122      * Create a new random {@code Integer64Chromosome} of length one.
123      *
124      @param min the minimum value of the {@link Integer64Gene}s (inclusively).
125      @param max the maximum value of the {@link Integer64Gene}s (inclusively).
126      @throws NullPointerException if {@code min} or {@code max} is
127      *         {@code null}.
128      */
129     public Integer64Chromosome(final Integer64 min, final Integer64 max) {
130         this(min, max, 1);
131     }
132 
133     @Override
134     public Integer64Chromosome newInstance(final ISeq<Integer64Gene> genes) {
135         return new Integer64Chromosome(genes);
136     }
137 
138     /**
139      * Return a more specific view of this chromosome factory.
140      *
141      @return a more specific view of this chromosome factory.
142      *
143      @deprecated No longer needed after adding new factory methods to the
144      *             {@link Array} class.
145      */
146     @Deprecated
147     @SuppressWarnings("unchecked")
148     public Factory<Integer64Chromosome> asFactory() {
149         return (Factory<Integer64Chromosome>)(Object)this;
150     }
151 
152     /**
153      * Create a new, <em>random</em> chromosome.
154      */
155     @Override
156     public Integer64Chromosome newInstance() {
157         return new Integer64Chromosome(_min, _max, length());
158     }
159 
160     @Override
161     public int hashCode() {
162         return hashCodeOf(getClass()).and(super.hashCode()).value();
163     }
164 
165     @Override
166     public boolean equals(final Object obj) {
167         return obj == this ||
168                 obj instanceof Integer64Chromosome && super.equals(obj);
169     }
170 
171     /* *************************************************************************
172      *  Property access methods
173      * ************************************************************************/
174 
175     /**
176      * Return a {@link Function} which returns the gene array from this
177      {@link Chromosome}.
178      */
179     public static final Function<AbstractChromosome<Integer64Gene>, ISeq<Integer64Gene>>
180         Genes = AbstractChromosome.genes();
181 
182     /**
183      * Return a {@link Function} which returns the first {@link Gene} from this
184      {@link Chromosome}.
185      */
186     public static final Function<Chromosome<Integer64Gene>, Integer64Gene>
187         Gene = AbstractChromosome.gene();
188 
189     /**
190      * Return a {@link Function} which returns the {@link Gene} with the given
191      * {@code index} from this {@link Chromosome}.
192      */
193     public static final Function<Chromosome<Integer64Gene>, Integer64Gene>
194     Gene(final int index)
195     {
196         return AbstractChromosome.gene(index);
197     }
198 
199     /* *************************************************************************
200      *  XML object serialization
201      * ************************************************************************/
202 
203     static final XMLFormat<Integer64Chromosome>
204     XML = new XMLFormat<Integer64Chromosome>(Integer64Chromosome.class) {
205         private static final String LENGTH = "length";
206         private static final String MIN = "min";
207         private static final String MAX = "max";
208 
209         @Override
210         public Integer64Chromosome newInstance(
211             final Class<Integer64Chromosome> cls, final InputElement xml
212         throws XMLStreamException
213         {
214             final int length = xml.getAttribute(LENGTH, 0);
215             final long min = xml.getAttribute(MIN, 0L);
216             final long max = xml.getAttribute(MAX, 100L);
217             final Array<Integer64Gene> genes = new Array<>(length);
218 
219             for (int i = 0; i < length; ++i) {
220                 final Integer64 value = xml.getNext();
221                 genes.set(i, Integer64Gene.valueOf(value.longValue(), min, max));
222             }
223 
224             final Integer64Chromosome chromosome = new Integer64Chromosome(genes.toISeq());
225             chromosome._min = Integer64.valueOf(min);
226             chromosome._max = Integer64.valueOf(max);
227 
228             return chromosome;
229         }
230         @Override
231         public void write(final Integer64Chromosome chromosome, final OutputElement xml)
232             throws XMLStreamException
233         {
234             xml.setAttribute(LENGTH, chromosome.length());
235             xml.setAttribute(MIN, chromosome._min.longValue());
236             xml.setAttribute(MAX, chromosome._max.longValue());
237             for (Integer64Gene gene : chromosome) {
238                 xml.add(gene.getAllele());
239             }
240         }
241         @Override
242         public void read(final InputElement e, final Integer64Chromosome c) {
243         }
244     };
245 
246     /* *************************************************************************
247      *  Java object serialization
248      * ************************************************************************/
249 
250     private void writeObject(final ObjectOutputStream out)
251         throws IOException
252     {
253         out.defaultWriteObject();
254 
255         out.writeInt(length());
256         out.writeLong(_min.longValue());
257         out.writeLong(_max.longValue());
258 
259         for (Integer64Gene gene : _genes) {
260             out.writeLong(gene.longValue());
261         }
262     }
263 
264     private void readObject(final ObjectInputStream in)
265         throws IOException, ClassNotFoundException
266     {
267         in.defaultReadObject();
268 
269         final int length = in.readInt();
270         Integer64 min = Integer64.valueOf(in.readLong());
271         Integer64 max = Integer64.valueOf(in.readLong());
272 
273         _min = min;
274         _max = max;
275         final Array<Integer64Gene> genes = new Array<>(length);
276         for (int i = 0; i < length; ++i) {
277             genes.set(i, Integer64Gene.valueOf(Integer64.valueOf(in.readLong()), min, max));
278         }
279 
280         _genes = genes.toISeq();
281     }
282 
283 }
284 
285 
286 
287 
288