This repository was archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperNode.java
More file actions
145 lines (135 loc) · 4.71 KB
/
Copy pathSuperNode.java
File metadata and controls
145 lines (135 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.ArrayList;
public class SuperNode implements BeliefPropagation.BPNode<String>{
public int id; //node id
public int r; //range of values for 1 node
public boolean observed; //has node value been observed? i.e. encoded?
public String obsVal;
public ArrayList<Node> V; //nodes in supernode
public ArrayList<CliqueStructures.Clique> C; //cliques in supernode
public ArrayList<SuperEdge> dN; //connecting superedges
public double[] Z; //belief vector for supernode (based on larger alphabet size using x-ary enumeration) --> length of Z needs to ALWAYS be r^(# of unobserved items in V)
public SuperNode(int num, int range){
id = num;
r = range;
V = new ArrayList<Node>(0);
C = new ArrayList<CliqueStructures.Clique>(0);
dN = new ArrayList<SuperEdge>(0);
Z = null;
observed = false;
obsVal = "";
}
//find edges internal to the supernode from list
public void findInternalCliques(ArrayList<CliqueStructures.Clique> c){
ArrayList<CliqueStructures.Clique> c_copy = (ArrayList<CliqueStructures.Clique>)c.clone();
for(CliqueStructures.Clique clique : c_copy)
if(V.containsAll(clique.nodes)){
C.add(clique);
c.remove(clique); //this is to cut down on search time =(
}
}
//construct string of observed values from internal nodes
public String obs(){
observed = true;
obsVal = "";
for(int i=0; i<V.size(); i++)
obsVal = obsVal+V.get(i).VAL;
return obsVal;
}
//supernode self potential
public double npot(String Val){
double pot = 1.0;
//----- value of nodes by matching node index in list and val substring, hence order matters (i.e. string char index matches order of nodes in V)
for(CliqueStructures.Clique clique : C){
int [] cliqueVals = new int[clique.nodes.size()];
int inx;
for(int j=0; j<clique.nodes.size(); j++){
inx = V.indexOf(clique.nodes.get(j));
cliqueVals[j] = Integer.parseInt(Val.substring(inx,inx+1),r);
}
pot = pot * clique.pot.U(cliqueVals);
}
return pot;
}
/*
//newOnly - if there are old observed (encoded) items in the supernode, we only need to look at potentials of new items (since all the old ones simply form a scaling factor)
public double npot(String Val, boolean newOnly){
double pot = 1.0;
//----- value of nodes by matching node index in list and val substring, hence order matters (i.e. string char index matches order of nodes in V)
if(newOnly)
for(CliqueStructures.Clique clique : newItems){
int [] cliqueVals = new int[clique.nodes.size()];
int inx;
for(int j=0; j<clique.nodes.size(); j++){
inx = V.indexOf(clique.nodes.get(j));
cliqueVals[j] = Integer.parseInt(Val.substring(inx,inx+1),r);
}
pot = pot * clique.pot.U(cliqueVals);
}
else
for(CliqueStructures.Clique clique : C){
int [] cliqueVals = new int[clique.nodes.size()];
int inx;
for(int j=0; j<clique.nodes.size(); j++){
inx = V.indexOf(clique.nodes.get(j));
cliqueVals[j] = Integer.parseInt(Val.substring(inx,inx+1),r);
}
pot = pot * clique.pot.U(cliqueVals);
}
return pot;
}
*/
//belief propagation
public double[] Z(){
return Z(false);
}
public double[] Z(boolean fresh){
Z = new double[(int)Math.pow(r,V.size())];
String NodeVal;
for(int i=0; i<Z.length; i++){ //----- THIS IS WHY length of Z needs to ALWAYS be r^(# of unobserved items in V)
NodeVal = Utilities.toPString(i,V.size(),r);
Z[i] = npot(NodeVal);
//System.out.println("> NODEVALUE: "+NodeVal);
for(int j=0; j<dN.size(); j++){
/*
// DEBUG OUTPUT SEQUENCE
System.out.println("> "+dN.get(j).getOtherNode(this).id+": "+dN.get(j).getOtherNode(this).observed+" links: "+dN.get(j).C.size());
for(int k=0; k<dN.get(j).C.size(); k++){
System.out.print("\tlink "+k+" "+dN.get(j).C.get(k).type+" "+dN.get(j).C.get(k).pot.THETA[0]+":");
for(Node node : dN.get(j).C.get(k).nodes){
if(!V.contains(node))
System.out.print(" "+node.toString()+node.VAL);
else
System.out.print(" =>"+node.toString()+node.VAL);
}
System.out.println();
}
System.exit(0);
*/
if(id==dN.get(j).N1.id){
Z[i] = Z[i]*dN.get(j).get_m21(NodeVal,j,fresh);
//System.out.println("\t"+dN.get(j).get_m21(NodeVal,j,fresh));
}
else{
Z[i] = Z[i]*dN.get(j).get_m12(NodeVal,j,fresh);
//System.out.println("\t"+dN.get(j).get_m12(NodeVal,j,fresh));
}
}
}
return Z;
}
//output to string
public String toString(){
String Info = "<SUPERNODE#"+id+": ";
for(int i=0;i<V.size();i++)
if(i!=V.size()-1)
Info = Info + V.get(i).toString()+",";
else
Info = Info + V.get(i).toString()+" | ";
for(int i=0;i<C.size();i++)
if(i!=C.size()-1)
Info = Info + C.get(i).toString()+",";
else
Info = Info + C.get(i).toString()+">";
return Info;
}
}