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