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.optim.univariate;
|
---|
18 |
|
---|
19 | import agents.anac.y2019.harddealer.math3.util.FastMath;
|
---|
20 | import agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException;
|
---|
21 | import agents.anac.y2019.harddealer.math3.optim.AbstractConvergenceChecker;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Simple implementation of the
|
---|
25 | * {@link agents.anac.y2019.harddealer.math3.optimization.ConvergenceChecker} interface
|
---|
26 | * that uses only objective function values.
|
---|
27 | *
|
---|
28 | * Convergence is considered to have been reached if either the relative
|
---|
29 | * difference between the objective function values is smaller than a
|
---|
30 | * threshold or if either the absolute difference between the objective
|
---|
31 | * function values is smaller than another threshold.
|
---|
32 | * <br/>
|
---|
33 | * The {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)
|
---|
34 | * converged} method will also return {@code true} if the number of iterations
|
---|
35 | * has been set (see {@link #SimpleUnivariateValueChecker(double,double,int)
|
---|
36 | * this constructor}).
|
---|
37 | *
|
---|
38 | * @since 3.1
|
---|
39 | */
|
---|
40 | public class SimpleUnivariateValueChecker
|
---|
41 | extends AbstractConvergenceChecker<UnivariatePointValuePair> {
|
---|
42 | /**
|
---|
43 | * If {@link #maxIterationCount} is set to this value, the number of
|
---|
44 | * iterations will never cause
|
---|
45 | * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
|
---|
46 | * to return {@code true}.
|
---|
47 | */
|
---|
48 | private static final int ITERATION_CHECK_DISABLED = -1;
|
---|
49 | /**
|
---|
50 | * Number of iterations after which the
|
---|
51 | * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
|
---|
52 | * method will return true (unless the check is disabled).
|
---|
53 | */
|
---|
54 | private final int maxIterationCount;
|
---|
55 |
|
---|
56 | /** Build an instance with specified thresholds.
|
---|
57 | *
|
---|
58 | * In order to perform only relative checks, the absolute tolerance
|
---|
59 | * must be set to a negative value. In order to perform only absolute
|
---|
60 | * checks, the relative tolerance must be set to a negative value.
|
---|
61 | *
|
---|
62 | * @param relativeThreshold relative tolerance threshold
|
---|
63 | * @param absoluteThreshold absolute tolerance threshold
|
---|
64 | */
|
---|
65 | public SimpleUnivariateValueChecker(final double relativeThreshold,
|
---|
66 | final double absoluteThreshold) {
|
---|
67 | super(relativeThreshold, absoluteThreshold);
|
---|
68 | maxIterationCount = ITERATION_CHECK_DISABLED;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Builds an instance with specified thresholds.
|
---|
73 | *
|
---|
74 | * In order to perform only relative checks, the absolute tolerance
|
---|
75 | * must be set to a negative value. In order to perform only absolute
|
---|
76 | * checks, the relative tolerance must be set to a negative value.
|
---|
77 | *
|
---|
78 | * @param relativeThreshold relative tolerance threshold
|
---|
79 | * @param absoluteThreshold absolute tolerance threshold
|
---|
80 | * @param maxIter Maximum iteration count.
|
---|
81 | * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
|
---|
82 | *
|
---|
83 | * @since 3.1
|
---|
84 | */
|
---|
85 | public SimpleUnivariateValueChecker(final double relativeThreshold,
|
---|
86 | final double absoluteThreshold,
|
---|
87 | final int maxIter) {
|
---|
88 | super(relativeThreshold, absoluteThreshold);
|
---|
89 |
|
---|
90 | if (maxIter <= 0) {
|
---|
91 | throw new NotStrictlyPositiveException(maxIter);
|
---|
92 | }
|
---|
93 | maxIterationCount = maxIter;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Check if the optimization algorithm has converged considering the
|
---|
98 | * last two points.
|
---|
99 | * This method may be called several time from the same algorithm
|
---|
100 | * iteration with different points. This can be detected by checking the
|
---|
101 | * iteration number at each call if needed. Each time this method is
|
---|
102 | * called, the previous and current point correspond to points with the
|
---|
103 | * same role at each iteration, so they can be compared. As an example,
|
---|
104 | * simplex-based algorithms call this method for all points of the simplex,
|
---|
105 | * not only for the best or worst ones.
|
---|
106 | *
|
---|
107 | * @param iteration Index of current iteration
|
---|
108 | * @param previous Best point in the previous iteration.
|
---|
109 | * @param current Best point in the current iteration.
|
---|
110 | * @return {@code true} if the algorithm has converged.
|
---|
111 | */
|
---|
112 | @Override
|
---|
113 | public boolean converged(final int iteration,
|
---|
114 | final UnivariatePointValuePair previous,
|
---|
115 | final UnivariatePointValuePair current) {
|
---|
116 | if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 |
|
---|
120 | final double p = previous.getValue();
|
---|
121 | final double c = current.getValue();
|
---|
122 | final double difference = FastMath.abs(p - c);
|
---|
123 | final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
|
---|
124 | return difference <= size * getRelativeThreshold() ||
|
---|
125 | difference <= getAbsoluteThreshold();
|
---|
126 | }
|
---|
127 | }
|
---|