001 /*
002 * Java Genetic Algorithm Library (jenetics-3.9.0).
003 * Copyright (c) 2007-2017 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.internal.math.random.nextInt;
023 import static org.jenetics.util.RandomRegistry.getRandom;
024
025 import java.io.Serializable;
026 import java.util.Random;
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.XmlRootElement;
032 import javax.xml.bind.annotation.XmlType;
033 import javax.xml.bind.annotation.XmlValue;
034 import javax.xml.bind.annotation.adapters.XmlAdapter;
035 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
036
037 import org.jenetics.internal.util.require;
038
039 import org.jenetics.util.ISeq;
040 import org.jenetics.util.IntRange;
041 import org.jenetics.util.MSeq;
042 import org.jenetics.util.Mean;
043
044 /**
045 * NumericGene implementation which holds a 32 bit integer number.
046 *
047 * <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">
048 * value-based</a> class; use of identity-sensitive operations (including
049 * reference equality ({@code ==}), identity hash code, or synchronization) on
050 * instances of {@code IntegerGene} may have unpredictable results and should
051 * be avoided.
052 *
053 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
054 * @since 2.0
055 * @version 3.2
056 */
057 @XmlJavaTypeAdapter(IntegerGene.Model.Adapter.class)
058 public final class IntegerGene
059 extends AbstractNumericGene<Integer, IntegerGene>
060 implements
061 NumericGene<Integer, IntegerGene>,
062 Mean<IntegerGene>,
063 Comparable<IntegerGene>,
064 Serializable
065 {
066
067 private static final long serialVersionUID = 1L;
068
069 /**
070 * Create a new random {@code IntegerGene} with the given value and the
071 * given range. If the {@code value} isn't within the interval [min, max],
072 * no exception is thrown. In this case the method
073 * {@link IntegerGene#isValid()} returns {@code false}.
074 *
075 * @param value the value of the gene.
076 * @param min the minimal valid value of this gene (inclusively).
077 * @param max the maximal valid value of this gene (inclusively).
078 * @throws NullPointerException if one of the arguments is {@code null}.
079 */
080 IntegerGene(final Integer value, final Integer min, final Integer max) {
081 super(value, min, max);
082 }
083
084 @Override
085 public int compareTo(final IntegerGene other) {
086 return _value.compareTo(other._value);
087 }
088
089 /**
090 * Create a new random {@code IntegerGene} with the given value and the
091 * given range. If the {@code value} isn't within the interval [min, max],
092 * no exception is thrown. In this case the method
093 * {@link IntegerGene#isValid()} returns {@code false}.
094 *
095 * @param value the value of the gene.
096 * @param min the minimal valid value of this gene (inclusively).
097 * @param max the maximal valid value of this gene (inclusively).
098 * @return a new {@code IntegerGene} with the given {@code value}
099 */
100 public static IntegerGene of(final int value, final int min, final int max) {
101 return new IntegerGene(value, min, max);
102 }
103
104 /**
105 * Create a new random {@code IntegerGene} with the given value and the
106 * given range. If the {@code value} isn't within the interval [min, max],
107 * no exception is thrown. In this case the method
108 * {@link IntegerGene#isValid()} returns {@code false}.
109 *
110 * @since 3.2
111 *
112 * @param value the value of the gene.
113 * @param range the integer range to use
114 * @return a new {@code IntegerGene} with the give {@code value}
115 * @throws NullPointerException if the given {@code range} is {@code null}.
116 */
117 public static IntegerGene of(final int value, final IntRange range) {
118 return new IntegerGene(value, range.getMin(), range.getMax());
119 }
120
121 /**
122 * Create a new random {@code IntegerGene}. It is guaranteed that the value of
123 * the {@code IntegerGene} lies in the interval [min, max].
124 *
125 * @param min the minimal valid value of this gene (inclusively).
126 * @param max the maximal valid value of this gene (inclusively).
127 * @return a new random {@code IntegerGene}
128 */
129 public static IntegerGene of(final int min, final int max) {
130 return of(nextInt(getRandom(), min, max), min, max);
131 }
132
133 /**
134 * Create a new random {@code IntegerGene}. It is guaranteed that the value of
135 * the {@code IntegerGene} lies in the interval [min, max].
136 *
137 * @since 3.2
138 *
139 * @param range the integer range to use
140 * @return a new random {@code IntegerGene}
141 * @throws NullPointerException if the given {@code range} is {@code null}.
142 */
143 public static IntegerGene of(final IntRange range) {
144 return of(nextInt(getRandom(), range.getMin(), range.getMax()), range);
145 }
146
147 static ISeq<IntegerGene> seq(
148 final Integer minimum,
149 final Integer maximum,
150 final int length
151 ) {
152 require.positive(length);
153
154 final int min = minimum;
155 final int max = maximum;
156 final Random r = getRandom();
157
158 return MSeq.<IntegerGene>ofLength(length)
159 .fill(() -> new IntegerGene(nextInt(r, min, max), minimum, maximum))
160 .toISeq();
161 }
162
163 @Override
164 public IntegerGene newInstance(final Number number) {
165 return new IntegerGene(number.intValue(), _min, _max);
166 }
167
168 @Override
169 public IntegerGene newInstance() {
170 return new IntegerGene(
171 nextInt(getRandom(), _min, _max), _min, _max
172 );
173 }
174
175 @Override
176 public IntegerGene mean(final IntegerGene that) {
177 return new IntegerGene(_value + (that._value - _value)/2, _min, _max);
178 }
179
180 /* *************************************************************************
181 * JAXB object serialization
182 * ************************************************************************/
183
184 @XmlRootElement(name = "int-gene")
185 @XmlType(name = "org.jenetics.IntegerGene")
186 @XmlAccessorType(XmlAccessType.FIELD)
187 final static class Model {
188
189 @XmlAttribute(name = "min", required = true)
190 public int min;
191
192 @XmlAttribute(name = "max", required = true)
193 public int max;
194
195 @XmlValue
196 public int value;
197
198 public final static class Adapter
199 extends XmlAdapter<Model, IntegerGene>
200 {
201 @Override
202 public Model marshal(final IntegerGene value) {
203 final Model m = new Model();
204 m.min = value.getMin();
205 m.max = value.getMax();
206 m.value = value.getAllele();
207 return m;
208 }
209
210 @Override
211 public IntegerGene unmarshal(final Model m) {
212 return IntegerGene.of(m.value, m.min, m.max);
213 }
214 }
215 }
216
217 }
|