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