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