EvolutionInit.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-8.0.0).
003  * Copyright (c) 2007-2024 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.engine;
021 
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 import static io.jenetics.internal.util.Hashes.hash;
025 import static io.jenetics.internal.util.SerialIO.readLong;
026 import static io.jenetics.internal.util.SerialIO.writeLong;
027 
028 import java.io.IOException;
029 import java.io.InvalidObjectException;
030 import java.io.ObjectInput;
031 import java.io.ObjectInputStream;
032 import java.io.ObjectOutput;
033 import java.io.Serial;
034 import java.io.Serializable;
035 import java.util.Objects;
036 
037 import io.jenetics.Gene;
038 import io.jenetics.Genotype;
039 import io.jenetics.internal.util.Requires;
040 import io.jenetics.util.ISeq;
041 
042 /**
043  * Represents the initialization value of an evolution stream/iterator.
044  *
045  @see EvolutionStart
046  @see EvolutionStreamable#stream(EvolutionInit)
047  *
048  @param <G> the gene type
049  *
050  * @implNote
051  * This class is immutable and thread-safe.
052  *
053  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
054  @version 6.0
055  @since 4.1
056  */
057 public final class EvolutionInit<G extends Gene<?, G>>
058     implements Serializable
059 {
060 
061     @Serial
062     private static final long serialVersionUID = 1L;
063 
064     private final ISeq<Genotype<G>> _population;
065     private final long _generation;
066 
067     private EvolutionInit(
068         final ISeq<Genotype<G>> population,
069         final long generation
070     ) {
071         _population = requireNonNull(population);
072         _generation = Requires.positive(generation);
073     }
074 
075     /**
076      * Return the initial population.
077      *
078      @return the initial population
079      */
080     public ISeq<Genotype<G>> population() {
081         return _population;
082     }
083 
084     /**
085      * Return the generation of the start population.
086      *
087      @return the start generation
088      */
089     public long generation() {
090         return _generation;
091     }
092 
093     @Override
094     public int hashCode() {
095         return hash(_generation, hash(_population));
096     }
097 
098     @Override
099     public boolean equals(final Object obj) {
100         return obj == this ||
101             obj instanceof EvolutionInit<?> other &&
102             _generation == other._generation &&
103             Objects.equals(_population, other._population);
104     }
105 
106     @Override
107     public String toString() {
108         return format(
109             "EvolutionStart[population-size=%d, generation=%d]",
110             _population.size(), _generation
111         );
112     }
113 
114     /**
115      * Create a new evolution start object with the given population and for the
116      * given generation.
117      *
118      @param <G> the gene type
119      @param population the start population.
120      @param generation the start generation of the population
121      @return a new evolution start object
122      @throws java.lang.NullPointerException if the given {@code population} is
123      *         {@code null}.
124      @throws IllegalArgumentException if the given {@code generation} is
125      *         smaller then one
126      */
127     public static <G extends Gene<?, G>>
128     EvolutionInit<G> of(
129         final ISeq<Genotype<G>> population,
130         final long generation
131     ) {
132         return new EvolutionInit<>(population, generation);
133     }
134 
135 
136     /* *************************************************************************
137      *  Java object serialization
138      * ************************************************************************/
139 
140     @Serial
141     private Object writeReplace() {
142         return new SerialProxy(SerialProxy.EVOLUTION_INIT, this);
143     }
144 
145     @Serial
146     private void readObject(final ObjectInputStream stream)
147         throws InvalidObjectException
148     {
149         throw new InvalidObjectException("Serialization proxy required.");
150     }
151 
152     void write(final ObjectOutput outthrows IOException {
153         out.writeObject(_population);
154         writeLong(_generation, out);
155     }
156 
157     @SuppressWarnings({"unchecked""rawtypes"})
158     static Object read(final ObjectInput in)
159         throws IOException, ClassNotFoundException
160     {
161         return new EvolutionInit(
162             (ISeq)in.readObject(),
163             readLong(in)
164         );
165     }
166 
167 }