001 /*
002 * Java Genetic Algorithm Library (jenetics-6.3.0).
003 * Copyright (c) 2007-2021 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 java.util.Objects.requireNonNull;
023 import static io.jenetics.internal.util.SerialIO.readInt;
024 import static io.jenetics.internal.util.SerialIO.writeInt;
025
026 import java.io.DataInput;
027 import java.io.DataOutput;
028 import java.io.IOException;
029 import java.io.InvalidObjectException;
030 import java.io.ObjectInputStream;
031 import java.io.Serializable;
032 import java.util.function.Function;
033 import java.util.stream.DoubleStream;
034 import java.util.stream.IntStream;
035 import java.util.stream.Stream;
036
037 import io.jenetics.util.DoubleRange;
038 import io.jenetics.util.ISeq;
039 import io.jenetics.util.IntRange;
040 import io.jenetics.util.MSeq;
041
042 /**
043 * Numeric chromosome implementation which holds 64 bit floating point numbers.
044 *
045 * @see DoubleGene
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 6.1
053 */
054 public class DoubleChromosome
055 extends AbstractBoundedChromosome<Double, DoubleGene>
056 implements
057 NumericChromosome<Double, DoubleGene>,
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 DoubleChromosome(
077 final ISeq<DoubleGene> genes,
078 final IntRange lengthRange
079 ) {
080 super(genes, lengthRange);
081 }
082
083 @Override
084 public DoubleChromosome newInstance(final ISeq<DoubleGene> genes) {
085 return new DoubleChromosome(genes, lengthRange());
086 }
087
088 @Override
089 public DoubleChromosome newInstance() {
090 return of(_min, _max, lengthRange());
091 }
092
093 /**
094 * Maps the gene alleles of this chromosome, given as {@code double[]} array,
095 * by applying the given mapper function {@code f}. The mapped gene values
096 * are then wrapped into a newly created chromosome.
097 *
098 * <pre>{@code
099 * final DoubleChromosome chromosome = ...;
100 * final DoubleChromosome normalized = chromosome.map(Main::normalize);
101 *
102 * static double[] normalize(final double[] values) {
103 * final double sum = sum(values);
104 * for (int i = 0; i < values.length; ++i) {
105 * values[i] /= sum;
106 * }
107 * return values;
108 * }
109 * }</pre>
110 *
111 * @since 6.1
112 *
113 * @param f the mapper function
114 * @return a newly created chromosome with the mapped gene values
115 * @throws NullPointerException if the mapper function is {@code null}.
116 * @throws IllegalArgumentException if the length of the mapped
117 * {@code double[]} array is empty or doesn't match with the allowed
118 * length range
119 */
120 public DoubleChromosome map(final Function<? super double[], double[]> f) {
121 requireNonNull(f);
122
123 final var range = DoubleRange.of(_min, _max);
124 final var genes = DoubleStream.of(f.apply(toArray()))
125 .mapToObj(v -> DoubleGene.of(v, range))
126 .collect(ISeq.toISeq());
127
128 return newInstance(genes);
129 }
130
131 /**
132 * Returns a sequential stream of the alleles with this chromosome as its
133 * source.
134 *
135 * @since 4.3
136 *
137 * @return a sequential stream of alleles
138 */
139 public DoubleStream doubleStream() {
140 return IntStream.range(0, length()).mapToDouble(this::doubleValue);
141 }
142
143 /**
144 * Returns an double array containing all of the elements in this chromosome
145 * in proper sequence. If the chromosome fits in the specified array, it is
146 * returned therein. Otherwise, a new array is allocated with the length of
147 * this chromosome.
148 *
149 * @since 3.0
150 *
151 * @param array the array into which the elements of this chromosomes are to
152 * be stored, if it is big enough; otherwise, a new array is
153 * allocated for this purpose.
154 * @return an array containing the elements of this chromosome
155 * @throws NullPointerException if the given {@code array} is {@code null}
156 */
157 public double[] toArray(final double[] array) {
158 final double[] a = array.length >= length()
159 ? array
160 : new double[length()];
161
162 for (int i = length(); --i >= 0;) {
163 a[i] = doubleValue(i);
164 }
165
166 return a;
167 }
168
169 /**
170 * Returns an double array containing all of the elements in this chromosome
171 * in proper sequence.
172 *
173 * @since 3.0
174 *
175 * @return an array containing the elements of this chromosome
176 */
177 public double[] toArray() {
178 return toArray(new double[length()]);
179 }
180
181
182 /* *************************************************************************
183 * Static factory methods.
184 * ************************************************************************/
185
186 /**
187 * Create a new {@code DoubleChromosome} with the given genes.
188 *
189 * @param genes the genes of the chromosome.
190 * @return a new chromosome with the given genes.
191 * @throws IllegalArgumentException if the length of the genes array is
192 * empty or the given {@code genes} doesn't have the same range.
193 * @throws NullPointerException if the given {@code genes} array is
194 * {@code null}
195 */
196 public static DoubleChromosome of(final DoubleGene... genes) {
197 checkGeneRange(Stream.of(genes).map(DoubleGene::range));
198 return new DoubleChromosome(ISeq.of(genes), IntRange.of(genes.length));
199 }
200
201 /**
202 * Create a new {@code DoubleChromosome} with the given genes.
203 *
204 * @since 4.3
205 *
206 * @param genes the genes of the chromosome.
207 * @return a new chromosome with the given genes.
208 * @throws NullPointerException if the given {@code genes} are {@code null}
209 * @throws IllegalArgumentException if the of the genes iterable is empty or
210 * the given {@code genes} doesn't have the same range.
211 */
212 public static DoubleChromosome of(final Iterable<DoubleGene> genes) {
213 final ISeq<DoubleGene> values = ISeq.of(genes);
214 checkGeneRange(values.stream().map(DoubleGene::range));
215 return new DoubleChromosome(values, IntRange.of(values.length()));
216 }
217
218 /**
219 * Create a new random chromosome.
220 *
221 * @since 4.0
222 *
223 * @param min the min value of the {@link DoubleGene}s (inclusively).
224 * @param max the max value of the {@link DoubleGene}s (exclusively).
225 * @param lengthRange the allowed length range of the chromosome.
226 * @return a new {@code DoubleChromosome} 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 DoubleChromosome of(
235 final double min,
236 final double max,
237 final IntRange lengthRange
238 ) {
239 final ISeq<DoubleGene> genes = DoubleGene.seq(min, max, lengthRange);
240 return new DoubleChromosome(genes, lengthRange);
241 }
242
243 /**
244 * Create a new random {@code DoubleChromosome}.
245 *
246 * @param min the min value of the {@link DoubleGene}s (inclusively).
247 * @param max the max value of the {@link DoubleGene}s (exclusively).
248 * @param length the length of the chromosome.
249 * @return a new {@code DoubleChromosome} with the given parameter
250 * @throws IllegalArgumentException if the {@code length} is smaller than
251 * one.
252 */
253 public static DoubleChromosome of(
254 final double min,
255 final double max,
256 final int length
257 ) {
258 return of(min, max, IntRange.of(length));
259 }
260
261 /**
262 * Create a new random chromosome.
263 *
264 * @since 4.0
265 *
266 * @param range the integer range of the chromosome.
267 * @param lengthRange the allowed length range of the chromosome.
268 * @return a new {@code DoubleChromosome} with the given parameter
269 * @throws IllegalArgumentException if the length of the gene sequence is
270 * empty, doesn't match with the allowed length range, the minimum
271 * or maximum of the range is smaller or equal zero or the given
272 * range size is zero.
273 * @throws NullPointerException if the given {@code lengthRange} is
274 * {@code null}
275 */
276 public static DoubleChromosome of(
277 final DoubleRange range,
278 final IntRange lengthRange
279 ) {
280 return of(range.min(), range.max(), lengthRange);
281 }
282
283 /**
284 * Create a new random {@code DoubleChromosome}.
285 *
286 * @since 3.2
287 *
288 * @param range the integer range of the chromosome.
289 * @param length the length of the chromosome.
290 * @return a new random {@code DoubleChromosome}
291 * @throws NullPointerException if the given {@code range} is {@code null}
292 * @throws IllegalArgumentException if the {@code length} is smaller than
293 * one.
294 */
295 public static DoubleChromosome of(final DoubleRange range, final int length) {
296 return of(range.min(), range.max(), length);
297 }
298
299 /**
300 * Create a new random {@code DoubleChromosome} of length one.
301 *
302 * @param min the minimal value of this chromosome (inclusively).
303 * @param max the maximal value of this chromosome (exclusively).
304 * @return a new {@code DoubleChromosome} with the given parameter
305 */
306 public static DoubleChromosome of(final double min, final double max) {
307 return of(min, max, 1);
308 }
309
310 /**
311 * Create a new random {@code DoubleChromosome} of length one.
312 *
313 * @since 3.2
314 *
315 * @param range the double range of the chromosome.
316 * @return a new random {@code DoubleChromosome} of length one
317 * @throws NullPointerException if the given {@code range} is {@code null}
318 */
319 public static DoubleChromosome of(final DoubleRange range) {
320 return of(range.min(), range.max());
321 }
322
323
324 /* *************************************************************************
325 * Java object serialization
326 * ************************************************************************/
327
328 private Object writeReplace() {
329 return new Serial(Serial.DOUBLE_CHROMOSOME, this);
330 }
331
332 private void readObject(final ObjectInputStream stream)
333 throws InvalidObjectException
334 {
335 throw new InvalidObjectException("Serialization proxy required.");
336 }
337
338 void write(final DataOutput out) throws IOException {
339 writeInt(length(), out);
340 writeInt(lengthRange().min(), out);
341 writeInt(lengthRange().max(), out);
342 out.writeDouble(_min);
343 out.writeDouble(_max);
344
345 for (int i = 0, n = length(); i < n; ++i) {
346 out.writeDouble(doubleValue(i));
347 }
348 }
349
350 static DoubleChromosome read(final DataInput in) throws IOException {
351 final var length = readInt(in);
352 final var lengthRange = IntRange.of(readInt(in), readInt(in));
353 final var min = in.readDouble();
354 final var max = in.readDouble();
355
356 final MSeq<DoubleGene> values = MSeq.ofLength(length);
357 for (int i = 0; i < length; ++i) {
358 values.set(i, DoubleGene.of(in.readDouble(), min, max));
359 }
360
361 return new DoubleChromosome(values.toISeq(), lengthRange);
362 }
363
364 }
|