mCRL2
Loading...
Searching...
No Matches
ComponentSolver.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2013 University of Twente
2// Copyright (c) 2009-2013 Michael Weber <michaelw@cs.utwente.nl>
3// Copyright (c) 2009-2013 Maks Verver <maksverver@geocities.com>
4// Copyright (c) 2009-2013 Eindhoven University of Technology
5//
6// Distributed under the Boost Software License, Version 1.0.
7// (See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
11#include "mcrl2/pg/attractor.h"
12
14 const ParityGame &game, ParityGameSolverFactory &pgsf,
15 int max_depth, const verti *vmap, verti vmap_size )
16 : ParityGameSolver(game), pgsf_(pgsf), max_depth_(max_depth),
17 vmap_(vmap), vmap_size_(vmap_size)
18{
19 pgsf_.ref();
20}
21
23{
24 pgsf_.deref();
25}
26
28{
29 verti V = game_.graph().V();
30 strategy_.assign(V, NO_VERTEX);
31 DenseSet<verti> W0(0, V), W1(0, V);
32 winning_[0] = &W0;
33 winning_[1] = &W1;
34 if (decompose_graph(game_.graph(), *this) != 0) strategy_.clear();
35 winning_[0] = NULL;
36 winning_[1] = NULL;
38 result.swap(strategy_);
39 return result;
40}
41
42int ComponentSolver::operator()(const verti *vertices, std::size_t num_vertices)
43{
44 if (aborted()) return -1;
45
46 assert(num_vertices > 0);
47
48 // Filter out solved vertices:
49 std::vector<verti> unsolved;
50 unsolved.reserve(num_vertices);
51 for (std::size_t n = 0; n < num_vertices; ++n)
52 {
53 verti v = vertices[n];
54 if (!winning_[0]->count(v) && !winning_[1]->count(v))
55 {
56 unsolved.push_back(vertices[n]);
57 }
58 }
59 mCRL2log(mcrl2::log::verbose) << "SCC of size " << num_vertices << " with "
60 << unsolved.size() << " unsolved vertices..." << std::endl;
61
62 if (unsolved.empty()) return 0;
63
64 // Construct a subgame for unsolved vertices in this component:
65 ParityGame subgame;
66 subgame.make_subgame(game_, unsolved.begin(), unsolved.end(), true);
67
68 ParityGame::Strategy substrat;
69 if (max_depth_ > 0 && unsolved.size() < num_vertices)
70 {
71 mCRL2log(mcrl2::log::verbose) << "Recursing on subgame of size "
72 << unsolved.size() << "..." << std::endl;
73 ComponentSolver(subgame, pgsf_, max_depth_ - 1).solve().swap(substrat);
74 }
75 else
76 {
77 // Compress vertex priorities
78 {
79 std::size_t old_d = subgame.d();
80 subgame.compress_priorities();
81 std::size_t new_d = subgame.d();
82 if (old_d != new_d)
83 {
84 mCRL2log(mcrl2::log::verbose) << "Priority compression removed "
85 << old_d - new_d << " of "
86 << old_d << " priorities" << std::endl;
87 }
88 }
89
90 // Solve the subgame
91 mCRL2log(mcrl2::log::verbose) << "Solving subgame of size "
92 << unsolved.size() << "..." << std::endl;
93 std::vector<verti> submap; // declared here so it survives subsolver
94 std::unique_ptr<ParityGameSolver> subsolver;
95 if (vmap_size_ > 0)
96 {
97 submap = unsolved;
98 merge_vertex_maps(submap.begin(), submap.end(), vmap_, vmap_size_);
99 subsolver.reset(
100 pgsf_.create(subgame, &submap[0], submap.size()) );
101 }
102 else
103 {
104 subsolver.reset(
105 pgsf_.create(subgame, &unsolved[0], unsolved.size()) );
106 }
107 subsolver->solve().swap(substrat);
108 }
109 if (substrat.empty()) return -1; // solving failed
110
111 mCRL2log(mcrl2::log::verbose) << "Merging strategies..." << std::endl;
112 merge_strategies(strategy_, substrat, unsolved);
113
114 mCRL2log(mcrl2::log::verbose) << "Building attractor sets for winning regions..." << std::endl;
115
116 // Extract winning sets from subgame:
117 std::deque<verti> todo[2];
118 for (std::size_t n = 0; n < unsolved.size(); ++n)
119 {
120 ParityGame::Player pl = subgame.winner(substrat, n);
121 verti v = unsolved[n];
122 winning_[pl]->insert(v);
123 todo[pl].push_back(v);
124 }
125
126 // Extend winning sets to attractor sets:
127 for (int player = 0; player < 2; ++player)
128 {
130 *winning_[player], todo[player], strategy_ );
131 }
132
133 mCRL2log(mcrl2::log::verbose) << "Leaving." << std::endl;
134 return 0;
135}
136
138 const verti *vertex_map, verti vertex_map_size )
139{
140 return new ComponentSolver( game, pgsf_, max_depth_,
141 vertex_map, vertex_map_size );
142}
std::size_t verti
type used to number vertices
Definition Graph.h:24
#define NO_VERTEX
Definition Graph.h:27
void merge_vertex_maps(ForwardIterator begin, ForwardIterator end, const verti *old_map, verti old_map_size)
void merge_strategies(std::vector< verti > &strategy, const std::vector< verti > &substrat, const std::vector< verti > &vertex_map)
player_t
Definition ParityGame.h:35
void make_attractor_set(const ParityGame &game, ParityGame::Player player, SetT &vertices, StrategyT &strategy)
bool aborted()
Returns whether this instance has been aborted.
Definition Abortable.h:25
const int max_depth_
Maximum recursion depth.
ParityGameSolver * create(const ParityGame &game, const verti *vertex_map, verti vertex_map_size)
Return a new ComponentSolver instance.
ParityGameSolverFactory & pgsf_
Factory used to create subsolvers.
ComponentSolver(const ParityGame &game, ParityGameSolverFactory &pgsf, int max_depth, const verti *vmap=0, verti vmap_size=0)
const verti vmap_size_
Size of vertex map.
const int max_depth_
Max. recusion depth.
ParityGame::Strategy solve()
ParityGameSolverFactory & pgsf_
Solver factory to use.
DenseSet< verti > * winning_[2]
Resulting winning sets.
const verti * vmap_
Current vertex map.
int operator()(const verti *vertices, std::size_t num_vertices)
ParityGame::Strategy strategy_
Resulting strategy.
std::pair< iterator, bool > insert(const Key &k)
Definition DenseSet.h:171
virtual ParityGameSolver * create(const ParityGame &game, const verti *vertex_map=NULL, verti vertex_map_size=0)=0
const ParityGame & game_
Game being solved.
priority_t d() const
Definition ParityGame.h:253
void make_subgame(const ParityGame &game, ForwardIterator vertices_begin, ForwardIterator vertices_end, bool proper, StaticGraph::EdgeDirection edge_dir=StaticGraph::EDGE_NONE)
Player winner(const StrategyT &s, verti v) const
void compress_priorities(const verti cardinality[]=0, bool preserve_parity=true)
const StaticGraph & graph() const
Definition ParityGame.h:256
std::vector< verti > Strategy
Definition ParityGame.h:97
void deref() const
Decrement reference count and delete the object if it becomes zero.
Definition RefCounted.h:39
void ref() const
Increment reference count.
Definition RefCounted.h:36
verti V() const
Definition Graph.h:179
int decompose_graph(const StaticGraph &graph, Callback &callback)
Definition SCC.h:32
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:391
@ verbose
Definition logger.h:37