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