spot  2.6
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  bool operator==(const reachability_state& o) const
170  {
171  return compare(&o) == 0;
172  }
173 
174  bool operator!=(const reachability_state& o) const
175  {
176  return compare(&o) != 0;
177  }
178 
179  bool operator<(const reachability_state& o) const;
180 
181  size_t hash() const override
182  {
183  size_t hash = wang32_hash(num_);
184  for (unsigned i = 0; i < b_.size(); ++i)
185  hash ^= wang32_hash(b_[i]) ^ wang32_hash(i);
186  return hash;
187  }
188 
189  reachability_state* clone() const override
190  {
191  return new reachability_state(*this);
192  }
193 
194  std::vector<unsigned> b() const
195  {
196  return b_;
197  }
198 
199  unsigned num() const
200  {
201  return num_;
202  }
203 
204  bool anke() const
205  {
206  return anke_;
207  }
208 };
209 
210 typedef std::shared_ptr<const reachability_state>
211  const_reachability_state_ptr;
212 
214 {
215  size_t operator()(const reachability_state& state) const
216  {
217  return state.hash();
218  }
219 };
220 
222 {
223 private:
224  const parity_game& pg_;
225  const reachability_state& state_;
227 
228 public:
230  const reachability_state& s)
231  : pg_(pg), state_(s)
232  {
233  }
234 
235  bool first() override
236  {
237  it_ = pg_.out(state_.num()).begin();
238  return it_ != pg_.out(state_.num()).end();
239  }
240 
241  bool next() override
242  {
243  ++it_;
244  return it_ != pg_.out(state_.num()).end();
245  }
246 
247  bool done() const override
248  {
249  return it_ == pg_.out(state_.num()).end();
250  }
251 
252  const reachability_state* dst() const override;
253 
254  bdd cond() const override
255  {
256  return bddtrue;
257  }
258 
259  acc_cond::mark_t acc() const override
260  {
261  return {};
262  }
263 };
264 
265 // On-the-fly reachability game interface for a max even parity game such
266 // that a target is reachable iff there is a memoryless winning strategy
267 // in the parity game for player 1.
268 class reachability_game final: public twa
269 {
270 private:
271  typedef std::unordered_map<spot::reachability_state, unsigned,
272  spot::reachability_state_hash> wincount_t;
273  typedef std::unordered_map<spot::reachability_state,
274  std::vector<spot::reachability_state>,
276 
277  const parity_game& pg_;
278  // number of successors that need to have a winning strategy in order for
279  // a given node to have a winning strategy.
280  wincount_t c_;
281  parents_t parents_;
282  const_reachability_state_ptr init_state_; // cache
283 
284 public:
285 
286  reachability_game(const parity_game& pg)
287  : twa(std::make_shared<bdd_dict>()), pg_(pg)
288  {
289  init_state_ = std::shared_ptr<const reachability_state>(get_init_state());
290  }
291 
292  const reachability_state* get_init_state() const override;
293 
294  reachability_game_succ_iterator* succ_iter(const state* s) const override;
295 
296  std::string format_state(const state* s) const override;
297 
298  bool is_reachable();
299 
300 private:
301  bool mark(const spot::reachability_state& s);
302 
303  bool is_target(const reachability_state& s);
304 
305 };
306 
307 }
Definition: automata.hh:26
Definition: game.hh:213
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:259
bool done() const override
Check whether the iteration is finished.
Definition: game.hh:247
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:254
reachability_state * clone() const override
Duplicate a state.
Definition: game.hh:189
Definition: game.hh:268
Iterate over the successors of a state.
Definition: twa.hh:397
bool next() override
Jump to the next successor (if any).
Definition: game.hh:241
bool first() override
Position the iterator on the first successor (if any).
Definition: game.hh:235
Definition: graph.hh:385
size_t hash() const override
Hash a state.
Definition: game.hh:181
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