EvolutionInit.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.1.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 
025 import java.util.Objects;
026 
027 import io.jenetics.Gene;
028 import io.jenetics.Genotype;
029 import io.jenetics.internal.util.require;
030 import io.jenetics.util.ISeq;
031 
032 /**
033  * Represents the initialization value of an evolution stream/iterator.
034  *
035  @see EvolutionStart
036  @see EvolutionStreamable#stream(EvolutionInit)
037  @see Engine#iterator(EvolutionInit)
038  *
039  @param <G> the gene type
040  *
041  * @implSpec
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         int hash = 17;
082         hash += 31*_generation + 17;
083         hash += 31*Objects.hashCode(_population17;
084         return hash;
085     }
086 
087     @Override
088     public boolean equals(final Object obj) {
089         return obj == this ||
090             obj instanceof EvolutionInit<?> &&
091             _generation == ((EvolutionInit<?>)obj)._generation &&
092             Objects.equals(_population, ((EvolutionInit<?>)obj)._population);
093     }
094 
095     @Override
096     public String toString() {
097         return format(
098             "EvolutionStart[population-size=%d, generation=%d]",
099             _population.size(), _generation
100         );
101     }
102 
103     /**
104      * Create a new evolution start object with the given population and for the
105      * given generation.
106      *
107      @param <G> the gene type
108      @param population the start population.
109      @param generation the start generation of the population
110      @return a new evolution start object
111      @throws java.lang.NullPointerException if the given {@code population} is
112      *         {@code null}.
113      @throws IllegalArgumentException if the given {@code generation} is
114      *         smaller then one
115      */
116     public static <G extends Gene<?, G>>
117     EvolutionInit<G> of(
118         final ISeq<Genotype<G>> population,
119         final long generation
120     ) {
121         return new EvolutionInit<>(population, generation);
122     }
123 
124 }