1 | package tuffy.ground.partition; |
2 | |
3 | |
4 | import java.util.ArrayList; |
5 | import java.util.HashMap; |
6 | |
7 | import tuffy.infer.MRF; |
8 | import tuffy.util.StringMan; |
9 | /** |
10 | * A partitioning scheme on an MRF. Such a scheme consists of one or more components, with |
11 | * each component consisting of one or more partitions. Components are disjoint from each other, |
12 | * whereas partitions within the same component may share hyper-edges (i.e., clauses). |
13 | * In the current policy, each hyper-edge being shared across partitioned is randomly |
14 | * assigned to only one of the adjacent partitions. |
15 | * |
16 | */ |
17 | public class PartitionScheme { |
18 | /** |
19 | * Components and partitions. |
20 | * Each component or partition has a unique ID. |
21 | */ |
22 | public ArrayList<Component> components = new ArrayList<Component>(); |
23 | private HashMap<Integer, Component> compMap = new HashMap<Integer, Component>(); |
24 | private HashMap<Integer, Partition> partMap = new HashMap<Integer, Partition>(); |
25 | |
26 | /** |
27 | * Stats. |
28 | */ |
29 | private int ncomp = 0; |
30 | private int npart = 0; |
31 | public double totalSize = 0, maxCompSize = 0, maxPartSize = 0, |
32 | maxNumAtomsInComp = 0, maxNumAtomsInPart = 0; |
33 | private long numAtoms = 0, numClauses = 0, numCutClauses = 0; |
34 | private int numSplitComps = 0, maxSplitFactor = 1; |
35 | |
36 | |
37 | /** |
38 | * Show stats about this partitioning scheme. |
39 | */ |
40 | public String getStats(){ |
41 | ArrayList<String> lines = new ArrayList<String>(); |
42 | lines.add("------BEGIN: PARTITION STATS------"); |
43 | lines.add("#atoms = " + numAtoms); |
44 | lines.add("#clauses = " + numClauses); |
45 | lines.add("#components = " + ncomp); |
46 | lines.add("#partitions = " + npart); |
47 | |
48 | lines.add("#max_comp_size = " + maxCompSize); |
49 | lines.add("#max_part_size = " + maxPartSize); |
50 | lines.add("#split_component = " + numSplitComps); |
51 | lines.add("#max_num_atoms_in_comp = " + maxNumAtomsInComp); |
52 | lines.add("#max_num_atoms_in_part = " + maxNumAtomsInPart); |
53 | lines.add("#max_partitions_in_one_comp = " + maxSplitFactor); |
54 | lines.add("#cut_clauses = " + numCutClauses); |
55 | |
56 | lines.add("------ END: PARTITION STATS-------"); |
57 | return StringMan.join("\n", lines); |
58 | } |
59 | |
60 | public long getNumAtoms(){ |
61 | return numAtoms; |
62 | } |
63 | |
64 | public Component getCompByID(int id){ |
65 | return compMap.get(id); |
66 | } |
67 | |
68 | public Component getCompByPartID(int pid){ |
69 | return partMap.get(pid).parentComponent; |
70 | } |
71 | |
72 | public Partition getPartitionByID(int pid){ |
73 | return partMap.get(pid); |
74 | } |
75 | |
76 | public MRF getMRFByPartID(int pid){ |
77 | return partMap.get(pid).mrf; |
78 | } |
79 | |
80 | public PartitionScheme(ArrayList<Component> comps){ |
81 | components = comps; |
82 | ncomp = comps.size(); |
83 | npart = 0; |
84 | for(Component c : comps){ |
85 | npart += c.numParts(); |
86 | totalSize += c.size(); |
87 | numAtoms += c.numAtoms; |
88 | numClauses += c.numClauses; |
89 | numCutClauses += c.numCutClauses; |
90 | if(c.size() > maxCompSize){ |
91 | maxCompSize = c.size(); |
92 | } |
93 | if(c.numAtoms > maxNumAtomsInComp){ |
94 | maxNumAtomsInComp = c.numAtoms; |
95 | } |
96 | if(c.parts.size() > 1){ |
97 | numSplitComps++; |
98 | if(c.parts.size() > maxSplitFactor){ |
99 | maxSplitFactor = c.parts.size(); |
100 | } |
101 | } |
102 | compMap.put(c.id, c); |
103 | for(Partition p : c.parts){ |
104 | partMap.put(p.id, p); |
105 | if(p.numAtoms > maxNumAtomsInPart){ |
106 | maxNumAtomsInPart = p.numAtoms; |
107 | } |
108 | if(p.size() > maxPartSize){ |
109 | maxPartSize = p.size(); |
110 | } |
111 | } |
112 | } |
113 | } |
114 | |
115 | /** |
116 | * Estimated RAM size required to hold everything. |
117 | */ |
118 | public double size(){ |
119 | return totalSize; |
120 | } |
121 | |
122 | public int numComponents(){ |
123 | return ncomp; |
124 | } |
125 | |
126 | public int numParts(){ |
127 | return npart; |
128 | } |
129 | |
130 | |
131 | } |