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.clusterers.forMetisMQI.util.Configuration; |
---|
14 | import weka.core.Attribute; |
---|
15 | import weka.core.Capabilities; |
---|
16 | import weka.core.Instance; |
---|
17 | import weka.core.Instances; |
---|
18 | import weka.core.Option; |
---|
19 | import weka.core.OptionHandler; |
---|
20 | import weka.core.Utils; |
---|
21 | import weka.core.Capabilities.Capability; |
---|
22 | |
---|
23 | public class MetisMQIClusterer extends AbstractClusterer implements |
---|
24 | OptionHandler { |
---|
25 | |
---|
26 | /** |
---|
27 | * It maps each cluster with an integer id. |
---|
28 | */ |
---|
29 | private Map<Set<Node>, Integer> clustersMap = null; |
---|
30 | |
---|
31 | /** |
---|
32 | * Holds the cluster membership for each node. |
---|
33 | */ |
---|
34 | private Map<Node, Integer> nodeMap = null; |
---|
35 | |
---|
36 | |
---|
37 | private Configuration conf = null; |
---|
38 | /** |
---|
39 | * |
---|
40 | */ |
---|
41 | private static final long serialVersionUID = 1L; |
---|
42 | |
---|
43 | @Override |
---|
44 | public void buildClusterer(Instances data) throws Exception { |
---|
45 | getCapabilities().testWithFail(data); |
---|
46 | UndirectedGraph g = new UndirectedGraph(); |
---|
47 | g.loadFromInstance(data); |
---|
48 | Set<Set<Node>> clusters = GraphAlgorithms.metisMqi(g); |
---|
49 | setNumClusters(clusters.size()); |
---|
50 | int i = 0; |
---|
51 | Iterator<Set<Node>> clusterIterator = clusters.iterator(); |
---|
52 | clustersMap = new HashMap<Set<Node>, Integer>(); |
---|
53 | nodeMap = new HashMap<Node, Integer>(); |
---|
54 | while (clusterIterator.hasNext()) { |
---|
55 | Set<Node> cluster = clusterIterator.next(); |
---|
56 | clustersMap.put(cluster, i); |
---|
57 | Iterator<Node> nodeIterator = cluster.iterator(); |
---|
58 | while (nodeIterator.hasNext()) { |
---|
59 | Node n = nodeIterator.next(); |
---|
60 | if (nodeMap.get(n) == null) { |
---|
61 | nodeMap.put(n, i); |
---|
62 | } |
---|
63 | } |
---|
64 | i++; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | @Override |
---|
69 | public int clusterInstance(Instance instance) throws Exception { |
---|
70 | Attribute from = instance.dataset().attribute("from"); |
---|
71 | Attribute to = instance.dataset().attribute("to"); |
---|
72 | Instance edge = instance; |
---|
73 | Node node1 = new Node(Integer.toString(((int) Math.round(edge |
---|
74 | .value(from))))); |
---|
75 | Node node2 = new Node(Integer.toString(((int) Math |
---|
76 | .round(edge.value(to))))); |
---|
77 | if (nodeMap.get(node1) == nodeMap.get(node2)) |
---|
78 | return nodeMap.get(node1); |
---|
79 | else |
---|
80 | throw new Exception(); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Parses a given list of options. |
---|
85 | * <p/> |
---|
86 | * |
---|
87 | * <!-- options-start --> Valid options are: |
---|
88 | * <p/> |
---|
89 | * |
---|
90 | * <pre> |
---|
91 | * -N <num> |
---|
92 | * number of clusters. |
---|
93 | * (default 2). |
---|
94 | * </pre> |
---|
95 | * |
---|
96 | * <pre> |
---|
97 | * -S |
---|
98 | * Maximum size of the finer graph during the coarsening phase. |
---|
99 | * </pre> |
---|
100 | * |
---|
101 | * <!-- options-end --> |
---|
102 | * |
---|
103 | * @param options |
---|
104 | * the list of options as an array of strings |
---|
105 | * @throws Exception |
---|
106 | * if an option is not supported |
---|
107 | */ |
---|
108 | @Override |
---|
109 | public void setOptions(String[] options) throws Exception { |
---|
110 | String optionString = Utils.getOption('N', options); |
---|
111 | if (optionString.length() != 0) { |
---|
112 | setNumClusters(Integer.parseInt(optionString)); |
---|
113 | } |
---|
114 | optionString = Utils.getOption('S', options); |
---|
115 | if (optionString.length() != 0) { |
---|
116 | setSizeFinerGraph(Integer.parseInt(optionString)); |
---|
117 | } |
---|
118 | optionString = Utils.getOption('R', options); |
---|
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); |
---|
128 | } |
---|
129 | |
---|
130 | private void setRandomBisection(boolean b) { |
---|
131 | Configuration.instance().setRandomBisection(b); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Gets the current settings of MetisMQIClusterer |
---|
136 | * |
---|
137 | * @return an array of strings suitable for passing to setOptions() |
---|
138 | */ |
---|
139 | @SuppressWarnings("unchecked") |
---|
140 | @Override |
---|
141 | public String[] getOptions() { |
---|
142 | Vector result; |
---|
143 | result = new Vector(); |
---|
144 | result.add("-N"); |
---|
145 | result.add("" + getNumClusters()); |
---|
146 | result.add("-S"); |
---|
147 | result.add("" + getSizeFinerGraph()); |
---|
148 | result.add("-R"); |
---|
149 | result.add("" + getRandomBisection()); |
---|
150 | result.add("-V"); |
---|
151 | result.add("" + getVerboseLevel()); |
---|
152 | return (String[]) result.toArray(new String[result.size()]); |
---|
153 | } |
---|
154 | |
---|
155 | private boolean getRandomBisection() { |
---|
156 | return Configuration.instance().isRandomBisection(); |
---|
157 | } |
---|
158 | |
---|
159 | private int getVerboseLevel() { |
---|
160 | return Configuration.instance().getVerboseLevel(); |
---|
161 | } |
---|
162 | |
---|
163 | private int getSizeFinerGraph() { |
---|
164 | return Configuration.instance().getSizeFinerGraph(); |
---|
165 | } |
---|
166 | |
---|
167 | private int getNumClusters() { |
---|
168 | return Configuration.instance().getNumberOfClusters(); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Returns an enumeration describing the available options. |
---|
173 | * |
---|
174 | * @return an enumeration of all the available options. |
---|
175 | */ |
---|
176 | @SuppressWarnings("unchecked") |
---|
177 | @Override |
---|
178 | public Enumeration listOptions() { |
---|
179 | Vector result = new Vector(); |
---|
180 | result.addElement(new Option("\tnumber of clusters.\n" |
---|
181 | + "\t(default 2).", "N", 1, "-N <num>")); |
---|
182 | result.addElement(new Option("\tsize of finer graph.\n" |
---|
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>")); |
---|
188 | return result.elements(); |
---|
189 | } |
---|
190 | |
---|
191 | private void setSizeFinerGraph(int size) { |
---|
192 | Configuration.instance().setSizeFinerGraph(size); |
---|
193 | } |
---|
194 | |
---|
195 | private void setNumClusters(int n) { |
---|
196 | Configuration.instance().setNumberOfClusters(n); |
---|
197 | } |
---|
198 | |
---|
199 | @Override |
---|
200 | public double[] distributionForInstance(Instance instance) throws Exception { |
---|
201 | double[] d = new double[numberOfClusters()]; |
---|
202 | d[clusterInstance(instance)] = 1.0; |
---|
203 | return d; |
---|
204 | } |
---|
205 | |
---|
206 | @Override |
---|
207 | public Capabilities getCapabilities() { |
---|
208 | Capabilities result = super.getCapabilities(); |
---|
209 | result.enable(Capability.NUMERIC_ATTRIBUTES); |
---|
210 | result.enable(Capability.NO_CLASS); |
---|
211 | return result; |
---|
212 | } |
---|
213 | |
---|
214 | @Override |
---|
215 | public int numberOfClusters() throws Exception { |
---|
216 | return Configuration.instance().getNumberOfClusters(); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Main method for executing this clusterer. |
---|
221 | * |
---|
222 | * @param args |
---|
223 | * the options, use "-h" to display options |
---|
224 | */ |
---|
225 | public static void main(String[] args) { |
---|
226 | AbstractClusterer.runClusterer(new MetisMQIClusterer(), args); |
---|
227 | } |
---|
228 | |
---|
229 | } |
---|