001 /*
002 * Java Genetic Algorithm Library (jenetics-2.0.2).
003 * Copyright (c) 2007-2014 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@gmx.at)
019 */
020 package org.jenetics;
021
022 import java.io.IOException;
023 import java.io.ObjectInputStream;
024 import java.io.ObjectOutputStream;
025 import java.io.Serializable;
026 import java.util.List;
027
028 import javax.xml.bind.annotation.XmlAccessType;
029 import javax.xml.bind.annotation.XmlAccessorType;
030 import javax.xml.bind.annotation.XmlAttribute;
031 import javax.xml.bind.annotation.XmlElement;
032 import javax.xml.bind.annotation.XmlRootElement;
033 import javax.xml.bind.annotation.XmlType;
034 import javax.xml.bind.annotation.adapters.XmlAdapter;
035 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
036
037 import org.jenetics.internal.util.HashBuilder;
038
039 import org.jenetics.util.Array;
040 import org.jenetics.util.Function;
041 import org.jenetics.util.ISeq;
042
043 /**
044 * Numeric chromosome implementation which holds 32 bit integer numbers.
045 *
046 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
047 * @version 2.0 — <em>$Date: 2014-04-10 $</em>
048 * @since 2.0
049 */
050 @XmlJavaTypeAdapter(IntegerChromosome.Model.Adapter.class)
051 public class IntegerChromosome
052 extends AbstractNumericChromosome<Integer, IntegerGene>
053 implements
054 NumericChromosome<Integer, IntegerGene>,
055 Serializable
056 {
057 private static final long serialVersionUID = 1L;
058
059
060 protected IntegerChromosome(final ISeq<IntegerGene> genes) {
061 super(genes);
062 }
063
064 /**
065 * Create a new random {@code IntegerChromosome}.
066 *
067 * @param min the min value of the {@link IntegerGene}s (inclusively).
068 * @param max the max value of the {@link IntegerGene}s (inclusively).
069 * @param length the length of the chromosome.
070 * @throws NullPointerException if one of the arguments is {@code null}.
071 */
072 public IntegerChromosome(final Integer min, final Integer max, final int length) {
073 this(IntegerGene.seq(min, max, length));
074 _valid = true;
075 }
076
077 /**
078 * Create a new random {@code IntegerChromosome} of length one.
079 *
080 * @param min the minimal value of this chromosome (inclusively).
081 * @param max the maximal value of this chromosome (inclusively).
082 * @throws NullPointerException if one of the arguments is {@code null}.
083 */
084 public IntegerChromosome(final Integer min, final Integer max) {
085 this(min, max, 1);
086 }
087
088 /**
089 * Create a new {@code IntegerChromosome} with the given genes.
090 *
091 * @param genes the genes of the chromosome.
092 * @return a new chromosome with the given genes.
093 * @throws IllegalArgumentException if the length of the genes array is
094 * empty.
095 */
096 public static IntegerChromosome of(final IntegerGene... genes) {
097 return new IntegerChromosome(Array.of(genes).toISeq());
098 }
099
100 /**
101 * Create a new random {@code IntegerChromosome}.
102 *
103 * @param min the min value of the {@link IntegerGene}s (inclusively).
104 * @param max the max value of the {@link IntegerGene}s (inclusively).
105 * @param length the length of the chromosome.
106 */
107 public static IntegerChromosome of(
108 final int min,
109 final int max,
110 final int length
111 ) {
112 return new IntegerChromosome(min, max, length);
113 }
114
115 /**
116 * Create a new random {@code IntegerChromosome} of length one.
117 *
118 * @param min the minimal value of this chromosome (inclusively).
119 * @param max the maximal value of this chromosome (inclusively).
120 */
121 public static IntegerChromosome of(final int min, final int max) {
122 return new IntegerChromosome(min, max);
123 }
124
125 @Override
126 public IntegerChromosome newInstance(final ISeq<IntegerGene> genes) {
127 return new IntegerChromosome(genes);
128 }
129
130 @Override
131 public IntegerChromosome newInstance() {
132 return new IntegerChromosome(_min, _max, length());
133 }
134
135 @Override
136 public int hashCode() {
137 return HashBuilder.of(getClass()).and(super.hashCode()).value();
138 }
139
140 @Override
141 public boolean equals(final Object o) {
142 return o == this || o instanceof IntegerChromosome && super.equals(o);
143 }
144
145 /* *************************************************************************
146 * Java object serialization
147 * ************************************************************************/
148
149 private void writeObject(final ObjectOutputStream out)
150 throws IOException
151 {
152 out.defaultWriteObject();
153
154 out.writeInt(length());
155 out.writeInt(_min.intValue());
156 out.writeInt(_max.intValue());
157
158 for (IntegerGene gene : _genes) {
159 out.writeInt(gene.getAllele().intValue());
160 }
161 }
162
163 private void readObject(final ObjectInputStream in)
164 throws IOException, ClassNotFoundException
165 {
166 in.defaultReadObject();
167
168 final Array<IntegerGene> genes = new Array<>(in.readInt());
169 _min = in.readInt();
170 _max = in.readInt();
171
172 for (int i = 0; i < genes.length(); ++i) {
173 genes.set(i, new IntegerGene(in.readInt(), _min, _max));
174 }
175
176 _genes = genes.toISeq();
177 }
178
179 /* *************************************************************************
180 * JAXB object serialization
181 * ************************************************************************/
182
183 @XmlRootElement(name = "int-chromosome")
184 @XmlType(name = "org.jenetics.IntegerChromosome")
185 @XmlAccessorType(XmlAccessType.FIELD)
186 final static class Model {
187
188 @XmlAttribute(name = "length", required = true)
189 public int length;
190
191 @XmlAttribute(name = "min", required = true)
192 public int min;
193
194 @XmlAttribute(name = "max", required = true)
195 public int max;
196
197 @XmlElement(name = "allele", required = true, nillable = false)
198 public List<Integer> values;
199
200 public final static class Adapter
201 extends XmlAdapter<Model, IntegerChromosome>
202 {
203 @Override
204 public Model marshal(final IntegerChromosome c) {
205 final Model m = new Model();
206 m.length = c.length();
207 m.min = c._min;
208 m.max = c._max;
209 m.values = c.toSeq().map(Allele).asList();
210 return m;
211 }
212
213 @Override
214 public IntegerChromosome unmarshal(final Model model) {
215 final Integer min = model.min;
216 final Integer max = model.max;
217 return new IntegerChromosome(
218 Array.of(model.values).map(Gene(min, max)).toISeq()
219 );
220 }
221 }
222
223 private static final Function<IntegerGene, Integer> Allele =
224 new Function<IntegerGene, Integer>() {
225 @Override
226 public Integer apply(IntegerGene value) {
227 return value.getAllele();
228 }
229 };
230
231 private static Function<Integer, IntegerGene>
232 Gene(final Integer min, final Integer max) {
233 return new Function<Integer, IntegerGene>() {
234 @Override
235 public IntegerGene apply(final Integer value) {
236 return new IntegerGene(value, min, max);
237 }
238 };
239 }
240
241 }
242 }
|