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