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