| 1 | /* |
| 2 | * IterativeProcess.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.math.iterations; |
| 27 | |
| 28 | import dr.math.functionEval.DrMath; |
| 29 | |
| 30 | /** |
| 31 | * An iterative process is a general structure managing iterations. |
| 32 | * |
| 33 | * @author Didier H. Besset |
| 34 | */ |
| 35 | public abstract class IterativeProcess |
| 36 | { |
| 37 | /** |
| 38 | * Number of iterations performed. |
| 39 | */ |
| 40 | private int iterations; |
| 41 | /** |
| 42 | * Maximum allowed number of iterations. |
| 43 | */ |
| 44 | private int maximumIterations = 50; |
| 45 | /** |
| 46 | * Desired precision. |
| 47 | */ |
| 48 | private double desiredPrecision = DrMath.defaultNumericalPrecision(); |
| 49 | /** |
| 50 | * Achieved precision. |
| 51 | */ |
| 52 | private double precision; |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Generic constructor. |
| 57 | */ |
| 58 | public IterativeProcess() { |
| 59 | } |
| 60 | /** |
| 61 | * Performs the iterative process. |
| 62 | * Note: this method does not return anything because Java does not |
| 63 | * allow mixing double, int, or objects |
| 64 | */ |
| 65 | public void evaluate() |
| 66 | { |
| 67 | iterations = 0; |
| 68 | initializeIterations(); |
| 69 | while ( iterations++ < maximumIterations ) |
| 70 | { |
| 71 | precision = evaluateIteration(); |
| 72 | if ( hasConverged() ) |
| 73 | break; |
| 74 | } |
| 75 | finalizeIterations(); |
| 76 | } |
| 77 | /** |
| 78 | * Evaluate the result of the current interation. |
| 79 | * @return the estimated precision of the result. |
| 80 | */ |
| 81 | abstract public double evaluateIteration(); |
| 82 | /** |
| 83 | * Perform eventual clean-up operations |
| 84 | * (mustbe implement by subclass when needed). |
| 85 | */ |
| 86 | public void finalizeIterations ( ) |
| 87 | { |
| 88 | } |
| 89 | /** |
| 90 | * Returns the desired precision. |
| 91 | */ |
| 92 | public double getDesiredPrecision( ) |
| 93 | { |
| 94 | return desiredPrecision; |
| 95 | } |
| 96 | /** |
| 97 | * Returns the number of iterations performed. |
| 98 | */ |
| 99 | public int getIterations() |
| 100 | { |
| 101 | return iterations; |
| 102 | } |
| 103 | /** |
| 104 | * Returns the maximum allowed number of iterations. |
| 105 | */ |
| 106 | public int getMaximumIterations( ) |
| 107 | { |
| 108 | return maximumIterations; |
| 109 | } |
| 110 | /** |
| 111 | * Returns the attained precision. |
| 112 | */ |
| 113 | public double getPrecision() |
| 114 | { |
| 115 | return precision; |
| 116 | } |
| 117 | /** |
| 118 | * Check to see if the result has been attained. |
| 119 | * @return boolean |
| 120 | */ |
| 121 | public boolean hasConverged() |
| 122 | { |
| 123 | return precision < desiredPrecision; |
| 124 | } |
| 125 | /** |
| 126 | * Initializes internal parameters to start the iterative process. |
| 127 | */ |
| 128 | public void initializeIterations() |
| 129 | { |
| 130 | } |
| 131 | /** |
| 132 | * @return double |
| 133 | * @param epsilon double |
| 134 | * @param x double |
| 135 | */ |
| 136 | public double relativePrecision( double epsilon, double x) |
| 137 | { |
| 138 | return x > DrMath.defaultNumericalPrecision() |
| 139 | ? epsilon / x: epsilon; |
| 140 | } |
| 141 | /** |
| 142 | * Defines the desired precision. |
| 143 | */ |
| 144 | public void setDesiredPrecision( double prec ) |
| 145 | throws IllegalArgumentException |
| 146 | { |
| 147 | if ( prec <= 0 ) |
| 148 | throw new IllegalArgumentException |
| 149 | ( "Non-positive precision: "+prec); |
| 150 | desiredPrecision = prec; |
| 151 | } |
| 152 | /** |
| 153 | * Defines the maximum allowed number of iterations. |
| 154 | */ |
| 155 | public void setMaximumIterations( int maxIter) |
| 156 | throws IllegalArgumentException |
| 157 | { |
| 158 | if ( maxIter < 1 ) |
| 159 | throw new IllegalArgumentException |
| 160 | ( "Non-positive maximum iteration: "+maxIter); |
| 161 | maximumIterations = maxIter; |
| 162 | } |
| 163 | } |