001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.math3.optimization; 019 020import org.apache.commons.math3.util.FastMath; 021import org.apache.commons.math3.exception.NotStrictlyPositiveException; 022 023/** 024 * Simple implementation of the {@link ConvergenceChecker} interface using 025 * only objective function values. 026 * 027 * Convergence is considered to have been reached if either the relative 028 * difference between the objective function values is smaller than a 029 * threshold or if either the absolute difference between the objective 030 * function values is smaller than another threshold. 031 * <br/> 032 * The {@link #converged(int,PointValuePair,PointValuePair) converged} 033 * method will also return {@code true} if the number of iterations has been set 034 * (see {@link #SimpleValueChecker(double,double,int) this constructor}). 035 * 036 * @deprecated As of 3.1 (to be removed in 4.0). 037 * @since 3.0 038 */ 039@Deprecated 040public class SimpleValueChecker 041 extends AbstractConvergenceChecker<PointValuePair> { 042 /** 043 * If {@link #maxIterationCount} is set to this value, the number of 044 * iterations will never cause 045 * {@link #converged(int,PointValuePair,PointValuePair)} 046 * to return {@code true}. 047 */ 048 private static final int ITERATION_CHECK_DISABLED = -1; 049 /** 050 * Number of iterations after which the 051 * {@link #converged(int,PointValuePair,PointValuePair)} method 052 * will return true (unless the check is disabled). 053 */ 054 private final int maxIterationCount; 055 056 /** 057 * Build an instance with default thresholds. 058 * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()} 059 */ 060 @Deprecated 061 public SimpleValueChecker() { 062 maxIterationCount = ITERATION_CHECK_DISABLED; 063 } 064 065 /** Build an instance with specified thresholds. 066 * 067 * In order to perform only relative checks, the absolute tolerance 068 * must be set to a negative value. In order to perform only absolute 069 * checks, the relative tolerance must be set to a negative value. 070 * 071 * @param relativeThreshold relative tolerance threshold 072 * @param absoluteThreshold absolute tolerance threshold 073 */ 074 public SimpleValueChecker(final double relativeThreshold, 075 final double absoluteThreshold) { 076 super(relativeThreshold, absoluteThreshold); 077 maxIterationCount = ITERATION_CHECK_DISABLED; 078 } 079 080 /** 081 * Builds an instance with specified thresholds. 082 * 083 * In order to perform only relative checks, the absolute tolerance 084 * must be set to a negative value. In order to perform only absolute 085 * checks, the relative tolerance must be set to a negative value. 086 * 087 * @param relativeThreshold relative tolerance threshold 088 * @param absoluteThreshold absolute tolerance threshold 089 * @param maxIter Maximum iteration count. 090 * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. 091 * 092 * @since 3.1 093 */ 094 public SimpleValueChecker(final double relativeThreshold, 095 final double absoluteThreshold, 096 final int maxIter) { 097 super(relativeThreshold, absoluteThreshold); 098 099 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}