DoubleChromosome.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.0.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      */
151     public static DoubleChromosome of(final DoubleGene... genes) {
152         checkGeneRange(Stream.of(genes).map(DoubleGene::range));
153         return new DoubleChromosome(ISeq.of(genes), IntRange.of(genes.length));
154     }
155 
156     /**
157      * Create a new {@code DoubleChromosome} with the given genes.
158      *
159      @since 4.3
160      *
161      @param genes the genes of the chromosome.
162      @return a new chromosome with the given genes.
163      @throws NullPointerException if the given {@code genes} are {@code null}
164      @throws IllegalArgumentException if the of the genes iterable is empty or
165      *         the given {@code genes} doesn't have the same range.
166      */
167     public static DoubleChromosome of(final Iterable<DoubleGene> genes) {
168         final ISeq<DoubleGene> values = ISeq.of(genes);
169         checkGeneRange(values.stream().map(DoubleGene::range));
170         return new DoubleChromosome(values, IntRange.of(values.length()));
171     }
172 
173     /**
174      * Create a new random chromosome.
175      *
176      @since 4.0
177      *
178      @param min the min value of the {@link DoubleGene}s (inclusively).
179      @param max the max value of the {@link DoubleGene}s (exclusively).
180      @param lengthRange the allowed length range of the chromosome.
181      @return a new {@code DoubleChromosome} with the given parameter
182      @throws IllegalArgumentException if the length of the gene sequence is
183      *         empty, doesn't match with the allowed length range, the minimum
184      *         or maximum of the range is smaller or equal zero or the given
185      *         range size is zero.
186      @throws NullPointerException if the given {@code lengthRange} is
187      *         {@code null}
188      */
189     public static DoubleChromosome of(
190         final double min,
191         final double max,
192         final IntRange lengthRange
193     ) {
194         final ISeq<DoubleGene> genes = DoubleGene.seq(min, max, lengthRange);
195         return new DoubleChromosome(genes, lengthRange);
196     }
197 
198     /**
199      * Create a new random {@code DoubleChromosome}.
200      *
201      @param min the min value of the {@link DoubleGene}s (inclusively).
202      @param max the max value of the {@link DoubleGene}s (exclusively).
203      @param length the length of the chromosome.
204      @return a new {@code DoubleChromosome} with the given parameter
205      @throws IllegalArgumentException if the {@code length} is smaller than
206      *         one.
207      */
208     public static DoubleChromosome of(
209         final double min,
210         final double max,
211         final int length
212     ) {
213         return of(min, max, IntRange.of(length));
214     }
215 
216     /**
217      * Create a new random chromosome.
218      *
219      @since 4.0
220      *
221      @param range the integer range of the chromosome.
222      @param lengthRange the allowed length range of the chromosome.
223      @return a new {@code DoubleChromosome} with the given parameter
224      @throws IllegalArgumentException if the length of the gene sequence is
225      *         empty, doesn't match with the allowed length range, the minimum
226      *         or maximum of the range is smaller or equal zero or the given
227      *         range size is zero.
228      @throws NullPointerException if the given {@code lengthRange} is
229      *         {@code null}
230      */
231     public static DoubleChromosome of(
232         final DoubleRange range,
233         final IntRange lengthRange
234     ) {
235         return of(range.getMin(), range.getMax(), lengthRange);
236     }
237 
238     /**
239      * Create a new random {@code DoubleChromosome}.
240      *
241      @since 3.2
242      *
243      @param range the integer range of the chromosome.
244      @param length the length of the chromosome.
245      @return a new random {@code DoubleChromosome}
246      @throws NullPointerException if the given {@code range} is {@code null}
247      @throws IllegalArgumentException if the {@code length} is smaller than
248      *         one.
249      */
250     public static DoubleChromosome of(final DoubleRange range, final int length) {
251         return of(range.getMin(), range.getMax(), length);
252     }
253 
254     /**
255      * Create a new random {@code DoubleChromosome} of length one.
256      *
257      @param min the minimal value of this chromosome (inclusively).
258      @param max the maximal value of this chromosome (exclusively).
259      @return a new {@code DoubleChromosome} with the given parameter
260      */
261     public static DoubleChromosome of(final double min, final double max) {
262         return of(min, max, 1);
263     }
264 
265     /**
266      * Create a new random {@code DoubleChromosome} of length one.
267      *
268      @since 3.2
269      *
270      @param range the double range of the chromosome.
271      @return a new random {@code DoubleChromosome} of length one
272      @throws NullPointerException if the given {@code range} is {@code null}
273      */
274     public static DoubleChromosome of(final DoubleRange range) {
275         return of(range.getMin(), range.getMax());
276     }
277 
278 
279     /* *************************************************************************
280      *  Java object serialization
281      * ************************************************************************/
282 
283     private Object writeReplace() {
284         return new Serial(Serial.DOUBLE_CHROMOSOME, this);
285     }
286 
287     private void readObject(final ObjectInputStream stream)
288         throws InvalidObjectException
289     {
290         throw new InvalidObjectException("Serialization proxy required.");
291     }
292 
293     void write(final DataOutput outthrows IOException {
294         writeInt(length(), out);
295         writeInt(lengthRange().getMin(), out);
296         writeInt(lengthRange().getMax(), out);
297         out.writeDouble(_min);
298         out.writeDouble(_max);
299 
300         for (int i = 0, n = length(); i < n; ++i) {
301             out.writeDouble(doubleValue(i));
302         }
303     }
304 
305     static DoubleChromosome read(final DataInput inthrows IOException {
306         final int length = readInt(in);
307         final IntRange lengthRange = IntRange.of(readInt(in), readInt(in));
308         final double min = in.readDouble();
309         final double max = in.readDouble();
310 
311         final MSeq<DoubleGene> values = MSeq.ofLength(length);
312         for (int i = 0; i < length; ++i) {
313             values.set(i, DoubleGene.of(in.readDouble(), min, max));
314         }
315 
316         return new DoubleChromosome(values.toISeq(), lengthRange);
317     }
318 
319 }