source: src/main/java/agents/anac/y2019/harddealer/math3/optimization/SimpleValueChecker.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

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