Float64Chromosome.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.5.0).
003  * Copyright (c) 2007-2013 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 org.jenetics.util.object.hashCodeOf;
023 
024 import java.io.IOException;
025 import java.io.ObjectInputStream;
026 import java.io.ObjectOutputStream;
027 
028 import javolution.xml.XMLFormat;
029 import javolution.xml.XMLSerializable;
030 import javolution.xml.stream.XMLStreamException;
031 
032 import org.jscience.mathematics.number.Float64;
033 
034 import org.jenetics.util.Array;
035 import org.jenetics.util.Factory;
036 import org.jenetics.util.Function;
037 import org.jenetics.util.ISeq;
038 
039 /**
040  * Number chromosome implementation which holds 64 bit floating point numbers.
041  *
042  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
043  @since 1.0
044  @version 1.0 &mdash; <em>$Date: 2013-12-05 $</em>
045  */
046 public class Float64Chromosome
047     extends NumberChromosome<Float64, Float64Gene>
048     implements XMLSerializable
049 {
050     private static final long serialVersionUID = 1L;
051 
052 
053     protected Float64Chromosome(final ISeq<Float64Gene> genes) {
054         super(genes);
055     }
056 
057     /**
058      * Create a new chromosome from the given {@code genes}.
059      *
060      @param genes the genes this chromosome consists.
061      @throws IllegalArgumentException if the number of genes is smaller than
062      *         one.
063      @throws NullPointerException if the {@code genes} are {@code null}.
064      */
065     public Float64Chromosome(final Float64Gene... genes) {
066         this(Array.valueOf(genes).toISeq());
067     }
068 
069     /**
070      * Create a new random DoubleChromosome.
071      *
072      @param min the min value of the {@link Float64Gene}s (inclusively).
073      @param max the max value of the {@link Float64Gene}s (exclusively).
074      @param length the length of the chromosome.
075      */
076     public Float64Chromosome(
077         final Float64 min,
078         final Float64 max,
079         final int length
080     ) {
081         this(
082             new Array<Float64Gene>(length).fill(
083                 Float64Gene.valueOf(min, max)
084             ).toISeq()
085         );
086         _valid = true;
087     }
088 
089     /**
090      * Create a new random chromosome of length one.
091      *
092      @param min the minimal value of this chromosome (inclusively).
093      @param max the maximal value of this chromosome (exclusively).
094      */
095     public Float64Chromosome(final double min, final double max) {
096         this(Float64.valueOf(min), Float64.valueOf(max));
097     }
098 
099     /**
100      * Create a new random chromosome of length one.
101      *
102      @param min the minimal value of this chromosome (inclusively).
103      @param max the maximal value of this chromosome (exclusively).
104      @throws NullPointerException if {@code min} or {@code max} is
105      *         {@code null}.
106      */
107     public Float64Chromosome(final Float64 min, final Float64 max) {
108         this(min, max, 1);
109     }
110 
111     /**
112      * Create a new chromosome
113      *
114      @param min the minimal value of this chromosome.
115      @param max the maximal value of this chromosome.
116      @param length the {@code length} of the new chromosome.
117      @throws IllegalArgumentException if the {@code length} is smaller than
118      *         one.
119      */
120     public Float64Chromosome(final double min, final double max, final int length) {
121         this(Float64.valueOf(min), Float64.valueOf(max), length);
122     }
123 
124     @Override
125     public Float64Chromosome newInstance(final ISeq<Float64Gene> genes) {
126         return new Float64Chromosome(genes);
127     }
128 
129     /**
130      * Return a more specific view of this chromosome factory.
131      *
132      @return a more specific view of this chromosome factory.
133      *
134      @deprecated No longer needed after adding new factory methods to the
135      *             {@link Array} class.
136      */
137     @Deprecated
138     @SuppressWarnings("unchecked")
139     public Factory<Float64Chromosome> asFactory() {
140         return (Factory<Float64Chromosome>)(Object)this;
141     }
142 
143     /**
144      * Create a new, <em>random</em> chromosome.
145      */
146     @Override
147     public Float64Chromosome newInstance() {
148         return new Float64Chromosome(_min, _max, length());
149     }
150 
151     @Override
152     public int hashCode() {
153         return hashCodeOf(getClass()).and(super.hashCode()).value();
154     }
155 
156     @Override
157     public boolean equals(final Object obj) {
158         if (obj == this) {
159             return true;
160         }
161         return obj instanceof Float64Chromosome && super.equals(obj);
162     }
163 
164     /* *************************************************************************
165      *  Property access methods
166      * ************************************************************************/
167 
168     /**
169      * Return a {@link Function} which returns the gene array from this
170      {@link Chromosome}.
171      */
172     public static final Function<AbstractChromosome<Float64Gene>, ISeq<Float64Gene>>
173         Genes = AbstractChromosome.genes();
174 
175     /**
176      * Return a {@link Function} which returns the first {@link Gene} from this
177      {@link Chromosome}.
178      */
179     public static final Function<Chromosome<Float64Gene>, Float64Gene>
180         Gene = AbstractChromosome.gene();
181 
182     /**
183      * Return a {@link Function} which returns the {@link Gene} with the given
184      * {@code index} from this {@link Chromosome}.
185      */
186     public static final Function<Chromosome<Float64Gene>, Float64Gene>
187     Gene(final int index)
188     {
189         return AbstractChromosome.gene(index);
190     }
191 
192     /* *************************************************************************
193      *  XML object serialization
194      * ************************************************************************/
195 
196     static final XMLFormat<Float64Chromosome>
197     XML = new XMLFormat<Float64Chromosome>(Float64Chromosome.class)
198     {
199         private static final String LENGTH = "length";
200         private static final String MIN = "min";
201         private static final String MAX = "max";
202 
203         @Override
204         public Float64Chromosome newInstance(
205             final Class<Float64Chromosome> cls, final InputElement xml
206         throws XMLStreamException {
207             final int length = xml.getAttribute(LENGTH, 0);
208             final double min = xml.getAttribute(MIN, 0.0);
209             final double max = xml.getAttribute(MAX, 1.0);
210 
211             final Array<Float64Gene> genes = new Array<>(length);
212             for (int i = 0; i < length; ++i) {
213                 final Float64 value = xml.getNext();
214                 genes.set(i, Float64Gene.valueOf(value.doubleValue(), min, max));
215             }
216 
217             final Float64Chromosome chromosome = new Float64Chromosome(genes.toISeq());
218             chromosome._min = Float64.valueOf(min);
219             chromosome._max = Float64.valueOf(max);
220 
221             return chromosome;
222         }
223         @Override
224         public void write(final Float64Chromosome chromosome, final OutputElement xml)
225             throws XMLStreamException
226         {
227             xml.setAttribute(LENGTH, chromosome.length());
228             xml.setAttribute(MIN, chromosome._min.doubleValue());
229             xml.setAttribute(MAX, chromosome._max.doubleValue());
230             for (Float64Gene gene : chromosome) {
231                 xml.add(gene.getAllele());
232             }
233         }
234         @Override
235         public void read(final InputElement e, final Float64Chromosome c) {
236         }
237     };
238 
239     /* *************************************************************************
240      *  Java object serialization
241      * ************************************************************************/
242 
243     private void writeObject(final ObjectOutputStream out)
244         throws IOException
245     {
246         out.defaultWriteObject();
247 
248         out.writeInt(length());
249         out.writeDouble(_min.doubleValue());
250         out.writeDouble(_max.doubleValue());
251 
252         for (Float64Gene gene : _genes) {
253             out.writeDouble(gene.doubleValue());
254         }
255     }
256 
257     private void readObject(final ObjectInputStream in)
258         throws IOException, ClassNotFoundException
259     {
260         in.defaultReadObject();
261 
262         final int length = in.readInt();
263         final Float64 min = Float64.valueOf(in.readDouble());
264         final Float64 max = Float64.valueOf(in.readDouble());
265 
266         _min = min;
267         _max = max;
268         final Array<Float64Gene> genes = new Array<>(length);
269         for (int i = 0; i < length; ++i) {
270             final Float64 value = Float64.valueOf(in.readDouble());
271             genes.set(i, Float64Gene.valueOf(value, min, max));
272         }
273 
274         _genes = genes.toISeq();
275     }
276 
277 }
278 
279 
280 
281