| 
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 floating point 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(DoubleChromosome.Model.Adapter.class)
 053 public class DoubleChromosome
 054     extends AbstractBoundedChromosome<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      * Returns an double 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 double[] toArray(final double[] array) {
 105         final double[] a = array.length >= length() ?
 106             array : new double[length()];
 107
 108         for (int i = length(); --i >= 0;) {
 109             a[i] = doubleValue(i);
 110         }
 111
 112         return a;
 113     }
 114
 115     /**
 116      * Returns an double 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 double[] toArray() {
 124         return toArray(new double[length()]);
 125     }
 126
 127     /**
 128      * Create a new {@code DoubleChromosome} 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 DoubleChromosome of(final DoubleGene... genes) {
 136         return new DoubleChromosome(ISeq.of(genes));
 137     }
 138
 139     /**
 140      * Create a new random {@code DoubleChromosome}.
 141      *
 142      * @param min the min value of the {@link DoubleGene}s (inclusively).
 143      * @param max the max value of the {@link DoubleGene}s (exclusively).
 144      * @param length the length of the chromosome.
 145      * @return a new {@code DoubleChromosome} with the given parameter
 146      */
 147     public static DoubleChromosome of(final double min, double max, final int length) {
 148         return new DoubleChromosome(min, max, length);
 149     }
 150
 151     /**
 152      * Create a new random {@code DoubleChromosome} of length one.
 153      *
 154      * @param min the minimal value of this chromosome (inclusively).
 155      * @param max the maximal value of this chromosome (exclusively).
 156      * @return a new {@code DoubleChromosome} with the given parameter
 157      */
 158     public static DoubleChromosome of(final double min, final double max) {
 159         return new DoubleChromosome(min, max);
 160     }
 161
 162     @Override
 163     public DoubleChromosome newInstance(final ISeq<DoubleGene> genes) {
 164         return new DoubleChromosome(genes);
 165     }
 166
 167     @Override
 168     public DoubleChromosome newInstance() {
 169         return new DoubleChromosome(_min, _max, length());
 170     }
 171
 172     @Override
 173     public int hashCode() {
 174         return Hash.of(getClass()).and(super.hashCode()).value();
 175     }
 176
 177     @Override
 178     public boolean equals(final Object obj) {
 179         return Equality.of(this, obj).test(super::equals);
 180     }
 181
 182
 183     /* *************************************************************************
 184      *  Java object serialization
 185      * ************************************************************************/
 186
 187     private void writeObject(final ObjectOutputStream out)
 188         throws IOException
 189     {
 190         out.defaultWriteObject();
 191
 192         out.writeInt(length());
 193         out.writeDouble(_min.doubleValue());
 194         out.writeDouble(_max.doubleValue());
 195
 196         for (DoubleGene gene : _genes) {
 197             out.writeDouble(gene.getAllele().doubleValue());
 198         }
 199     }
 200
 201     private void readObject(final ObjectInputStream in)
 202         throws IOException, ClassNotFoundException
 203     {
 204         in.defaultReadObject();
 205
 206         final MSeq<DoubleGene> genes = MSeq.ofLength(in.readInt());
 207         _min = in.readDouble();
 208         _max = in.readDouble();
 209
 210         for (int i = 0; i < genes.length(); ++i) {
 211             genes.set(i, new DoubleGene(in.readDouble(), _min, _max));
 212         }
 213
 214         _genes = genes.toISeq();
 215     }
 216
 217     /* *************************************************************************
 218      *  JAXB object serialization
 219      * ************************************************************************/
 220
 221     @XmlRootElement(name = "double-chromosome")
 222     @XmlType(name = "org.jenetics.DoubleChromosome")
 223     @XmlAccessorType(XmlAccessType.FIELD)
 224     final static class Model {
 225
 226         @XmlAttribute(name = "length", required = true)
 227         public int length;
 228
 229         @XmlAttribute(name = "min", required = true)
 230         public double min;
 231
 232         @XmlAttribute(name = "max", required = true)
 233         public double max;
 234
 235         @XmlElement(name = "allele", required = true, nillable = false)
 236         public List<Double> values;
 237
 238         public final static class Adapter
 239             extends XmlAdapter<Model, DoubleChromosome>
 240         {
 241             @Override
 242             public Model marshal(final DoubleChromosome c) {
 243                 final Model m = new Model();
 244                 m.length = c.length();
 245                 m.min = c._min;
 246                 m.max = c._max;
 247                 m.values = c.toSeq().map(DoubleGene::getAllele).asList();
 248                 return m;
 249             }
 250
 251             @Override
 252             public DoubleChromosome unmarshal(final Model model) {
 253                 final Double min = model.min;
 254                 final Double max = model.max;
 255                 return new DoubleChromosome(
 256                     model.values.stream()
 257                         .map(value -> new DoubleGene(value, min, max))
 258                         .collect(toISeq())
 259                 );
 260             }
 261         }
 262
 263     }
 264 }
 |