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