LongChromosome.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 integer 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(LongChromosome.Model.Adapter.class)
053 public class LongChromosome
054     extends AbstractNumericChromosome<Long, LongGene>
055     implements
056         NumericChromosome<Long, LongGene>,
057         Serializable
058 {
059     private static final long serialVersionUID = 1L;
060 
061 
062     protected LongChromosome(final ISeq<LongGene> genes) {
063         super(genes);
064     }
065 
066     /**
067      * Create a new random {@code LongChromosome}.
068      *
069      @param min the min value of the {@link LongGene}s (inclusively).
070      @param max the max value of the {@link LongGene}s (inclusively).
071      @param length the length of the chromosome.
072      @throws NullPointerException if one of the arguments is {@code null}.
073      */
074     public LongChromosome(final Long min, final Long max, final int length) {
075         this(LongGene.seq(min, max, length));
076         _valid = true;
077     }
078 
079     /**
080      * Create a new random {@code LongChromosome} of length one.
081      *
082      @param min the minimal value of this chromosome (inclusively).
083      @param max the maximal value of this chromosome (inclusively).
084      @throws NullPointerException if one of the arguments is {@code null}.
085      */
086     public LongChromosome(final Long min, final Long max) {
087         this(min, max, 1);
088     }
089 
090     /**
091      * Create a new {@code LongChromosome} 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 LongChromosome of(final LongGene... genes) {
099         return new LongChromosome(Array.of(genes).toISeq());
100     }
101 
102     /**
103      * Create a new random {@code LongChromosome}.
104      *
105      @param min the min value of the {@link LongGene}s (inclusively).
106      @param max the max value of the {@link LongGene}s (inclusively).
107      @param length the length of the chromosome.
108      */
109     public static LongChromosome of(
110         final long min,
111         final long max,
112         final int length
113     ) {
114         return new LongChromosome(min, max, length);
115     }
116 
117     /**
118      * Create a new random {@code LongChromosome} of length one.
119      *
120      @param min the minimal value of this chromosome (inclusively).
121      @param max the maximal value of this chromosome (inclusively).
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 = "org.jenetics.LongChromosome")
186     @XmlType(name = "org.jenetics.LongChromosome")
187     @XmlAccessorType(XmlAccessType.FIELD)
188     final static class Model {
189 
190         @XmlAttribute
191         public int length;
192 
193         @XmlAttribute
194         public long min;
195 
196         @XmlAttribute
197         public long max;
198 
199         @XmlElement(name = "allele")
200         public List<Long> values;
201 
202         @ValueType(LongChromosome.class)
203         @ModelType(Model.class)
204         public final static class Adapter
205             extends XmlAdapter<Model, LongChromosome>
206         {
207             @Override
208             public Model marshal(final LongChromosome c) {
209                 final Model m = new Model();
210                 m.length = c.length();
211                 m.min = c._min;
212                 m.max = c._max;
213                 m.values = c.toSeq().map(Allele).asList();
214                 return m;
215             }
216 
217             @Override
218             public LongChromosome unmarshal(final Model model) {
219                 final Long min = model.min;
220                 final Long max = model.max;
221                 return new LongChromosome(
222                     Array.of(model.values).map(Gene(min, max)).toISeq()
223                 );
224             }
225         }
226     }
227 
228     private static final Function<LongGene, Long> Allele =
229         new Function<LongGene, Long>() {
230             @Override
231             public Long apply(LongGene value) {
232                 return value.getAllele();
233             }
234         };
235 
236     private static Function<Long, LongGene> Gene(final Long min, final Long max) {
237         return new Function<Long, LongGene>() {
238             @Override
239             public LongGene apply(final Long value) {
240                 return new LongGene(value, min, max);
241             }
242         };
243     }
244 }