-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzone_graph.hh
449 lines (395 loc) · 18.8 KB
/
zone_graph.hh
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#pragma once
#include <boost/algorithm/cxx11/none_of.hpp>
#include <boost/algorithm/cxx11/any_of.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/optional.hpp>
#include <type_traits>
#include "dbm.hh"
#include "constraint.hh"
#include "timed_automaton.hh"
template<class SignalVariables, class ClockVariables, class Value>
struct BoostZoneGraphState {
//! @brief The corresponding state in the TA
typename BoostTimedAutomaton<SignalVariables, ClockVariables>::vertex_descriptor vertex;
/*!
* @brief The flag showing if one can fire a (discrete) transition. This is used to forbid having multiple jumps at the same time
* @note In the current implementation, this flag is unnecessary because we have the following:
* @code{.cpp}
* jumpable == !(valuations.empty())
* @endcode
*/
bool jumpable;
//! @brief The corresponding zone
DBM zone;
//! @brief The signal valuations observed after the latest (discrete) transition
std::vector<std::vector<Value>> valuations;
};
//! The type of the vertices must be listS because we might remove them.
// https://www.boost.org/doc/libs/1_68_0/libs/graph/doc/adjacency_list.html
template<class SignalVariables, class ClockVariables, class Weight, class Value>
using BoostZoneGraph = boost::adjacency_list<boost::listS, boost::listS, boost::directedS, BoostZoneGraphState<SignalVariables, ClockVariables, Value>, boost::property<boost::edge_weight_t, Weight>>;
template<class SignalVariables, class ClockVariables, class Weight, class Value>
void zoneConstruction(const BoostTimedAutomaton<SignalVariables, ClockVariables> &TA,
const std::vector<typename BoostTimedAutomaton<SignalVariables, ClockVariables>::vertex_descriptor> &initStatesTA,
BoostZoneGraph<SignalVariables, ClockVariables, Weight, Value> &ZG,
std::vector<typename BoostZoneGraph<SignalVariables, ClockVariables, Weight, Value>::vertex_descriptor> &initStatesZG) {
using TA_t = BoostTimedAutomaton<SignalVariables, ClockVariables>;
using ZG_t = BoostZoneGraph<SignalVariables, ClockVariables, Weight, Value>;
boost::unordered_map<std::pair<typename TA_t::vertex_descriptor, DBM::Tuple>, typename ZG_t::vertex_descriptor> toZGState;
const auto max_constraints = boost::get_property(TA, boost::graph_max_constraints);
const auto num_of_vars = boost::get_property(TA, boost::graph_num_of_vars);
auto zeroDBM = DBM::zero(num_of_vars + 1);
zeroDBM.M = Bounds{max_constraints, true};
initStatesZG.reserve(initStatesTA.size());
for (const auto &initState: initStatesTA) {
auto v = boost::add_vertex(ZG);
ZG[v].vertex = initState;
ZG[v].zone = zeroDBM;
initStatesZG.push_back(v);
toZGState[std::make_pair(initState, zeroDBM.toTuple())] = v;
}
auto nextConf = initStatesZG;
while (!nextConf.empty()) {
auto currentConf = std::move(nextConf);
nextConf.clear();
for (auto ¤tZGState : currentConf) {
auto taState = ZG[currentZGState].vertex;
DBM nowZone = ZG[currentZGState].zone;
nowZone.elapse();
for (auto range = boost::out_edges(taState, TA); range.first != range.second; range.first++) {
const auto edge = *range.first;
DBM nextZone = nowZone;
auto nextState = boost::target(edge, TA);
const auto guard = TA[edge].guard;
for (const auto &delta : guard) {
switch (delta.odr) {
case Constraint<ClockVariables>::Order::lt:
nextZone.tighten(delta.x,-1,{delta.c, false});
case Constraint<ClockVariables>::Order::le:
nextZone.tighten(delta.x,-1,{delta.c, true});
break;
case Constraint<ClockVariables>::Order::gt:
nextZone.tighten(-1,delta.x,{-delta.c, false});
case Constraint<ClockVariables>::Order::ge:
nextZone.tighten(-1,delta.x,{-delta.c, true});
break;
}
}
if (nextZone.isSatisfiable()) {
for (auto x : TA[edge].resetVars.resetVars) {
nextZone.reset(x);
}
nextZone.abstractize();
nextZone.canonize();
auto zgState = toZGState.find(std::make_pair(nextState, nextZone.toTuple()));
if (zgState != toZGState.end()) {
// targetStateInZA is already added
boost::add_edge(currentZGState, zgState->second, ZG);
} else {
// targetStateInZA is new
auto zgState = boost::add_vertex(ZG);
ZG[zgState].vertex = nextState;
ZG[zgState].zone = nextZone;
toZGState[std::make_pair(nextState, nextZone.toTuple())] = zgState;
boost::add_edge(currentZGState, zgState, ZG);
nextConf.push_back (zgState);
}
}
}
}
}
}
/*!
@brief Zone construction with an additional clock variable.
@tparam SignalVariables
@tparam ClockVariables
@tparam CostFunction
@tparam Weight
@param [in] TA A timed automaton.
@param [in] initConfTA Initial configuarion of the timed automaton
@param [in] cost A cost function.
@param [in] valuation A data valuation
@param [in] duartion A length of the signal
@param [out] ZG The zone graph with weight.
@param [out] initStatesZG The initial states of the zone graph.
*/
template<class SignalVariables, class ClockVariables, class Weight, class Value>
void zoneConstructionWithT(const BoostTimedAutomaton<SignalVariables, ClockVariables> &TA,
const std::vector<std::pair<BoostZoneGraphState<SignalVariables, ClockVariables, Value>, Weight>> &initConfTA,
const std::function<Weight(const std::vector<Constraint<ClockVariables>> &,const std::vector<std::vector<Value>> &)> &cost,
const std::vector<Value> &valuation,
const double duration,
BoostZoneGraph<SignalVariables, ClockVariables, Weight, Value> &ZG,
std::unordered_map<typename BoostZoneGraph<SignalVariables, ClockVariables, Weight, Value>::vertex_descriptor,Weight> &initStatesZG) {
using TA_t = BoostTimedAutomaton<SignalVariables, ClockVariables>;
using ZG_t = BoostZoneGraph<SignalVariables, ClockVariables, Weight, Value>;
using TAState = typename TA_t::vertex_descriptor;
boost::unordered_map<std::tuple<typename TA_t::vertex_descriptor, bool, DBM::Tuple, std::vector<std::vector<Value>>>, typename ZG_t::vertex_descriptor> toZGState;
// const double max_constraints = std::max<double>(ceil(duration), boost::get_property(TA, boost::graph_max_constraints));
#ifdef DEBUG
const auto num_of_vars = boost::get_property(TA, boost::graph_num_of_vars);
#endif
const auto convToKey = [] (BoostZoneGraphState<SignalVariables, ClockVariables, Value> x) {
return std::make_tuple(x.vertex, x.jumpable, x.zone.toTuple(), x.valuations);
};
const auto dwellTimeClockVar = initConfTA.front().first.zone.getNumOfVar() - 1;
std::vector<typename BoostZoneGraph<SignalVariables, ClockVariables, Weight, Value>::vertex_descriptor> nextConf;
nextConf.reserve(initConfTA.size());
initStatesZG.clear();
for (const auto &initState: initConfTA) {
auto v = boost::add_vertex(ZG);
ZG[v] = initState.first;
// the zone must contain the new clock variable T for the dwell time.
// we admit > ... + 1 to use this function for timed pattern matching too.
#ifdef DEBUG
assert(ZG[v].zone.getNumOfVar() >= num_of_vars + 1);
assert(!TA[initState.first.vertex].isMatch);
assert(!TA[ZG[v].vertex].isMatch);
#endif
ZG[v].zone.tighten(dwellTimeClockVar, -1, {duration, true});
initStatesZG[v] = initState.second;
nextConf.push_back(v);
toZGState[convToKey(ZG[v])] = v;
}
const auto addEdge = [&toZGState,&ZG,&nextConf,&TA,&cost,&convToKey] (const auto currentZGState, const auto nextTAState, const bool jumpable, const DBM &zone, const std::vector<std::vector<Value>> &nextValuations) -> bool {
auto zgState = toZGState.find(std::make_tuple(nextTAState, jumpable, zone.toTuple(), nextValuations));
typename ZG_t::edge_descriptor edge;
const bool isNew = zgState == toZGState.end();
if (!isNew) {
// targetStateInZA is already added
// TODO: we can merge some edges
edge = std::get<0>(boost::add_edge(currentZGState, zgState->second, ZG));
} else {
// targetStateInZA is new
auto nextZGState = boost::add_vertex(ZG);
ZG[nextZGState].vertex = nextTAState;
ZG[nextZGState].jumpable = jumpable;
ZG[nextZGState].zone = zone;
ZG[nextZGState].valuations = nextValuations;
toZGState[convToKey(ZG[nextZGState])] = nextZGState;
edge = std::get<0>(boost::add_edge(currentZGState, nextZGState, ZG));
if (!jumpable) {
nextConf.push_back (nextZGState);
}
#ifdef DEBUG
assert((toZGState.find(convToKey(ZG[nextZGState])) != toZGState.end()));
#endif
}
if (!jumpable) {
boost::put(boost::edge_weight, ZG, edge, cost(TA[ZG[currentZGState].vertex].label,
ZG[currentZGState].valuations));
} else {
boost::put(boost::edge_weight, ZG, edge, Weight::one());
}
return isNew;
};
while (!nextConf.empty()) {
#ifdef DEBUG
assert(std::all_of(nextConf.begin(), nextConf.end(), [&ZG](auto p) {
return boost::algorithm::any_of_equal(boost::vertices(ZG), p);}));
#endif
auto currentConf = std::move(nextConf);
nextConf.clear();
std::unordered_set<typename decltype(currentConf)::value_type> removedVertices;
for (const auto ¤tZGState : currentConf) {
// OPTIMIZATION: This find is unnecessary if currentConf is std::list (I can remove an element during its iteration)
if (removedVertices.find(currentZGState) != removedVertices.end()) {
continue;
}
#ifdef DEBUG
assert(boost::algorithm::any_of_equal(boost::vertices(ZG), currentZGState));
#endif
auto taState = ZG[currentZGState].vertex;
bool jumpable = ZG[currentZGState].jumpable;
DBM nowZone = ZG[currentZGState].zone;
if (nowZone.value.cols() == 0) {
// when the vertex does not exist
continue;
}
nowZone.tighten(dwellTimeClockVar, -1, {duration, true});
const auto listDiscreteTransitions =
[&TA,&taState] (const DBM& nowZone, std::vector<std::pair<TAState, DBM>> &v) {
// discrete transition
for (auto range = boost::out_edges(taState, TA); range.first != range.second; range.first++) {
const auto edge = *range.first;
DBM nextZone = nowZone;
auto nextTAState = boost::target(edge, TA);
const auto guard = TA[edge].guard;
for (const auto &delta : guard) {
switch (delta.odr) {
case Constraint<ClockVariables>::Order::lt:
nextZone.tightenWithoutClose(delta.x,-1,{delta.c, false});
case Constraint<ClockVariables>::Order::le:
nextZone.tightenWithoutClose(delta.x,-1,{delta.c, true});
break;
case Constraint<ClockVariables>::Order::gt:
nextZone.tightenWithoutClose(-1,delta.x,{-delta.c, false});
case Constraint<ClockVariables>::Order::ge:
nextZone.tightenWithoutClose(-1,delta.x,{-delta.c, true});
}
}
if (nextZone.isSatisfiable()) {
for (auto x : TA[edge].resetVars.resetVars) {
nextZone.reset(x);
}
v.emplace_back(nextTAState, nextZone);
}
}
};
if (jumpable) {
// discrete transition
std::vector<std::pair<TAState, DBM>> nextTAStates;
listDiscreteTransitions(nowZone, nextTAStates);
if (nextTAStates.empty()) {
// If there is no out going transition, it checks if there is a transition later.
auto futureZone = nowZone;
futureZone.elapse();
std::vector<std::pair<TAState, DBM>> futureTAStates;
listDiscreteTransitions(nowZone, futureTAStates);
// If there is no transition in the future, the current state is useless and we remove it.
if (futureTAStates.empty()) {
const auto nextZGStateP = toZGState.find(convToKey(ZG[currentZGState]));
if (nextZGStateP != toZGState.end()) {
initStatesZG.erase(nextZGStateP->second);
clear_vertex(nextZGStateP->second, ZG);
remove_vertex(nextZGStateP->second, ZG);
removedVertices.insert(nextZGStateP->second);
toZGState.erase(nextZGStateP);
}
#ifdef DEBUG
assert(std::none_of(initStatesZG.begin(), initStatesZG.end(), [&](auto p) {
return TA[ZG[p.first].vertex].isMatch;
}));
assert(std::all_of(initStatesZG.begin(), initStatesZG.end(), [&](auto p) {
return boost::algorithm::any_of_equal(boost::vertices(ZG), p.first);
}));
#endif
}
continue;
}
for (auto &p: nextTAStates) {
addEdge(currentZGState, std::move(p.first), false, std::move(p.second), {});
}
} else {
// continuous transition
auto nextValuations = ZG[currentZGState].valuations;
nextValuations.push_back(valuation);
nowZone.elapse();
nowZone.tighten(dwellTimeClockVar, -1, {duration, true});
if (!nowZone.isSatisfiableWithoutCanonize()) {
continue;
}
std::vector<std::pair<TAState, DBM>> nextTAStates;
listDiscreteTransitions(nowZone, nextTAStates);
// We add the state only if it has a next state
if (!nextTAStates.empty()) {
bool isNew = addEdge(currentZGState, ZG[currentZGState].vertex, true, nowZone, nextValuations);
if (isNew) {
const auto nextZGStateP = toZGState.find(std::make_tuple(ZG[currentZGState].vertex, true, nowZone.toTuple(), nextValuations));
for (auto &p: nextTAStates) {
addEdge(nextZGStateP->second, std::move(p.first), false, std::move(p.second), {});
}
}
} else {
// if the state is accepting, the state is useful even if it has no outgoing transition.
if (TA[ZG[currentZGState].vertex].isMatch) {
continue;
}
// If there is no out going transition, it checks if there is a transition later.
auto futureZone = nowZone;
futureZone.elapse();
std::vector<std::pair<TAState, DBM>> futureTAStates;
listDiscreteTransitions(nowZone, futureTAStates);
// If there is no transition in the future, the current state is useless and we remove it.
if (futureTAStates.empty()) {
const auto nextZGStateP = toZGState.find(convToKey(ZG[currentZGState]));
// If we cannot go out and the state already exists, we remove the state.
if (nextZGStateP != toZGState.end()) {
initStatesZG.erase(nextZGStateP->second);
clear_vertex(nextZGStateP->second, ZG);
remove_vertex(nextZGStateP->second, ZG);
removedVertices.insert(nextZGStateP->second);
toZGState.erase(nextZGStateP);
}
}
}
// nowZone.canonize();
#ifdef DEBUG
assert(std::none_of(initStatesZG.begin(), initStatesZG.end(), [&](auto p) {
return TA[ZG[p.first].vertex].isMatch;
}));
assert(std::all_of(initStatesZG.begin(), initStatesZG.end(), [&](auto p) {
return boost::algorithm::any_of_equal(boost::vertices(ZG), p.first);
}));
#endif
}
#ifdef DEBUG
assert(std::none_of(initStatesZG.begin(), initStatesZG.end(), [&](auto p) {
return TA[ZG[p.first].vertex].isMatch;
}));
assert(std::all_of(initStatesZG.begin(), initStatesZG.end(), [&](auto p) {
return boost::algorithm::any_of_equal(boost::vertices(ZG), p.first);
}));
#endif
}
#ifdef DEBUG
assert(std::none_of(initStatesZG.begin(), initStatesZG.end(), [&](auto p) {
return TA[ZG[p.first].vertex].isMatch;
}));
assert(std::all_of(initStatesZG.begin(), initStatesZG.end(), [&](auto p) {
return boost::algorithm::any_of_equal(boost::vertices(ZG), p.first);
}));
#endif
// remove the removedVertices from nextConf
nextConf.erase(std::remove_if(nextConf.begin(), nextConf.end(), [&removedVertices](auto v) {
return boost::algorithm::any_of_equal(removedVertices, v);
}), nextConf.end());
}
}
template <class Graph>
struct weight_label_writer {
weight_label_writer(const Graph& g) : graph_(g) {}
template <class Edge>
void operator()(std::ostream& out, const Edge& edge) const
{
write(out, edge, boost::get(boost::edge_weight, graph_, edge));
}
private:
template <class Edge, class Weight>
void write(std::ostream& out, const Edge&, const Weight& weight) const
{
out << "[weight="
<< weight.data
<< ", label="
<< weight.data
<< "]";
}
const Graph& graph_;
};
template <class Graph>
inline weight_label_writer<Graph> make_weight_label_writer(const Graph& g)
{
return weight_label_writer<Graph>(g);
}
template <class ZoneGraph, class TimedAutomaton, class Weight>
struct ZoneGraphLabelWriter {
ZoneGraphLabelWriter(const ZoneGraph& ZG, const TimedAutomaton& TA, const std::unordered_map<typename ZoneGraph::vertex_descriptor, Weight> &distance) : ZG(ZG), TA(TA), distance(distance) {}
template <class Vertex>
void operator()(std::ostream& out, const Vertex& vertex) const
{
out << "[label=\""
<< ZG[vertex].vertex << "/"
<< TA[ZG[vertex].vertex].isMatch << "/"
<< distance.at(vertex).data
<< "\"]";
}
private:
const ZoneGraph& ZG;
const TimedAutomaton& TA;
const std::unordered_map<typename ZoneGraph::vertex_descriptor, Weight> &distance;
};
template <class ZoneGraph, class TimedAutomaton, class Weight>
inline ZoneGraphLabelWriter<ZoneGraph, TimedAutomaton, Weight> makeZoneGraphLabelWriter(const ZoneGraph& ZG, const TimedAutomaton& TA, const std::unordered_map<typename ZoneGraph::vertex_descriptor, Weight> &distance)
{
return ZoneGraphLabelWriter<ZoneGraph, TimedAutomaton, Weight>(ZG, TA, distance);
}