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