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 * https://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 18 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 23 package org.hipparchus.optim; 24 25 import org.hipparchus.exception.LocalizedCoreFormats; 26 import org.hipparchus.exception.MathIllegalArgumentException; 27 import org.hipparchus.util.FastMath; 28 29 /** 30 * Simple implementation of the {@link ConvergenceChecker} interface using 31 * only objective function values. 32 * 33 * Convergence is considered to have been reached if either the relative 34 * difference between the objective function values is smaller than a 35 * threshold or if either the absolute difference between the objective 36 * function values is smaller than another threshold for all vectors elements. 37 * <br> 38 * The {@link #converged(int,PointVectorValuePair,PointVectorValuePair) converged} 39 * method will also return {@code true} if the number of iterations has been set 40 * (see {@link #SimpleVectorValueChecker(double,double,int) this constructor}). 41 * 42 */ 43 public class SimpleVectorValueChecker 44 extends AbstractConvergenceChecker<PointVectorValuePair> { 45 /** 46 * If {@link #maxIterationCount} is set to this value, the number of 47 * iterations will never cause 48 * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)} 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,PointVectorValuePair,PointVectorValuePair)} method 55 * will return true (unless the check is disabled). 56 */ 57 private final int maxIterationCount; 58 59 /** 60 * Build an instance with specified thresholds. 61 * 62 * In order to perform only relative checks, the absolute tolerance 63 * must be set to a negative value. In order to perform only absolute 64 * checks, the relative tolerance must be set to a negative value. 65 * 66 * @param relativeThreshold relative tolerance threshold 67 * @param absoluteThreshold absolute tolerance threshold 68 */ 69 public SimpleVectorValueChecker(final double relativeThreshold, 70 final double absoluteThreshold) { 71 super(relativeThreshold, absoluteThreshold); 72 maxIterationCount = ITERATION_CHECK_DISABLED; 73 } 74 75 /** 76 * Builds an instance with specified tolerance thresholds and 77 * iteration count. 78 * 79 * In order to perform only relative checks, the absolute tolerance 80 * must be set to a negative value. In order to perform only absolute 81 * checks, the relative tolerance must be set to a negative value. 82 * 83 * @param relativeThreshold Relative tolerance threshold. 84 * @param absoluteThreshold Absolute tolerance threshold. 85 * @param maxIter Maximum iteration count. 86 * @throws MathIllegalArgumentException if {@code maxIter <= 0}. 87 * 88 */ 89 public SimpleVectorValueChecker(final double relativeThreshold, 90 final double absoluteThreshold, 91 final int maxIter) { 92 super(relativeThreshold, absoluteThreshold); 93 94 if (maxIter <= 0) { 95 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED, 96 maxIter, 0); 97 } 98 maxIterationCount = maxIter; 99 } 100 101 /** 102 * Check if the optimization algorithm has converged considering the 103 * last two points. 104 * This method may be called several times from the same algorithm 105 * iteration with different points. This can be detected by checking the 106 * iteration number at each call if needed. Each time this method is 107 * called, the previous and current point correspond to points with the 108 * same role at each iteration, so they can be compared. As an example, 109 * simplex-based algorithms call this method for all points of the simplex, 110 * not only for the best or worst ones. 111 * 112 * @param iteration Index of current iteration 113 * @param previous Best point in the previous iteration. 114 * @param current Best point in the current iteration. 115 * @return {@code true} if the arguments satify the convergence criterion. 116 */ 117 @Override 118 public boolean converged(final int iteration, 119 final PointVectorValuePair previous, 120 final PointVectorValuePair current) { 121 if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { 122 return true; 123 } 124 125 final double[] p = previous.getValueRef(); 126 final double[] c = current.getValueRef(); 127 for (int i = 0; i < p.length; ++i) { 128 final double pi = p[i]; 129 final double ci = c[i]; 130 final double difference = FastMath.abs(pi - ci); 131 final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci)); 132 if (difference > size * getRelativeThreshold() && 133 difference > getAbsoluteThreshold()) { 134 return false; 135 } 136 } 137 return true; 138 } 139 }