001/* 002 * Java Genetic Algorithm Library (jenetics-8.2.0). 003 * Copyright (c) 2007-2025 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 */ 020package io.jenetics.engine; 021 022import static java.lang.String.format; 023import static java.util.Objects.requireNonNull; 024import static io.jenetics.internal.util.Hashes.hash; 025import static io.jenetics.internal.util.SerialIO.readLong; 026import static io.jenetics.internal.util.SerialIO.writeLong; 027 028import java.io.IOException; 029import java.io.InvalidObjectException; 030import java.io.ObjectInput; 031import java.io.ObjectInputStream; 032import java.io.ObjectOutput; 033import java.io.Serial; 034import java.io.Serializable; 035import java.util.Objects; 036 037import io.jenetics.Gene; 038import io.jenetics.Phenotype; 039import io.jenetics.internal.util.Requires; 040import io.jenetics.util.ISeq; 041 042/** 043 * Represents a state of the GA at the start of an evolution step. 044 * 045 * @see EvolutionResult 046 * @see EvolutionInit 047 * @see EvolutionStreamable#stream(EvolutionStart) 048 * 049 * @param <G> the gene type 050 * @param <C> the fitness type 051 * 052 * @implNote 053 * This class is immutable and thread-safe. 054 * 055 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 056 * @since 3.1 057 * @version 6.0 058 */ 059public final class EvolutionStart< 060 G extends Gene<?, G>, 061 C extends Comparable<? super C> 062> 063 implements Serializable 064{ 065 066 @Serial 067 private static final long serialVersionUID = 2L; 068 069 private final ISeq<Phenotype<G, C>> _population; 070 private final long _generation; 071 072 private final boolean _dirty; 073 074 EvolutionStart( 075 final ISeq<Phenotype<G, C>> population, 076 final long generation, 077 final boolean dirty 078 ) { 079 _population = requireNonNull(population); 080 _generation = Requires.positive(generation); 081 _dirty = dirty; 082 } 083 084 /** 085 * Return the population before the evolution step. 086 * 087 * @return the start population 088 */ 089 public ISeq<Phenotype<G, C>> population() { 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 * Indicates whether the population is guaranteed to be evaluated. If this 104 * flag is {@code true}, the population possibly contains unevaluated 105 * individuals. 106 * 107 * @return {@code false}, if it is guaranteed that all individuals has 108 * already been evaluated, {@code true} otherwise 109 */ 110 boolean isDirty() { 111 return _dirty; 112 } 113 114 @Override 115 public int hashCode() { 116 return hash(_generation, hash(_population, hash(getClass()))); 117 } 118 119 @Override 120 public boolean equals(final Object obj) { 121 return obj instanceof EvolutionStart<?, ?> other && 122 _generation == other._generation && 123 Objects.equals(_population, other._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 <C> the fitness type 140 * @param population the start population. 141 * @param generation the start generation of the population 142 * @return a new evolution start object 143 * @throws java.lang.NullPointerException if the given {@code population} is 144 * {@code null}. 145 * @throws IllegalArgumentException if the given {@code generation} is 146 * smaller then one 147 */ 148 public static <G extends Gene<?, G>, C extends Comparable<? super C>> 149 EvolutionStart<G, C> of( 150 final ISeq<Phenotype<G, C>> population, 151 final long generation 152 ) { 153 return new EvolutionStart<>(population, generation, true); 154 } 155 156 /** 157 * An empty evolution start object, which can be used as initial evolution 158 * value. The evolution {@link Engine} is then responsible for creating the 159 * proper initial population, 160 * 161 * @since 5.1 162 * 163 * @param <G> the gene type 164 * @param <C> the fitness type 165 * @return an empty evolution start object 166 */ 167 public static <G extends Gene<?, G>, C extends Comparable<? super C>> 168 EvolutionStart<G, C> empty() { 169 return new EvolutionStart<>(ISeq.empty(), 1, false); 170 } 171 172 173 /* ************************************************************************* 174 * Java object serialization 175 * ************************************************************************/ 176 177 @Serial 178 private Object writeReplace() { 179 return new SerialProxy(SerialProxy.EVOLUTION_START, this); 180 } 181 182 @Serial 183 private void readObject(final ObjectInputStream stream) 184 throws InvalidObjectException 185 { 186 throw new InvalidObjectException("Serialization proxy required."); 187 } 188 189 void write(final ObjectOutput out) throws IOException { 190 out.writeObject(_population); 191 writeLong(_generation, out); 192 } 193 194 @SuppressWarnings({"unchecked", "rawtypes"}) 195 static Object read(final ObjectInput in) 196 throws IOException, ClassNotFoundException 197 { 198 return new EvolutionStart( 199 (ISeq)in.readObject(), 200 readLong(in), 201 true 202 ); 203 } 204 205}