| 
001 /*002  * Java Genetic Algorithm Library (jenetics-3.0.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.Equality;
 038 import org.jenetics.internal.util.Hash;
 039
 040 import org.jenetics.util.ISeq;
 041 import org.jenetics.util.MSeq;
 042
 043 /**
 044  * Numeric chromosome implementation which holds 32 bit integer numbers.
 045  *
 046  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz  Wilhelmstötter</a>
 047  * @since 2.0
 048  * @version 3.0 — <em>$Date: 2014-12-28 $</em>
 049  */
 050 @XmlJavaTypeAdapter(IntegerChromosome.Model.Adapter.class)
 051 public class IntegerChromosome
 052     extends AbstractBoundedChromosome<Integer, IntegerGene>
 053     implements
 054             NumericChromosome<Integer, IntegerGene>,
 055             Serializable
 056 {
 057     private static final long serialVersionUID = 1L;
 058
 059
 060     protected IntegerChromosome(final ISeq<IntegerGene> genes) {
 061         super(genes);
 062     }
 063
 064     /**
 065      * Create a new random {@code IntegerChromosome}.
 066      *
 067      * @param min the min value of the {@link IntegerGene}s (inclusively).
 068      * @param max the max value of the {@link IntegerGene}s (inclusively).
 069      * @param length the length of the chromosome.
 070      * @throws NullPointerException if one of the arguments is {@code null}.
 071      */
 072     public IntegerChromosome(final Integer min, final Integer max, final int length) {
 073         this(IntegerGene.seq(min, max, length));
 074         _valid = true;
 075     }
 076
 077     /**
 078      * Create a new random {@code IntegerChromosome} 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 IntegerChromosome(final Integer min, final Integer max) {
 085         this(min, max, 1);
 086     }
 087
 088     /**
 089      * Returns an int array containing all of the elements in this chromosome
 090      * in proper sequence.  If the chromosome fits in the specified array, it is
 091      * returned therein. Otherwise, a new array is allocated with the length of
 092      * this chromosome.
 093      *
 094      * @since 3.0
 095      *
 096      * @param array the array into which the elements of this chromosomes are to
 097      *        be stored, if it is big enough; otherwise, a new array is
 098      *        allocated for this purpose.
 099      * @return an array containing the elements of this chromosome
 100      * @throws NullPointerException if the given {@code array} is {@code null}
 101      */
 102     public int[] toArray(final int[] array) {
 103         final int[] a = array.length >= length() ?
 104             array : new int[length()];
 105
 106         for (int i = length(); --i >= 0;) {
 107             a[i] = intValue(i);
 108         }
 109
 110         return a;
 111     }
 112
 113     /**
 114      * Returns an int array containing all of the elements in this chromosome
 115      * in proper sequence.
 116      *
 117      * @since 3.0
 118      *
 119      * @return an array containing the elements of this chromosome
 120      */
 121     public int[] toArray() {
 122         return toArray(new int[length()]);
 123     }
 124
 125     /**
 126      * Create a new {@code IntegerChromosome} with the given genes.
 127      *
 128      * @param genes the genes of the chromosome.
 129      * @return a new chromosome with the given genes.
 130      * @throws IllegalArgumentException if the length of the genes array is
 131      *         empty.
 132      */
 133     public static IntegerChromosome of(final IntegerGene... genes) {
 134         return new IntegerChromosome(ISeq.of(genes));
 135     }
 136
 137     /**
 138      * Create a new random {@code IntegerChromosome}.
 139      *
 140      * @param min the min value of the {@link IntegerGene}s (inclusively).
 141      * @param max the max value of the {@link IntegerGene}s (inclusively).
 142      * @param length the length of the chromosome.
 143      * @return a new random {@code IntegerChromosome}
 144      */
 145     public static IntegerChromosome of(
 146         final int min,
 147         final int max,
 148         final int length
 149     ) {
 150         return new IntegerChromosome(min, max, length);
 151     }
 152
 153     /**
 154      * Create a new random {@code IntegerChromosome} of length one.
 155      *
 156      * @param min the minimal value of this chromosome (inclusively).
 157      * @param max the maximal value of this chromosome (inclusively).
 158      * @return a new random {@code IntegerChromosome} of length one
 159      */
 160     public static IntegerChromosome of(final int min, final int max) {
 161         return new IntegerChromosome(min, max);
 162     }
 163
 164     @Override
 165     public IntegerChromosome newInstance(final ISeq<IntegerGene> genes) {
 166         return new IntegerChromosome(genes);
 167     }
 168
 169     @Override
 170     public IntegerChromosome newInstance() {
 171         return new IntegerChromosome(_min, _max, length());
 172     }
 173
 174     @Override
 175     public int hashCode() {
 176         return Hash.of(getClass()).and(super.hashCode()).value();
 177     }
 178
 179     @Override
 180     public boolean equals(final Object obj) {
 181         return Equality.of(this, obj).test(super::equals);
 182     }
 183
 184     /* *************************************************************************
 185      *  Java object serialization
 186      * ************************************************************************/
 187
 188     private void writeObject(final ObjectOutputStream out)
 189         throws IOException
 190     {
 191         out.defaultWriteObject();
 192
 193         out.writeInt(length());
 194         out.writeInt(_min.intValue());
 195         out.writeInt(_max.intValue());
 196
 197         for (IntegerGene gene : _genes) {
 198             out.writeInt(gene.getAllele().intValue());
 199         }
 200     }
 201
 202     private void readObject(final ObjectInputStream in)
 203         throws IOException, ClassNotFoundException
 204     {
 205         in.defaultReadObject();
 206
 207         final MSeq<IntegerGene> genes = MSeq.ofLength(in.readInt());
 208         _min = in.readInt();
 209         _max = in.readInt();
 210
 211         for (int i = 0; i < genes.length(); ++i) {
 212             genes.set(i, new IntegerGene(in.readInt(), _min, _max));
 213         }
 214
 215         _genes = genes.toISeq();
 216     }
 217
 218     /* *************************************************************************
 219      *  JAXB object serialization
 220      * ************************************************************************/
 221
 222     @XmlRootElement(name = "int-chromosome")
 223     @XmlType(name = "org.jenetics.IntegerChromosome")
 224     @XmlAccessorType(XmlAccessType.FIELD)
 225     final static class Model {
 226
 227         @XmlAttribute(name = "length", required = true)
 228         public int length;
 229
 230         @XmlAttribute(name = "min", required = true)
 231         public int min;
 232
 233         @XmlAttribute(name = "max", required = true)
 234         public int max;
 235
 236         @XmlElement(name = "allele", required = true, nillable = false)
 237         public List<Integer> values;
 238
 239         public final static class Adapter
 240             extends XmlAdapter<Model, IntegerChromosome>
 241         {
 242             @Override
 243             public Model marshal(final IntegerChromosome c) {
 244                 final Model m = new Model();
 245                 m.length = c.length();
 246                 m.min = c._min;
 247                 m.max = c._max;
 248                 m.values = c.toSeq().map(IntegerGene::getAllele).asList();
 249                 return m;
 250             }
 251
 252             @Override
 253             public IntegerChromosome unmarshal(final Model model) {
 254                 final Integer min = model.min;
 255                 final Integer max = model.max;
 256                 return new IntegerChromosome(
 257                     ISeq.of(model.values)
 258                         .map(a -> new IntegerGene(a, min, max))
 259                 );
 260             }
 261         }
 262
 263     }
 264 }
 |