Coverage details for edu.uci.ics.jung.algorithms.IterativeProcess

LineHitsSource
1 /*
2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
3 * of California
4 * All rights reserved.
5 *
6 * This software is open-source under the BSD license; see either
7 * "license.txt" or
8 * http://jung.sourceforge.net/license.txt for a description.
9 */
10 package edu.uci.ics.jung.algorithms;
11  
12 import edu.uci.ics.jung.utils.NumericalPrecision;
13  
14 /**
15  * Provides basic infrastructure for iterative algorithms. Services provided include:
16  * <ul>
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>
20  * <li> etc. </li>
21  * </ul>
22  * <p>
23  * Algorithms that subclass this class are typically used in the following way: <br>
24  * <pre>
25  * FooAlgorithm foo = new FooAlgorithm(...)
26  * foo.setMaximumIterations(100); //set up conditions
27  * ...
28  * foo.evaluate(); //key method which initiates iterative process
29  * foo.getSomeResult();
30  * </pre>
31  *
32  * @author Scott White (originally written by Didier Besset)
33  */
34 public abstract class IterativeProcess {
35     /**
36      * Number of iterations performed.
37      */
38     private int iterations;
39     /**
40      * Maximum allowed number of iterations.
41      */
4234    private int maximumIterations = 50;
43     /**
44      * Desired precision.
45      */
4634    private double desiredPrecision = NumericalPrecision.defaultNumericalPrecision();
47     /**
48      * Achieved precision.
49      */
50     private double precision;
51  
52  
53     /**
54      * Generic constructor.
55      */
5634    public IterativeProcess() {
5734    }
58  
59     /**
60      * Performs the iterative process.
61      * Note: this method does not return anything because Java does not
62      * allow mixing double, int, or objects
63      */
64     public void evaluate() {
6530        iterations = 0;
6630        initializeIterations();
67261        while (iterations++ < maximumIterations) {
68261            precision = evaluateIteration();
69261            if (hasConverged())
7030                break;
71         }
7230        finalizeIterations();
7330    }
74  
75     /**
76      * Evaluate the result of the current interation.
77      * @return the estimated precision of the result.
78      */
79     abstract protected double evaluateIteration();
80  
81     /**
82      * Perform eventual clean-up operations
83      * (must be implement by subclass when needed).
84      */
85     protected void finalizeIterations() {
860    }
87  
88     /**
89      * Returns the desired precision.
90      */
91     public double getDesiredPrecision() {
920        return desiredPrecision;
93     }
94  
95     /**
96      * Returns the number of iterations performed.
97      */
98     public int getIterations() {
990        return iterations;
100     }
101  
102     /**
103      * Returns the maximum allowed number of iterations.
104      */
105     public int getMaximumIterations() {
1060        return maximumIterations;
107     }
108  
109     /**
110      * Returns the attained precision.
111      */
112     public double getPrecision() {
1130        return precision;
114     }
115  
116     /**
117      *
118      * Check to see if the result has been attained.
119      * @return boolean
120      */
121     public boolean hasConverged() {
122261        return precision < desiredPrecision;
123     }
124  
125     /**
126      * Initializes internal parameters to start the iterative process.
127      */
128     protected void initializeIterations() {
12924    }
130  
131     protected void reinitialize() {
1320    }
133  
134     /**
135      * @return double
136      * @param epsilon double
137      * @param x double
138      */
139     public double relativePrecision(double epsilon, double x) {
1400        return x > NumericalPrecision.defaultNumericalPrecision() ? epsilon / x: epsilon;
141     }
142  
143     /**
144      * Defines the desired precision.
145      */
146     public void setDesiredPrecision(double prec) throws IllegalArgumentException {
1470        if (prec <= 0)
1480            throw new IllegalArgumentException("Non-positive precision: " + prec);
1490        desiredPrecision = prec;
1500    }
151  
152     /**
153      * Defines the maximum allowed number of iterations.
154      */
155     public void setMaximumIterations(int maxIter) throws IllegalArgumentException {
1568        if (maxIter < 1)
1570            throw new IllegalArgumentException("Non-positive maximum iteration: " + maxIter);
1588        maximumIterations = maxIter;
1598    }
160 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.