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