LongChromosome.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.2.0).
003  * Copyright (c) 2007-2018 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@gmail.com)
019  */
020 package io.jenetics;
021 
022 import java.io.IOException;
023 import java.io.ObjectInputStream;
024 import java.io.ObjectOutputStream;
025 import java.io.Serializable;
026 
027 import io.jenetics.internal.util.Equality;
028 import io.jenetics.internal.util.Hash;
029 import io.jenetics.internal.util.reflect;
030 import io.jenetics.util.ISeq;
031 import io.jenetics.util.IntRange;
032 import io.jenetics.util.LongRange;
033 import io.jenetics.util.MSeq;
034 
035 /**
036  * Numeric chromosome implementation which holds 64 bit integer numbers.
037  *
038  @see LongGene
039  *
040  * @implNote
041  * This class is immutable and thread-safe.
042  *
043  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
044  @since 1.6
045  @version 4.0
046  */
047 public class LongChromosome
048     extends AbstractBoundedChromosome<Long, LongGene>
049     implements
050         NumericChromosome<Long, LongGene>,
051         Serializable
052 {
053     private static final long serialVersionUID = 2L;
054 
055     /**
056      * Create a new chromosome from the given {@code genes} and the allowed
057      * length range of the chromosome.
058      *
059      @since 4.0
060      *
061      @param genes the genes that form the chromosome.
062      @param lengthRange the allowed length range of the chromosome
063      @throws NullPointerException if one of the arguments is {@code null}.
064      @throws IllegalArgumentException if the length of the gene sequence is
065      *         empty, doesn't match with the allowed length range, the minimum
066      *         or maximum of the range is smaller or equal zero or the given
067      *         range size is zero.
068      */
069     protected LongChromosome(
070         final ISeq<LongGene> genes,
071         final IntRange lengthRange
072     ) {
073         super(genes, lengthRange);
074     }
075 
076     /**
077      * Create a new random chromosome.
078      *
079      @since 4.0
080      *
081      @param min the min value of the {@link LongGene}s (inclusively).
082      @param max the max value of the {@link LongGene}s (inclusively).
083      @param lengthRange the allowed length range of the chromosome.
084      @throws NullPointerException if one of the arguments is {@code null}.
085      @throws IllegalArgumentException if the length is smaller than one
086      */
087     public LongChromosome(
088         final Long min,
089         final Long max,
090         final IntRange lengthRange
091     ) {
092         this(LongGene.seq(min, max, lengthRange), lengthRange);
093         _valid = true;
094     }
095 
096     /**
097      * Create a new random {@code LongChromosome}.
098      *
099      @param min the min value of the {@link LongGene}s (inclusively).
100      @param max the max value of the {@link LongGene}s (inclusively).
101      @param length the length of the chromosome.
102      @throws NullPointerException if one of the arguments is {@code null}.
103      @throws IllegalArgumentException if the {@code length} is smaller than
104      *         one.
105      */
106     public LongChromosome(final Long min, final Long max, final int length) {
107         this(min, max, IntRange.of(length));
108     }
109 
110     /**
111      * Create a new random {@code LongChromosome} of length one.
112      *
113      @param min the minimal value of this chromosome (inclusively).
114      @param max the maximal value of this chromosome (inclusively).
115      @throws NullPointerException if one of the arguments is {@code null}.
116      */
117     public LongChromosome(final Long min, final Long max) {
118         this(min, max, 1);
119     }
120 
121     /**
122      * Returns an long array containing all of the elements in this chromosome
123      * in proper sequence.  If the chromosome fits in the specified array, it is
124      * returned therein. Otherwise, a new array is allocated with the length of
125      * this chromosome.
126      *
127      @since 3.0
128      *
129      @param array the array into which the elements of this chromosomes are to
130      *        be stored, if it is big enough; otherwise, a new array is
131      *        allocated for this purpose.
132      @return an array containing the elements of this chromosome
133      @throws NullPointerException if the given {@code array} is {@code null}
134      */
135     public long[] toArray(final long[] array) {
136         final long[] a = array.length >= length() ? array : new long[length()];
137         for (int i = length(); --i >= 0;) {
138             a[i= longValue(i);
139         }
140 
141         return a;
142     }
143 
144     /**
145      * Returns an long array containing all of the elements in this chromosome
146      * in proper sequence.
147      *
148      @since 3.0
149      *
150      @return an array containing the elements of this chromosome
151      */
152     public long[] toArray() {
153         return toArray(new long[length()]);
154     }
155 
156     /**
157      * Create a new {@code LongChromosome} with the given genes.
158      *
159      @param genes the genes of the chromosome.
160      @return a new chromosome with the given genes.
161      @throws IllegalArgumentException if the length of the genes array is
162      *         empty.
163      @throws NullPointerException if the given {@code genes} are {@code null}
164      */
165     public static LongChromosome of(final LongGene... genes) {
166         return new LongChromosome(ISeq.of(genes), IntRange.of(genes.length));
167     }
168 
169     /**
170      * Create a new random chromosome.
171      *
172      @since 4.0
173      *
174      @param min the min value of the {@link LongGene}s (inclusively).
175      @param max the max value of the {@link LongGene}s (inclusively).
176      @param lengthRange the allowed length range of the chromosome.
177      @return a new {@code IntegerChromosome} with the given parameter
178      @throws IllegalArgumentException if the length of the gene sequence is
179      *         empty, doesn't match with the allowed length range, the minimum
180      *         or maximum of the range is smaller or equal zero or the given
181      *         range size is zero.
182      @throws NullPointerException if the given {@code lengthRange} is
183      *         {@code null}
184      */
185     public static LongChromosome of(
186         final long min,
187         final long max,
188         final IntRange lengthRange
189     ) {
190         return new LongChromosome(min, max, lengthRange);
191     }
192 
193     /**
194      * Create a new random {@code LongChromosome}.
195      *
196      @param min the min value of the {@link LongGene}s (inclusively).
197      @param max the max value of the {@link LongGene}s (inclusively).
198      @param length the length of the chromosome.
199      @return a new {@code LongChromosome} with the given gene parameters.
200      @throws IllegalArgumentException if the {@code length} is smaller than
201      *         one.
202      */
203     public static LongChromosome of(
204         final long min,
205         final long max,
206         final int length
207     ) {
208         return new LongChromosome(min, max, length);
209     }
210 
211     /**
212      * Create a new random chromosome.
213      *
214      @since 4.0
215      *
216      @param range the integer range of the chromosome.
217      @param lengthRange the allowed length range of the chromosome.
218      @return a new {@code LongChromosome} with the given parameter
219      @throws IllegalArgumentException if the length of the gene sequence is
220      *         empty, doesn't match with the allowed length range, the minimum
221      *         or maximum of the range is smaller or equal zero or the given
222      *         range size is zero.
223      @throws NullPointerException if the given {@code lengthRange} is
224      *         {@code null}
225      */
226     public static LongChromosome of(
227         final LongRange range,
228         final IntRange lengthRange
229     ) {
230         return new LongChromosome(range.getMin(), range.getMax(), lengthRange);
231     }
232 
233     /**
234      * Create a new random {@code LongChromosome}.
235      *
236      @since 3.2
237      *
238      @param range the long range of the chromosome.
239      @param length the length of the chromosome.
240      @return a new random {@code LongChromosome}
241      @throws NullPointerException if the given {@code range} is {@code null}
242      @throws IllegalArgumentException if the {@code length} is smaller than
243      *         one.
244      */
245     public static LongChromosome of(final LongRange range, final int length) {
246         return new LongChromosome(range.getMin(), range.getMax(), length);
247     }
248 
249     /**
250      * Create a new random {@code LongChromosome} of length one.
251      *
252      @param min the minimal value of this chromosome (inclusively).
253      @param max the maximal value of this chromosome (inclusively).
254      @return a new {@code LongChromosome} with the given gene parameters.
255      */
256     public static LongChromosome of(final long min, final long max) {
257         return new LongChromosome(min, max);
258     }
259 
260     /**
261      * Create a new random {@code LongChromosome} of length one.
262      *
263      @since 3.2
264      *
265      @param range the long range of the chromosome.
266      @return a new random {@code LongChromosome} of length one
267      @throws NullPointerException if the given {@code range} is {@code null}
268      */
269     public static LongChromosome of(final LongRange range) {
270         return new LongChromosome(range.getMin(), range.getMax());
271     }
272 
273     @Override
274     public LongChromosome newInstance(final ISeq<LongGene> genes) {
275         return new LongChromosome(genes, lengthRange());
276     }
277 
278     @Override
279     public LongChromosome newInstance() {
280         return new LongChromosome(_min, _max, lengthRange());
281     }
282 
283     @Override
284     public int hashCode() {
285         return Hash.of(getClass()).and(super.hashCode()).value();
286     }
287 
288     @Override
289     public boolean equals(final Object obj) {
290         return Equality.of(this, obj).test(super::equals);
291     }
292 
293     /* *************************************************************************
294      *  Java object serialization
295      * ************************************************************************/
296 
297     private void writeObject(final ObjectOutputStream out)
298         throws IOException
299     {
300         out.defaultWriteObject();
301 
302         out.writeInt(length());
303         out.writeObject(lengthRange());
304         out.writeLong(_min);
305         out.writeLong(_max);
306 
307         for (LongGene gene : _genes) {
308             out.writeLong(gene.getAllele());
309         }
310     }
311 
312     private void readObject(final ObjectInputStream in)
313         throws IOException, ClassNotFoundException
314     {
315         in.defaultReadObject();
316 
317         final MSeq<LongGene> genes = MSeq.ofLength(in.readInt());
318         reflect.setField(this, "_lengthRange", in.readObject());
319         reflect.setField(this, "_min", in.readLong());
320         reflect.setField(this, "_max", in.readLong());
321 
322         for (int i = 0; i < genes.length(); ++i) {
323             genes.set(i, new LongGene(in.readLong(), _min, _max));
324         }
325 
326         reflect.setField(this, "_genes", genes.toISeq());
327     }
328 
329 }