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