001 /*
002 * Java Genetic Algorithm Library (jenetics-4.3.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.ext;
021
022 import java.io.IOException;
023 import java.io.ObjectInputStream;
024 import java.io.ObjectOutputStream;
025 import java.io.Serializable;
026 import java.math.BigInteger;
027
028 import io.jenetics.AbstractChromosome;
029 import io.jenetics.DoubleGene;
030 import io.jenetics.NumericChromosome;
031 import io.jenetics.internal.util.reflect;
032 import io.jenetics.util.ISeq;
033 import io.jenetics.util.MSeq;
034
035 /**
036 * Numeric chromosome implementation which holds arbitrary sized integer numbers.
037 *
038 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
039 * @version 3.5
040 * @since 3.5
041 */
042 public class BigIntegerChromosome
043 extends AbstractChromosome<BigIntegerGene>
044 implements
045 NumericChromosome<BigInteger, BigIntegerGene>,
046 Serializable
047 {
048
049 private static final long serialVersionUID = 1L;
050
051 private final BigInteger _min;
052 private final BigInteger _max;
053
054 /**
055 * Create a new chromosome from the given genes array.
056 *
057 * @param genes the genes of the new chromosome.
058 * @throws IllegalArgumentException if the {@code genes.length()} is smaller
059 * than one.
060 * @throws NullPointerException if the {@code genes} are {@code null}.
061 * @throws IllegalArgumentException if the gene sequence is empty
062 */
063 protected BigIntegerChromosome(final ISeq<BigIntegerGene> genes) {
064 super(genes);
065 _min = genes.get(0).getMin();
066 _max = genes.get(0).getMax();
067 }
068
069 /**
070 * Create a new random {@code BigIntegerChromosome} with the given
071 * {@code length}.
072 *
073 * @param min the min value of the {@link BigIntegerGene}s (inclusively).
074 * @param max the max value of the {@link BigIntegerGene}s (inclusively).
075 * @param length the length of the chromosome.
076 * @throws NullPointerException if one of the arguments is {@code null}.
077 * @throws IllegalArgumentException if the {@code length} is smaller than
078 * one.
079 */
080 public BigIntegerChromosome(
081 final BigInteger min,
082 final BigInteger max,
083 final int length
084 ) {
085 this(BigIntegerGene.seq(min, max, length));
086 _valid = true;
087 }
088
089 /**
090 * Create a new random {@code DoubleChromosome} of length one.
091 *
092 * @param min the minimal value of this chromosome (inclusively).
093 * @param max the maximal value of this chromosome (exclusively).
094 * @throws NullPointerException if one of the arguments is {@code null}.
095 */
096 public BigIntegerChromosome(final BigInteger min, final BigInteger max) {
097 this(min, max, 1);
098 }
099
100 @Override
101 public BigInteger getMin() {
102 return _min;
103 }
104
105 @Override
106 public BigInteger getMax() {
107 return _max;
108 }
109
110 @Override
111 public BigIntegerChromosome newInstance(final ISeq<BigIntegerGene> genes) {
112 return new BigIntegerChromosome(genes);
113 }
114
115 @Override
116 public BigIntegerChromosome newInstance() {
117 return new BigIntegerChromosome(_min, _max, length());
118 }
119
120 /* *************************************************************************
121 * Static factory methods.
122 **************************************************************************/
123
124 /**
125 * Create a new {@code DoubleChromosome} with the given genes.
126 *
127 * @param genes the genes of the chromosome.
128 * @return a new chromosome with the given genes.
129 * @throws IllegalArgumentException if the length of the genes array is
130 * empty.
131 * @throws NullPointerException if the given {@code genes} array is
132 * {@code null}
133 */
134 public static BigIntegerChromosome of(final BigIntegerGene... genes) {
135 return new BigIntegerChromosome(ISeq.of(genes));
136 }
137
138 /**
139 * Create a new random {@code DoubleChromosome}.
140 *
141 * @param min the min value of the {@link DoubleGene}s (inclusively).
142 * @param max the max value of the {@link DoubleGene}s (exclusively).
143 * @param length the length of the chromosome.
144 * @return a new {@code DoubleChromosome} with the given parameter
145 */
146 public static BigIntegerChromosome of(final BigInteger min, BigInteger max, final int length) {
147 return new BigIntegerChromosome(min, max, length);
148 }
149
150 /**
151 * Create a new random {@code DoubleChromosome} of length one.
152 *
153 * @param min the minimal value of this chromosome (inclusively).
154 * @param max the maximal value of this chromosome (exclusively).
155 * @return a new {@code DoubleChromosome} with the given parameter
156 */
157 public static BigIntegerChromosome of(final BigInteger min, final BigInteger max) {
158 return new BigIntegerChromosome(min, max);
159 }
160
161 /* *************************************************************************
162 * Java object serialization
163 * ************************************************************************/
164
165 private void writeObject(final ObjectOutputStream out)
166 throws IOException
167 {
168 out.defaultWriteObject();
169
170 out.writeInt(length());
171 out.writeObject(_min);
172 out.writeObject(_max);
173
174 for (BigIntegerGene gene : _genes) {
175 out.writeObject(gene.getAllele());
176 }
177 }
178
179 private void readObject(final ObjectInputStream in)
180 throws IOException, ClassNotFoundException
181 {
182 in.defaultReadObject();
183
184 final MSeq<BigIntegerGene> genes = MSeq.ofLength(in.readInt());
185 reflect.setField(this, "_min", in.readObject());
186 reflect.setField(this, "_max", in.readObject());
187
188 for (int i = 0; i < genes.length(); ++i) {
189 final BigInteger value = (BigInteger)in.readObject();
190 genes.set(i, BigIntegerGene.of(value, _min, _max));
191 }
192
193 reflect.setField(this, "_genes", genes.toISeq());
194 }
195
196 }
|