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