001 /*
002 * Java Genetic Algorithm Library (jenetics-3.5.0).
003 * Copyright (c) 2007-2016 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@gmx.at)
019 */
020 package org.jenetics.engine;
021
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 import static org.jenetics.internal.util.Equality.eq;
025
026 import org.jenetics.internal.util.Hash;
027 import org.jenetics.internal.util.require;
028
029 import org.jenetics.Gene;
030 import org.jenetics.Population;
031
032 /**
033 * Represents a state of the GA at the start of an evolution step.
034 *
035 * @see EvolutionResult
036 *
037 * @param <G> the gene type
038 * @param <C> the fitness type
039 *
040 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
041 * @since 3.1
042 * @version 3.1
043 */
044 public final class EvolutionStart<
045 G extends Gene<?, G>,
046 C extends Comparable<? super C>
047 >
048 {
049
050 private final Population<G, C> _population;
051 private final long _generation;
052
053 private EvolutionStart(
054 final Population<G, C> population,
055 final long generation
056 ) {
057 _population = requireNonNull(population);
058 _generation = require.positive(generation);
059 }
060
061 /**
062 * Return the population before the evolution step.
063 *
064 * @return the start population
065 */
066 public Population<G, C> 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.of(getClass())
082 .and(_population)
083 .and(_generation).value();
084 }
085
086 @Override
087 public boolean equals(final Object obj) {
088 return obj instanceof EvolutionStart<?, ?> &&
089 eq(_generation, ((EvolutionStart<?, ?>)obj)._generation) &&
090 eq(_population, ((EvolutionStart<?, ?>)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 <C> the fitness type
107 * @param population the start population.
108 * @param generation the start generation of the population
109 * @return a new evolution start object
110 * @throws java.lang.NullPointerException if the given {@code population} is
111 * {@code null}.
112 * @throws IllegalArgumentException if the given {@code generation} is
113 * smaller then one
114 */
115 public static <G extends Gene<?, G>, C extends Comparable<? super C>>
116 EvolutionStart<G, C> of(
117 final Population<G, C> population,
118 final long generation
119 ) {
120 return new EvolutionStart<>(population, generation);
121 }
122
123 }
|