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