-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutvpi_graph-inl.h
346 lines (280 loc) · 8.97 KB
/
utvpi_graph-inl.h
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#ifndef UTVPI_GRAPH_INL_H
#define UTVPI_GRAPH_INL_H
#include <unordered_set>
#include <boost/graph/bellman_ford_shortest_paths.hpp>
#include <boost/graph/strong_components.hpp>
#include <boost/graph/lookup_edge.hpp>
template <typename T>
void UtvpiGraph<T>::Print() {
EdgeIter iter, end;
Vertex src, trg;
for (tie(iter, end) = boost::edges(graph_); iter != end; ++iter) {
src = boost::source(*iter, graph_);
trg = boost::target(*iter, graph_);
/* Suppress printing the special vertex. It's highly confusing since
* its corresponding SignedVarId is initialized to 0.. */
if (src == special_vertex_)
continue;
std::cout << graph_[src]
<< "(" << src << ")"
<< " goes with weight "
<< graph_[*iter]
<< " to "
<< graph_[trg]
<< "(" << trg << ")"
<< " because "
<< *reasons_[std::make_pair(src, trg)]
<< std::endl;
}
}
/* Looks up a vertex for a given variable with sign and if it doesn't exist,
* creates it. Returns the vertex. */
template <typename T>
typename UtvpiGraph<T>::Vertex
UtvpiGraph<T>::LookupAddVertex(Sign sign, VarId var_x) {
SignedVarId s_var = SignedVarId(var_x, sign);
typename VertexMap::const_iterator iter = vertex_map_.find(s_var);
Vertex x;
if (iter == vertex_map_.end()) {
x = boost::add_vertex(graph_);
vertex_map_[s_var] = x;
/* Every vertex must have an edge from the special_vertex_ with weight 0. */
Edge e = boost::add_edge(special_vertex_, x, graph_).first;
graph_[e] = 0;
}
else {
x = iter->second;
}
/* Record the actual variable with sign. */
graph_[x] = s_var;
return x;
}
/* Assumes that the vertices exist in the graph. */
template <typename T>
typename UtvpiGraph<T>::Edge
UtvpiGraph<T>::UpdateAddEdge(Vertex src, T weight, Vertex trg, ReasonPtr reason) {
Edge edge;
bool exists;
tie(edge, exists) = boost::lookup_edge(src, trg, this->graph_);
if (exists) {
/* Found the edge! Update the weight if necessary. */
if (weight < graph_[edge]) {
AddWeightRollback(src, graph_[edge], trg,
reasons_[std::make_pair(src, trg)]);
graph_[edge] = weight;
reasons_[std::make_pair(src, trg)] = reason;
}
} else {
/* We haven't found the edge, so we need to create it. */
edge = boost::add_edge(src, trg, graph_).first;
graph_[edge] = weight;
reasons_[std::make_pair(src, trg)] = reason;
AddEdgeRollback(src, trg, reasons_[std::make_pair(src, trg)]);
}
return edge;
}
template <typename T>
bool UtvpiGraph<T>::FindEdge(Vertex src, Vertex trg, Edge& edge) {
OutEdgeIter iter, end;
for (boost::tie(iter, end) = boost::out_edges(src, graph_);
iter != end; ++iter) {
if (boost::target(*iter, graph_) == trg) {
edge = *iter;
return true;
}
}
return false;
}
template <typename T>
void UtvpiGraph<T>::SetEdge(Vertex src, T weight, Vertex trg) {
Edge edge;
bool found = FindEdge(src, trg, edge);
if (found) {
graph_[edge] = weight;
}
assert(found);
}
template <typename T>
void UtvpiGraph<T>::RemoveEdge(Vertex src, Vertex trg) {
Edge edge;
bool found = FindEdge(src, trg, edge);
if (found) {
boost::remove_edge(edge, graph_);
}
assert(found);
}
/*
* For every new constraint we need to add two edges:
* - for x - y <= k we add y+ -k-> x+ and x- -k-> y-
* - for x + y <= k we add y- -k-> x+ and x- -k-> y+
* - for -x - y <= k we add y+ -k-> x- and x+ -k-> y-
* - for -x + y <= k we add y- -k-> x- and x+ -k-> y+
* Now we can express this a bit more succintly as
* - for ax + by <= k we add y(-b) -k-> xa and x(-a) -k-> yb
*/
template <typename T>
void UtvpiGraph<T>::AddInequality(Sign a, VarId var_x, Sign b, VarId var_y, T c) {
Vertex x = LookupAddVertex(a, var_x);
Vertex y = LookupAddVertex(b, var_y);
Vertex x_neg = LookupAddVertex(negate(a), var_x);
Vertex y_neg = LookupAddVertex(negate(b), var_y);
std::shared_ptr<Reason> reason(new Inequality2<T>(a, var_x, b, var_y, c));
UpdateAddEdge(y_neg, c, x, reason);
UpdateAddEdge(x_neg, c, y, reason);
}
/*
void AddUpdateReason(Vertex x, Vertex y, std::shared_ptr<Reason> reason) {
reasons_[std::make_pair(x, y)] = reason;
}
*/
/*
* This is a simple inequality. We need to add just one edge:
* - for x <= k we add x- -2k-> x+
* - for -x <= k we add x+ -2k-> x-
* Now we can express this a bit more succintly as
* - for ax <= k we add x(-a) -2k-> xa
* Note: the weight is multiplied by 2!
*/
template <typename T>
void UtvpiGraph<T>::AddInequality(Sign a, VarId var_x, T c) {
Vertex x = LookupAddVertex(a, var_x);
Vertex x_neg = LookupAddVertex(negate(a), var_x);
std::shared_ptr<Reason> reason(new Inequality1<T>(a, var_x, c));
UpdateAddEdge(x_neg, 2 * c, x, reason);
}
template <typename T>
void UtvpiGraph<T>::AddEquality(VarId var_x, VarId var_y) {
Vertex x = LookupAddVertex(Pos, var_x);
Vertex y = LookupAddVertex(Pos, var_y);
std::shared_ptr<Reason> reason(new Equality(var_x, var_y));
UpdateAddEdge(x, 0, y, reason);
UpdateAddEdge(y, 0, x, reason);
}
template <typename T>
void UtvpiGraph<T>::AddEdgeRollback(Vertex src, Vertex trg, ReasonPtr reason) {
/* We assume that there was a push callback from Z3 and the list has been
* allocated. */
assert(logs_.front() != NULL);
Rollback *r = new EdgeRollback(src, trg, reason);
logs_.front()->push_front(r);
}
template <typename T>
void UtvpiGraph<T>::AddWeightRollback(Vertex src, T weight, Vertex trg, ReasonPtr reason) {
/* We assume that there was a push callback from Z3 and the list has been
* allocated. */
assert(logs_.front() != NULL);
Rollback *r = new WeightRollback(src, weight, trg, reason);
logs_.front()->push_front(r);
}
template <typename T>
void UtvpiGraph<T>::Pop() {
#ifdef DEBUG
std::cout << "UtvpiGraph: Pop" << std::endl;
#endif
std::list<Rollback *> *list = logs_.front();
for (auto r : *list) {
r->Execute(*this);
delete r;
}
logs_.pop_front();
}
template <typename T>
void UtvpiGraph<T>::Push() {
#ifdef DEBUG
std::cout << "UtvpiGraph: Push" << std::endl;
#endif
logs_.push_front(new std::list<Rollback *>());
}
/**
* Clear everything. The state is the same as just after constructing the graph.
*/
template <typename T>
void UtvpiGraph<T>::Reset() {
#ifdef DEBUG
std::cout << "UtvpiGraph: Reset" << std::endl;
#endif
graph_.clear();
vertex_map_.clear();
logs_.clear();
logs_.push_front(new std::list<Rollback *>());
special_vertex_ = boost::add_vertex(graph_);
}
template <typename T>
std::list<ReasonPtr>*
UtvpiGraph<T>::GetNegativeCycle(const Edge &start_edge,
const std::vector<Vertex> &parent) {
std::unordered_set<Vertex> so_far;
Vertex cycle_vertex = boost::target(start_edge, this->graph_);
while (so_far.find(cycle_vertex) == so_far.end()) {
so_far.insert(cycle_vertex);
cycle_vertex = parent[cycle_vertex];
}
/* Record the vertex that starts/ends the cycle. */
Vertex src, trg = cycle_vertex;
Edge edge;
auto cycle = new std::list<ReasonPtr>();
do {
src = parent[trg];
ReasonPtr ptr = this->reasons_[std::make_pair(src, trg)];
assert(ptr != NULL);
cycle->push_front(ptr);
trg = src;
} while (trg != cycle_vertex);
#ifdef VERBOSE
std::cout << "UtvpiGraph: Reason for negative cycle:" << std::endl;
for (auto r : *cycle) {
std::cout << "\t" << *r << std::endl;
}
#endif
cycle->unique();
return cycle;
}
template <typename T>
std::list<ReasonPtr>*
UtvpiGraph<T>::GetNegativeCycle(const Vertex &start_vertex,
const Vertex &neg_start_vertex, const Graph &tmp_graph,
const ReasonMap &tmp_reasons) {
std::size_t graph_size = boost::num_vertices(tmp_graph);
std::vector<T> distance(graph_size, 0);
std::vector<Vertex> parent(graph_size, 0);
std::vector<Vertex> weights(graph_size, 0);
/* FIXME: check for exceptions from tmp_reasons.at(..) */
BfsVisitor vis(parent, neg_start_vertex);
try {
breadth_first_search(tmp_graph, start_vertex, boost::visitor(vis));
assert(false);
} catch (bool x) { }
auto cycle = new std::list<ReasonPtr>();
Vertex src, trg = neg_start_vertex;
do {
src = parent[trg];
ReasonPtr ptr = tmp_reasons.at(std::make_pair(src, trg));
assert(ptr != NULL);
cycle->push_front(ptr);
trg = src;
} while (trg != start_vertex);
/* We can use parent vector without any resetting --- the parents should be
* simply overwritten. */
vis.SetTarget(start_vertex);
try {
breadth_first_search(tmp_graph, neg_start_vertex, boost::visitor(vis));
assert(false);
} catch (bool x) { }
trg = start_vertex;
do {
src = parent[trg];
ReasonPtr ptr = tmp_reasons.at(std::make_pair(src, trg));
assert(ptr != NULL);
cycle->push_front(ptr);
trg = src;
} while (trg != neg_start_vertex);
#ifdef VERBOSE
std::cout << "UtvpiGraph: Reason for negative cycle:" << std::endl;
for (auto r : *cycle) {
std::cout << *r << std::endl;
}
#endif
cycle->unique();
return cycle;
}
#endif /* UTVPI_GRAPH_INL_H */