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