DoubleChromosome.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.DoubleStream;
027 import java.util.stream.IntStream;
028 import java.util.stream.Stream;
029 
030 import io.jenetics.internal.util.reflect;
031 import io.jenetics.util.DoubleRange;
032 import io.jenetics.util.ISeq;
033 import io.jenetics.util.IntRange;
034 import io.jenetics.util.MSeq;
035 
036 /**
037  * Numeric chromosome implementation which holds 64 bit floating point numbers.
038  *
039  @see DoubleGene
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 DoubleChromosome
049     extends AbstractBoundedChromosome<Double, DoubleGene>
050     implements
051         NumericChromosome<Double, DoubleGene>,
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 DoubleChromosome(
071         final ISeq<DoubleGene> 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 DoubleGene}s (inclusively).
083      @param max the max value of the {@link DoubleGene}s (exclusively).
084      @param lengthRange the allowed length range of the chromosome. The start
085      *        of the range is inclusively and the range end exclusively
086      @throws NullPointerException if one of the arguments is {@code null}.
087      @throws IllegalArgumentException if the length is smaller than one
088      *
089      @deprecated Use {@link #of(double, double, IntRange)} instead.
090      */
091     @Deprecated
092     public DoubleChromosome(
093         final Double min,
094         final Double max,
095         final IntRange lengthRange
096     ) {
097         this(DoubleGene.seq(min, max, lengthRange), lengthRange);
098         _valid = true;
099     }
100 
101     /**
102      * Create a new random {@code DoubleChromosome}.
103      *
104      @param min the min value of the {@link DoubleGene}s (inclusively).
105      @param max the max value of the {@link DoubleGene}s (exclusively).
106      @param length the length of the chromosome.
107      @throws NullPointerException if one of the arguments is {@code null}.
108      @throws IllegalArgumentException if the length is smaller than one
109      *
110      @deprecated Use {@link #of(double, double, int)} instead.
111      */
112     @Deprecated
113     public DoubleChromosome(final Double min,final Double max,final int length) {
114         this(min, max, IntRange.of(length));
115     }
116 
117     /**
118      * Create a new random {@code DoubleChromosome} of length one.
119      *
120      @param min the minimal value of this chromosome (inclusively).
121      @param max the maximal value of this chromosome (exclusively).
122      @throws NullPointerException if one of the arguments is {@code null}.
123      *
124      @deprecated Use {@link #of(double, double)} instead.
125      */
126     @Deprecated
127     public DoubleChromosome(final Double min, final Double max) {
128         this(min, max, 1);
129     }
130 
131     @Override
132     public DoubleChromosome newInstance(final ISeq<DoubleGene> genes) {
133         return new DoubleChromosome(genes, lengthRange());
134     }
135 
136     @Override
137     public DoubleChromosome 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 DoubleStream doubleStream() {
150         return IntStream.range(0, length()).mapToDouble(this::doubleValue);
151     }
152 
153     /**
154      * Returns an double 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 double[] toArray(final double[] array) {
168         final double[] a = array.length >= length() ? array : new double[length()];
169         for (int i = length(); --i >= 0;) {
170             a[i= doubleValue(i);
171         }
172 
173         return a;
174     }
175 
176     /**
177      * Returns an double 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 double[] toArray() {
185         return toArray(new double[length()]);
186     }
187 
188 
189     /* *************************************************************************
190      * Static factory methods.
191      * ************************************************************************/
192 
193     /**
194      * Create a new {@code DoubleChromosome} with the given genes.
195      *
196      @param genes the genes of the chromosome.
197      @return a new chromosome with the given genes.
198      @throws IllegalArgumentException if the length of the genes array is
199      *         empty or the given {@code genes} doesn't have the same range.
200      */
201     public static DoubleChromosome of(final DoubleGene... genes) {
202         checkGeneRange(Stream.of(genes).map(DoubleGene::range));
203         return new DoubleChromosome(ISeq.of(genes), IntRange.of(genes.length));
204     }
205 
206     /**
207      * Create a new {@code DoubleChromosome} with the given genes.
208      *
209      @since 4.3
210      *
211      @param genes the genes of the chromosome.
212      @return a new chromosome with the given genes.
213      @throws NullPointerException if the given {@code genes} are {@code null}
214      @throws IllegalArgumentException if the of the genes iterable is empty or
215      *         the given {@code genes} doesn't have the same range.
216      */
217     public static DoubleChromosome of(final Iterable<DoubleGene> genes) {
218         final ISeq<DoubleGene> values = ISeq.of(genes);
219         checkGeneRange(values.stream().map(DoubleGene::range));
220         return new DoubleChromosome(values, IntRange.of(values.length()));
221     }
222 
223     /**
224      * Create a new random chromosome.
225      *
226      @since 4.0
227      *
228      @param min the min value of the {@link DoubleGene}s (inclusively).
229      @param max the max value of the {@link DoubleGene}s (exclusively).
230      @param lengthRange the allowed length range of the chromosome.
231      @return a new {@code DoubleChromosome} with the given parameter
232      @throws IllegalArgumentException if the length of the gene sequence is
233      *         empty, doesn't match with the allowed length range, the minimum
234      *         or maximum of the range is smaller or equal zero or the given
235      *         range size is zero.
236      @throws NullPointerException if the given {@code lengthRange} is
237      *         {@code null}
238      */
239     public static DoubleChromosome of(
240         final double min,
241         final double max,
242         final IntRange lengthRange
243     ) {
244         final ISeq<DoubleGene> genes = DoubleGene.seq(min, max, lengthRange);
245         return new DoubleChromosome(genes, lengthRange);
246     }
247 
248     /**
249      * Create a new random {@code DoubleChromosome}.
250      *
251      @param min the min value of the {@link DoubleGene}s (inclusively).
252      @param max the max value of the {@link DoubleGene}s (exclusively).
253      @param length the length of the chromosome.
254      @return a new {@code DoubleChromosome} with the given parameter
255      @throws IllegalArgumentException if the {@code length} is smaller than
256      *         one.
257      */
258     public static DoubleChromosome of(
259         final double min,
260         final double max,
261         final int length
262     ) {
263         return of(min, max, IntRange.of(length));
264     }
265 
266     /**
267      * Create a new random chromosome.
268      *
269      @since 4.0
270      *
271      @param range the integer range of the chromosome.
272      @param lengthRange the allowed length range of the chromosome.
273      @return a new {@code DoubleChromosome} with the given parameter
274      @throws IllegalArgumentException if the length of the gene sequence is
275      *         empty, doesn't match with the allowed length range, the minimum
276      *         or maximum of the range is smaller or equal zero or the given
277      *         range size is zero.
278      @throws NullPointerException if the given {@code lengthRange} is
279      *         {@code null}
280      */
281     public static DoubleChromosome of(
282         final DoubleRange range,
283         final IntRange lengthRange
284     ) {
285         return of(range.getMin(), range.getMax(), lengthRange);
286     }
287 
288     /**
289      * Create a new random {@code DoubleChromosome}.
290      *
291      @since 3.2
292      *
293      @param range the integer range of the chromosome.
294      @param length the length of the chromosome.
295      @return a new random {@code DoubleChromosome}
296      @throws NullPointerException if the given {@code range} is {@code null}
297      @throws IllegalArgumentException if the {@code length} is smaller than
298      *         one.
299      */
300     public static DoubleChromosome of(final DoubleRange range, final int length) {
301         return of(range.getMin(), range.getMax(), length);
302     }
303 
304     /**
305      * Create a new random {@code DoubleChromosome} of length one.
306      *
307      @param min the minimal value of this chromosome (inclusively).
308      @param max the maximal value of this chromosome (exclusively).
309      @return a new {@code DoubleChromosome} with the given parameter
310      */
311     public static DoubleChromosome of(final double min, final double max) {
312         return of(min, max, 1);
313     }
314 
315     /**
316      * Create a new random {@code DoubleChromosome} of length one.
317      *
318      @since 3.2
319      *
320      @param range the double range of the chromosome.
321      @return a new random {@code DoubleChromosome} of length one
322      @throws NullPointerException if the given {@code range} is {@code null}
323      */
324     public static DoubleChromosome of(final DoubleRange range) {
325         return of(range.getMin(), range.getMax());
326     }
327 
328 
329     /* *************************************************************************
330      *  Java object serialization
331      * ************************************************************************/
332 
333     private void writeObject(final ObjectOutputStream out)
334         throws IOException
335     {
336         out.defaultWriteObject();
337 
338         out.writeInt(length());
339         out.writeObject(lengthRange());
340         out.writeDouble(_min);
341         out.writeDouble(_max);
342 
343         for (DoubleGene gene : _genes) {
344             out.writeDouble(gene.getAllele());
345         }
346     }
347 
348     private void readObject(final ObjectInputStream in)
349         throws IOException, ClassNotFoundException
350     {
351         in.defaultReadObject();
352 
353         final MSeq<DoubleGene> genes = MSeq.ofLength(in.readInt());
354         reflect.setField(this, "_lengthRange", in.readObject());
355         reflect.setField(this, "_min", in.readDouble());
356         reflect.setField(this, "_max", in.readDouble());
357 
358         for (int i = 0; i < genes.length(); ++i) {
359             genes.set(i, DoubleGene.of(in.readDouble(), _min, _max));
360         }
361 
362         reflect.setField(this, "_genes", genes.toISeq());
363     }
364 
365 }