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