001 /*
002 * Java Genetic Algorithm Library (jenetics-1.5.0).
003 * Copyright (c) 2007-2013 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.object.eq;
023 import static org.jenetics.util.object.hashCodeOf;
024
025 import org.jscience.mathematics.number.Number;
026
027 import org.jenetics.util.ISeq;
028
029
030 /**
031 * Abstract number chromosome.
032 *
033 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
034 * @since 1.0
035 * @version 1.0 — <em>$Date: 2013-12-06 $</em>
036 */
037 public abstract class NumberChromosome<
038 N extends Number<N>,
039 G extends NumberGene<N, G>
040 >
041 extends AbstractChromosome<G>
042 {
043 private static final long serialVersionUID = 1L;
044
045 /**
046 * The minimum value of this {@code NumberChromosome}.
047 */
048 protected transient N _min;
049
050 /**
051 * The maximum value of this {@code NumberChromosome}.
052 */
053 protected transient N _max;
054
055 /**
056 * Create a new chromosome from the given genes array.
057 *
058 * @param genes the genes of the new chromosome.
059 * @throws IllegalArgumentException if the {@code genes.length()} is smaller
060 * than one.
061 * @throws NullPointerException if the {@code genes} are {@code null}.
062 */
063 protected NumberChromosome(final ISeq<? extends G> genes) {
064 super(genes);
065 _min = genes.get(0)._min;
066 _max = genes.get(0)._max;
067 }
068
069 /**
070 * Return the minimum value of this {@code NumberChromosome}.
071 *
072 * @return the minimum value of this {@code NumberChromosome}.
073 */
074 public N getMin() {
075 return _min;
076 }
077
078 /**
079 * Return the maximum value of this {@code NumberChromosome}.
080 *
081 * @return the maximum value of this {@code NumberChromosome}.
082 */
083 public N getMax() {
084 return _max;
085 }
086
087 /**
088 * Return the byte value of this {@code NumberChromosome} at the given
089 * {@code index}.
090 *
091 * @param index the index of the {@link NumberGene}.
092 * @return the byte value of the {@link Gene} with the given {@code index}.
093 * @throws IndexOutOfBoundsException if the index is out of range
094 * (index < 0 || index >= length()).
095 */
096 public byte byteValue(final int index) {
097 return getGene(index).getAllele().byteValue();
098 }
099
100 /**
101 * Return the byte value of this {@code NumberChromosome} at the
102 * {@code index} 0.
103 *
104 * @return the byte value of the {@link Gene} with {@code index} 0.
105 */
106 public byte byteValue() {
107 return byteValue(0);
108 }
109
110 /**
111 * Return the short value of this {@code NumberChromosome} at the given
112 * {@code index}.
113 *
114 * @param index the index of the {@link NumberGene}.
115 * @return the short value of the {@link Gene} with the given {@code index}.
116 * @throws IndexOutOfBoundsException if the index is out of range
117 * (index < 0 || index >= length()).
118 */
119 public short shortValue(final int index) {
120 return getGene(index).getAllele().shortValue();
121 }
122
123 /**
124 * Return the short value of this {@code NumberChromosome} at the
125 * {@code index} 0.
126 *
127 * @return the short value of the {@link Gene} with {@code index} 0.
128 */
129 public short shortValue() {
130 return shortValue(0);
131 }
132
133 /**
134 * Return the int value of this {@code NumberChromosome} at the given
135 * {@code index}.
136 *
137 * @param index the index of the {@link NumberGene}.
138 * @return the int value of the {@link Gene} with the given {@code index}.
139 * @throws IndexOutOfBoundsException if the index is out of range
140 * (index < 0 || index >= length()).
141 */
142 public int intValue(final int index) {
143 return getGene(index).getAllele().intValue();
144 }
145
146 /**
147 * Return the int value of this {@code NumberChromosome} at the
148 * {@code index} 0.
149 *
150 * @return the int value of the {@link Gene} with {@code index} 0.
151 */
152 public int intValue() {
153 return intValue(0);
154 }
155
156 /**
157 * Return the long value of this {@code NumberChromosome} at the given
158 * {@code index}.
159 *
160 * @param index the index of the {@link NumberGene}.
161 * @return the long value of the {@link Gene} with the given {@code index}.
162 * @throws IndexOutOfBoundsException if the index is out of range
163 * (index < 0 || index >= length()).
164 */
165 public long longValue(final int index) {
166 return getGene(index).getAllele().longValue();
167 }
168
169 /**
170 * Return the long value of this {@code NumberChromosome} at the
171 * {@code index} 0.
172 *
173 * @return the long value of the {@link Gene} with {@code index} 0.
174 */
175 public long longValue() {
176 return longValue(0);
177 }
178
179 /**
180 * Return the float value of this {@code NumberChromosome} at the given
181 * {@code index}.
182 *
183 * @param index the index of the {@link NumberGene}.
184 * @return the float value of the {@link Gene} with the given {@code index}.
185 * @throws IndexOutOfBoundsException if the index is out of range
186 * (index < 0 || index >= length()).
187 */
188 public float floatValue(final int index) {
189 return getGene(index).getAllele().floatValue();
190 }
191
192 /**
193 * Return the float value of this {@code NumberChromosome} at the
194 * {@code index} 0.
195 *
196 * @return the float value of the {@link Gene} with {@code index} 0.
197 */
198 public float floatValue() {
199 return floatValue(0);
200 }
201
202 /**
203 * Return the double value of this {@code NumberChromosome} at the given
204 * {@code index}.
205 *
206 * @param index the index of the {@link NumberGene}.
207 * @return the double value of the {@link Gene} with the given {@code index}.
208 * @throws IndexOutOfBoundsException if the index is out of range
209 * (index < 0 || index >= length()).
210 */
211 public double doubleValue(final int index) {
212 return getGene(index).getAllele().doubleValue();
213 }
214
215 /**
216 * Return the double value of this {@code NumberChromosome} at the
217 * {@code index} 0.
218 *
219 * @return the double value of the {@link Gene} with {@code index} 0.
220 */
221 public double doubleValue() {
222 return doubleValue(0);
223 }
224
225 @Override
226 public int hashCode() {
227 return hashCodeOf(getClass()).
228 and(super.hashCode()).
229 and(_min).
230 and(_max).value();
231 }
232
233 @Override
234 public boolean equals(final Object object) {
235 if (object == this) {
236 return true;
237 }
238 if (!(object instanceof NumberChromosome<?, ?>)) {
239 return false;
240 }
241
242 final NumberChromosome<?, ?> nc = (NumberChromosome<?, ?>)object;
243 return eq(_min, nc._min) && eq(_max, nc._max) && super.equals(object);
244 }
245
246
247 }
248
249
250
|