Changeset 24 for src/main/java
- Timestamp:
- Oct 6, 2010, 4:02:40 PM (14 years ago)
- Location:
- src/main/java/weka/clusterers
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/weka/clusterers/MetisMQIClusterer.java
r22 r24 11 11 import weka.clusterers.forMetisMQI.graph.Node; 12 12 import weka.clusterers.forMetisMQI.graph.UndirectedGraph; 13 import weka.clusterers.forMetisMQI.util.Configuration; 13 14 import weka.core.Attribute; 14 15 import weka.core.Capabilities; … … 23 24 OptionHandler { 24 25 25 private int numberOfClusters = 2;26 27 private int sizeFinerGraph = 10;28 29 26 /** 30 27 * It maps each cluster with an integer id. … … 37 34 private Map<Node, Integer> nodeMap = null; 38 35 39 /** 40 * True if a random bisection must be used. 41 */ 42 private boolean randomBisection = false; 43 36 37 private Configuration conf = null; 44 38 /** 45 39 * … … 52 46 UndirectedGraph g = new UndirectedGraph(); 53 47 g.loadFromInstance(data); 54 Set<Set<Node>> clusters = GraphAlgorithms.metisMqi(g, numberOfClusters, 55 sizeFinerGraph, randomBisection); 48 Set<Set<Node>> clusters = GraphAlgorithms.metisMqi(g); 56 49 setNumClusters(clusters.size()); 57 50 int i = 0; … … 125 118 optionString = Utils.getOption('R', options); 126 119 setRandomBisection(Boolean.parseBoolean(optionString)); 120 optionString = Utils.getOption('V', options); 121 if (optionString.length() != 0) { 122 setVerboseLevel(Integer.parseInt(optionString)); 123 } 124 } 125 126 private void setVerboseLevel(int verboseLevel) { 127 Configuration.instance().setVerboseLevel(verboseLevel); 127 128 } 128 129 129 130 private void setRandomBisection(boolean b) { 130 this.randomBisection = b;131 Configuration.instance().setRandomBisection(b); 131 132 } 132 133 … … 146 147 result.add("" + getSizeFinerGraph()); 147 148 result.add("-R"); 149 result.add("" + getRandomBisection()); 150 result.add("-V"); 151 result.add("" + getVerboseLevel()); 148 152 return (String[]) result.toArray(new String[result.size()]); 149 153 } 150 154 155 private boolean getRandomBisection() { 156 return Configuration.instance().isRandomBisection(); 157 } 158 159 private int getVerboseLevel() { 160 return Configuration.instance().getVerboseLevel(); 161 } 162 151 163 private int getSizeFinerGraph() { 152 return sizeFinerGraph;164 return Configuration.instance().getSizeFinerGraph(); 153 165 } 154 166 155 167 private int getNumClusters() { 156 return numberOfClusters;168 return Configuration.instance().getNumberOfClusters(); 157 169 } 158 170 … … 170 182 result.addElement(new Option("\tsize of finer graph.\n" 171 183 + "\t(default 10).", "S", 1, "-S <num>")); 184 result.addElement(new Option("\trandom bisection.\n" 185 + "\t(default false).", "R", 1, "-V <boolean>")); 186 result.addElement(new Option("\tverbosity of graphical output.\n" 187 + "\t(default 1).", "V", 1, "-V <num>")); 172 188 return result.elements(); 173 189 } 174 190 175 191 private void setSizeFinerGraph(int size) { 176 this.sizeFinerGraph = size;192 Configuration.instance().setSizeFinerGraph(size); 177 193 } 178 194 179 195 private void setNumClusters(int n) { 180 this.numberOfClusters = n;196 Configuration.instance().setNumberOfClusters(n); 181 197 } 182 198 … … 198 214 @Override 199 215 public int numberOfClusters() throws Exception { 200 return numberOfClusters;216 return Configuration.instance().getNumberOfClusters(); 201 217 } 202 218 -
src/main/java/weka/clusterers/forMetisMQI/GraphAlgorithms.java
r23 r24 11 11 import weka.clusterers.forMetisMQI.graph.UndirectedGraph; 12 12 import weka.clusterers.forMetisMQI.util.CoarserGraphElement; 13 import weka.clusterers.forMetisMQI.util.Configuration; 13 14 import weka.clusterers.forMetisMQI.util.GraphsFrame; 14 15 import weka.clusterers.forMetisMQI.util.Util; … … 68 69 if (stack.size() > 0) { 69 70 partition = KL(stack.peek().getContracted()); 71 if(Configuration.instance().getVerboseLevel() > 1) { 72 GraphsFrame.instance().addPanel(Util.panelContractedGraph(stack.peek())); 73 GraphsFrame.instance().addPanel(Util.panelContractedGraph(stack.peek(),partition.getSmallerSubgraph().createInducedSubgraph().getVertices())); 74 } 70 75 partition = Uncoarse.uncoarse(stack, partition); 71 76 } … … 81 86 * @param sizeFinerGraph 82 87 */ 83 static public Set<Set<Node>> metisMqi(UndirectedGraph g, int numberOfCluster, int sizeFinerGraph, boolean randomBisection) { 84 GraphsFrame gf = new GraphsFrame("Prova"); 88 static public Set<Set<Node>> metisMqi(UndirectedGraph g) { 89 int numberOfCluster = Configuration.instance().getNumberOfClusters(); 90 boolean randomBisection = Configuration.instance().isRandomBisection(); 91 int sizeFinerGraph = Configuration.instance().getSizeFinerGraph(); 92 int verboseLevel = Configuration.instance().getVerboseLevel(); 93 GraphsFrame gf = GraphsFrame.instance(); 85 94 System.out.println("Vertex count: " + g.getVertexCount()); 86 95 System.out.println("Edges count: " + g.getEdgeCount()); 87 Iterator<Node> iNodes = g.getVertices().iterator();88 int degreeCounter = 0;89 while(iNodes.hasNext()) {90 Node node = iNodes.next();91 if(g.degree(node) == 1) {92 degreeCounter++;93 }94 }95 96 Set<Set<Node>> clusters = new HashSet<Set<Node>>(); 96 97 UndirectedGraph gclone = g.clone(); 97 gf.addPanel(Util.panelGraph(gclone));98 98 for (int i = 0; i < numberOfCluster; i++) { 99 99 Bisection bisection = null; 100 if(verboseLevel > 1) 101 gf.addPanel(Util.panelGraph(g.clone())); 100 102 if(!randomBisection) 101 103 bisection = metis(g,sizeFinerGraph); 102 104 else 103 105 bisection = new Bisection(g); 106 107 if(verboseLevel > 1) 108 gf.addPanel(Util.panelCluster(g.clone(),bisection.getSmallerSubgraph().createInducedSubgraph().getVertices())); 109 104 110 System.out.print("Partizione iniziale: "); 105 111 System.out.print("V1: " + bisection.getSubgraph().getVertexCount()); … … 127 133 } 128 134 } 129 gf.addPanel(Util.panelClusters(gclone, clusters)); 135 if(verboseLevel > 0) 136 gf.addPanel(Util.panelClusters(gclone, clusters)); 130 137 gf.setVisible(true); 131 138 return clusters; -
src/main/java/weka/clusterers/forMetisMQI/MQI.java
r22 r24 19 19 import edu.uci.ics.jung.graph.DirectedGraph; 20 20 import edu.uci.ics.jung.graph.DirectedSparseGraph; 21 import weka.clusterers.forMetisMQI.util.Configuration; 22 import weka.clusterers.forMetisMQI.util.GraphsFrame; 21 23 import weka.clusterers.forMetisMQI.util.Util; 22 24 … … 199 201 } 200 202 alg.evaluate(); 201 // Util.viewFlowGraph(directedGraph, edgeFlowMap); 203 if(Configuration.instance().getVerboseLevel() > 1) 204 GraphsFrame.instance().addPanel(Util.panelFlowGraph(directedGraph, edgeFlowMap)); 202 205 System.out.println("MAX FLOW: " + alg.getMaxFlow() + " THRESHOLD: " 203 206 + maxFlowThreshold); … … 210 213 bisection.getGraph(), cluster)); 211 214 // System.out.println("REFINED BISECTION: " + bisection.toString()); 215 if(Configuration.instance().getVerboseLevel() > 1) 216 GraphsFrame.instance().addPanel(Util.panelCluster(bisection.getGraph(), cluster)); 212 217 } else 213 218 finished = true; -
src/main/java/weka/clusterers/forMetisMQI/util/GraphsFrame.java
r23 r24 16 16 */ 17 17 private static final long serialVersionUID = 1L; 18 19 private static GraphsFrame instance = null; 18 20 19 21 private int numberOfPanels = 0; … … 23 25 private JButton nextButton = null; 24 26 private JButton prevButton = null; 27 28 public static GraphsFrame instance() { 29 if(instance == null) { 30 instance = new GraphsFrame("Graphs Frame"); 31 } 32 return instance; 33 } 34 35 private GraphsFrame() { 36 37 } 25 38 26 p ublicGraphsFrame(String name) {39 private GraphsFrame(String name) { 27 40 super(name); 28 41 setBounds(new Rectangle(800, 700)); 42 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 43 29 44 30 45 graphPanel = new JPanel(); -
src/main/java/weka/clusterers/forMetisMQI/util/Util.java
r23 r24 4 4 import java.awt.Dimension; 5 5 import java.awt.Paint; 6 import java.util.Collection; 6 7 import java.util.HashMap; 7 8 import java.util.HashSet; … … 9 10 import java.util.Map; 10 11 import java.util.Set; 12 import java.util.Map.Entry; 11 13 12 import javax.swing.JFrame;13 14 import javax.swing.JPanel; 14 15 … … 20 21 import weka.clusterers.forMetisMQI.graph.UndirectedGraph; 21 22 import edu.uci.ics.jung.algorithms.layout.FRLayout; 23 import edu.uci.ics.jung.algorithms.layout.KKLayout; 22 24 import edu.uci.ics.jung.algorithms.layout.Layout; 23 25 import edu.uci.ics.jung.graph.Graph; … … 27 29 public class Util { 28 30 29 public static JPanel panelCluster(Graph<Node, Edge> g, Set<Node> cluster) { 31 public static JPanel panelCluster(Graph<Node, Edge> g, Collection<Node> cluster) { 32 Set<Node> setNodes = new HashSet<Node>(); 33 setNodes.addAll(cluster); 30 34 Set<Set<Node>> clusters = new HashSet<Set<Node>>(); 31 clusters.add( cluster);35 clusters.add(setNodes); 32 36 return panelClusters(g, clusters); 33 37 } 34 38 39 public static JPanel panelClusters(Graph<Node, Edge> g, Set<Set<Node>> clusters) { 40 return panelClusters(g,clusters,new ToStringLabeller<Node>()); 41 } 35 42 36 public static JPanel panelClusters(Graph<Node, Edge> g, Set<Set<Node>> clusters ) {37 Layout<Node, Edge> layout = new FRLayout<Node, Edge>(g);43 public static JPanel panelClusters(Graph<Node, Edge> g, Set<Set<Node>> clusters, Transformer<Node,String> transformer) { 44 Layout<Node, Edge> layout = new KKLayout<Node, Edge>(g); 38 45 layout.setSize(new Dimension(800, 600)); // sets the initial size of the space 39 46 // The BasicVisualizationServer<V,E> is parameterized by the edge types … … 80 87 vv.setPreferredSize(new Dimension(800, 600)); // Sets the viewing area 81 88 // size 82 vv.getRenderContext().setVertexLabelTransformer( 83 new ToStringLabeller<Node>()); 84 vv.getRenderContext().setEdgeLabelTransformer( 85 new ToStringLabeller<Edge>()); 89 vv.getRenderContext().setVertexLabelTransformer(transformer); 86 90 vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint); 87 91 return vv; … … 89 93 90 94 public static JPanel panelGraph(Graph<Node, Edge> g){ 91 Layout<Node, Edge> layout = new FRLayout<Node, Edge>(g);95 Layout<Node, Edge> layout = new KKLayout<Node, Edge>(g); 92 96 layout.setSize(new Dimension(800,600)); // sets the initial size of the space 93 97 // The BasicVisualizationServer<V,E> is parameterized by the edge types … … 96 100 vv.setPreferredSize(new Dimension(800,600)); //Sets the viewing area size 97 101 vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>()); 98 vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>()); 102 vv.getRenderContext().setEdgeLabelTransformer( 103 new Transformer<Edge, String>() { 104 public String transform(Edge e) { 105 return ""; 106 } 107 }); 99 108 return vv; 100 109 } … … 120 129 return vv; 121 130 } 131 132 static public JPanel panelContractedGraph(CoarserGraphElement cge, Collection<Node> cluster) { 133 Set<Node> setNodes = new HashSet<Node>(); 134 setNodes.addAll(cluster); 135 Set<Set<Node>> clusters = new HashSet<Set<Node>>(); 136 clusters.add(setNodes); 137 return panelClusters(cge.getContracted(), clusters, new ContractedVertexTransformer(cge)); 138 } 139 140 static public JPanel panelContractedGraph(CoarserGraphElement cge) { 141 Layout<Node, Edge> layout = new KKLayout<Node, Edge>(cge.getContracted()); 142 layout.setSize(new Dimension(800,600)); // sets the initial size of the space 143 // The BasicVisualizationServer<V,E> is parameterized by the edge types 144 BasicVisualizationServer<Node,Edge> vv = 145 new BasicVisualizationServer<Node,Edge>(layout); 146 vv.setPreferredSize(new Dimension(800,600)); //Sets the viewing area size 147 vv.getRenderContext().setVertexLabelTransformer(new ContractedVertexTransformer(cge)); 148 return vv; 149 } 150 122 151 123 152 /**
Note: See TracChangeset
for help on using the changeset viewer.