-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
277 lines (227 loc) · 7.2 KB
/
Copy pathgraph.cpp
File metadata and controls
277 lines (227 loc) · 7.2 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "graph.hpp"
Graph::~Graph() {
// Clean up memory
for(auto& [name, rv] : _rvs) {
delete rv;
}
for(auto& factor : _factors) {
delete factor;
}
}
RV* Graph::rv(const std::string& name, int n_opts,
const std::vector<std::string>& labels,
std::map<std::string, bool> meta,
bool debug) {
RV* rv = new RV(name, n_opts, labels, meta, debug);
rv->init_lbp();
add_rv(rv);
return rv;
}
void Graph::add_rv(RV* rv) {
rv->meta["pruned"] = false;
_rvs[rv->name] = rv;
_just_rvs.push_back(rv);
}
int Graph::remove_loner_rvs() {
int removed = 0;
std::vector<std::string> to_remove;
for(const auto& [name, rv] : _rvs) {
if(rv->n_edges() == 0) {
rv->meta["pruned"] = true;
to_remove.push_back(name);
removed++;
}
}
for(const auto& name : to_remove) {
delete _rvs[name];
_rvs.erase(name);
}
return removed;
}
Factor* Graph::factor(std::vector<RV*> rvs,
const std::string& name,
const Eigen::MatrixXd* potential,
const double& weight,
std::map<std::string, bool> meta,
bool debug) {
// Convert string RV names to RV pointers if needed
for(auto& rv : rvs) {
if(rv == nullptr) {
throw std::runtime_error("Invalid RV pointer");
}
}
// std::cout << "In factor " << weight <<std::endl;
Factor* f = new Factor(rvs, name, potential, weight, meta, debug);
f->init_lbp();
add_factor(f);
if(rvs.size() == 1){
// std::cout << rvs[0]->name << " " << potential->size() << std::endl;
add_node_factor(rvs[0]->name, f);
}
return f;
}
double Graph::joint(const std::map<std::string, int>& x) const {
if(debug) {
// Check assignments
assert(x.size() == _rvs.size());
for(const auto& [name, label] : x) {
auto it = _rvs.find(name);
assert(it != _rvs.end());
assert(it->second->has_label(label));
}
for(const auto& [name, rv] : _rvs) {
assert(x.find(name) != x.end());
assert(rv->has_label(x.at(name)));
}
}
double prod = 1.0;
for(const auto& f : _factors) {
prod *= f->eval(x);
}
return prod;
}
std::pair<std::map<std::string, int>, double> Graph::bf_best_joint() {
std::map<std::string, int> assigned;
std::vector<RV*> todo;
for(const auto& [name, rv] : _rvs) {
todo.push_back(rv);
}
return _bf_bj_recurse(assigned, todo);
}
std::pair<std::map<std::string, int>, double> Graph::_bf_bj_recurse(
std::map<std::string, int>& assigned,
std::vector<RV*> todo) {
if(todo.empty()) {
return {assigned, joint(assigned)};
}
// Try all options for first RV
RV* rv = todo.front();
todo.erase(todo.begin());
std::map<std::string, int> best_a;
double best_r = 0.0;
bool first = true;
for(int val = 0; val < rv->n_opts; ++val) {
auto new_a = assigned;
new_a[rv->name] = val;
auto [full_a, r] = _bf_bj_recurse(new_a, todo);
if(first || r > best_r) {
best_r = r;
best_a = full_a;
first = false;
}
}
return {best_a, best_r};
}
std::vector<RV*> Graph::_sorted_nodes() const {
std::vector<RV*> nodes;
// std::cout << _rvs.size() << std::endl;
for(const auto& [name, rv] : _rvs) {
nodes.push_back(rv);
}
std::sort(nodes.begin(), nodes.end(),
[](const RV* a, const RV* b) {
return a->n_edges() < b->n_edges();
});
return nodes;
}
std::pair<int, bool> Graph::lbp(bool init, bool normalize,
int max_iters, bool progress) {
auto nodes = _sorted_nodes();
// auto nodes = _just_rvs;
if(init) {
init_messages(nodes);
}
int cur_iter = 0;
bool converged = false;
// while(cur_iter < max_iters && !converged && !E_STOP) {
while(cur_iter < max_iters && !converged) {
cur_iter++;
converged = true;
for(auto* n : nodes) {
bool n_converged = n->recompute_outgoing(normalize);
converged = converged && n_converged;
}
for(auto* n : _factors) {
bool n_converged = n->recompute_outgoing(normalize);
converged = converged && n_converged;
}
// std::cout << "Iteration " << cur_iter << " converged: " << converged << std::endl;
}
return {cur_iter, converged};
}
void Graph::init_messages(const std::vector<RV*>& nodes) {
auto n = nodes.empty() ? _sorted_nodes() : nodes;
for(auto* node : n) {
node->init_lbp();
}
}
void Graph::print_sorted_nodes() const {
auto nodes = _sorted_nodes();
for(const auto* node : nodes) {
std::cout << node->name << " ";
}
std::cout << std::endl;
}
void Graph::print_messages(const std::vector<RV*>& nodes) const {
auto n = nodes.empty() ? _sorted_nodes() : nodes;
std::cout << "Current outgoing messages:" << std::endl;
for(const auto* node : n) {
node->print_messages();
}
}
std::vector<std::pair<RV*, Eigen::VectorXd>> Graph::rv_marginals(
const std::vector<RV*>& rvs,
bool normalize) {
std::vector<RV*> rv_list = rvs.empty() ? _sorted_nodes() : rvs;
std::vector<std::pair<RV*, Eigen::VectorXd>> tuples;
for(auto* rv : rv_list) {
auto [marg, _] = rv->get_belief();
if(normalize) {
double sum = marg.sum();
if(sum != 0) {
marg /= sum;
}
}
tuples.emplace_back(rv, marg);
}
return tuples;
}
void Graph::print_rv_marginals(const std::vector<RV*>& rvs,
bool normalize) const {
std::string disp = "Marginals for RVs";
if(normalize) {
disp += " (normalized)";
}
disp += ":";
std::cout << disp << std::endl;
auto tuples = const_cast<Graph*>(this)->rv_marginals(rvs, normalize);
for(const auto& [rv, marg] : tuples) {
std::cout << rv->name << std::endl;
std::vector<std::string> vals;
if(rv->labels.empty()) {
for(int i = 0; i < rv->n_opts; ++i) {
vals.push_back(std::to_string(i));
}
} else {
vals = rv->labels;
}
for(size_t i = 0; i < vals.size(); ++i) {
std::cout << "\t" << vals[i] << "\t" << marg(i) << std::endl;
}
}
}
std::map<std::string, Eigen::VectorXd> Graph::get_rv_marginals() {
auto tuples = const_cast<Graph*>(this)->rv_marginals(_just_rvs, true);
std::map<std::string, Eigen::VectorXd> rtn;
for(const auto& [rv, marg] : tuples) {
// std::cout << rv->name << std::endl;
rtn[rv->name] = marg;
}
return rtn;
}
void Graph::update_factor_marginals(std::map<std::string, Eigen::VectorXd>& node_factor_marginals){
for (auto [node, p] : node_factor_marginals){
// Eigen::MatrixXd mat = p.transpose();
_node_factors[node]->set_potential(p);
}
}