001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 */
017package org.apache.commons.math3.genetics;
018
019import java.util.concurrent.TimeUnit;
020
021import org.apache.commons.math3.exception.NumberIsTooSmallException;
022
023/**
024 * Stops after a fixed amount of time has elapsed.
025 * <p>
026 * The first time {@link #isSatisfied(Population)} is invoked, the end time of the evolution is determined based on the
027 * provided <code>maxTime</code> value. Once the elapsed time reaches the configured <code>maxTime</code> value,
028 * {@link #isSatisfied(Population)} returns true.
029 *
030 * @since 3.1
031 */
032public class FixedElapsedTime implements StoppingCondition {
033    /** Maximum allowed time period (in nanoseconds). */
034    private final long maxTimePeriod;
035
036    /** The predetermined termination time (stopping condition). */
037    private long endTime = -1;
038
039    /**
040     * Create a new {@link FixedElapsedTime} instance.
041     *
042     * @param maxTime maximum number of seconds generations are allowed to evolve
043     * @throws NumberIsTooSmallException if the provided time is &lt; 0
044     */
045    public FixedElapsedTime(final long maxTime) throws NumberIsTooSmallException {
046        this(maxTime, TimeUnit.SECONDS);
047    }
048
049    /**
050     * Create a new {@link FixedElapsedTime} instance.
051     *
052     * @param maxTime maximum time generations are allowed to evolve
053     * @param unit {@link TimeUnit} of the maxTime argument
054     * @throws NumberIsTooSmallException if the provided time is &lt; 0
055     */
056    public FixedElapsedTime(final long maxTime, final TimeUnit unit) throws NumberIsTooSmallException {
057        if (maxTime < 0) {
058            throw new NumberIsTooSmallException(maxTime, 0, true);
059        }
060        maxTimePeriod = unit.toNanos(maxTime);
061    }
062
063    /**
064     * Determine whether or not the maximum allowed time has passed.
065     * The termination time is determined after the first generation.
066     *
067     * @param population ignored (no impact on result)
068     * @return <code>true</code> IFF the maximum allowed time period has elapsed
069     */
070    public boolean isSatisfied(final Population population) {
071        if (endTime < 0) {
072            endTime = System.nanoTime() + maxTimePeriod;
073        }
074
075        return System.nanoTime() >= endTime;
076    }
077}