Coverage details for edu.uci.ics.jung.visualization.ISOMLayout

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.visualization;
11  
12 import java.util.ConcurrentModificationException;
13 import java.util.Iterator;
14 import java.util.Set;
15 import java.util.Vector;
16  
17 import cern.colt.matrix.DoubleMatrix1D;
18 import cern.colt.matrix.impl.DenseDoubleMatrix1D;
19 import edu.uci.ics.jung.graph.Graph;
20 import edu.uci.ics.jung.graph.Vertex;
21 import edu.uci.ics.jung.utils.Pair;
22 import edu.uci.ics.jung.utils.UserData;
23  
24 /**
25  * Implements a self-organizing map layout algorithm, based on Meyer's
26  * self-organizing graph methods.
27  *
28  * @author Yan Biao Boey
29  */
30 public class ISOMLayout extends AbstractLayout {
31  
320    private static final Object ISOM_KEY =
33         "edu.uci.ics.jung.ISOM_Visualization_Key";
34  
350    private Object key = null;
36     public Object getIsomKey() {
370        if (key == null)
380            key = new Pair(this, ISOM_KEY);
390        return key;
40     }
41  
42     private int maxEpoch;
43     private int epoch;
44  
45     private int radiusConstantTime;
46     private int radius;
47     private int minRadius;
48  
49     private double adaption;
50     private double initialAdaption;
51     private double minAdaption;
52     
53     protected GraphElementAccessor elementAccessor;
54  
55 // private double factor;
56     private double coolingFactor;
57  
58     //private double temperature;
59     //private int initialJumpRadius;
60     //private int jumpRadius;
61  
62     //private int delay;
63  
64     //private ISOMVertexData temp;
65     private Vector queue;
660    private String status = null;
67     
68     /**
69      * Returns the current number of epochs and execution status, as a string.
70      */
71     public String getStatus() {
720        return status;
73     }
74  
75 // private boolean trace;
76 // private boolean done;
77  
78     public ISOMLayout(Graph g) {
790        super(g);
800        elementAccessor = new RadiusGraphElementAccessor(this);
810        queue = new Vector();
82 // trace = false;
830    }
84  
85     protected void initialize_local() {
86 // done = false;
87  
880        maxEpoch = 2000;
890        epoch = 1;
90  
910        radiusConstantTime = 100;
920        radius = 5;
930        minRadius = 1;
94  
950        initialAdaption = 90.0D / 100.0D;
960        adaption = initialAdaption;
970        minAdaption = 0;
98  
99         //factor = 0; //Will be set later on
1000        coolingFactor = 2;
101  
102         //temperature = 0.03;
103         //initialJumpRadius = 100;
104         //jumpRadius = initialJumpRadius;
105  
106         //delay = 100;
1070    }
108     
109     /**
110      * (non-Javadoc)
111      * @see edu.uci.ics.jung.visualization.AbstractLayout#initialize_local_vertex(edu.uci.ics.jung.graph.Vertex)
112      */
113     protected void initialize_local_vertex(Vertex v) {
1140        ISOMVertexData vd = getISOMVertexData(v);
1150        if (vd == null) {
1160            vd = new ISOMVertexData();
1170            v.addUserDatum(getIsomKey(), vd, UserData.REMOVE);
118         }
1190        vd.visited = false;
1200    }
121  
122     /**
123     * Advances the current positions of the graph elements.
124     */
125     public void advancePositions() {
1260        status = "epoch: " + epoch + "; ";
1270        if (epoch < maxEpoch) {
1280            adjust();
1290            updateParameters();
1300            status += " status: running";
131  
132         } else {
1330            status += "adaption: " + adaption + "; ";
1340            status += "status: done";
135 // done = true;
136         }
1370    }
138  
139     ISOMVertexData tempISOM;
140     Coordinates tempXYD;
141  
142     private synchronized void adjust() {
143         //Generate random position in graph space
1440        tempISOM = new ISOMVertexData();
1450        tempXYD = new Coordinates();
146  
147         // creates a new XY data location
1480        tempXYD.setX(10 + Math.random() * getCurrentSize().getWidth());
1490        tempXYD.setY(10 + Math.random() * getCurrentSize().getHeight());
150  
151         //Get closest vertex to random position
1520        Vertex winner = elementAccessor.getVertex(tempXYD.getX(), tempXYD.getY());
153  
154         while(true) {
155             try {
1560                for (Iterator iter = getVisibleVertices().iterator();
1570                iter.hasNext();
158                 ) {
1590                    Vertex v = (Vertex) iter.next();
1600                    ISOMVertexData ivd = getISOMVertexData(v);
1610                    ivd.distance = 0;
1620                    ivd.visited = false;
163                 }
1640                break;
1650            } catch(ConcurrentModificationException cme) {}
166         }
1670        adjustVertex(winner);
1680    }
169  
170     private synchronized void updateParameters() {
1710        epoch++;
1720        double factor = Math.exp(-1 * coolingFactor * (1.0 * epoch / maxEpoch));
1730        adaption = Math.max(minAdaption, factor * initialAdaption);
174         //jumpRadius = (int) factor * jumpRadius;
175         //temperature = factor * temperature;
1760        if ((radius > minRadius) && (epoch % radiusConstantTime == 0)) {
1770            radius--;
178         }
1790    }
180  
181     private synchronized void adjustVertex(Vertex v) {
1820        queue.removeAllElements();
1830        ISOMVertexData ivd = getISOMVertexData(v);
1840        ivd.distance = 0;
1850        ivd.visited = true;
1860        queue.add(v);
187         Vertex current;
188  
1890        while (!queue.isEmpty()) {
1900            current = (Vertex) queue.remove(0);
1910            ISOMVertexData currData = getISOMVertexData(current);
1920            Coordinates currXYData = getCoordinates(current);
193  
1940            double dx = tempXYD.getX() - currXYData.getX();
1950            double dy = tempXYD.getY() - currXYData.getY();
1960            double factor = adaption / Math.pow(2, currData.distance);
197  
1980            currXYData.addX(factor * dx);
1990            currXYData.addY(factor * dy);
200  
2010            if (currData.distance < radius) {
2020                Set s = current.getNeighbors();
203                 while(true) {
204                     try {
2050                        for (Iterator iter = s.iterator(); iter.hasNext();) {
2060                            Vertex child = (Vertex) iter.next();
2070                            ISOMVertexData childData = getISOMVertexData(child);
2080                            if (childData != null && !childData.visited) {
2090                                childData.visited = true;
2100                                childData.distance = currData.distance + 1;
2110                                queue.addElement(child);
212                             }
213                         }
2140                        break;
2150                    } catch(ConcurrentModificationException cme) {}
216                 }
217             }
218         }
2190    }
220  
221     public ISOMVertexData getISOMVertexData(Vertex v) {
2220        return (ISOMVertexData) (v.getUserDatum(getIsomKey()));
223     }
224  
225     /**
226      * This one is an incremental visualization.
227      * @return <code>true</code> is the layout algorithm is incremental, <code>false</code> otherwise
228      */
229     public boolean isIncremental() {
2300        return true;
231     }
232  
233     /**
234      * For now, we pretend it never finishes.
235      * @return <code>true</code> is the increments are done, <code>false</code> otherwise
236      */
237     public boolean incrementsAreDone() {
2380        return false;
239     }
240  
241     public static class ISOMVertexData {
242         public DoubleMatrix1D disp;
243  
244         int distance;
245         boolean visited;
246  
247         public ISOMVertexData() {
248             initialize();
249         }
250  
251         public void initialize() {
252             disp = new DenseDoubleMatrix1D(2);
253  
254             distance = 0;
255             visited = false;
256         }
257  
258         public double getXDisp() {
259             return disp.get(0);
260         }
261  
262         public double getYDisp() {
263             return disp.get(1);
264         }
265  
266         public void setDisp(double x, double y) {
267             disp.set(0, x);
268             disp.set(1, y);
269         }
270  
271         public void incrementDisp(double x, double y) {
272             disp.set(0, disp.get(0) + x);
273             disp.set(1, disp.get(1) + y);
274         }
275  
276         public void decrementDisp(double x, double y) {
277             disp.set(0, disp.get(0) - x);
278             disp.set(1, disp.get(1) - y);
279         }
280     }
281 }

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.