001 /*
002 * Java Genetic Algorithm Library (jenetics-4.3.0).
003 * Copyright (c) 2007-2018 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 java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 import static io.jenetics.internal.util.Hashes.hash;
025
026 import java.util.Objects;
027
028 import io.jenetics.Gene;
029 import io.jenetics.Phenotype;
030 import io.jenetics.internal.util.require;
031 import io.jenetics.util.ISeq;
032
033 /**
034 * Represents a state of the GA at the start of an evolution step.
035 *
036 * @see EvolutionResult
037 * @see EvolutionInit
038 * @see EvolutionStreamable#stream(EvolutionStart)
039 * @see EvolutionIterable#iterator(EvolutionStart)
040 *
041 * @param <G> the gene type
042 * @param <C> the fitness type
043 *
044 * @implNote
045 * This class is immutable and thread-safe.
046 *
047 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
048 * @since 3.1
049 * @version 4.0
050 */
051 public final class EvolutionStart<
052 G extends Gene<?, G>,
053 C extends Comparable<? super C>
054 >
055 {
056
057 private final ISeq<Phenotype<G, C>> _population;
058 private final long _generation;
059
060 private EvolutionStart(
061 final ISeq<Phenotype<G, C>> population,
062 final long generation
063 ) {
064 _population = requireNonNull(population);
065 _generation = require.positive(generation);
066 }
067
068 /**
069 * Return the population before the evolution step.
070 *
071 * @return the start population
072 */
073 public ISeq<Phenotype<G, C>> getPopulation() {
074 return _population;
075 }
076
077 /**
078 * Return the generation of the start population.
079 *
080 * @return the start generation
081 */
082 public long getGeneration() {
083 return _generation;
084 }
085
086 @Override
087 public int hashCode() {
088 return hash(_generation, hash(_population, hash(getClass())));
089 }
090
091 @Override
092 public boolean equals(final Object obj) {
093 return obj == this ||
094 obj instanceof EvolutionStart &&
095 _generation == ((EvolutionStart)obj)._generation &&
096 Objects.equals(_population, ((EvolutionStart)obj)._population);
097 }
098
099 @Override
100 public String toString() {
101 return format(
102 "EvolutionStart[population-size=%d, generation=%d]",
103 _population.size(), _generation
104 );
105 }
106
107 /**
108 * Create a new evolution start object with the given population and for the
109 * given generation.
110 *
111 * @param <G> the gene type
112 * @param <C> the fitness type
113 * @param population the start population.
114 * @param generation the start generation of the population
115 * @return a new evolution start object
116 * @throws java.lang.NullPointerException if the given {@code population} is
117 * {@code null}.
118 * @throws IllegalArgumentException if the given {@code generation} is
119 * smaller then one
120 */
121 public static <G extends Gene<?, G>, C extends Comparable<? super C>>
122 EvolutionStart<G, C> of(
123 final ISeq<Phenotype<G, C>> population,
124 final long generation
125 ) {
126 return new EvolutionStart<>(population, generation);
127 }
128
129 }
|