Line | Hits | Source |
---|---|---|
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 | */ | |
42 | 34 | private int maximumIterations = 50; |
43 | /** | |
44 | * Desired precision. | |
45 | */ | |
46 | 34 | private double desiredPrecision = NumericalPrecision.defaultNumericalPrecision(); |
47 | /** | |
48 | * Achieved precision. | |
49 | */ | |
50 | private double precision; | |
51 | ||
52 | ||
53 | /** | |
54 | * Generic constructor. | |
55 | */ | |
56 | 34 | public IterativeProcess() { |
57 | 34 | } |
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() { | |
65 | 30 | iterations = 0; |
66 | 30 | initializeIterations(); |
67 | 261 | while (iterations++ < maximumIterations) { |
68 | 261 | precision = evaluateIteration(); |
69 | 261 | if (hasConverged()) |
70 | 30 | break; |
71 | } | |
72 | 30 | finalizeIterations(); |
73 | 30 | } |
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() { | |
86 | 0 | } |
87 | ||
88 | /** | |
89 | * Returns the desired precision. | |
90 | */ | |
91 | public double getDesiredPrecision() { | |
92 | 0 | return desiredPrecision; |
93 | } | |
94 | ||
95 | /** | |
96 | * Returns the number of iterations performed. | |
97 | */ | |
98 | public int getIterations() { | |
99 | 0 | return iterations; |
100 | } | |
101 | ||
102 | /** | |
103 | * Returns the maximum allowed number of iterations. | |
104 | */ | |
105 | public int getMaximumIterations() { | |
106 | 0 | return maximumIterations; |
107 | } | |
108 | ||
109 | /** | |
110 | * Returns the attained precision. | |
111 | */ | |
112 | public double getPrecision() { | |
113 | 0 | return precision; |
114 | } | |
115 | ||
116 | /** | |
117 | * | |
118 | * Check to see if the result has been attained. | |
119 | * @return boolean | |
120 | */ | |
121 | public boolean hasConverged() { | |
122 | 261 | return precision < desiredPrecision; |
123 | } | |
124 | ||
125 | /** | |
126 | * Initializes internal parameters to start the iterative process. | |
127 | */ | |
128 | protected void initializeIterations() { | |
129 | 24 | } |
130 | ||
131 | protected void reinitialize() { | |
132 | 0 | } |
133 | ||
134 | /** | |
135 | * @return double | |
136 | * @param epsilon double | |
137 | * @param x double | |
138 | */ | |
139 | public double relativePrecision(double epsilon, double x) { | |
140 | 0 | return x > NumericalPrecision.defaultNumericalPrecision() ? epsilon / x: epsilon; |
141 | } | |
142 | ||
143 | /** | |
144 | * Defines the desired precision. | |
145 | */ | |
146 | public void setDesiredPrecision(double prec) throws IllegalArgumentException { | |
147 | 0 | if (prec <= 0) |
148 | 0 | throw new IllegalArgumentException("Non-positive precision: " + prec); |
149 | 0 | desiredPrecision = prec; |
150 | 0 | } |
151 | ||
152 | /** | |
153 | * Defines the maximum allowed number of iterations. | |
154 | */ | |
155 | public void setMaximumIterations(int maxIter) throws IllegalArgumentException { | |
156 | 8 | if (maxIter < 1) |
157 | 0 | throw new IllegalArgumentException("Non-positive maximum iteration: " + maxIter); |
158 | 8 | maximumIterations = maxIter; |
159 | 8 | } |
160 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |