001 /*
002 * Java Genetic Algorithm Library (jenetics-1.6.0).
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 static java.util.Objects.requireNonNull;
023 import static org.jenetics.Float64Gene.Value;
024 import static org.jenetics.internal.util.model.Float64Model.Marshaller;
025 import static org.jenetics.internal.util.model.Float64Model.Unmarshaller;
026 import static org.jenetics.util.functions.compose;
027
028 import java.io.IOException;
029 import java.io.ObjectInputStream;
030 import java.io.ObjectOutputStream;
031 import java.util.List;
032
033 import javax.xml.bind.annotation.XmlAccessType;
034 import javax.xml.bind.annotation.XmlAccessorType;
035 import javax.xml.bind.annotation.XmlAttribute;
036 import javax.xml.bind.annotation.XmlElement;
037 import javax.xml.bind.annotation.XmlRootElement;
038 import javax.xml.bind.annotation.XmlType;
039 import javax.xml.bind.annotation.adapters.XmlAdapter;
040 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
041
042 import javolution.xml.XMLFormat;
043 import javolution.xml.XMLSerializable;
044 import javolution.xml.stream.XMLStreamException;
045
046 import org.jscience.mathematics.number.Float64;
047
048 import org.jenetics.internal.util.HashBuilder;
049 import org.jenetics.internal.util.model.Float64Model;
050 import org.jenetics.internal.util.model.ModelType;
051 import org.jenetics.internal.util.model.ValueType;
052
053 import org.jenetics.util.Array;
054 import org.jenetics.util.Factory;
055 import org.jenetics.util.Function;
056 import org.jenetics.util.ISeq;
057
058 /**
059 * Number chromosome implementation which holds 64 bit floating point numbers.
060 *
061 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
062 * @since 1.0
063 * @version 1.6 — <em>$Date: 2014-02-27 $</em>
064 *
065 * @deprecated Use {@link org.jenetics.DoubleChromosome} instead. This classes
066 * uses the <i>JScience</i> library, which will be removed in the
067 * next major version.
068 */
069 @Deprecated
070 @XmlJavaTypeAdapter(Float64Chromosome.Model.Adapter.class)
071 public class Float64Chromosome
072 extends NumberChromosome<Float64, Float64Gene>
073 implements XMLSerializable
074 {
075 private static final long serialVersionUID = 1L;
076
077
078 protected Float64Chromosome(final ISeq<Float64Gene> genes) {
079 super(genes);
080 }
081
082 private Float64Chromosome(
083 final ISeq<Float64Gene> genes,
084 final Float64 min,
085 final Float64 max
086 ) {
087 this(genes);
088 _min = requireNonNull(min);
089 _max = requireNonNull(max);
090 }
091
092 /**
093 * Create a new chromosome from the given {@code genes}.
094 *
095 * @param genes the genes this chromosome consists.
096 * @throws IllegalArgumentException if the number of genes is smaller than
097 * one.
098 * @throws NullPointerException if the {@code genes} are {@code null}.
099 */
100 public Float64Chromosome(final Float64Gene... genes) {
101 this(Array.of(genes).toISeq());
102 }
103
104 /**
105 * Create a new random DoubleChromosome.
106 *
107 * @param min the min value of the {@link Float64Gene}s (inclusively).
108 * @param max the max value of the {@link Float64Gene}s (exclusively).
109 * @param length the length of the chromosome.
110 */
111 public Float64Chromosome(
112 final Float64 min,
113 final Float64 max,
114 final int length
115 ) {
116 this(
117 new Array<Float64Gene>(length).fill(
118 Float64Gene.valueOf(min, max)
119 ).toISeq()
120 );
121 _valid = true;
122 }
123
124 /**
125 * Create a new random chromosome of length one.
126 *
127 * @param min the minimal value of this chromosome (inclusively).
128 * @param max the maximal value of this chromosome (exclusively).
129 */
130 public Float64Chromosome(final double min, final double max) {
131 this(Float64.valueOf(min), Float64.valueOf(max));
132 }
133
134 /**
135 * Create a new random chromosome of length one.
136 *
137 * @param min the minimal value of this chromosome (inclusively).
138 * @param max the maximal value of this chromosome (exclusively).
139 * @throws NullPointerException if {@code min} or {@code max} is
140 * {@code null}.
141 */
142 public Float64Chromosome(final Float64 min, final Float64 max) {
143 this(min, max, 1);
144 }
145
146 /**
147 * Create a new chromosome
148 *
149 * @param min the minimal value of this chromosome.
150 * @param max the maximal value of this chromosome.
151 * @param length the {@code length} of the new chromosome.
152 * @throws IllegalArgumentException if the {@code length} is smaller than
153 * one.
154 */
155 public Float64Chromosome(final double min, final double max, final int length) {
156 this(Float64.valueOf(min), Float64.valueOf(max), length);
157 }
158
159 @Override
160 public Float64Chromosome newInstance(final ISeq<Float64Gene> genes) {
161 return new Float64Chromosome(genes);
162 }
163
164 /**
165 * Return a more specific view of this chromosome factory.
166 *
167 * @return a more specific view of this chromosome factory.
168 *
169 * @deprecated No longer needed after adding new factory methods to the
170 * {@link Array} class.
171 */
172 @Deprecated
173 @SuppressWarnings("unchecked")
174 public Factory<Float64Chromosome> asFactory() {
175 return (Factory<Float64Chromosome>)(Object)this;
176 }
177
178 /**
179 * Create a new, <em>random</em> chromosome.
180 */
181 @Override
182 public Float64Chromosome newInstance() {
183 return new Float64Chromosome(_min, _max, length());
184 }
185
186 @Override
187 public int hashCode() {
188 return HashBuilder.of(getClass()).and(super.hashCode()).value();
189 }
190
191 @Override
192 public boolean equals(final Object obj) {
193 if (obj == this) {
194 return true;
195 }
196 return obj instanceof Float64Chromosome && super.equals(obj);
197 }
198
199 /* *************************************************************************
200 * Property access methods
201 * ************************************************************************/
202
203 /**
204 * Return a {@link Function} which returns the gene array from this
205 * {@link Chromosome}.
206 */
207 public static final Function<AbstractChromosome<Float64Gene>, ISeq<Float64Gene>>
208 Genes = AbstractChromosome.genes();
209
210 /**
211 * Return a {@link Function} which returns the first {@link Gene} from this
212 * {@link Chromosome}.
213 */
214 public static final Function<Chromosome<Float64Gene>, Float64Gene>
215 Gene = AbstractChromosome.gene();
216
217 /**
218 * Return a {@link Function} which returns the {@link Gene} with the given
219 * {@code index} from this {@link Chromosome}.
220 */
221 public static Function<Chromosome<Float64Gene>, Float64Gene>
222 Gene(final int index)
223 {
224 return AbstractChromosome.gene(index);
225 }
226
227 /* *************************************************************************
228 * Java object serialization
229 * ************************************************************************/
230
231 private void writeObject(final ObjectOutputStream out)
232 throws IOException
233 {
234 out.defaultWriteObject();
235
236 out.writeInt(length());
237 out.writeDouble(_min.doubleValue());
238 out.writeDouble(_max.doubleValue());
239
240 for (Float64Gene gene : _genes) {
241 out.writeDouble(gene.doubleValue());
242 }
243 }
244
245 private void readObject(final ObjectInputStream in)
246 throws IOException, ClassNotFoundException
247 {
248 in.defaultReadObject();
249
250 final int length = in.readInt();
251 final Float64 min = Float64.valueOf(in.readDouble());
252 final Float64 max = Float64.valueOf(in.readDouble());
253
254 _min = min;
255 _max = max;
256 final Array<Float64Gene> genes = new Array<>(length);
257 for (int i = 0; i < length; ++i) {
258 final Float64 value = Float64.valueOf(in.readDouble());
259 genes.set(i, Float64Gene.valueOf(value, min, max));
260 }
261
262 _genes = genes.toISeq();
263 }
264
265 /* *************************************************************************
266 * XML object serialization
267 * ************************************************************************/
268
269 static final XMLFormat<Float64Chromosome>
270 XML = new XMLFormat<Float64Chromosome>(Float64Chromosome.class)
271 {
272 private static final String LENGTH = "length";
273 private static final String MIN = "min";
274 private static final String MAX = "max";
275
276 @Override
277 public Float64Chromosome newInstance(
278 final Class<Float64Chromosome> cls, final InputElement xml
279 ) throws XMLStreamException {
280 final int length = xml.getAttribute(LENGTH, 0);
281 final double min = xml.getAttribute(MIN, 0.0);
282 final double max = xml.getAttribute(MAX, 1.0);
283
284 final Array<Float64Gene> genes = new Array<>(length);
285 for (int i = 0; i < length; ++i) {
286 final Float64 value = xml.getNext();
287 genes.set(i, Float64Gene.valueOf(value.doubleValue(), min, max));
288 }
289
290 final Float64Chromosome chromosome = new Float64Chromosome(genes.toISeq());
291 chromosome._min = Float64.valueOf(min);
292 chromosome._max = Float64.valueOf(max);
293
294 return chromosome;
295 }
296 @Override
297 public void write(final Float64Chromosome chromosome, final OutputElement xml)
298 throws XMLStreamException
299 {
300 xml.setAttribute(LENGTH, chromosome.length());
301 xml.setAttribute(MIN, chromosome._min.doubleValue());
302 xml.setAttribute(MAX, chromosome._max.doubleValue());
303 for (Float64Gene gene : chromosome) {
304 xml.add(gene.getAllele());
305 }
306 }
307 @Override
308 public void read(final InputElement e, final Float64Chromosome c) {
309 }
310 };
311
312 /* *************************************************************************
313 * JAXB object serialization
314 * ************************************************************************/
315
316 @XmlRootElement(name = "org.jenetics.Float64Chromosome")
317 @XmlType(name = "org.jenetics.Float64Chromosome")
318 @XmlAccessorType(XmlAccessType.FIELD)
319 final static class Model {
320
321 @XmlAttribute
322 public int length;
323
324 @XmlAttribute
325 public double min;
326
327 @XmlAttribute
328 public double max;
329
330 @XmlElement(name = "org.jscience.mathematics.number.Float64")
331 public List<Float64Model> values;
332
333 @ValueType(Float64Chromosome.class)
334 @ModelType(Model.class)
335 public final static class Adapter
336 extends XmlAdapter<Model, Float64Chromosome>
337 {
338 @Override
339 public Model marshal(final Float64Chromosome c) {
340 final Model m = new Model();
341 m.length = c.length();
342 m.min = c._min.doubleValue();
343 m.max = c._max.doubleValue();
344 m.values = c.toSeq().map(compose(Value, Marshaller)).asList();
345 return m;
346 }
347
348 @Override
349 public Float64Chromosome unmarshal(final Model model) {
350 final Float64 min = Float64.valueOf(model.min);
351 final Float64 max = Float64.valueOf(model.max);
352 final ISeq<Float64Gene> genes = Array.of(model.values)
353 .map(compose(Unmarshaller, Float64Gene.Gene(min, max)))
354 .toISeq();
355
356 return new Float64Chromosome(genes, min, max);
357 }
358 }
359 }
360
361 }
|