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