1 | package tuffy.ground.partition; |
2 | |
3 | |
4 | import java.util.ArrayList; |
5 | import java.util.HashMap; |
6 | import java.util.HashSet; |
7 | |
8 | import tuffy.infer.ds.GAtom; |
9 | import tuffy.util.UIMan; |
10 | /** |
11 | * A component in the MRF. |
12 | */ |
13 | |
14 | public class Component implements Comparable<Component>{ |
15 | public int id = 0; |
16 | public int rep = 0; // representative atom id |
17 | public int numAtoms = 0; |
18 | public int numClauses = 0; |
19 | public int numCutClauses = 0; |
20 | public int numPins = 0; |
21 | public double totalWeight = 0; |
22 | public double totalCutWeight = 0; |
23 | public double ramSize = 0; |
24 | |
25 | // partitions in this component; maybe only one |
26 | public ArrayList<Partition> parts = new ArrayList<Partition>(); |
27 | // atoms that are at the boundary |
28 | public HashSet<Integer> cutset = new HashSet<Integer>(); |
29 | |
30 | // aid --> atom |
31 | public HashMap<Integer, GAtom> atoms; |
32 | |
33 | |
34 | /** |
35 | * Add a new atom into this component. |
36 | * @param a the atom |
37 | */ |
38 | public void addAtom(GAtom a){ |
39 | atoms.put(a.id, a); |
40 | } |
41 | |
42 | /** |
43 | * Discard all data structures to reclaim the RAM. |
44 | */ |
45 | public void discard(){ |
46 | cutset.clear(); |
47 | atoms.clear(); |
48 | atoms = null; |
49 | for(Partition p : parts){ |
50 | p.discard(); |
51 | } |
52 | } |
53 | |
54 | /** |
55 | * Show basic stats of this component. |
56 | */ |
57 | public void showStats(){ |
58 | String s = "[Component #" + id + "]" + |
59 | "\n\tRAM = " + ramSize + " bytes" + |
60 | "\n\t#parts = " + parts.size() + |
61 | "\n\t#atoms = " + numAtoms + |
62 | "\n\t#clauses = " + numClauses + |
63 | "\n\t#cut_atoms = " + cutset.size(); |
64 | UIMan.println(s); |
65 | } |
66 | |
67 | public int compareTo(Component c){ |
68 | double d = c.size() - size(); |
69 | return (int)(Math.signum(d)); |
70 | } |
71 | |
72 | /** |
73 | * The size of this component estimated in |
74 | * the number fo bytes consumed to store this component in RAM. |
75 | */ |
76 | public double size(){ |
77 | return ramSize; |
78 | } |
79 | |
80 | /** |
81 | * Get the number of partitions in this component. |
82 | */ |
83 | public int numParts(){ |
84 | return parts.size(); |
85 | } |
86 | } |