spot  2.6.2
game.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2017-2018 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 arena_;
39  const std::vector<bool> owner_;
40 
41 public:
50  parity_game(const twa_graph_ptr& arena, const std::vector<bool>& owner);
51 
52  unsigned num_states() const
53  {
54  return arena_->num_states();
55  }
56 
57  unsigned get_init_state_number() const
58  {
59  return arena_->get_init_state_number();
60  }
61 
63  out(unsigned src) const
64  {
65  return arena_->out(src);
66  }
67 
69  out(unsigned src)
70  {
71  return arena_->out(src);
72  }
73 
74  bool owner(unsigned src) const
75  {
76  return owner_[src];
77  }
78 
79  unsigned max_parity() const
80  {
81  unsigned max_parity = 0;
82  for (const auto& e: arena_->edges())
83  max_parity = std::max(max_parity, e.acc.max_set());
84  SPOT_ASSERT(max_parity);
85  return max_parity - 1;
86  }
87 
89  void print(std::ostream& os);
90 
91  typedef std::unordered_set<unsigned> region_t;
92  // Map state number to index of the transition to take.
93  typedef std::unordered_map<unsigned, unsigned> strategy_t;
94 
97 
109  void solve(region_t (&w)[2], strategy_t (&s)[2]) const;
110 
113 
134  bool solve_qp() const;
135 
136 private:
138 
139  // Compute (in place) a set of states from which player can force a visit
140  // through set, and a strategy to do it.
141  // if attr_max is true, states that can force a visit through an edge with
142  // max parity are also counted in.
143  strategy_t attractor(const region_t& subgame, region_t& set,
144  unsigned max_parity, int odd,
145  bool attr_max = false) const;
146 
147  // Compute the winning strategy and winning region for both players.
148  void solve_rec(region_t& subgame, unsigned max_parity,
149  region_t (&w)[2], strategy_t (&s)[2]) const;
150 };
151 
152 
154 {
155 private:
156  unsigned num_;
157  std::vector<unsigned> b_;
158  bool anke_;
159 
160 public:
161  reachability_state(unsigned state, const std::vector<unsigned>& b,
162  bool anke)
163  : num_(state), b_(b), anke_(anke)
164  {
165  }
166 
167  int compare(const state* other) const override;
168 
169  reachability_state& operator=(const reachability_state& other)
170  {
171  num_ = other.num_;
172  b_ = other.b_;
173  anke_ = other.anke_;
174  return *this;
175  }
176 
178  : num_(other.num_), b_(other.b_), anke_(other.anke_)
179  {
180  }
181 
182  bool operator==(const reachability_state& o) const
183  {
184  return compare(&o) == 0;
185  }
186 
187  bool operator!=(const reachability_state& o) const
188  {
189  return compare(&o) != 0;
190  }
191 
192  bool operator<(const reachability_state& o) const;
193 
194  size_t hash() const override
195  {
196  size_t hash = wang32_hash(num_);
197  for (unsigned i = 0; i < b_.size(); ++i)
198  hash ^= wang32_hash(b_[i]) ^ wang32_hash(i);
199  return hash;
200  }
201 
202  reachability_state* clone() const override
203  {
204  return new reachability_state(*this);
205  }
206 
207  std::vector<unsigned> b() const
208  {
209  return b_;
210  }
211 
212  unsigned num() const
213  {
214  return num_;
215  }
216 
217  bool anke() const
218  {
219  return anke_;
220  }
221 };
222 
223 typedef std::shared_ptr<const reachability_state>
224  const_reachability_state_ptr;
225 
227 {
228  size_t operator()(const reachability_state& state) const
229  {
230  return state.hash();
231  }
232 };
233 
235 {
236 private:
237  const parity_game& pg_;
238  const reachability_state& state_;
240 
241 public:
243  const reachability_state& s)
244  : pg_(pg), state_(s)
245  {
246  }
247 
248  bool first() override
249  {
250  it_ = pg_.out(state_.num()).begin();
251  return it_ != pg_.out(state_.num()).end();
252  }
253 
254  bool next() override
255  {
256  ++it_;
257  return it_ != pg_.out(state_.num()).end();
258  }
259 
260  bool done() const override
261  {
262  return it_ == pg_.out(state_.num()).end();
263  }
264 
265  const reachability_state* dst() const override;
266 
267  bdd cond() const override
268  {
269  return bddtrue;
270  }
271 
272  acc_cond::mark_t acc() const override
273  {
274  return {};
275  }
276 };
277 
278 // On-the-fly reachability game interface for a max even parity game such
279 // that a target is reachable iff there is a memoryless winning strategy
280 // in the parity game for player 1.
281 class reachability_game final: public twa
282 {
283 private:
284  typedef std::unordered_map<spot::reachability_state, unsigned,
285  spot::reachability_state_hash> wincount_t;
286  typedef std::unordered_map<spot::reachability_state,
287  std::vector<spot::reachability_state>,
289 
290  const parity_game& pg_;
291  // number of successors that need to have a winning strategy in order for
292  // a given node to have a winning strategy.
293  wincount_t c_;
294  parents_t parents_;
295  const_reachability_state_ptr init_state_; // cache
296 
297 public:
298 
299  reachability_game(const parity_game& pg)
300  : twa(std::make_shared<bdd_dict>()), pg_(pg)
301  {
302  init_state_ = std::shared_ptr<const reachability_state>(get_init_state());
303  }
304 
305  const reachability_state* get_init_state() const override;
306 
307  reachability_game_succ_iterator* succ_iter(const state* s) const override;
308 
309  std::string format_state(const state* s) const override;
310 
311  bool is_reachable();
312 
313 private:
314  bool mark(const spot::reachability_state& s);
315 
316  bool is_target(const reachability_state& s);
317 
318 };
319 
320 }
Definition: automata.hh:26
Definition: game.hh:226
size_t wang32_hash(size_t key)
Thomas Wang&#39;s 32 bit hash function.
Definition: hashfunc.hh:41
Definition: game.hh:35
Definition: game.hh:153
acc_cond::mark_t acc() const override
Get the acceptance mark of the edge leading to this successor.
Definition: game.hh:272
bool done() const override
Check whether the iteration is finished.
Definition: game.hh:260
Definition: graph.hh:242
A Transition-based ω-Automaton.
Definition: twa.hh:622
Abstract class for states.
Definition: twa.hh:50
Definition: graph.hh:183
bdd cond() const override
Get the condition on the edge leading to this successor.
Definition: game.hh:267
reachability_state * clone() const override
Duplicate a state.
Definition: game.hh:202
Definition: game.hh:281
Iterate over the successors of a state.
Definition: twa.hh:397
bool next() override
Jump to the next successor (if any).
Definition: game.hh:254
bool first() override
Position the iterator on the first successor (if any).
Definition: game.hh:248
Definition: graph.hh:384
size_t hash() const override
Hash a state.
Definition: game.hh:194
Definition: acc.hh:55

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