| 
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 static org.jenetics.util.ISeq.toISeq;
 023
 024 import java.io.IOException;
 025 import java.io.ObjectInputStream;
 026 import java.io.ObjectOutputStream;
 027 import java.io.Serializable;
 028 import java.util.List;
 029
 030 import javax.xml.bind.annotation.XmlAccessType;
 031 import javax.xml.bind.annotation.XmlAccessorType;
 032 import javax.xml.bind.annotation.XmlAttribute;
 033 import javax.xml.bind.annotation.XmlElement;
 034 import javax.xml.bind.annotation.XmlRootElement;
 035 import javax.xml.bind.annotation.XmlType;
 036 import javax.xml.bind.annotation.adapters.XmlAdapter;
 037 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 038
 039 import org.jenetics.internal.util.Equality;
 040 import org.jenetics.internal.util.Hash;
 041
 042 import org.jenetics.util.ISeq;
 043 import org.jenetics.util.MSeq;
 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  * @since 1.6
 050  * @version 3.0 — <em>$Date: 2014-12-28 $</em>
 051  */
 052 @XmlJavaTypeAdapter(LongChromosome.Model.Adapter.class)
 053 public class LongChromosome
 054     extends AbstractBoundedChromosome<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      * Returns an long array containing all of the elements in this chromosome
 092      * in proper sequence.  If the chromosome fits in the specified array, it is
 093      * returned therein. Otherwise, a new array is allocated with the length of
 094      * this chromosome.
 095      *
 096      * @since 3.0
 097      *
 098      * @param array the array into which the elements of this chromosomes are to
 099      *        be stored, if it is big enough; otherwise, a new array is
 100      *        allocated for this purpose.
 101      * @return an array containing the elements of this chromosome
 102      * @throws NullPointerException if the given {@code array} is {@code null}
 103      */
 104     public long[] toArray(final long[] array) {
 105         final long[] a = array.length >= length() ?
 106             array : new long[length()];
 107
 108         for (int i = length(); --i >= 0;) {
 109             a[i] = longValue(i);
 110         }
 111
 112         return a;
 113     }
 114
 115     /**
 116      * Returns an long array containing all of the elements in this chromosome
 117      * in proper sequence.
 118      *
 119      * @since 3.0
 120      *
 121      * @return an array containing the elements of this chromosome
 122      */
 123     public long[] toArray() {
 124         return toArray(new long[length()]);
 125     }
 126
 127     /**
 128      * Create a new {@code LongChromosome} with the given genes.
 129      *
 130      * @param genes the genes of the chromosome.
 131      * @return a new chromosome with the given genes.
 132      * @throws IllegalArgumentException if the length of the genes array is
 133      *         empty.
 134      */
 135     public static LongChromosome of(final LongGene... genes) {
 136         return new LongChromosome(ISeq.of(genes));
 137     }
 138
 139     /**
 140      * Create a new random {@code LongChromosome}.
 141      *
 142      * @param min the min value of the {@link LongGene}s (inclusively).
 143      * @param max the max value of the {@link LongGene}s (inclusively).
 144      * @param length the length of the chromosome.
 145      * @return a new {@code LongChromosome} with the given gene parameters.
 146      */
 147     public static LongChromosome of(
 148         final long min,
 149         final long max,
 150         final int length
 151     ) {
 152         return new LongChromosome(min, max, length);
 153     }
 154
 155     /**
 156      * Create a new random {@code LongChromosome} of length one.
 157      *
 158      * @param min the minimal value of this chromosome (inclusively).
 159      * @param max the maximal value of this chromosome (inclusively).
 160      * @return a new {@code LongChromosome} with the given gene parameters.
 161      */
 162     public static LongChromosome of(final long min, final long max) {
 163         return new LongChromosome(min, max);
 164     }
 165
 166     @Override
 167     public LongChromosome newInstance(final ISeq<LongGene> genes) {
 168         return new LongChromosome(genes);
 169     }
 170
 171     @Override
 172     public LongChromosome newInstance() {
 173         return new LongChromosome(_min, _max, length());
 174     }
 175
 176     @Override
 177     public int hashCode() {
 178         return Hash.of(getClass()).and(super.hashCode()).value();
 179     }
 180
 181     @Override
 182     public boolean equals(final Object obj) {
 183         return Equality.of(this, obj).test(super::equals);
 184     }
 185
 186     /* *************************************************************************
 187      *  Java object serialization
 188      * ************************************************************************/
 189
 190     private void writeObject(final ObjectOutputStream out)
 191         throws IOException
 192     {
 193         out.defaultWriteObject();
 194
 195         out.writeInt(length());
 196         out.writeLong(_min.longValue());
 197         out.writeLong(_max.longValue());
 198
 199         for (LongGene gene : _genes) {
 200             out.writeLong(gene.getAllele().longValue());
 201         }
 202     }
 203
 204     private void readObject(final ObjectInputStream in)
 205         throws IOException, ClassNotFoundException
 206     {
 207         in.defaultReadObject();
 208
 209         final MSeq<LongGene> genes = MSeq.ofLength(in.readInt());
 210         _min = in.readLong();
 211         _max = in.readLong();
 212
 213         for (int i = 0; i < genes.length(); ++i) {
 214             genes.set(i, new LongGene(in.readLong(), _min, _max));
 215         }
 216
 217         _genes = genes.toISeq();
 218     }
 219
 220     /* *************************************************************************
 221      *  JAXB object serialization
 222      * ************************************************************************/
 223
 224     @XmlRootElement(name = "long-chromosome")
 225     @XmlType(name = "org.jenetics.LongChromosome")
 226     @XmlAccessorType(XmlAccessType.FIELD)
 227     final static class Model {
 228
 229         @XmlAttribute(name = "length", required = true)
 230         public int length;
 231
 232         @XmlAttribute(name = "min", required = true)
 233         public long min;
 234
 235         @XmlAttribute(name = "max", required = true)
 236         public long max;
 237
 238         @XmlElement(name = "allele", required = true, nillable = false)
 239         public List<Long> values;
 240
 241         public final static class Adapter
 242             extends XmlAdapter<Model, LongChromosome>
 243         {
 244             @Override
 245             public Model marshal(final LongChromosome c) {
 246                 final Model m = new Model();
 247                 m.length = c.length();
 248                 m.min = c._min;
 249                 m.max = c._max;
 250                 m.values = c.toSeq().map(LongGene::getAllele).asList();
 251                 return m;
 252             }
 253
 254             @Override
 255             public LongChromosome unmarshal(final Model model) {
 256                 final Long min = model.min;
 257                 final Long max = model.max;
 258                 return new LongChromosome(
 259                     model.values.stream()
 260                         .map(value -> new LongGene(value, min, max))
 261                         .collect(toISeq())
 262                 );
 263             }
 264         }
 265     }
 266 }
 |