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 agents.anac.y2019.harddealer.math3.exception.NumberIsTooSmallException;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Stops after a fixed number of generations. Each time {@link #isSatisfied(Population)} is invoked, a generation
|
---|
23 | * counter is incremented. Once the counter reaches the configured <code>maxGenerations</code> value,
|
---|
24 | * {@link #isSatisfied(Population)} returns true.
|
---|
25 | *
|
---|
26 | * @since 2.0
|
---|
27 | */
|
---|
28 | public class FixedGenerationCount implements StoppingCondition {
|
---|
29 | /** Number of generations that have passed */
|
---|
30 | private int numGenerations = 0;
|
---|
31 |
|
---|
32 | /** Maximum number of generations (stopping criteria) */
|
---|
33 | private final int maxGenerations;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Create a new FixedGenerationCount instance.
|
---|
37 | *
|
---|
38 | * @param maxGenerations number of generations to evolve
|
---|
39 | * @throws NumberIsTooSmallException if the number of generations is < 1
|
---|
40 | */
|
---|
41 | public FixedGenerationCount(final int maxGenerations) throws NumberIsTooSmallException {
|
---|
42 | if (maxGenerations <= 0) {
|
---|
43 | throw new NumberIsTooSmallException(maxGenerations, 1, true);
|
---|
44 | }
|
---|
45 | this.maxGenerations = maxGenerations;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Determine whether or not the given number of generations have passed. Increments the number of generations
|
---|
50 | * counter if the maximum has not been reached.
|
---|
51 | *
|
---|
52 | * @param population ignored (no impact on result)
|
---|
53 | * @return <code>true</code> IFF the maximum number of generations has been exceeded
|
---|
54 | */
|
---|
55 | public boolean isSatisfied(final Population population) {
|
---|
56 | if (this.numGenerations < this.maxGenerations) {
|
---|
57 | numGenerations++;
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 | return true;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Returns the number of generations that have already passed.
|
---|
65 | * @return the number of generations that have passed
|
---|
66 | */
|
---|
67 | public int getNumGenerations() {
|
---|
68 | return numGenerations;
|
---|
69 | }
|
---|
70 |
|
---|
71 | }
|
---|