FixedElapsedTime.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.genetics;

  18. import java.util.concurrent.TimeUnit;

  19. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;

  20. /**
  21.  * Stops after a fixed amount of time has elapsed.
  22.  * <p>
  23.  * The first time {@link #isSatisfied(Population)} is invoked, the end time of
  24.  * the evolution is determined based on the provided <code>maxTime</code> value.
  25.  * Once the elapsed time reaches the configured <code>maxTime</code> value,
  26.  * {@link #isSatisfied(Population)} returns true.
  27.  *
  28.  * @since 3.1
  29.  */
  30. public class FixedElapsedTime implements StoppingCondition {
  31.     /** Maximum allowed time period (in nanoseconds). */
  32.     private final long maxTimePeriod;

  33.     /** The predetermined termination time (stopping condition). */
  34.     private long endTime = -1;

  35.     /**
  36.      * Create a new {@link FixedElapsedTime} instance.
  37.      *
  38.      * @param maxTime maximum number of seconds generations are allowed to evolve
  39.      * @throws NumberIsTooSmallException if the provided time is &lt; 0
  40.      */
  41.     public FixedElapsedTime(final long maxTime) throws NumberIsTooSmallException {
  42.         this(maxTime, TimeUnit.SECONDS);
  43.     }

  44.     /**
  45.      * Create a new {@link FixedElapsedTime} instance.
  46.      *
  47.      * @param maxTime maximum time generations are allowed to evolve
  48.      * @param unit {@link TimeUnit} of the maxTime argument
  49.      * @throws NumberIsTooSmallException if the provided time is &lt; 0
  50.      */
  51.     public FixedElapsedTime(final long maxTime, final TimeUnit unit) throws NumberIsTooSmallException {
  52.         if (maxTime < 0) {
  53.             throw new NumberIsTooSmallException(maxTime, 0, true);
  54.         }
  55.         maxTimePeriod = unit.toNanos(maxTime);
  56.     }

  57.     /**
  58.      * Determine whether or not the maximum allowed time has passed.
  59.      * The termination time is determined after the first generation.
  60.      *
  61.      * @param population ignored (no impact on result)
  62.      * @return <code>true</code> IFF the maximum allowed time period has elapsed
  63.      */
  64.     @Override
  65.     public boolean isSatisfied(final Population population) {
  66.         if (endTime < 0) {
  67.             endTime = System.nanoTime() + maxTimePeriod;
  68.         }

  69.         return System.nanoTime() >= endTime;
  70.     }
  71. }