Changeset 14 for src/main/java/weka/clusterers/MetisMQIClusterer.java
- Timestamp:
- Sep 17, 2010, 1:09:06 PM (14 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/weka/clusterers/MetisMQIClusterer.java
r11 r14 1 1 package weka.clusterers; 2 2 3 import java.util.Enumeration; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.Map; 7 import java.util.Set; 8 import java.util.Vector; 3 9 4 10 import weka.clusterers.forMetisMQI.GraphAlgorithms; 11 import weka.clusterers.forMetisMQI.graph.Node; 5 12 import weka.clusterers.forMetisMQI.graph.UndirectedGraph; 13 import weka.core.Attribute; 6 14 import weka.core.Capabilities; 7 15 import weka.core.Instance; 8 16 import weka.core.Instances; 17 import weka.core.Option; 18 import weka.core.OptionHandler; 19 import weka.core.Utils; 9 20 import weka.core.Capabilities.Capability; 10 21 11 public class SimpleClusterer extends AbstractClusterer { 12 13 private int numberOfClusters = 0; 22 public class MetisMQIClusterer extends AbstractClusterer implements 23 OptionHandler { 24 25 private int numberOfClusters = 2; 26 27 private int sizeFinerGraph = 10; 28 29 /** 30 * It maps each cluster with an integer id. 31 */ 32 private Map<Set<Node>, Integer> clustersMap = null; 33 34 /** 35 * Holds the cluster membership for each node. 36 */ 37 private Map<Node, Integer> nodeMap = null; 14 38 15 39 /** … … 21 45 public void buildClusterer(Instances data) throws Exception { 22 46 getCapabilities().testWithFail(data); 23 24 47 UndirectedGraph g = new UndirectedGraph(); 25 48 g.loadFromInstance(data); 26 GraphAlgorithms ga = new GraphAlgorithms(); 27 ga.METIS(g); 28 numberOfClusters = 1; 49 Set<Set<Node>> clusters = GraphAlgorithms.metisMqi(g, numberOfClusters, 50 sizeFinerGraph); 51 setNumClusters(clusters.size()); 52 int i = 0; 53 Iterator<Set<Node>> clusterIterator = clusters.iterator(); 54 clustersMap = new HashMap<Set<Node>, Integer>(); 55 nodeMap = new HashMap<Node, Integer>(); 56 while (clusterIterator.hasNext()) { 57 Set<Node> cluster = clusterIterator.next(); 58 clustersMap.put(cluster, i); 59 Iterator<Node> nodeIterator = cluster.iterator(); 60 while (nodeIterator.hasNext()) { 61 Node n = nodeIterator.next(); 62 if (nodeMap.get(n) == null) { 63 nodeMap.put(n, i); 64 } 65 } 66 i++; 67 } 29 68 } 30 69 31 70 @Override 32 71 public int clusterInstance(Instance instance) throws Exception { 33 return 0; 34 } 35 36 @Override 37 public double[] distributionForInstance(Instance instance) 38 throws Exception { 72 Attribute from = instance.dataset().attribute("from"); 73 Attribute to = instance.dataset().attribute("to"); 74 Instance edge = instance; 75 Node node1 = new Node(Integer.toString(((int) Math.round(edge 76 .value(from))))); 77 Node node2 = new Node(Integer.toString(((int) Math 78 .round(edge.value(to))))); 79 if (nodeMap.get(node1) == nodeMap.get(node2)) 80 return nodeMap.get(node1); 81 else 82 throw new Exception(); 83 } 84 85 /** 86 * Parses a given list of options. 87 * <p/> 88 * 89 * <!-- options-start --> Valid options are: 90 * <p/> 91 * 92 * <pre> 93 * -N <num> 94 * number of clusters. 95 * (default 2). 96 * </pre> 97 * 98 * <pre> 99 * -S 100 * Maximum size of the finer graph during the coarsening phase. 101 * </pre> 102 * 103 * <!-- options-end --> 104 * 105 * @param options 106 * the list of options as an array of strings 107 * @throws Exception 108 * if an option is not supported 109 */ 110 @Override 111 public void setOptions(String[] options) throws Exception { 112 String optionString = Utils.getOption('N', options); 113 if (optionString.length() != 0) { 114 setNumClusters(Integer.parseInt(optionString)); 115 } 116 optionString = Utils.getOption('S', options); 117 if (optionString.length() != 0) { 118 setSizeFinerGraph(Integer.parseInt(optionString)); 119 } 120 } 121 122 /** 123 * Gets the current settings of MetisMQIClusterer 124 * 125 * @return an array of strings suitable for passing to setOptions() 126 */ 127 @SuppressWarnings("unchecked") 128 @Override 129 public String[] getOptions() { 130 Vector result; 131 result = new Vector(); 132 result.add("-N"); 133 result.add("" + getNumClusters()); 134 result.add("-S"); 135 result.add("" + getSizeFinerGraph()); 136 return (String[]) result.toArray(new String[result.size()]); 137 } 138 139 private int getSizeFinerGraph() { 140 return sizeFinerGraph; 141 } 142 143 private int getNumClusters() { 144 return numberOfClusters; 145 } 146 147 /** 148 * Returns an enumeration describing the available options. 149 * 150 * @return an enumeration of all the available options. 151 */ 152 @SuppressWarnings("unchecked") 153 @Override 154 public Enumeration listOptions() { 155 Vector result = new Vector(); 156 result.addElement(new Option("\tnumber of clusters.\n" 157 + "\t(default 2).", "N", 1, "-N <num>")); 158 result.addElement(new Option("\tsize of finer graph.\n" 159 + "\t(default 10).", "S", 1, "-S <num>")); 160 return result.elements(); 161 } 162 163 private void setSizeFinerGraph(int size) { 164 this.sizeFinerGraph = size; 165 } 166 167 private void setNumClusters(int n) { 168 this.numberOfClusters = n; 169 } 170 171 @Override 172 public double[] distributionForInstance(Instance instance) throws Exception { 39 173 double[] d = new double[numberOfClusters()]; 40 174 d[clusterInstance(instance)] = 1.0; … … 62 196 */ 63 197 public static void main(String[] args) { 64 AbstractClusterer.runClusterer(new SimpleClusterer(), args);198 AbstractClusterer.runClusterer(new MetisMQIClusterer(), args); 65 199 } 66 200
Note: See TracChangeset
for help on using the changeset viewer.