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