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