001 /*
002 * Java Genetic Algorithm Library (jenetics-3.0.0).
003 * Copyright (c) 2007-2014 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.util.Objects.requireNonNull;
023 import static org.jenetics.internal.util.Equality.eq;
024
025 import java.io.Serializable;
026 import java.time.Duration;
027
028 import org.jenetics.internal.util.Equality;
029 import org.jenetics.internal.util.Hash;
030
031 /**
032 * This class contains timing information about one evolution step.
033 *
034 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
035 * @since 3.0
036 * @version 3.0 — <em>$Date: 2014-12-28 $</em>
037 */
038 public final class EvolutionDurations
039 implements
040 Comparable<EvolutionDurations>,
041 Serializable
042 {
043 private static final long serialVersionUID = 1L;
044
045 /**
046 * Constant for zero evolution durations.
047 */
048 public static final EvolutionDurations ZERO = EvolutionDurations.of(
049 Duration.ZERO,
050 Duration.ZERO,
051 Duration.ZERO,
052 Duration.ZERO,
053 Duration.ZERO,
054 Duration.ZERO,
055 Duration.ZERO
056 );
057
058 private final Duration _offspringSelectionDuration;
059 private final Duration _survivorsSelectionDuration;
060 private final Duration _offspringAlterDuration;
061 private final Duration _offspringFilterDuration;
062 private final Duration _survivorFilterDuration;
063 private final Duration _evaluationDuration;
064 private final Duration _evolveDuration;
065
066 EvolutionDurations(
067 final Duration offspringSelectionDuration,
068 final Duration survivorsSelectionDuration,
069 final Duration offspringAlterDuration,
070 final Duration offspringFilterDuration,
071 final Duration survivorFilterDuration,
072 final Duration evaluationDuration,
073 final Duration evolveDuration
074 ) {
075 _offspringSelectionDuration = requireNonNull(offspringSelectionDuration);
076 _survivorsSelectionDuration = requireNonNull(survivorsSelectionDuration);
077 _offspringAlterDuration = requireNonNull(offspringAlterDuration);
078 _offspringFilterDuration = requireNonNull(offspringFilterDuration);
079 _survivorFilterDuration = requireNonNull(survivorFilterDuration);
080 _evaluationDuration = requireNonNull(evaluationDuration);
081 _evolveDuration = requireNonNull(evolveDuration);
082 }
083
084 /**
085 * Return the duration needed for selecting the offspring population.
086 *
087 * @return the duration needed for selecting the offspring population
088 */
089 public Duration getOffspringSelectionDuration() {
090 return _offspringSelectionDuration;
091 }
092
093 /**
094 * Return the duration needed for selecting the survivors population.
095 *
096 * @return the duration needed for selecting the survivors population
097 */
098 public Duration getSurvivorsSelectionDuration() {
099 return _survivorsSelectionDuration;
100 }
101
102 /**
103 * Return the duration needed for altering the offspring population.
104 *
105 * @return the duration needed for altering the offspring population
106 */
107 public Duration getOffspringAlterDuration() {
108 return _offspringAlterDuration;
109 }
110
111 /**
112 * Return the duration needed for removing and replacing invalid offspring
113 * individuals.
114 *
115 * @return the duration needed for removing and replacing invalid offspring
116 * individuals
117 */
118 public Duration getOffspringFilterDuration() {
119 return _offspringFilterDuration;
120 }
121
122 /**
123 * Return the duration needed for removing and replacing old and invalid
124 * survivor individuals.
125 *
126 * @return the duration needed for removing and replacing old and invalid
127 * survivor individuals
128 */
129 public Duration getSurvivorFilterDuration() {
130 return _survivorFilterDuration;
131 }
132
133 /**
134 * Return the duration needed for evaluating the fitness function of the new
135 * individuals.
136 *
137 * @return the duration needed for evaluating the fitness function of the new
138 * individuals
139 */
140 public Duration getEvaluationDuration() {
141 return _evaluationDuration;
142 }
143
144 /**
145 * Return the duration needed for the whole evolve step.
146 *
147 * @return the duration needed for the whole evolve step
148 */
149 public Duration getEvolveDuration() {
150 return _evolveDuration;
151 }
152
153 /**
154 * Compares two durations objects. Only the {@link #getEvolveDuration()}
155 * property is taken into account for the comparison.
156 *
157 * @param other the other durations object this object is compared with
158 * @return a integer smaller/equal/greater than 0 if the
159 * {@link #getEvolveDuration()} property of {@code this} object is
160 * smaller/equal/greater than the corresponding property of the
161 * {@code other} project.
162 */
163 @Override
164 public int compareTo(final EvolutionDurations other) {
165 return _evolveDuration.compareTo(other._evolveDuration);
166 }
167
168 @Override
169 public int hashCode() {
170 return Hash.of(getClass())
171 .and(_offspringSelectionDuration)
172 .and(_survivorsSelectionDuration)
173 .and(_offspringAlterDuration)
174 .and(_offspringFilterDuration)
175 .and(_survivorFilterDuration)
176 .and(_evaluationDuration)
177 .and(_evolveDuration).value();
178 }
179
180 @Override
181 public boolean equals(final Object obj) {
182 return Equality.of(this, obj).test(d ->
183 eq(_offspringSelectionDuration, d._offspringSelectionDuration) &&
184 eq(_survivorsSelectionDuration, d._survivorsSelectionDuration) &&
185 eq(_offspringAlterDuration, d._offspringAlterDuration) &&
186 eq(_offspringFilterDuration, d._offspringFilterDuration) &&
187 eq(_survivorFilterDuration, d._survivorFilterDuration) &&
188 eq(_evaluationDuration, d._evaluationDuration) &&
189 eq(_evolveDuration, d._evolveDuration)
190 );
191 }
192
193 /**
194 * Return an new {@code EvolutionDurations} object with the given values.
195 *
196 * @param offspringSelectionDuration the duration needed for selecting the
197 * offspring population
198 * @param survivorsSelectionDuration the duration needed for selecting the
199 * survivors population
200 * @param offspringAlterDuration the duration needed for altering the
201 * offspring population
202 * @param offspringFilterDuration the duration needed for removing and
203 * replacing invalid offspring individuals
204 * @param survivorFilterDuration the duration needed for removing and
205 * replacing old and invalid survivor individuals
206 * @param evaluationDuration the duration needed for evaluating the fitness
207 * function of the new individuals
208 * @param evolveDuration the duration needed for the whole evolve step
209 * @return an new durations object
210 * @throws NullPointerException if one of the arguments is
211 * {@code null}
212 */
213 public static EvolutionDurations of(
214 final Duration offspringSelectionDuration,
215 final Duration survivorsSelectionDuration,
216 final Duration offspringAlterDuration,
217 final Duration offspringFilterDuration,
218 final Duration survivorFilterDuration,
219 final Duration evaluationDuration,
220 final Duration evolveDuration
221 ) {
222 return new EvolutionDurations(
223 offspringSelectionDuration,
224 survivorsSelectionDuration,
225 offspringAlterDuration,
226 offspringFilterDuration,
227 survivorFilterDuration,
228 evaluationDuration,
229 evolveDuration
230 );
231 }
232
233 }
|