EvolutionDurations.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.3.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.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     /**
179      * Compares two durations objects. Only the {@link #getEvolveDuration()}
180      * property is taken into account for the comparison.
181      *
182      @param other the other durations object this object is compared with
183      @return a integer smaller/equal/greater than 0 if the
184      *         {@link #getEvolveDuration()} property of {@code this} object is
185      *         smaller/equal/greater than the corresponding property of the
186      *         {@code other} project.
187      */
188     @Override
189     public int compareTo(final EvolutionDurations other) {
190         return _evolveDuration.compareTo(other._evolveDuration);
191     }
192 
193     @Override
194     public int hashCode() {
195         return
196             hash(_offspringSelectionDuration,
197             hash(_survivorFilterDuration,
198             hash(_offspringAlterDuration,
199             hash(_offspringFilterDuration,
200             hash(_survivorsSelectionDuration,
201             hash(_evaluationDuration,
202             hash(_evolveDuration)))))));
203     }
204 
205     @Override
206     public boolean equals(final Object obj) {
207         return obj == this ||
208             obj instanceof EvolutionDurations &&
209             Objects.equals(_offspringSelectionDuration,
210                 ((EvolutionDurations)obj)._offspringSelectionDuration&&
211             Objects.equals(_survivorsSelectionDuration,
212                 ((EvolutionDurations)obj)._survivorsSelectionDuration&&
213             Objects.equals(_offspringAlterDuration,
214                 ((EvolutionDurations)obj)._offspringAlterDuration&&
215             Objects.equals(_offspringFilterDuration,
216                 ((EvolutionDurations)obj)._offspringFilterDuration&&
217             Objects.equals(_survivorFilterDuration,
218                 ((EvolutionDurations)obj)._survivorFilterDuration&&
219             Objects.equals(_evaluationDuration,
220                 ((EvolutionDurations)obj)._evaluationDuration&&
221             Objects.equals(_evolveDuration,
222                 ((EvolutionDurations)obj)._evolveDuration);
223     }
224 
225     /**
226      * Return an new {@code EvolutionDurations} object with the given values.
227      *
228      @param offspringSelectionDuration the duration needed for selecting the
229      *        offspring population
230      @param survivorsSelectionDuration the duration needed for selecting the
231      *        survivors population
232      @param offspringAlterDuration the duration needed for altering the
233      *        offspring population
234      @param offspringFilterDuration the duration needed for removing and
235      *        replacing invalid offspring individuals
236      @param survivorFilterDuration the duration needed for removing and
237      *        replacing old and invalid survivor individuals
238      @param evaluationDuration the duration needed for evaluating the fitness
239      *        function of the new individuals
240      @param evolveDuration the duration needed for the whole evolve step
241      @return an new durations object
242      @throws NullPointerException if one of the arguments is
243      *         {@code null}
244      */
245     public static EvolutionDurations of(
246         final Duration offspringSelectionDuration,
247         final Duration survivorsSelectionDuration,
248         final Duration offspringAlterDuration,
249         final Duration offspringFilterDuration,
250         final Duration survivorFilterDuration,
251         final Duration evaluationDuration,
252         final Duration evolveDuration
253     ) {
254         return new EvolutionDurations(
255             offspringSelectionDuration,
256             survivorsSelectionDuration,
257             offspringAlterDuration,
258             offspringFilterDuration,
259             survivorFilterDuration,
260             evaluationDuration,
261             evolveDuration
262         );
263     }
264 
265 }