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 static org.jenetics.util.math.random.nextLong;
023
024 import java.util.Random;
025
026 import javax.xml.bind.annotation.XmlAccessType;
027 import javax.xml.bind.annotation.XmlAccessorType;
028 import javax.xml.bind.annotation.XmlAttribute;
029 import javax.xml.bind.annotation.XmlRootElement;
030 import javax.xml.bind.annotation.XmlType;
031 import javax.xml.bind.annotation.XmlValue;
032 import javax.xml.bind.annotation.adapters.XmlAdapter;
033 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
034
035 import org.jenetics.util.Array;
036 import org.jenetics.util.ISeq;
037 import org.jenetics.util.Mean;
038 import org.jenetics.util.RandomRegistry;
039
040 /**
041 * NumericGene implementation which holds a 64 bit integer number.
042 *
043 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044 * @version 1.6 — <em>$Date: 2014-04-09 $</em>
045 * @since 1.6
046 */
047 @XmlJavaTypeAdapter(LongGene.Model.Adapter.class)
048 public final class LongGene
049 extends AbstractNumericGene<Long, LongGene>
050 implements
051 NumericGene<Long, LongGene>,
052 Mean<LongGene>
053 {
054
055 private static final long serialVersionUID = 1L;
056
057 /**
058 * Create a new random {@code LongGene} with the given value and the
059 * given range. If the {@code value} isn't within the interval [min, max],
060 * no exception is thrown. In this case the method
061 * {@link LongGene#isValid()} returns {@code false}.
062 *
063 * @param value the value of the gene.
064 * @param min the minimal valid value of this gene (inclusively).
065 * @param max the maximal valid value of this gene (inclusively).
066 * @throws NullPointerException if one of the arguments is {@code null}.
067 */
068 public LongGene(final Long value, final Long min, final Long max) {
069 super(value, min, max);
070 }
071
072 /**
073 * Create a new random {@code LongGene} with the given value and the
074 * given range. If the {@code value} isn't within the interval [min, max],
075 * no exception is thrown. In this case the method
076 * {@link LongGene#isValid()} returns {@code false}.
077 *
078 * @param value the value of the gene.
079 * @param min the minimal valid value of this gene (inclusively).
080 * @param max the maximal valid value of this gene (inclusively).
081 * @return a new {@code LongGene} with the given parameters.
082 */
083 public static LongGene of(final long value, final long min, final long max) {
084 return new LongGene(value, min, max);
085 }
086
087 /**
088 * Create a new random {@code LongGene}. It is guaranteed that the value of
089 * the {@code LongGene} lies in the interval [min, max].
090 *
091 * @param min the minimal valid value of this gene (inclusively).
092 * @param max the maximal valid value of this gene (inclusively).
093 * @return a new {@code LongGene} with the given parameters.
094 */
095 public static LongGene of(final long min, final long max) {
096 return of(nextLong(RandomRegistry.getRandom(), min, max), min, max);
097 }
098
099 static ISeq<LongGene> seq(
100 final Long minimum,
101 final Long maximum,
102 final int length
103 ) {
104 final long min = minimum;
105 final long max = maximum;
106 final Random r = RandomRegistry.getRandom();
107
108 final Array<LongGene> genes = new Array<>(length);
109 for (int i = 0; i < length; ++i) {
110 genes.set(i, new LongGene(nextLong(r, min, max), minimum, maximum));
111 }
112 return genes.toISeq();
113 }
114
115 @Override
116 public LongGene newInstance(final Number number) {
117 return new LongGene(number.longValue(), _min, _max);
118 }
119
120 @Override
121 public LongGene newInstance() {
122 return new LongGene(
123 nextLong(RandomRegistry.getRandom(), _min, _max), _min, _max
124 );
125 }
126
127 @Override
128 public LongGene mean(final LongGene that) {
129 return new LongGene(_value + (that._value - _value) / 2, _min, _max);
130 }
131
132 /* *************************************************************************
133 * JAXB object serialization
134 * ************************************************************************/
135
136 @XmlRootElement(name = "long-gene")
137 @XmlType(name = "org.jenetics.LongGene")
138 @XmlAccessorType(XmlAccessType.FIELD)
139 final static class Model {
140
141 @XmlAttribute(name = "min", required = true)
142 public long min;
143
144 @XmlAttribute(name = "max", required = true)
145 public long max;
146
147 @XmlValue
148 public long value;
149
150 public final static class Adapter
151 extends XmlAdapter<Model, LongGene>
152 {
153 @Override
154 public Model marshal(final LongGene value) {
155 final Model m = new Model();
156 m.min = value.getMin();
157 m.max = value.getMax();
158 m.value = value.getAllele();
159 return m;
160 }
161
162 @Override
163 public LongGene unmarshal(final Model m) {
164 return LongGene.of(m.value, m.min, m.max);
165 }
166 }
167 }
168
169 }
|