001 /*
002 * Java Genetic Algorithm Library (jenetics-5.2.0).
003 * Copyright (c) 2007-2020 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 io.jenetics.internal.util.SerialIO.readLong;
023 import static io.jenetics.internal.util.SerialIO.writeLong;
024 import static java.lang.String.format;
025 import static java.util.Objects.requireNonNull;
026 import static io.jenetics.internal.util.Hashes.hash;
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.Serializable;
034 import java.util.Objects;
035
036 import io.jenetics.Gene;
037 import io.jenetics.Phenotype;
038 import io.jenetics.internal.util.Requires;
039 import io.jenetics.util.ISeq;
040
041 /**
042 * Represents a state of the GA at the start of an evolution step.
043 *
044 * @see EvolutionResult
045 * @see EvolutionInit
046 * @see EvolutionStreamable#stream(EvolutionStart)
047 *
048 * @param <G> the gene type
049 * @param <C> the fitness type
050 *
051 * @implNote
052 * This class is immutable and thread-safe.
053 *
054 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
055 * @since 3.1
056 * @version 5.2
057 */
058 public final /*record*/ class EvolutionStart<
059 G extends Gene<?, G>,
060 C extends Comparable<? super C>
061 >
062 implements Serializable
063 {
064
065 private static final long serialVersionUID = 2L;
066
067 private final ISeq<Phenotype<G, C>> _population;
068 private final long _generation;
069
070 private EvolutionStart(
071 final ISeq<Phenotype<G, C>> population,
072 final long generation
073 ) {
074 _population = requireNonNull(population);
075 _generation = Requires.positive(generation);
076 }
077
078 /**
079 * Return the population before the evolution step.
080 *
081 * @return the start population
082 */
083 public ISeq<Phenotype<G, C>> population() {
084 return _population;
085 }
086
087 /**
088 * Return the population before the evolution step.
089 *
090 * @return the start population
091 * @deprecated Use {@link #population()} instead
092 */
093 @Deprecated
094 public ISeq<Phenotype<G, C>> getPopulation() {
095 return _population;
096 }
097
098 /**
099 * Return the generation of the start population.
100 *
101 * @return the start generation
102 */
103 public long generation() {
104 return _generation;
105 }
106
107 /**
108 * Return the generation of the start population.
109 *
110 * @return the start generation
111 * @deprecated Use {@link #generation()} instead
112 */
113 @Deprecated
114 public long getGeneration() {
115 return _generation;
116 }
117
118 @Override
119 public int hashCode() {
120 return hash(_generation, hash(_population, hash(getClass())));
121 }
122
123 @Override
124 public boolean equals(final Object obj) {
125 return obj == this ||
126 obj instanceof EvolutionStart &&
127 _generation == ((EvolutionStart)obj)._generation &&
128 Objects.equals(_population, ((EvolutionStart)obj)._population);
129 }
130
131 @Override
132 public String toString() {
133 return format(
134 "EvolutionStart[population-size=%d, generation=%d]",
135 _population.size(), _generation
136 );
137 }
138
139 /**
140 * Create a new evolution start object with the given population and for the
141 * given generation.
142 *
143 * @param <G> the gene type
144 * @param <C> the fitness type
145 * @param population the start population.
146 * @param generation the start generation of the population
147 * @return a new evolution start object
148 * @throws java.lang.NullPointerException if the given {@code population} is
149 * {@code null}.
150 * @throws IllegalArgumentException if the given {@code generation} is
151 * smaller then one
152 */
153 public static <G extends Gene<?, G>, C extends Comparable<? super C>>
154 EvolutionStart<G, C> of(
155 final ISeq<Phenotype<G, C>> population,
156 final long generation
157 ) {
158 return new EvolutionStart<>(population, generation);
159 }
160
161 /**
162 * An empty evolution start object, which can be used as initial evolution
163 * value. The evolution {@link Engine} is then responsible for creating the
164 * proper initial population,
165 *
166 * @since 5.1
167 *
168 * @param <G> the gene type
169 * @param <C> the fitness type
170 * @return an empty evolution start object
171 */
172 public static <G extends Gene<?, G>, C extends Comparable<? super C>>
173 EvolutionStart<G, C> empty() {
174 return new EvolutionStart<>(ISeq.empty(), 1);
175 }
176
177
178 /* *************************************************************************
179 * Java object serialization
180 * ************************************************************************/
181
182 private Object writeReplace() {
183 return new Serial(Serial.EVOLUTION_START, this);
184 }
185
186 private void readObject(final ObjectInputStream stream)
187 throws InvalidObjectException
188 {
189 throw new InvalidObjectException("Serialization proxy required.");
190 }
191
192 void write(final ObjectOutput out) throws IOException {
193 out.writeObject(_population);
194 writeLong(_generation, out);
195 }
196
197 @SuppressWarnings({"unchecked", "rawtypes"})
198 static EvolutionStart read(final ObjectInput in)
199 throws IOException, ClassNotFoundException
200 {
201 return new EvolutionStart(
202 (ISeq)in.readObject(),
203 readLong(in)
204 );
205 }
206
207 }
|