EvolutionDurations.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.1.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.util.Objects.requireNonNull;
023 import static io.jenetics.internal.util.Hashes.hash;
024 
025 import java.io.Serializable;
026 import java.time.Duration;
027 import java.util.Objects;
028 
029 /**
030  * This class contains timing information about one evolution step.
031  *
032  * @implNote
033  * This class is immutable and thread-safe.
034  *
035  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
036  @since 3.0
037  @version 3.0
038  */
039 public final class EvolutionDurations
040     implements
041         Comparable<EvolutionDurations>,
042         Serializable
043 {
044     private static final long serialVersionUID = 1L;
045 
046     /**
047      * Constant for zero evolution durations.
048      */
049     public static final EvolutionDurations ZERO = EvolutionDurations.of(
050         Duration.ZERO,
051         Duration.ZERO,
052         Duration.ZERO,
053         Duration.ZERO,
054         Duration.ZERO,
055         Duration.ZERO,
056         Duration.ZERO
057     );
058 
059     private final Duration _offspringSelectionDuration;
060     private final Duration _survivorsSelectionDuration;
061     private final Duration _offspringAlterDuration;
062     private final Duration _offspringFilterDuration;
063     private final Duration _survivorFilterDuration;
064     private final Duration _evaluationDuration;
065     private final Duration _evolveDuration;
066 
067     EvolutionDurations(
068         final Duration offspringSelectionDuration,
069         final Duration survivorsSelectionDuration,
070         final Duration offspringAlterDuration,
071         final Duration offspringFilterDuration,
072         final Duration survivorFilterDuration,
073         final Duration evaluationDuration,
074         final Duration evolveDuration
075     ) {
076         _offspringSelectionDuration = requireNonNull(offspringSelectionDuration);
077         _survivorsSelectionDuration = requireNonNull(survivorsSelectionDuration);
078         _offspringAlterDuration = requireNonNull(offspringAlterDuration);
079         _offspringFilterDuration = requireNonNull(offspringFilterDuration);
080         _survivorFilterDuration = requireNonNull(survivorFilterDuration);
081         _evaluationDuration = requireNonNull(evaluationDuration);
082         _evolveDuration = requireNonNull(evolveDuration);
083     }
084 
085     /**
086      * Return the duration needed for selecting the offspring population.
087      *
088      @return the duration needed for selecting the offspring population
089      */
090     public Duration getOffspringSelectionDuration() {
091         return _offspringSelectionDuration;
092     }
093 
094     /**
095      * Return the duration needed for selecting the survivors population.
096      *
097      @return the duration needed for selecting the survivors population
098      */
099     public Duration getSurvivorsSelectionDuration() {
100         return _survivorsSelectionDuration;
101     }
102 
103     /**
104      * Return the duration needed for altering the offspring population.
105      *
106      @return the duration needed for altering the offspring population
107      */
108     public Duration getOffspringAlterDuration() {
109         return _offspringAlterDuration;
110     }
111 
112     /**
113      * Return the duration needed for removing and replacing invalid offspring
114      * individuals.
115      *
116      @return the duration needed for removing and replacing invalid offspring
117      *         individuals
118      */
119     public Duration getOffspringFilterDuration() {
120         return _offspringFilterDuration;
121     }
122 
123     /**
124      * Return the duration needed for removing and replacing old and invalid
125      * survivor individuals.
126      *
127      @return the duration needed for removing and replacing old and invalid
128      *         survivor individuals
129      */
130     public Duration getSurvivorFilterDuration() {
131         return _survivorFilterDuration;
132     }
133 
134     /**
135      * Return the duration needed for evaluating the fitness function of the new
136      * individuals.
137      *
138      @return the duration needed for evaluating the fitness function of the new
139      *         individuals
140      */
141     public Duration getEvaluationDuration() {
142         return _evaluationDuration;
143     }
144 
145     /**
146      * Return the duration needed for the whole evolve step.
147      *
148      @return the duration needed for the whole evolve step
149      */
150     public Duration getEvolveDuration() {
151         return _evolveDuration;
152     }
153 
154     /**
155      * Returns a copy of this duration with the specified duration added.
156      <p>
157      * This instance is immutable and unaffected by this method call.
158      *
159      @param other the duration to add
160      @return a {@code EvolutionDurations} based on this duration with the
161      *         specified duration added
162      @throws NullPointerException if the {@code other} duration is {@code null}
163      @throws ArithmeticException if numeric overflow occurs
164      */
165     public EvolutionDurations plus(final EvolutionDurations other) {
166         requireNonNull(other);
167         return of(
168             _offspringSelectionDuration.plus(other._offspringSelectionDuration),
169             _survivorsSelectionDuration.plus(other._survivorsSelectionDuration),
170             _offspringAlterDuration.plus(other._offspringAlterDuration),
171             _offspringFilterDuration.plus(other._offspringFilterDuration),
172             _survivorFilterDuration.plus(other._survivorFilterDuration),
173             _evaluationDuration.plus(other._evaluationDuration),
174             _evolveDuration.plus(other._evolveDuration)
175         );
176     }
177 
178     EvolutionDurations plusEvaluation(final Duration duration) {
179         return of(
180             _offspringSelectionDuration,
181             _survivorsSelectionDuration,
182             _offspringAlterDuration,
183             _offspringFilterDuration,
184             _survivorFilterDuration,
185             _evaluationDuration.plus(duration),
186             _evolveDuration
187         );
188     }
189 
190     EvolutionDurations plusEvolve(final Duration duration) {
191         return of(
192             _offspringSelectionDuration,
193             _survivorsSelectionDuration,
194             _offspringAlterDuration,
195             _offspringFilterDuration,
196             _survivorFilterDuration,
197             _evaluationDuration,
198             _evolveDuration.plus(duration)
199         );
200     }
201 
202     /**
203      * Compares two durations objects. Only the {@link #getEvolveDuration()}
204      * property is taken into account for the comparison.
205      *
206      @param other the other durations object this object is compared with
207      @return a integer smaller/equal/greater than 0 if the
208      *         {@link #getEvolveDuration()} property of {@code this} object is
209      *         smaller/equal/greater than the corresponding property of the
210      *         {@code other} project.
211      */
212     @Override
213     public int compareTo(final EvolutionDurations other) {
214         return _evolveDuration.compareTo(other._evolveDuration);
215     }
216 
217     @Override
218     public int hashCode() {
219         return
220             hash(_offspringSelectionDuration,
221             hash(_survivorFilterDuration,
222             hash(_offspringAlterDuration,
223             hash(_offspringFilterDuration,
224             hash(_survivorsSelectionDuration,
225             hash(_evaluationDuration,
226             hash(_evolveDuration)))))));
227     }
228 
229     @Override
230     public boolean equals(final Object obj) {
231         return obj == this ||
232             obj instanceof EvolutionDurations &&
233             Objects.equals(_offspringSelectionDuration,
234                 ((EvolutionDurations)obj)._offspringSelectionDuration&&
235             Objects.equals(_survivorsSelectionDuration,
236                 ((EvolutionDurations)obj)._survivorsSelectionDuration&&
237             Objects.equals(_offspringAlterDuration,
238                 ((EvolutionDurations)obj)._offspringAlterDuration&&
239             Objects.equals(_offspringFilterDuration,
240                 ((EvolutionDurations)obj)._offspringFilterDuration&&
241             Objects.equals(_survivorFilterDuration,
242                 ((EvolutionDurations)obj)._survivorFilterDuration&&
243             Objects.equals(_evaluationDuration,
244                 ((EvolutionDurations)obj)._evaluationDuration&&
245             Objects.equals(_evolveDuration,
246                 ((EvolutionDurations)obj)._evolveDuration);
247     }
248 
249     /**
250      * Return an new {@code EvolutionDurations} object with the given values.
251      *
252      @param offspringSelectionDuration the duration needed for selecting the
253      *        offspring population
254      @param survivorsSelectionDuration the duration needed for selecting the
255      *        survivors population
256      @param offspringAlterDuration the duration needed for altering the
257      *        offspring population
258      @param offspringFilterDuration the duration needed for removing and
259      *        replacing invalid offspring individuals
260      @param survivorFilterDuration the duration needed for removing and
261      *        replacing old and invalid survivor individuals
262      @param evaluationDuration the duration needed for evaluating the fitness
263      *        function of the new individuals
264      @param evolveDuration the duration needed for the whole evolve step
265      @return an new durations object
266      @throws NullPointerException if one of the arguments is
267      *         {@code null}
268      */
269     public static EvolutionDurations of(
270         final Duration offspringSelectionDuration,
271         final Duration survivorsSelectionDuration,
272         final Duration offspringAlterDuration,
273         final Duration offspringFilterDuration,
274         final Duration survivorFilterDuration,
275         final Duration evaluationDuration,
276         final Duration evolveDuration
277     ) {
278         return new EvolutionDurations(
279             offspringSelectionDuration,
280             survivorsSelectionDuration,
281             offspringAlterDuration,
282             offspringFilterDuration,
283             survivorFilterDuration,
284             evaluationDuration,
285             evolveDuration
286         );
287     }
288 
289 }