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