Changeset 26 for src/main/java
- Timestamp:
- Oct 9, 2010, 12:24:38 PM (14 years ago)
- Location:
- src/main/java/weka/clusterers
- Files:
-
- 7 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/weka/clusterers/MetisMQIClusterer.java
r24 r26 35 35 36 36 37 private Configuration conf = null;38 37 /** 39 38 * … … 122 121 setVerboseLevel(Integer.parseInt(optionString)); 123 122 } 123 optionString = Utils.getOption('o', options); 124 if (optionString.length() != 0) { 125 setOutputFile(optionString); 126 } 127 } 128 129 private void setOutputFile(String outputFile) { 130 Configuration.instance().setOutputFile(outputFile); 131 } 132 133 private String getOutputFile() { 134 return Configuration.instance().getOutputFile(); 124 135 } 125 136 … … 150 161 result.add("-V"); 151 162 result.add("" + getVerboseLevel()); 163 result.add("-o"); 164 result.add("" + getOutputFile()); 152 165 return (String[]) result.toArray(new String[result.size()]); 153 166 } … … 186 199 result.addElement(new Option("\tverbosity of graphical output.\n" 187 200 + "\t(default 1).", "V", 1, "-V <num>")); 201 result.addElement(new Option("\tpath of the output file.\n" 202 , "o", 1, "-o <path>")); 188 203 return result.elements(); 189 204 } -
src/main/java/weka/clusterers/forMetisMQI/GraphAlgorithms.java
r25 r26 1 1 package weka.clusterers.forMetisMQI; 2 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 3 6 import java.util.HashSet; 4 7 import java.util.Iterator; 5 8 import java.util.Set; 6 9 import java.util.Stack; 10 11 import org.jdom.Document; 12 import org.jdom.Element; 13 import org.jdom.output.Format; 14 import org.jdom.output.XMLOutputter; 7 15 8 16 import weka.clusterers.forMetisMQI.graph.Bisection; … … 13 21 import weka.clusterers.forMetisMQI.util.Configuration; 14 22 import weka.clusterers.forMetisMQI.util.GraphsFrame; 23 import weka.clusterers.forMetisMQI.util.Random; 15 24 import weka.clusterers.forMetisMQI.util.Util; 16 25 … … 92 101 int verboseLevel = Configuration.instance().getVerboseLevel(); 93 102 GraphsFrame gf = GraphsFrame.instance(); 103 104 Element rootXML = new Element("run"); 105 Document logXML = new Document(rootXML); 106 rootXML.setAttribute("seed", Long.toString(Random.instance().getSeed())); 107 Element graphXML = new Element("graph"); 108 graphXML.addContent(new Element("vertex").setText(Integer.toString(g.getVertexCount()))); 109 graphXML.addContent(new Element("edge").setText(Integer.toString(g.getEdgeCount()))); 110 rootXML.addContent(graphXML); 111 Element clustersXML = new Element("clusters"); 112 rootXML.addContent(clustersXML); 113 94 114 System.out.println("Seed: " + Random.instance().getSeed()); 95 115 System.out.println("Vertex count: " + g.getVertexCount()); … … 97 117 Set<Set<Node>> clusters = new HashSet<Set<Node>>(); 98 118 UndirectedGraph gclone = g.clone(); 119 99 120 for (int i = 0; i < numberOfCluster; i++) { 100 121 Bisection bisection = null; … … 109 130 gf.addPanel(Util.panelCluster(g.clone(),bisection.getSmallerSubgraph().createInducedSubgraph().getVertices())); 110 131 111 System.out.print(" Partizione iniziale: ");132 System.out.print("Initial partition: "); 112 133 System.out.print("V1: " + bisection.getSubgraph().getVertexCount()); 113 134 System.out.print(" V2: " + bisection.getComplement().getVertexCount()); … … 121 142 // System.out.println(cluster); 122 143 Bisection mqiBisection = new Bisection(new Subgraph(g,cluster)); 123 System.out.println(" Partizione raffinata(MQI)");144 System.out.println("Refined partition (MQI)"); 124 145 double newConductance = ((double)mqiBisection.edgeCut() / 2) / Math.min(mqiBisection.getSubgraph().totalDegree(),mqiBisection.getComplement().totalDegree()); 125 System.out.println("Cluster "+ i + ": V=" + clusterGraph.getVertexCount() + ", E=" + clusterGraph.getEdgeCount()+"."); 146 System.out.print("Cluster "+ i + ": V=" + clusterGraph.getVertexCount() + ", E=" + clusterGraph.getEdgeCount()+", "); 147 System.out.println("Cond: " + newConductance); 148 149 150 mqiBisection = new Bisection(new Subgraph(gclone,cluster)); 151 newConductance = ((double)mqiBisection.edgeCut() / 2) / Math.min(mqiBisection.getSubgraph().totalDegree(),mqiBisection.getComplement().totalDegree()); 126 152 System.out.println("Cluster conductance: " + newConductance); 153 154 155 Element clusterXML = new Element("cluster"); 156 clusterXML.setAttribute("id", Integer.toString(i)); 157 clusterXML.addContent(new Element("vertex").setText(Integer.toString(clusterGraph.getVertexCount()))); 158 clusterXML.addContent(new Element("edge").setText(Integer.toString(clusterGraph.getEdgeCount()))); 159 clusterXML.addContent(new Element("conductance").setText(Double.toString(newConductance))); 160 clustersXML.addContent(clusterXML); 127 161 128 162 clusters.add(cluster); … … 134 168 } 135 169 } 170 171 172 XMLOutputter xmlOutputter = new XMLOutputter(); 173 xmlOutputter.setFormat(Format.getPrettyFormat()); 174 try { 175 xmlOutputter.output(logXML, new FileOutputStream(new File(Configuration.instance().getOutputFile()))); 176 } catch (IOException e) { 177 e.printStackTrace(); 178 } 179 136 180 if(verboseLevel > 0) { 137 181 gf.addPanel(Util.panelClusters(gclone, clusters)); -
src/main/java/weka/clusterers/forMetisMQI/MQI.java
r25 r26 16 16 import weka.clusterers.forMetisMQI.graph.Node; 17 17 import weka.clusterers.forMetisMQI.graph.Subgraph; 18 import weka.clusterers.forMetisMQI.graph.UndirectedGraph; 18 19 import edu.uci.ics.jung.algorithms.flows.EdmondsKarpMaxFlow; 19 20 import edu.uci.ics.jung.graph.DirectedGraph; … … 83 84 84 85 static private DirectedGraph<Node, Edge> prepareDirectedGraph( 85 Bisection bisection, Node source, Node sink, boolean forConductance) {86 Subgraph B = bisection.getLargerSubgraph();87 Subgraph A = bisection.getSmallerSubgraph();86 Subgraph A, Node source, Node sink, boolean forConductance) { 87 Bisection bisection = new Bisection(A); 88 Subgraph B = bisection.getComplement(); 88 89 int a = 0; 89 90 if (!forConductance) … … 161 162 static public Set<Node> mqi(Bisection partition, boolean forConductance) { 162 163 // System.out.println("INITIAL BISECTION: " + partition.toString()); 164 UndirectedGraph startingGraph = partition.getGraph(); 163 165 boolean finished = false; 164 166 Bisection bisection = partition; 165 Set<Node> cluster = new HashSet<Node>(partition.get SmallerSubgraph()167 Set<Node> cluster = new HashSet<Node>(partition.getLargerSubgraph() 166 168 .createInducedSubgraph().getVertices()); 167 169 // System.out.println("IMPROVING SUBGRAPH: " + cluster); … … 170 172 Node source = new Node("$$$$S"); 171 173 Node sink = new Node("$$$$T"); 172 DirectedGraph<Node, Edge> directedGraph = prepareDirectedGraph(173 bisection, source, sink, true);174 Subgraph A = new Subgraph(startingGraph,cluster); 175 DirectedGraph<Node, Edge> directedGraph = prepareDirectedGraph(A, source, sink, true); 174 176 Transformer<Edge, Number> capTransformer = new Transformer<Edge, Number>() { 175 177 public Double transform(Edge e) { … … 191 193 192 194 if (!forConductance) 193 maxFlowThreshold = bisection.getLargerSubgraph() 194 .getVertexCount() 195 maxFlowThreshold = A.getVertexCount() 195 196 * bisection.edgeCut() / 2; 196 197 else { 197 198 // maxFlowThreshold = Math.min(bisection.getLargerSubgraph().totalDegree(), bisection.getSmallerSubgraph().totalDegree()); 198 maxFlowThreshold = bisection.getSmallerSubgraph().totalDegree();199 maxFlowThreshold = A.totalDegree(); 199 200 maxFlowThreshold = maxFlowThreshold 200 201 * (bisection.edgeCut() / 2); … … 216 217 // System.out.println("REFINED BISECTION: " + bisection.toString()); 217 218 if(Configuration.instance().getVerboseLevel() > 1) 218 GraphsFrame.instance().addPanel(Util.panelCluster(bisection.getGraph() , cluster));219 GraphsFrame.instance().addPanel(Util.panelCluster(bisection.getGraph().clone(), cluster)); 219 220 } else 220 221 finished = true; -
src/main/java/weka/clusterers/forMetisMQI/graph/Bisection.java
r20 r26 4 4 import java.util.Iterator; 5 5 import java.util.Set; 6 7 import weka.clusterers.forMetisMQI.util.Random; 6 8 7 9 … … 42 44 */ 43 45 public Bisection(UndirectedGraph g){ 46 double limitingProbabilities = 0.1; 44 47 this.g = g; 45 48 a = new Subgraph(g); 46 49 b = new Subgraph(g); 47 50 Iterator<Node> graph = g.vtxsPermutation().iterator(); 48 int i = 0;49 51 while(graph.hasNext()) { 50 52 Node u = graph.next(); 51 if( (i%2)==0)53 if(Random.instance().nextDouble() < limitingProbabilities) 52 54 a.addVertex(u); 53 55 else 54 56 b.addVertex(u); 55 i++;56 57 } 57 58 marked = new HashSet<Node>(); -
src/main/java/weka/clusterers/forMetisMQI/graph/UndirectedGraph.java
r20 r26 9 9 import java.util.TreeMap; 10 10 11 import weka.clusterers.forMetisMQI. Random;11 import weka.clusterers.forMetisMQI.util.Random; 12 12 import weka.core.Attribute; 13 13 import weka.core.Instance; -
src/main/java/weka/clusterers/forMetisMQI/util/Configuration.java
r24 r26 11 11 private int numberOfClusters = 2; 12 12 13 private String outputFile = null; 14 15 public String getOutputFile() { 16 return outputFile; 17 } 18 19 public void setOutputFile(String outputFile) { 20 this.outputFile = outputFile; 21 } 22 13 23 private static Configuration instance = null; 14 24 -
src/main/java/weka/clusterers/forMetisMQI/util/Random.java
r25 r26 1 package weka.clusterers.forMetisMQI ;1 package weka.clusterers.forMetisMQI.util; 2 2 3 3 public class Random extends java.util.Random { … … 15 15 public static Random instance() { 16 16 if(instance == null) { 17 instance = new Random( /*new java.util.Random().nextLong()*/1234567890);17 instance = new Random(new java.util.Random().nextLong()/*1234567890*/); 18 18 } 19 19 return instance; -
src/main/java/weka/clusterers/forMetisMQI/util/Util.java
r24 r26 16 16 import org.apache.commons.collections15.Transformer; 17 17 18 import weka.clusterers.forMetisMQI.Random;19 18 import weka.clusterers.forMetisMQI.graph.Edge; 20 19 import weka.clusterers.forMetisMQI.graph.Node; … … 93 92 94 93 public static JPanel panelGraph(Graph<Node, Edge> g){ 95 Layout<Node, Edge> layout = new KKLayout<Node, Edge>(g);94 Layout<Node, Edge> layout = new FRLayout<Node, Edge>(g); 96 95 layout.setSize(new Dimension(800,600)); // sets the initial size of the space 97 96 // The BasicVisualizationServer<V,E> is parameterized by the edge types
Note: See TracChangeset
for help on using the changeset viewer.