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.Genotype; 039import io.jenetics.internal.util.Requires; 040import 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 */ 057public 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 instanceof EvolutionInit<?> other && 101 _generation == other._generation && 102 Objects.equals(_population, other._population); 103 } 104 105 @Override 106 public String toString() { 107 return format( 108 "EvolutionStart[population-size=%d, generation=%d]", 109 _population.size(), _generation 110 ); 111 } 112 113 /** 114 * Create a new evolution start object with the given population and for the 115 * given generation. 116 * 117 * @param <G> the gene type 118 * @param population the start population. 119 * @param generation the start generation of the population 120 * @return a new evolution start object 121 * @throws java.lang.NullPointerException if the given {@code population} is 122 * {@code null}. 123 * @throws IllegalArgumentException if the given {@code generation} is 124 * smaller then one 125 */ 126 public static <G extends Gene<?, G>> 127 EvolutionInit<G> of( 128 final ISeq<Genotype<G>> population, 129 final long generation 130 ) { 131 return new EvolutionInit<>(population, generation); 132 } 133 134 135 /* ************************************************************************* 136 * Java object serialization 137 * ************************************************************************/ 138 139 @Serial 140 private Object writeReplace() { 141 return new SerialProxy(SerialProxy.EVOLUTION_INIT, this); 142 } 143 144 @Serial 145 private void readObject(final ObjectInputStream stream) 146 throws InvalidObjectException 147 { 148 throw new InvalidObjectException("Serialization proxy required."); 149 } 150 151 void write(final ObjectOutput out) throws IOException { 152 out.writeObject(_population); 153 writeLong(_generation, out); 154 } 155 156 @SuppressWarnings({"unchecked", "rawtypes"}) 157 static Object read(final ObjectInput in) 158 throws IOException, ClassNotFoundException 159 { 160 return new EvolutionInit( 161 (ISeq)in.readObject(), 162 readLong(in) 163 ); 164 } 165 166}