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