001 /*
002 * Java Genetic Algorithm Library (jenetics-4.1.0).
003 * Copyright (c) 2007-2018 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@gmail.com)
019 */
020 package io.jenetics;
021
022 import static java.lang.String.format;
023 import static io.jenetics.util.RandomRegistry.getRandom;
024
025 import java.io.Serializable;
026 import java.util.Random;
027
028 import io.jenetics.internal.math.random;
029 import io.jenetics.util.ISeq;
030 import io.jenetics.util.IntRange;
031 import io.jenetics.util.LongRange;
032 import io.jenetics.util.MSeq;
033 import io.jenetics.util.Mean;
034
035 /**
036 * NumericGene implementation which holds a 64 bit integer number.
037 *
038 * <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">
039 * value-based</a> class; use of identity-sensitive operations (including
040 * reference equality ({@code ==}), identity hash code, or synchronization) on
041 * instances of {@code LongGene} may have unpredictable results and should
042 * be avoided.
043 *
044 * @see LongChromosome
045 *
046 * @implSpec
047 * This class is immutable and thread-safe.
048 *
049 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
050 * @since 1.6
051 * @version 4.0
052 */
053 public final class LongGene
054 extends AbstractNumericGene<Long, LongGene>
055 implements
056 NumericGene<Long, LongGene>,
057 Mean<LongGene>,
058 Comparable<LongGene>,
059 Serializable
060 {
061
062 private static final long serialVersionUID = 1L;
063
064 /**
065 * Create a new random {@code LongGene} with the given value and the
066 * given range. If the {@code value} isn't within the interval [min, max],
067 * no exception is thrown. In this case the method
068 * {@link LongGene#isValid()} returns {@code false}.
069 *
070 * @param value the value of the gene.
071 * @param min the minimal valid value of this gene (inclusively).
072 * @param max the maximal valid value of this gene (inclusively).
073 * @throws NullPointerException if one of the arguments is {@code null}.
074 */
075 LongGene(final Long value, final Long min, final Long max) {
076 super(value, min, max);
077 }
078
079 @Override
080 public int compareTo(final LongGene other) {
081 return _value.compareTo(other._value);
082 }
083
084 /**
085 * Create a new random {@code LongGene} with the given value and the
086 * given range. If the {@code value} isn't within the interval [min, max],
087 * no exception is thrown. In this case the method
088 * {@link LongGene#isValid()} returns {@code false}.
089 *
090 * @param value the value of the gene.
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 value, final long min, final long max) {
096 return new LongGene(value, min, max);
097 }
098
099 /**
100 * Create a new random {@code LongGene} with the given value and the
101 * given range. If the {@code value} isn't within the interval [min, max],
102 * no exception is thrown. In this case the method
103 * {@link LongGene#isValid()} returns {@code false}.
104 *
105 * @since 3.2
106 *
107 * @param value the value of the gene.
108 * @param range the long range to use
109 * @return a new random {@code LongGene}
110 * @throws NullPointerException if the given {@code range} is {@code null}.
111 */
112 public static LongGene of(final long value, final LongRange range) {
113 return new LongGene(value, range.getMin(), range.getMax());
114 }
115
116 /**
117 * Create a new random {@code LongGene}. It is guaranteed that the value of
118 * the {@code LongGene} lies in the interval [min, max].
119 *
120 * @param min the minimal valid value of this gene (inclusively).
121 * @param max the maximal valid value of this gene (inclusively).
122 * @return a new {@code LongGene} with the given parameters.
123 */
124 public static LongGene of(final long min, final long max) {
125 return of(nextLong(getRandom(), min, max), min, max);
126 }
127
128 /**
129 * Create a new random {@code LongGene}. It is guaranteed that the value of
130 * the {@code LongGene} lies in the interval [min, max].
131 *
132 * @since 3.2
133 *
134 * @param range the long range to use
135 * @return a new random {@code LongGene}
136 * @throws NullPointerException if the given {@code range} is {@code null}.
137 */
138 public static LongGene of(final LongRange range) {
139 return of(nextLong(getRandom(), range.getMin(), range.getMax()), range);
140 }
141
142 static ISeq<LongGene> seq(
143 final Long minimum,
144 final Long maximum,
145 final IntRange lengthRange
146 ) {
147 final long min = minimum;
148 final long max = maximum;
149 final Random r = getRandom();
150
151 return MSeq.<LongGene>ofLength(random.nextInt(lengthRange, r))
152 .fill(() -> new LongGene(nextLong(r, min, max), minimum, maximum))
153 .toISeq();
154 }
155
156 @Override
157 public LongGene newInstance(final Number number) {
158 return new LongGene(number.longValue(), _min, _max);
159 }
160
161 @Override
162 public LongGene newInstance() {
163 return new LongGene(
164 nextLong(getRandom(), _min, _max), _min, _max
165 );
166 }
167
168 @Override
169 public LongGene mean(final LongGene that) {
170 return new LongGene(_value + (that._value - _value)/2, _min, _max);
171 }
172
173 /**
174 * Returns a pseudo-random, uniformly distributed int value between min
175 * and max (min and max included).
176 *
177 * @param random the random engine to use for calculating the random
178 * long value
179 * @param min lower bound for generated long integer
180 * @param max upper bound for generated long integer
181 * @return a random long integer greater than or equal to {@code min}
182 * and less than or equal to {@code max}
183 * @throws IllegalArgumentException if {@code min > max}
184 * @throws NullPointerException if the given {@code random}
185 * engine is {@code null}.
186 */
187 static long nextLong(
188 final Random random,
189 final long min, final long max
190 ) {
191 if (min > max) {
192 throw new IllegalArgumentException(format(
193 "min >= max: %d >= %d.", min, max
194 ));
195 }
196
197 final long diff = (max - min) + 1;
198 long result = 0;
199
200 if (diff <= 0) {
201 do {
202 result = random.nextLong();
203 } while (result < min || result > max);
204 } else if (diff < Integer.MAX_VALUE) {
205 result = random.nextInt((int)diff) + min;
206 } else {
207 result = nextLong(random, diff) + min;
208 }
209
210 return result;
211 }
212
213 /**
214 * Returns a pseudo-random, uniformly distributed int value between 0
215 * (inclusive) and the specified value (exclusive), drawn from the given
216 * random number generator's sequence.
217 *
218 * @param random the random engine used for creating the random number.
219 * @param n the bound on the random number to be returned. Must be
220 * positive.
221 * @return the next pseudo-random, uniformly distributed int value
222 * between 0 (inclusive) and n (exclusive) from the given random
223 * number generator's sequence
224 * @throws IllegalArgumentException if n is smaller than 1.
225 * @throws NullPointerException if the given {@code random}
226 * engine is {@code null}.
227 */
228 static long nextLong(final Random random, final long n) {
229 if (n <= 0) {
230 throw new IllegalArgumentException(format(
231 "n is smaller than one: %d", n
232 ));
233 }
234
235 long bits;
236 long result;
237 do {
238 bits = random.nextLong() & 0x7fffffffffffffffL;
239 result = bits%n;
240 } while (bits - result + (n - 1) < 0);
241
242 return result;
243 }
244
245 }
|