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.nextInt;
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 32 bit integer number.
042 *
043 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044 * @version 2.0 — <em>$Date: 2014-04-10 $</em>
045 * @since 2.0
046 */
047 @XmlJavaTypeAdapter(IntegerGene.Model.Adapter.class)
048 public final class IntegerGene
049 extends AbstractNumericGene<Integer, IntegerGene>
050 implements
051 NumericGene<Integer, IntegerGene>,
052 Mean<IntegerGene>
053 {
054
055 private static final long serialVersionUID = 1L;
056
057 /**
058 * Create a new random {@code IntegerGene} 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 IntegerGene#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 IntegerGene(final Integer value, final Integer min, final Integer max) {
069 super(value, min, max);
070 }
071
072 /**
073 * Create a new random {@code IntegerGene} 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 IntegerGene#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 */
082 public static IntegerGene of(final int value, final int min, final int max) {
083 return new IntegerGene(value, min, max);
084 }
085
086 /**
087 * Create a new random {@code IntegerGene}. It is guaranteed that the value of
088 * the {@code IntegerGene} lies in the interval [min, max].
089 *
090 * @param min the minimal valid value of this gene (inclusively).
091 * @param max the maximal valid value of this gene (inclusively).
092 */
093 public static IntegerGene of(final int min, final int max) {
094 return of(nextInt(RandomRegistry.getRandom(), min, max), min, max);
095 }
096
097 static ISeq<IntegerGene> seq(
098 final Integer minimum,
099 final Integer maximum,
100 final int length
101 ) {
102 final int min = minimum;
103 final int max = maximum;
104 final Random r = RandomRegistry.getRandom();
105
106 final Array<IntegerGene> genes = new Array<>(length);
107 for (int i = 0; i < length; ++i) {
108 genes.set(i, new IntegerGene(nextInt(r, min, max), minimum, maximum));
109 }
110 return genes.toISeq();
111 }
112
113 @Override
114 public IntegerGene newInstance(final Number number) {
115 return new IntegerGene(number.intValue(), _min, _max);
116 }
117
118 @Override
119 public IntegerGene newInstance() {
120 return new IntegerGene(
121 nextInt(RandomRegistry.getRandom(), _min, _max), _min, _max
122 );
123 }
124
125 @Override
126 public IntegerGene mean(final IntegerGene that) {
127 return new IntegerGene(_value + (that._value - _value) / 2, _min, _max);
128 }
129
130 /* *************************************************************************
131 * JAXB object serialization
132 * ************************************************************************/
133
134 @XmlRootElement(name = "int-gene")
135 @XmlType(name = "org.jenetics.IntegerGene")
136 @XmlAccessorType(XmlAccessType.FIELD)
137 final static class Model {
138
139 @XmlAttribute(name = "min", required = true)
140 public int min;
141
142 @XmlAttribute(name = "max", required = true)
143 public int max;
144
145 @XmlValue
146 public int value;
147
148 public final static class Adapter
149 extends XmlAdapter<Model, IntegerGene>
150 {
151 @Override
152 public Model marshal(final IntegerGene value) {
153 final Model m = new Model();
154 m.min = value.getMin();
155 m.max = value.getMax();
156 m.value = value.getAllele();
157 return m;
158 }
159
160 @Override
161 public IntegerGene unmarshal(final Model m) {
162 return IntegerGene.of(m.value, m.min, m.max);
163 }
164 }
165 }
166
167 }
|