spot  2.5.3
game.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2017 Laboratoire de Recherche et Développement
3 // de l'Epita (LRDE).
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #pragma once
21 
22 #include <algorithm>
23 #include <memory>
24 #include <ostream>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include <bddx.h>
29 #include <spot/twa/twagraph.hh>
30 #include <spot/twaalgos/parity.hh>
31 
32 namespace spot
33 {
34 
35 class SPOT_API parity_game
36 {
37 private:
38  const const_twa_graph_ptr dpa_;
39  const std::vector<bool> owner_;
40 
41 public:
49  parity_game(const twa_graph_ptr dpa, std::vector<bool> owner)
50  : dpa_(dpa), owner_(owner)
51  {
52  bool max;
53  bool odd;
54  dpa_->acc().is_parity(max, odd, true);
55  SPOT_ASSERT(max && odd);
56  SPOT_ASSERT(owner_.size() == dpa_->num_states());
57  }
58 
59  unsigned num_states() const
60  {
61  return dpa_->num_states();
62  }
63 
64  unsigned get_init_state_number() const
65  {
66  return dpa_->get_init_state_number();
67  }
68 
70  out(unsigned src) const
71  {
72  return dpa_->out(src);
73  }
74 
76  out(unsigned src)
77  {
78  return dpa_->out(src);
79  }
80 
81  bool owner(unsigned src) const
82  {
83  return owner_[src];
84  }
85 
86  unsigned max_parity() const
87  {
88  unsigned max_parity = 0;
89  for (auto& e: dpa_->edges())
90  max_parity = std::max(max_parity, e.acc.max_set());
91  SPOT_ASSERT(max_parity);
92  return max_parity - 1;
93  }
94 
96  void print(std::ostream& os);
97 
98  typedef std::unordered_set<unsigned> region_t;
99  // Map state number to index of the transition to take.
100  typedef std::unordered_map<unsigned, unsigned> strategy_t;
101 
104 
116  std::pair<region_t, strategy_t> solve() const;
117 
120 
141  bool solve_qp() const;
142 
143 private:
145 
146  // Compute (in place) a set of states from which player can force a visit
147  // through set, and a strategy to do it.
148  // if attr_max is true, states that can force a visit through an edge with
149  // max parity are also counted in.
150  strategy_t attractor(const region_t& subgame, region_t& set,
151  unsigned max_parity, bool odd,
152  bool attr_max = false) const;
153 
154  // Compute the winning strategy and winning region for player 1.
155  std::pair<region_t, strategy_t>
156  solve_rec(region_t& subgame, unsigned max_parity) const;
157 };
158 
159 
161 {
162 private:
163  unsigned num_;
164  std::vector<unsigned> b_;
165  bool anke_;
166 
167 public:
168  reachability_state(unsigned state, const std::vector<unsigned>& b,
169  bool anke)
170  : num_(state), b_(b), anke_(anke)
171  {
172  }
173 
174  int compare(const state* other) const override;
175 
176  bool operator==(const reachability_state& o) const
177  {
178  return compare(&o) == 0;
179  }
180 
181  bool operator!=(const reachability_state& o) const
182  {
183  return compare(&o) != 0;
184  }
185 
186  bool operator<(const reachability_state& o) const;
187 
188  size_t hash() const override
189  {
190  size_t hash = wang32_hash(num_);
191  for (unsigned i = 0; i < b_.size(); ++i)
192  hash ^= wang32_hash(b_[i]) ^ wang32_hash(i);
193  return hash;
194  }
195 
196  reachability_state* clone() const override
197  {
198  return new reachability_state(*this);
199  }
200 
201  std::vector<unsigned> b() const
202  {
203  return b_;
204  }
205 
206  unsigned num() const
207  {
208  return num_;
209  }
210 
211  bool anke() const
212  {
213  return anke_;
214  }
215 };
216 
217 typedef std::shared_ptr<const reachability_state>
218  const_reachability_state_ptr;
219 
221 {
222  size_t operator()(const reachability_state& state) const
223  {
224  return state.hash();
225  }
226 };
227 
229 {
230 private:
231  const parity_game& pg_;
232  const reachability_state& state_;
234 
235 public:
237  const reachability_state& s)
238  : pg_(pg), state_(s)
239  {
240  }
241 
242  bool first() override
243  {
244  it_ = pg_.out(state_.num()).begin();
245  return it_ != pg_.out(state_.num()).end();
246  }
247 
248  bool next() override
249  {
250  ++it_;
251  return it_ != pg_.out(state_.num()).end();
252  }
253 
254  bool done() const override
255  {
256  return it_ == pg_.out(state_.num()).end();
257  }
258 
259  const reachability_state* dst() const override;
260 
261  bdd cond() const override
262  {
263  return bddtrue;
264  }
265 
266  acc_cond::mark_t acc() const override
267  {
268  return 0;
269  }
270 };
271 
272 // On-the-fly reachability game interface for a max even parity game such
273 // that a target is reachable iff there is a memoryless winning strategy
274 // in the parity game for player 1.
275 class reachability_game final: public twa
276 {
277 private:
278  typedef std::unordered_map<spot::reachability_state, unsigned,
279  spot::reachability_state_hash> wincount_t;
280  typedef std::unordered_map<spot::reachability_state,
281  std::vector<spot::reachability_state>,
283 
284  const parity_game& pg_;
285  // number of successors that need to have a winning strategy in order for
286  // a given node to have a winning strategy.
287  wincount_t c_;
288  parents_t parents_;
289  const_reachability_state_ptr init_state_; // cache
290 
291 public:
292 
293  reachability_game(const parity_game& pg)
294  : twa(std::make_shared<bdd_dict>()), pg_(pg)
295  {
296  init_state_ = std::shared_ptr<const reachability_state>(get_init_state());
297  }
298 
299  const reachability_state* get_init_state() const override;
300 
301  reachability_game_succ_iterator* succ_iter(const state* s) const override;
302 
303  std::string format_state(const state* s) const override;
304 
305  bool is_reachable();
306 
307 private:
308  bool mark(const spot::reachability_state& s);
309 
310  bool is_target(const reachability_state& s);
311 
312 };
313 
314 }
Definition: automata.hh:26
Definition: game.hh:220
size_t wang32_hash(size_t key)
Thomas Wang&#39;s 32 bit hash function.
Definition: hashfunc.hh:40
Definition: game.hh:35
Definition: game.hh:160
acc_cond::mark_t acc() const override
Get the acceptance mark of the edge leading to this successor.
Definition: game.hh:266
bool done() const override
Check whether the iteration is finished.
Definition: game.hh:254
Definition: graph.hh:243
A Transition-based ω-Automaton.
Definition: twa.hh:622
Abstract class for states.
Definition: twa.hh:50
Definition: graph.hh:184
bdd cond() const override
Get the condition on the edge leading to this successor.
Definition: game.hh:261
reachability_state * clone() const override
Duplicate a state.
Definition: game.hh:196
Definition: game.hh:275
Iterate over the successors of a state.
Definition: twa.hh:397
parity_game(const twa_graph_ptr dpa, std::vector< bool > owner)
Definition: game.hh:49
bool next() override
Jump to the next successor (if any).
Definition: game.hh:248
bool first() override
Position the iterator on the first successor (if any).
Definition: game.hh:242
Definition: graph.hh:385
size_t hash() const override
Hash a state.
Definition: game.hh:188
Definition: acc.hh:40

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.8.13