DoubleChromosome.java
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 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 import org.jenetics.internal.util.model.ModelType;
039 import org.jenetics.internal.util.model.ValueType;
040 
041 import org.jenetics.util.Array;
042 import org.jenetics.util.Function;
043 import org.jenetics.util.ISeq;
044 
045 /**
046  * Numeric chromosome implementation which holds 64 bit floating point numbers.
047  *
048  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
049  @version 1.6 &mdash; <em>$Date: 2014-03-05 $</em>
050  @since 1.6
051  */
052 @XmlJavaTypeAdapter(DoubleChromosome.Model.Adapter.class)
053 public class DoubleChromosome
054     extends AbstractNumericChromosome<Double, DoubleGene>
055     implements
056         NumericChromosome<Double, DoubleGene>,
057         Serializable
058 {
059     private static final long serialVersionUID = 1L;
060 
061 
062     protected DoubleChromosome(final ISeq<DoubleGene> genes) {
063         super(genes);
064     }
065 
066     /**
067      * Create a new random {@code DoubleChromosome}.
068      *
069      @param min the min value of the {@link DoubleGene}s (inclusively).
070      @param max the max value of the {@link DoubleGene}s (exclusively).
071      @param length the length of the chromosome.
072      @throws NullPointerException if one of the arguments is {@code null}.
073      */
074     public DoubleChromosome(final Double min,final Double max,final int length) {
075         this(DoubleGene.seq(min, max, length));
076         _valid = true;
077     }
078 
079     /**
080      * Create a new random {@code DoubleChromosome} of length one.
081      *
082      @param min the minimal value of this chromosome (inclusively).
083      @param max the maximal value of this chromosome (exclusively).
084      @throws NullPointerException if one of the arguments is {@code null}.
085      */
086     public DoubleChromosome(final Double min, final Double max) {
087         this(min, max, 1);
088     }
089 
090     /**
091      * Create a new {@code DoubleChromosome} with the given genes.
092      *
093      @param genes the genes of the chromosome.
094      @return a new chromosome with the given genes.
095      @throws IllegalArgumentException if the length of the genes array is
096      *         empty.
097      */
098     public static DoubleChromosome of(final DoubleGene... genes) {
099         return new DoubleChromosome(Array.of(genes).toISeq());
100     }
101 
102     /**
103      * Create a new random {@code DoubleChromosome}.
104      *
105      @param min the min value of the {@link DoubleGene}s (inclusively).
106      @param max the max value of the {@link DoubleGene}s (exclusively).
107      @param length the length of the chromosome.
108      */
109     public static DoubleChromosome of(final double min, double max, final int length) {
110         return new DoubleChromosome(min, max, length);
111     }
112 
113     /**
114      * Create a new random {@code DoubleChromosome} of length one.
115      *
116      @param min the minimal value of this chromosome (inclusively).
117      @param max the maximal value of this chromosome (exclusively).
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 = "org.jenetics.DoubleChromosome")
183     @XmlType(name = "org.jenetics.DoubleChromosome")
184     @XmlAccessorType(XmlAccessType.FIELD)
185     final static class Model {
186 
187         @XmlAttribute
188         public int length;
189 
190         @XmlAttribute
191         public double min;
192 
193         @XmlAttribute
194         public double max;
195 
196         @XmlElement(name = "allele")
197         public List<Double> values;
198 
199         @ValueType(DoubleChromosome.class)
200         @ModelType(Model.class)
201         public final static class Adapter
202             extends XmlAdapter<Model, DoubleChromosome>
203         {
204             @Override
205             public Model marshal(final DoubleChromosome c) {
206                 final Model m = new Model();
207                 m.length = c.length();
208                 m.min = c._min;
209                 m.max = c._max;
210                 m.values = c.toSeq().map(Allele).asList();
211                 return m;
212             }
213 
214             @Override
215             public DoubleChromosome unmarshal(final Model model) {
216                 final Double min = model.min;
217                 final Double max = model.max;
218                 return new DoubleChromosome(
219                     Array.of(model.values).map(Gene(min, max)).toISeq()
220                 );
221             }
222         }
223     }
224 
225     private static final Function<DoubleGene, Double> Allele =
226         new Function<DoubleGene, Double>() {
227             @Override
228             public Double apply(final DoubleGene value) {
229                 return value.getAllele();
230             }
231         };
232 
233     private static Function<Double, DoubleGene> Gene(final Double min, final Double max) {
234         return new Function<Double, DoubleGene>() {
235             @Override
236             public DoubleGene apply(final Double value) {
237                 return new DoubleGene(value, min, max);
238             }
239         };
240     }
241 }