1 | package weka.clusterers; |
---|
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; |
---|
9 | |
---|
10 | import weka.clusterers.forMetisMQI.GraphAlgorithms; |
---|
11 | import weka.clusterers.forMetisMQI.graph.Node; |
---|
12 | import weka.clusterers.forMetisMQI.graph.UndirectedGraph; |
---|
13 | import weka.core.Attribute; |
---|
14 | import weka.core.Capabilities; |
---|
15 | import weka.core.Instance; |
---|
16 | import weka.core.Instances; |
---|
17 | import weka.core.Option; |
---|
18 | import weka.core.OptionHandler; |
---|
19 | import weka.core.Utils; |
---|
20 | import weka.core.Capabilities.Capability; |
---|
21 | |
---|
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; |
---|
38 | |
---|
39 | /** |
---|
40 | * |
---|
41 | */ |
---|
42 | private static final long serialVersionUID = 1L; |
---|
43 | |
---|
44 | @Override |
---|
45 | public void buildClusterer(Instances data) throws Exception { |
---|
46 | getCapabilities().testWithFail(data); |
---|
47 | UndirectedGraph g = new UndirectedGraph(); |
---|
48 | g.loadFromInstance(data); |
---|
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 | } |
---|
68 | } |
---|
69 | |
---|
70 | @Override |
---|
71 | public int clusterInstance(Instance instance) 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 { |
---|
173 | double[] d = new double[numberOfClusters()]; |
---|
174 | d[clusterInstance(instance)] = 1.0; |
---|
175 | return d; |
---|
176 | } |
---|
177 | |
---|
178 | @Override |
---|
179 | public Capabilities getCapabilities() { |
---|
180 | Capabilities result = super.getCapabilities(); |
---|
181 | result.enable(Capability.NUMERIC_ATTRIBUTES); |
---|
182 | result.enable(Capability.NO_CLASS); |
---|
183 | return result; |
---|
184 | } |
---|
185 | |
---|
186 | @Override |
---|
187 | public int numberOfClusters() throws Exception { |
---|
188 | return numberOfClusters; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Main method for executing this clusterer. |
---|
193 | * |
---|
194 | * @param args |
---|
195 | * the options, use "-h" to display options |
---|
196 | */ |
---|
197 | public static void main(String[] args) { |
---|
198 | AbstractClusterer.runClusterer(new MetisMQIClusterer(), args); |
---|
199 | } |
---|
200 | |
---|
201 | } |
---|