001 /*
002 * Java Genetic Algorithm Library (jenetics-3.9.0).
003 * Copyright (c) 2007-2017 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
025 import java.util.Objects;
026
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 int hash = 17;
082 hash += 31*_generation + 17;
083 hash += 31*Objects.hashCode(_population) + 17;
084 return hash;
085 }
086
087 @Override
088 public boolean equals(final Object obj) {
089 return obj instanceof EvolutionStart<?, ?> &&
090 _generation == ((EvolutionStart<?, ?>)obj)._generation &&
091 Objects.equals(_population, ((EvolutionStart<?, ?>)obj)._population);
092 }
093
094 @Override
095 public String toString() {
096 return format(
097 "EvolutionStart[population-size=%d, generation=%d]",
098 _population.size(), _generation
099 );
100 }
101
102 /**
103 * Create a new evolution start object with the given population and for the
104 * given generation.
105 *
106 * @param <G> the gene type
107 * @param <C> the fitness 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>, C extends Comparable<? super C>>
117 EvolutionStart<G, C> of(
118 final Population<G, C> population,
119 final long generation
120 ) {
121 return new EvolutionStart<>(population, generation);
122 }
123
124 }
|