001 /*
002 * Java Genetic Algorithm Library (jenetics-4.4.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.CharacterGene.DEFAULT_CHARACTERS;
023 import static io.jenetics.internal.util.Hashes.hash;
024
025 import java.io.IOException;
026 import java.io.ObjectInputStream;
027 import java.io.ObjectOutputStream;
028 import java.io.Serializable;
029 import java.util.Objects;
030 import java.util.function.Supplier;
031 import java.util.stream.Collectors;
032
033 import io.jenetics.internal.util.IntRef;
034 import io.jenetics.internal.util.reflect;
035 import io.jenetics.util.CharSeq;
036 import io.jenetics.util.ISeq;
037 import io.jenetics.util.IntRange;
038 import io.jenetics.util.MSeq;
039
040 /**
041 * CharacterChromosome which represents character sequences.
042 *
043 * @see CharacterGene
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.0
050 * @version 4.3
051 */
052 public class CharacterChromosome
053 extends
054 VariableChromosome<CharacterGene>
055 implements
056 CharSequence,
057 Serializable
058 {
059 private static final long serialVersionUID = 3L;
060
061 private transient CharSeq _validCharacters;
062
063 /**
064 * Create a new chromosome from the given {@code genes} array. The genes
065 * array is copied, so changes to the given genes array doesn't effect the
066 * genes of this chromosome.
067 *
068 * @since 4.0
069 *
070 * @param genes the genes that form the chromosome.
071 * @param lengthRange the allowed length range of the chromosome.
072 * @throws NullPointerException if the given gene array is {@code null}.
073 * @throws IllegalArgumentException if the length of the gene array is
074 * smaller than one.
075 */
076 protected CharacterChromosome(
077 final ISeq<CharacterGene> genes,
078 final IntRange lengthRange
079 ) {
080 super(genes, lengthRange);
081 _validCharacters = genes.get(0).getValidCharacters();
082 }
083
084 /**
085 * Create a new chromosome with the {@code validCharacters} char set as
086 * valid characters.
087 *
088 * @since 4.0
089 *
090 * @param validCharacters the valid characters for this chromosome.
091 * @param lengthRange the allowed length range of the chromosome.
092 * @throws NullPointerException if the {@code validCharacters} is
093 * {@code null}.
094 * @throws IllegalArgumentException if the length of the gene sequence is
095 * empty, doesn't match with the allowed length range, the minimum
096 * or maximum of the range is smaller or equal zero or the given
097 * range size is zero.
098 *
099 * @deprecated Use {@link #of(CharSeq, IntRange)} instead.
100 */
101 @Deprecated
102 public CharacterChromosome(
103 final CharSeq validCharacters,
104 final IntRange lengthRange
105 ) {
106 this(CharacterGene.seq(validCharacters, lengthRange), lengthRange);
107 _valid = true;
108 }
109
110 /**
111 * Create a new chromosome with the {@code validCharacters} char set as
112 * valid characters.
113 *
114 * @param validCharacters the valid characters for this chromosome.
115 * @param length the length of the chromosome.
116 * @throws NullPointerException if the {@code validCharacters} is
117 * {@code null}.
118 * @throws IllegalArgumentException if the length of the gene sequence is
119 * empty, doesn't match with the allowed length range, the minimum
120 * or maximum of the range is smaller or equal zero or the given
121 * range size is zero.
122 *
123 * @deprecated Use {@link #of(CharSeq, int)} instead.
124 */
125 @Deprecated
126 public CharacterChromosome(
127 final CharSeq validCharacters,
128 final int length
129 ) {
130 this(validCharacters, IntRange.of(length));
131 }
132
133 @Override
134 public char charAt(final int index) {
135 return getGene(index).charValue();
136 }
137
138 @Override
139 public CharacterChromosome subSequence(final int start, final int end) {
140 return new CharacterChromosome(_genes.subSeq(start, end), lengthRange());
141 }
142
143 /**
144 * @throws NullPointerException if the given gene array is {@code null}.
145 */
146 @Override
147 public CharacterChromosome newInstance(final ISeq<CharacterGene> genes) {
148 return new CharacterChromosome(genes, lengthRange());
149 }
150
151 /**
152 * Create a new, <em>random</em> chromosome.
153 */
154 @Override
155 public CharacterChromosome newInstance() {
156 return of(_validCharacters, lengthRange());
157 }
158
159 @Override
160 public int hashCode() {
161 return hash(super.hashCode(), hash(_validCharacters));
162 }
163
164 @Override
165 public boolean equals(final Object obj) {
166 return obj == this ||
167 obj != null &&
168 getClass() == obj.getClass() &&
169 Objects.equals(_validCharacters, ((CharacterChromosome)obj)._validCharacters) &&
170 super.equals(obj);
171 }
172
173 @Override
174 public String toString() {
175 return new String(toArray());
176 }
177
178 /**
179 * Returns an char array containing all of the elements in this chromosome
180 * in proper sequence. If the chromosome fits in the specified array, it is
181 * returned therein. Otherwise, a new array is allocated with the length of
182 * this chromosome.
183 *
184 * @since 3.0
185 *
186 * @param array the array into which the elements of this chromosomes are to
187 * be stored, if it is big enough; otherwise, a new array is
188 * allocated for this purpose.
189 * @return an array containing the elements of this chromosome
190 * @throws NullPointerException if the given {@code array} is {@code null}
191 */
192 public char[] toArray(final char[] array) {
193 final char[] a = array.length >= length() ?
194 array : new char[length()];
195
196 for (int i = length(); --i >= 0;) {
197 a[i] = charAt(i);
198 }
199
200 return a;
201 }
202
203 /**
204 * Returns an char array containing all of the elements in this chromosome
205 * in proper sequence.
206 *
207 * @since 3.0
208 *
209 * @return an array containing the elements of this chromosome
210 */
211 public char[] toArray() {
212 return toArray(new char[length()]);
213 }
214
215
216 /* *************************************************************************
217 * Static factory methods.
218 * ************************************************************************/
219
220 /**
221 * Create a new chromosome with the {@code validCharacters} char set as
222 * valid characters.
223 *
224 * @since 4.3
225 *
226 * @param validCharacters the valid characters for this chromosome.
227 * @param lengthRange the allowed length range of the chromosome.
228 * @return a new {@code CharacterChromosome} with the given parameter
229 * @throws NullPointerException if the {@code validCharacters} is
230 * {@code null}.
231 * @throws IllegalArgumentException if the length of the gene sequence is
232 * empty, doesn't match with the allowed length range, the minimum
233 * or maximum of the range is smaller or equal zero or the given
234 * range size is zero.
235 */
236 public static CharacterChromosome of(
237 final CharSeq validCharacters,
238 final IntRange lengthRange
239 ) {
240 return new CharacterChromosome(
241 CharacterGene.seq(validCharacters, lengthRange),
242 lengthRange
243 );
244 }
245
246 /**
247 * Create a new chromosome with the {@link CharacterGene#DEFAULT_CHARACTERS}
248 * char set as valid characters.
249 *
250 * @param lengthRange the allowed length range of the chromosome.
251 * @return a new {@code CharacterChromosome} with the given parameter
252 * @throws IllegalArgumentException if the {@code length} is smaller than
253 * one.
254 */
255 public static CharacterChromosome of(final IntRange lengthRange) {
256 return of(DEFAULT_CHARACTERS, lengthRange);
257 }
258
259 /**
260 * Create a new chromosome with the {@code validCharacters} char set as
261 * valid characters.
262 *
263 * @since 4.3
264 *
265 * @param validCharacters the valid characters for this chromosome.
266 * @param length the {@code length} of the new chromosome.
267 * @return a new {@code CharacterChromosome} with the given parameter
268 * @throws NullPointerException if the {@code validCharacters} is
269 * {@code null}.
270 * @throws IllegalArgumentException if the length of the gene sequence is
271 * empty, doesn't match with the allowed length range, the minimum
272 * or maximum of the range is smaller or equal zero or the given
273 * range size is zero.
274 */
275 public static CharacterChromosome of(
276 final CharSeq validCharacters,
277 final int length
278 ) {
279 return of(validCharacters, IntRange.of(length));
280 }
281
282 /**
283 * Create a new chromosome with the {@link CharacterGene#DEFAULT_CHARACTERS}
284 * char set as valid characters.
285 *
286 * @param length the {@code length} of the new chromosome.
287 * @return a new {@code CharacterChromosome} with the given parameter
288 * @throws IllegalArgumentException if the {@code length} is smaller than
289 * one.
290 */
291 public static CharacterChromosome of(final int length) {
292 return of(DEFAULT_CHARACTERS, length);
293 }
294
295 /**
296 * Create a new chromosome from the given genes (given as string).
297 *
298 * @param alleles the character genes.
299 * @param validChars the valid characters.
300 * @return a new {@code CharacterChromosome} with the given parameter
301 * @throws IllegalArgumentException if the genes string is empty.
302 */
303 public static CharacterChromosome of(
304 final String alleles,
305 final CharSeq validChars
306 ) {
307 final IntRef index = new IntRef();
308 final Supplier<CharacterGene> geneFactory = () -> CharacterGene.of(
309 alleles.charAt(index.value++), validChars
310 );
311
312 final ISeq<CharacterGene> genes =
313 MSeq.<CharacterGene>ofLength(alleles.length())
314 .fill(geneFactory)
315 .toISeq();
316
317 return new CharacterChromosome(genes, IntRange.of(alleles.length()));
318 }
319
320 /**
321 * Create a new chromosome from the given genes (given as string).
322 *
323 * @param alleles the character genes.
324 * @return a new {@code CharacterChromosome} with the given parameter
325 * @throws IllegalArgumentException if the genes string is empty.
326 */
327 public static CharacterChromosome of(final String alleles) {
328 return of(alleles, DEFAULT_CHARACTERS);
329 }
330
331
332 /* *************************************************************************
333 * Java object serialization
334 * ************************************************************************/
335
336 private void writeObject(final ObjectOutputStream out)
337 throws IOException
338 {
339 out.defaultWriteObject();
340
341 out.writeInt(length());
342 out.writeObject(_validCharacters);
343
344 for (CharacterGene gene : _genes) {
345 out.writeChar(gene.getAllele());
346 }
347 }
348
349 private void readObject(final ObjectInputStream in)
350 throws IOException, ClassNotFoundException
351 {
352 in.defaultReadObject();
353
354 final int length = in.readInt();
355 _validCharacters = (CharSeq)in.readObject();
356
357 final MSeq<CharacterGene> genes = MSeq.ofLength(length);
358 for (int i = 0; i < length; ++i) {
359 final CharacterGene gene = CharacterGene.of(
360 in.readChar(),
361 _validCharacters
362 );
363 genes.set(i, gene);
364 }
365 reflect.setField(this, "_genes", genes.toISeq());
366 }
367
368 }
|