mCRL2
Loading...
Searching...
No Matches
pbessolve_vertex_set.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9/// \file mcrl2/pbes/pbessolve_vertex_set.h
10/// \brief add your file description here.
11
12#ifndef MCRL2_PBES_PBESSOLVE_VERTEX_SET_H
13#define MCRL2_PBES_PBESSOLVE_VERTEX_SET_H
14
15#include <deque>
16#include "mcrl2/pbes/structure_graph.h"
17
18namespace mcrl2::pbes_system {
19
21{
22 protected:
25
26 public:
27 deque_vertex_set() = default;
28
29 explicit deque_vertex_set(std::size_t N)
30 : m_include(N)
31 {}
32
33 template <typename Iter>
34 deque_vertex_set(std::size_t N, Iter first, Iter last)
35 : m_include(N)
36 {
37 for (auto i = first; i != last; ++i)
38 {
39 insert(*i);
40 }
41 }
42
43 bool is_empty() const
44 {
45 return m_vertices.empty();
46 }
47
48 bool contains(structure_graph::index_type u) const
49 {
50 return m_include[u];
51 }
52
53 void insert(structure_graph::index_type u)
54 {
55 assert(u < m_include.size());
56 if (m_include[u])
57 {
58 return;
59 }
60 m_vertices.push_back(u);
61 m_include[u] = true;
62 }
63
64 void clear()
65 {
66 m_vertices.clear();
67 m_include = boost::dynamic_bitset<>(m_include.size());
68 }
69
70 std::size_t extent() const
71 {
72 return m_include.size();
73 }
74
76 {
77 return m_vertices;
78 }
79
80 std::size_t size() const
81 {
82 return m_vertices.size();
83 }
84
85 const boost::dynamic_bitset<>& include() const
86 {
87 return m_include;
88 }
89
91 {
92 structure_graph::index_type u = m_vertices.front();
93 m_vertices.pop_front();
94 m_include[u] = false;
95 return u;
96 }
97
99 {
100 structure_graph::index_type u = m_vertices.back();
101 m_vertices.pop_back();
102 m_include[u] = false;
103 return u;
104 }
105
106 bool operator==(const deque_vertex_set& other) const
107 {
108 return m_include == other.m_include;
109 }
110
111 bool operator!=(const deque_vertex_set& other) const
112 {
113 return !(*this == other);
114 }
115};
116
118{
119 protected:
122
123 public:
124 bool self_check(const std::string& msg = "") const
125 {
126 if (!msg.empty())
127 {
128 std::cout << "--- " << msg << " ---" << std::endl;
129 }
130 for (structure_graph::index_type v: m_vertices)
131 {
132 if (v >= m_include.size())
133 {
134 throw mcrl2::runtime_error("The value " + std::to_string(v) + " is bigger than I.size() = " + std::to_string(m_include.size()));
135 }
136 if (!m_include[v])
137 {
138 throw mcrl2::runtime_error("Inconsistency detected with element " + std::to_string(v) + "!");
139 }
140 }
141 return true;
142 }
143
144 vertex_set() = default;
145
146 vertex_set(const vertex_set& other) = default;
147 vertex_set& operator=(const vertex_set& other) = default;
148
149 explicit vertex_set(std::size_t N)
150 : m_include(N)
151 {
152 assert(self_check());
153 }
154
155 template <typename Iter>
156 vertex_set(std::size_t N, Iter first, Iter last)
157 : m_include(N)
158 {
159 for (auto i = first; i != last; ++i)
160 {
161 insert(*i);
162 }
163 assert(self_check());
164 }
165
166 // resize to at least n elements
167 void resize(std::size_t n)
168 {
169 std::size_t m = m_include.size();
170 if (m < 1024)
171 {
172 m = 1024;
173 }
174 while (m < n)
175 {
176 m *= 2;
177 }
178 m_include.resize(m);
179 }
180
181 // truncate the size to n
182 void truncate(std::size_t n)
183 {
184 if (m_include.size() > n)
185 {
186 m_include = boost::dynamic_bitset<>(n);
187 for (auto u: m_vertices)
188 {
189 m_include[u] = true;
190 }
191 }
192 }
193
194 bool is_empty() const
195 {
196 return m_vertices.empty();
197 }
198
199 bool contains(structure_graph::index_type u) const
200 {
201 return m_include[u];
202 }
203
204 void insert(structure_graph::index_type u)
205 {
206 assert(u < m_include.size());
207 if (m_include[u])
208 {
209 return;
210 }
211 m_vertices.push_back(u);
212 m_include[u] = true;
213 assert(self_check());
214 }
215
216 /// @brief Sorts the set of vertices, but resets the include set.
217 void sort()
218 {
219 std::sort(m_vertices.begin(), m_vertices.end());
220 assert(self_check());
221 }
222
223 void clear()
224 {
225 m_vertices.clear();
226 m_include = boost::dynamic_bitset<>(m_include.size());
227 assert(self_check());
228 }
229
231 {
232 return m_include.size();
233 }
234
236 {
237 return m_vertices;
238 }
239
240 std::size_t size() const
241 {
242 return m_vertices.size();
243 }
244
245 const boost::dynamic_bitset<>& include() const
246 {
247 return m_include;
248 }
249
251 {
252 structure_graph::index_type u = m_vertices.back();
253 m_vertices.pop_back();
254 m_include[u] = false;
255 return u;
256 }
257
258 bool operator==(const vertex_set& other) const
259 {
260 return m_include == other.m_include;
261 }
262
263 bool operator!=(const vertex_set& other) const
264 {
265 return !(*this == other);
266 }
267};
268
269inline
271{
272 return out << core::detail::print_set(V.vertices());
273}
274
275inline
277{
278 vertex_set result = V;
279 for (structure_graph::index_type w: W.vertices())
280 {
281 result.insert(w);
282 }
283 return result;
284}
285
286// VertexSet can be either vertex_set or deque_vertex_set
287template <typename VertexSet>
288vertex_set set_intersection(const VertexSet& V, const vertex_set& W)
289{
290 vertex_set result(V.extent());
291 for (structure_graph::index_type v: V.vertices())
292 {
293 if (W.contains(v))
294 {
295 result.insert(v);
296 }
297 }
298 return result;
299}
300
301inline
303{
304 vertex_set result(V.extent());
305 for (structure_graph::index_type v: V.vertices())
306 {
307 if (!W.contains(v))
308 {
309 result.insert(v);
310 }
311 }
312 return result;
313}
314
316{
317 const vertex_set& A;
318 const vertex_set& B;
319
320 lazy_union(const vertex_set& A_, const vertex_set& B_)
321 : A(A_), B(B_)
322 {}
323
324 bool contains(structure_graph::index_type u) const
325 {
326 return A.contains(u) || B.contains(u);
327 }
328};
329
330inline
332{
333 return out << "(" << V.A << " U " << V.B << ")";
334}
335
336inline
337bool is_subset_of(const vertex_set& V, const vertex_set& W)
338{
339 return V.include().is_subset_of(W.include());
340}
341
342// returns a successor of u that is in A, or undefined_index if not found
343template <typename StructureGraph, typename VertexSet>
344structure_graph::index_type find_successor_in(const StructureGraph& G, structure_graph::index_type u, const VertexSet& A)
345{
346 for (auto v: G.successors(u))
347 {
348 if (A.contains(v))
349 {
350 return v;
351 }
352 }
353 mCRL2log(log::log_level_t::debug) << "No successor found for node " << u << " in " << A << std::endl;
354 return undefined_vertex();
355}
356
357template <typename StructureGraph>
358void log_vertex_set(const StructureGraph& G, const vertex_set& V, const std::string& name)
359{
360 mCRL2log(log::log_level_t::debug) << "--- " << name << " ---" << std::endl;
361 for (auto v: V.vertices())
362 {
363 mCRL2log(log::log_level_t::debug) << " " << v << " " << G.find_vertex(v) << std::endl;
364 }
365}
366
367// strategy vector that resizes automatically
369{
370 protected:
372
373 // resize to at least n elements
374 void resize(std::size_t n) const
375 {
376 std::size_t m = m_strategy.size();
377 if (m < 16)
378 {
379 m = 16;
380 }
381 while (m < n)
382 {
383 m *= 2;
384 }
385 m_strategy.resize(m, undefined_vertex());
386 }
387
388 public:
389 structure_graph::index_type operator[](std::size_t i) const
390 {
391 if (i >= m_strategy.size())
392 {
393 resize(i + 1);
394 }
395 return m_strategy[i];
396 }
397
398 structure_graph::index_type& operator[](std::size_t i)
399 {
400 if (i >= m_strategy.size())
401 {
402 resize(i + 1);
403 }
404 return m_strategy[i];
405 }
406
407 std::size_t size() const
408 {
409 return m_strategy.size();
410 }
411
412 // truncate the size to n
413 void truncate(std::size_t n)
414 {
415 if (m_strategy.size() > n)
416 {
417 m_strategy.erase(m_strategy.begin() + static_cast<std::ptrdiff_t>(n), m_strategy.end());
418 m_strategy.shrink_to_fit();
419 }
420 }
421
423 {
424 return m_strategy;
425 }
426};
427
428inline
430{
431 return out << core::detail::print_list(tau_alpha.strategy());
432}
433
434inline
436{
437 std::ostringstream out;
438 bool first = true;
439 for (structure_graph::index_type u: S_alpha.vertices())
440 {
441 if (tau_alpha[u] != undefined_vertex())
442 {
443 if (!first)
444 {
445 out << ", ";
446 }
447 out << u << " -> " << tau_alpha[u];
448 first = false;
449 }
450 }
451 return out.str();
452}
453
454template <typename StructureGraph>
456{
457 using utilities::detail::contains;
458
459 using vertex = structure_graph::vertex;
460 std::vector<const vertex*> result;
461
462 std::set<structure_graph::index_type> todo = { init };
463 std::set<structure_graph::index_type> done;
464 while (!todo.empty())
465 {
466 structure_graph::index_type u = *todo.begin();
467 todo.erase(todo.begin());
468 done.insert(u);
469 if ((S0.contains(u) && G.decoration(u) == structure_graph::decoration_type::d_disjunction) || (S1.contains(u) && G.decoration(u) == structure_graph::decoration_type::d_conjunction))
470 {
471 // explore only the strategy edge
472 structure_graph::index_type v = G.strategy(u);
473 assert (v != undefined_vertex());
474 if (!contains(done, v))
475 {
476 todo.insert(v);
477 }
478 }
479 else
480 {
481 // explore all outgoing edges
482 for (structure_graph::index_type v: G.successors(u))
483 {
484 if (!contains(done, v))
485 {
486 todo.insert(v);
487 }
488 }
489 }
490 }
491 mCRL2log(log::log_level_t::debug) << "Extracted minimal structure graph " << core::detail::print_set(done) << std::endl;
492 for (const auto& index : done) {
493 mCRL2log(log::log_level_t::debug) << std::setw(4) << index << " " << G.find_vertex(index) << std::endl;
494 }
495
496 return done;
497}
498
499template <typename StructureGraph>
503 const vertex_set& S0,
504 const vertex_set& S1,
505 const strategy_vector& tau0,
506 const strategy_vector& tau1
507)
508{
509 using utilities::detail::contains;
510
511 using vertex = structure_graph::vertex;
512 std::vector<const vertex*> result;
513
514 std::set<structure_graph::index_type> todo = { init };
515 std::set<structure_graph::index_type> done;
516 while (!todo.empty())
517 {
518 structure_graph::index_type u = *todo.begin();
519 todo.erase(todo.begin());
520 done.insert(u);
521 if (S0.contains(u) && G.decoration(u) == structure_graph::decoration_type::d_disjunction)
522 {
523 // explore only the strategy edge
524 structure_graph::index_type v = tau0[u];
525 assert (v != undefined_vertex());
526 if (!contains(done, v))
527 {
528 todo.insert(v);
529 }
530 }
531 else if (S1.contains(u) && G.decoration(u) == structure_graph::decoration_type::d_conjunction)
532 {
533 // explore only the strategy edge
534 structure_graph::index_type v = tau1[u];
535 assert (v != undefined_vertex());
536 if (!contains(done, v))
537 {
538 todo.insert(v);
539 }
540 }
541 else
542 {
543 // explore all outgoing edges
544 for (structure_graph::index_type v: G.successors(u))
545 {
546 if (!contains(done, v))
547 {
548 todo.insert(v);
549 }
550 }
551 }
552 }
553 mCRL2log(log::log_level_t::debug) << "\nExtracted minimal structure graph " << core::detail::print_set(done) << std::endl;
554 for (const auto& index : done) {
555 mCRL2log(log::log_level_t::debug) << std::setw(4) << index << " " << G.find_vertex(index) << std::endl;
556 }
557
558 return done;
559}
560
561template <typename StructureGraph>
563{
564 std::size_t n = G.extent();
565
566 vertex_set all(n);
567 for (std::size_t i = 0; i < n; i++)
568 {
569 all.insert(i);
570 }
571 vertex_set empty(n);
572
573 if (is_disjunctive)
574 {
575 return extract_minimal_structure_graph(G, init, all, empty);
576 }
577 else
578 {
579 return extract_minimal_structure_graph(G, init, empty, all);
580 }
581}
582
583} // namespace mcrl2::pbes_system
584
585
586
587#endif // MCRL2_PBES_PBESSOLVE_VERTEX_SET_H
const std::vector< structure_graph::index_type > & strategy() const
std::vector< structure_graph::index_type > m_strategy
structure_graph::index_type operator[](std::size_t i) const
structure_graph::index_type & operator[](std::size_t i)
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:435
std::ostream & operator<<(std::ostream &out, const vertex_set &V)
constexpr unsigned int undefined_vertex()
std::set< structure_graph::index_type > extract_minimal_structure_graph(StructureGraph &G, typename StructureGraph::index_type init, const vertex_set &S0, const vertex_set &S1, const strategy_vector &tau0, const strategy_vector &tau1)
std::set< structure_graph::index_type > extract_minimal_structure_graph(StructureGraph &G, typename StructureGraph::index_type init, bool is_disjunctive)
vertex_set set_union(const vertex_set &V, const vertex_set &W)
bool is_subset_of(const vertex_set &V, const vertex_set &W)
structure_graph::index_type find_successor_in(const StructureGraph &G, structure_graph::index_type u, const VertexSet &A)
std::string print_strategy_vector(const vertex_set &S_alpha, const strategy_vector &tau_alpha)
std::ostream & operator<<(std::ostream &out, const strategy_vector &tau_alpha)
std::set< structure_graph::index_type > extract_minimal_structure_graph(StructureGraph &G, typename StructureGraph::index_type init, const vertex_set &S0, const vertex_set &S1)
void log_vertex_set(const StructureGraph &G, const vertex_set &V, const std::string &name)
std::ostream & operator<<(std::ostream &out, const lazy_union &V)
vertex_set set_minus(const vertex_set &V, const vertex_set &W)
vertex_set set_intersection(const VertexSet &V, const vertex_set &W)
const boost::dynamic_bitset & include() const
const std::deque< structure_graph::index_type > & vertices() const
bool contains(structure_graph::index_type u) const
deque_vertex_set(std::size_t N, Iter first, Iter last)
bool operator!=(const deque_vertex_set &other) const
std::deque< structure_graph::index_type > m_vertices
void insert(structure_graph::index_type u)
structure_graph::index_type pop_back()
bool operator==(const deque_vertex_set &other) const
structure_graph::index_type pop_front()
lazy_union(const vertex_set &A_, const vertex_set &B_)
bool contains(structure_graph::index_type u) const
vertex_set & operator=(const vertex_set &other)=default
std::vector< structure_graph::index_type > m_vertices
bool operator!=(const vertex_set &other) const
void insert(structure_graph::index_type u)
bool operator==(const vertex_set &other) const
vertex_set(std::size_t N, Iter first, Iter last)
bool contains(structure_graph::index_type u) const
const std::vector< structure_graph::index_type > & vertices() const
vertex_set(const vertex_set &other)=default
const boost::dynamic_bitset & include() const
bool self_check(const std::string &msg="") const
void sort()
Sorts the set of vertices, but resets the include set.
structure_graph::index_type pop_back()