2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
6 * This software is open-source under the BSD license; see either
8 * http://jung.sourceforge.net/license.txt for a description.
10 package edu.uci.ics.jung.algorithms.util;
15 * Provides basic infrastructure for iterative algorithms. Services provided include:
17 * <li> storage of current and max iteration count </li>
18 * <li> framework for initialization, iterative evaluation, and finalization </li>
19 * <li> test for convergence </li>
23 * Algorithms that subclass this class are typically used in the following way: <br>
25 * FooAlgorithm foo = new FooAlgorithm(...)
26 * foo.setMaximumIterations(100); //set up conditions
28 * foo.evaluate(); //key method which initiates iterative process
29 * foo.getSomeResult();
32 * @author Scott White (originally written by Didier Besset)
34 public abstract class IterativeProcess implements IterativeContext {
36 * Number of iterations performed.
38 private int iterations;
40 * Maximum allowed number of iterations.
42 private int maximumIterations = 50;
46 private double desiredPrecision = Double.MIN_VALUE;
50 private double precision;
54 * Generic constructor.
56 public IterativeProcess() {
60 * Performs the iterative process.
61 * Note: this method does not return anything because Java does not
62 * allow mixing double, int, or objects
64 public void evaluate() {
66 initializeIterations();
67 while (iterations++ < maximumIterations) {
69 precision = getPrecision();
77 * Evaluate the result of the current iteration.
79 abstract public void step();
82 * Perform eventual clean-up operations
83 * (must be implement by subclass when needed).
85 protected void finalizeIterations() {
89 * Returns the desired precision.
91 public double getDesiredPrecision() {
92 return desiredPrecision;
96 * Returns the number of iterations performed.
98 public int getIterations() {
103 * Returns the maximum allowed number of iterations.
105 public int getMaximumIterations() {
106 return maximumIterations;
110 * Returns the attained precision.
112 public double getPrecision() {
117 * @param precision the precision to set
119 public void setPrecision(double precision) {
120 this.precision = precision;
125 * Check to see if the result has been attained.
128 public boolean hasConverged() {
129 return precision < desiredPrecision;
132 public boolean done() {
133 return hasConverged();
137 * Initializes internal parameters to start the iterative process.
139 protected void initializeIterations() {
145 public void reset() {
150 * @param epsilon double
153 public double relativePrecision(double epsilon, double x) {
154 return x > desiredPrecision ? epsilon / x: epsilon;
158 * Defines the desired precision.
160 public void setDesiredPrecision(double prec) throws IllegalArgumentException {
162 throw new IllegalArgumentException("Non-positive precision: " + prec);
163 desiredPrecision = prec;
167 * Defines the maximum allowed number of iterations.
169 public void setMaximumIterations(int maxIter) throws IllegalArgumentException {
171 throw new IllegalArgumentException("Non-positive maximum iteration: " + maxIter);
172 maximumIterations = maxIter;