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 agents.anac.y2019.harddealer.math3.genetics;
|
---|
18 |
|
---|
19 | import java.util.concurrent.TimeUnit;
|
---|
20 |
|
---|
21 | import agents.anac.y2019.harddealer.math3.exception.NumberIsTooSmallException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Stops after a fixed amount of time has elapsed.
|
---|
25 | * <p>
|
---|
26 | * The first time {@link #isSatisfied(Population)} is invoked, the end time of the evolution is determined based on the
|
---|
27 | * provided <code>maxTime</code> value. Once the elapsed time reaches the configured <code>maxTime</code> value,
|
---|
28 | * {@link #isSatisfied(Population)} returns true.
|
---|
29 | *
|
---|
30 | * @since 3.1
|
---|
31 | */
|
---|
32 | public class FixedElapsedTime implements StoppingCondition {
|
---|
33 | /** Maximum allowed time period (in nanoseconds). */
|
---|
34 | private final long maxTimePeriod;
|
---|
35 |
|
---|
36 | /** The predetermined termination time (stopping condition). */
|
---|
37 | private long endTime = -1;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create a new {@link FixedElapsedTime} instance.
|
---|
41 | *
|
---|
42 | * @param maxTime maximum number of seconds generations are allowed to evolve
|
---|
43 | * @throws NumberIsTooSmallException if the provided time is < 0
|
---|
44 | */
|
---|
45 | public FixedElapsedTime(final long maxTime) throws NumberIsTooSmallException {
|
---|
46 | this(maxTime, TimeUnit.SECONDS);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Create a new {@link FixedElapsedTime} instance.
|
---|
51 | *
|
---|
52 | * @param maxTime maximum time generations are allowed to evolve
|
---|
53 | * @param unit {@link TimeUnit} of the maxTime argument
|
---|
54 | * @throws NumberIsTooSmallException if the provided time is < 0
|
---|
55 | */
|
---|
56 | public FixedElapsedTime(final long maxTime, final TimeUnit unit) throws NumberIsTooSmallException {
|
---|
57 | if (maxTime < 0) {
|
---|
58 | throw new NumberIsTooSmallException(maxTime, 0, true);
|
---|
59 | }
|
---|
60 | maxTimePeriod = unit.toNanos(maxTime);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Determine whether or not the maximum allowed time has passed.
|
---|
65 | * The termination time is determined after the first generation.
|
---|
66 | *
|
---|
67 | * @param population ignored (no impact on result)
|
---|
68 | * @return <code>true</code> IFF the maximum allowed time period has elapsed
|
---|
69 | */
|
---|
70 | public boolean isSatisfied(final Population population) {
|
---|
71 | if (endTime < 0) {
|
---|
72 | endTime = System.nanoTime() + maxTimePeriod;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return System.nanoTime() >= endTime;
|
---|
76 | }
|
---|
77 | }
|
---|