EvolutionDurations.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.2.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 
024 import java.io.Serializable;
025 import java.time.Duration;
026 import java.util.Objects;
027 
028 /**
029  * This class contains timing information about one evolution step.
030  *
031  * @implNote
032  * This class is immutable and thread-safe.
033  *
034  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
035  @since 3.0
036  @version 3.0
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      * Returns a copy of this duration with the specified duration added.
155      <p>
156      * This instance is immutable and unaffected by this method call.
157      *
158      @param other the duration to add
159      @return a {@code EvolutionDurations} based on this duration with the
160      *         specified duration added
161      @throws NullPointerException if the {@code other} duration is {@code null}
162      @throws ArithmeticException if numeric overflow occurs
163      */
164     public EvolutionDurations plus(final EvolutionDurations other) {
165         requireNonNull(other);
166         return of(
167             _offspringSelectionDuration.plus(other._offspringSelectionDuration),
168             _survivorsSelectionDuration.plus(other._survivorsSelectionDuration),
169             _offspringAlterDuration.plus(other._offspringAlterDuration),
170             _offspringFilterDuration.plus(other._offspringFilterDuration),
171             _survivorFilterDuration.plus(other._survivorFilterDuration),
172             _evaluationDuration.plus(other._evaluationDuration),
173             _evolveDuration.plus(other._evolveDuration)
174         );
175     }
176 
177     /**
178      * Compares two durations objects. Only the {@link #getEvolveDuration()}
179      * property is taken into account for the comparison.
180      *
181      @param other the other durations object this object is compared with
182      @return a integer smaller/equal/greater than 0 if the
183      *         {@link #getEvolveDuration()} property of {@code this} object is
184      *         smaller/equal/greater than the corresponding property of the
185      *         {@code other} project.
186      */
187     @Override
188     public int compareTo(final EvolutionDurations other) {
189         return _evolveDuration.compareTo(other._evolveDuration);
190     }
191 
192     @Override
193     public int hashCode() {
194         int hash = 17;
195         hash += 31*Objects.hashCode(_offspringSelectionDuration17;
196         hash += 31*Objects.hashCode(_survivorsSelectionDuration17;
197         hash += 31*Objects.hashCode(_offspringAlterDuration17;
198         hash += 31*Objects.hashCode(_offspringFilterDuration17;
199         hash += 31*Objects.hashCode(_survivorFilterDuration17;
200         hash += 31*Objects.hashCode(_evaluationDuration17;
201         hash += 31*Objects.hashCode(_evolveDuration17;
202         return hash;
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 }