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
018 package org.apache.commons.math.optimization;
019
020 import org.apache.commons.math.util.FastMath;
021
022 /**
023 * Simple implementation of the {@link ConvergenceChecker} interface using
024 * only objective function values.
025 *
026 * Convergence is considered to have been reached if either the relative
027 * difference between the objective function values is smaller than a
028 * threshold or if either the absolute difference between the objective
029 * function values is smaller than another threshold for all vectors elements.
030 *
031 * @version $Id: SimpleVectorialValueChecker.java 1131229 2011-06-03 20:49:25Z luc $
032 * @since 3.0
033 */
034 public class SimpleVectorialValueChecker
035 extends AbstractConvergenceChecker<VectorialPointValuePair> {
036 /**
037 * Build an instance with default thresholds.
038 */
039 public SimpleVectorialValueChecker() {}
040
041 /**
042 * Build an instance with specified thresholds.
043 *
044 * In order to perform only relative checks, the absolute tolerance
045 * must be set to a negative value. In order to perform only absolute
046 * checks, the relative tolerance must be set to a negative value.
047 *
048 * @param relativeThreshold relative tolerance threshold
049 * @param absoluteThreshold absolute tolerance threshold
050 */
051 public SimpleVectorialValueChecker(final double relativeThreshold,
052 final double absoluteThreshold) {
053 super(relativeThreshold, absoluteThreshold);
054 }
055
056 /**
057 * Check if the optimization algorithm has converged considering the
058 * last two points.
059 * This method may be called several time from the same algorithm
060 * iteration with different points. This can be detected by checking the
061 * iteration number at each call if needed. Each time this method is
062 * called, the previous and current point correspond to points with the
063 * same role at each iteration, so they can be compared. As an example,
064 * simplex-based algorithms call this method for all points of the simplex,
065 * not only for the best or worst ones.
066 *
067 * @param iteration Index of current iteration
068 * @param previous Best point in the previous iteration.
069 * @param current Best point in the current iteration.
070 * @return {@code true} if the algorithm has converged.
071 */
072 @Override
073 public boolean converged(final int iteration,
074 final VectorialPointValuePair previous,
075 final VectorialPointValuePair current) {
076 final double[] p = previous.getValueRef();
077 final double[] c = current.getValueRef();
078 for (int i = 0; i < p.length; ++i) {
079 final double pi = p[i];
080 final double ci = c[i];
081 final double difference = FastMath.abs(pi - ci);
082 final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci));
083 if (difference > size * getRelativeThreshold() &&
084 difference > getAbsoluteThreshold()) {
085 return false;
086 }
087 }
088 return true;
089 }
090 }