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