symbolic
state.h
1 
10 #ifndef SYMBOLIC_STATE_H_
11 #define SYMBOLIC_STATE_H_
12 
13 #define SYMBOLIC_STATE_USE_SET
14 
15 #include <Eigen/Eigen>
16 #include <exception> // std::exception
17 #include <functional> // std::hash
18 #include <optional> // std::optional
19 #include <ostream> // std::ostream
20 #include <unordered_map> // std::unordered_map
21 #include <unordered_set> // std::unordered_set
22 #include <utility> // std::pair
23 
24 #ifndef SYMBOLIC_STATE_USE_SET
25 #include "symbolic/utils/unique_vector.h"
26 #else // SYMBOLIC_STATE_USE_SET
27 #include "symbolic/utils/hash_set.h"
28 #endif // SYMBOLIC_STATE_USE_SET
29 
30 #include "symbolic/proposition.h"
31 
32 namespace symbolic {
33 
34 class Predicate;
35 
36 #ifndef SYMBOLIC_STATE_USE_SET
37 class State : private UniqueVector<Proposition> {
38  using Base = UniqueVector<Proposition>;
39 #else // SYMBOLIC_STATE_USE_SET
40 class State : private HashSet<Proposition> {
41  using Base = HashSet<Proposition>;
42 #endif // SYMBOLIC_STATE_USE_SET
43 
44  public:
45  using iterator = Base::iterator;
46  using const_iterator = Base::const_iterator;
47 
48  State() = default;
49  State(std::initializer_list<Proposition> l) : Base(l){};
50 
51  State(const Pddl& pddl, const std::unordered_set<std::string>& str_state);
52 
56  bool contains(const PropositionBase& prop) const {
57  return Base::contains(prop);
58  }
59 
64  bool insert(const PropositionBase& prop) { return Base::insert(prop); }
65  bool insert(Proposition&& prop) { return Base::insert(std::move(prop)); }
66 
67  template <class InputIt>
68  bool insert(InputIt first, InputIt last);
69 
74  template <class... Args>
75  bool emplace(Args&&... args) {
76  return insert(Proposition(args...));
77  }
78 
83  bool erase(const PropositionBase& prop) { return Base::erase(prop); }
84 
85  const_iterator begin() const { return Base::begin(); }
86  const_iterator end() const { return Base::end(); };
87 
88  iterator begin() { return Base::begin(); }
89  iterator end() { return Base::end(); }
90 
91  bool empty() const { return Base::empty(); }
92  size_t size() const { return Base::size(); }
93 
94 #ifndef SYMBOLIC_STATE_USE_SET
95  void reserve(size_t size) { static_cast<Base&>(*this).reserve(size); }
96 #else // SYMBOLIC_STATE_USE_SET
97  void reserve(size_t size) {}
98 #endif // SYMBOLIC_STATE_USE_SET
99 
100  std::unordered_set<std::string> Stringify() const;
101 
102  friend bool operator==(const State& lhs, const State& rhs) {
103  return static_cast<const Base&>(lhs) == static_cast<const Base&>(rhs);
104  }
105  friend bool operator!=(const State& lhs, const State& rhs) {
106  return !(lhs == rhs);
107  }
108 
109  friend bool operator<(const State& lhs, const State& rhs) {
110  return static_cast<const Base&>(lhs) < static_cast<const Base&>(rhs);
111  }
112 
113  friend std::ostream& operator<<(std::ostream& os, const State& state);
114 };
115 
116 template <class InputIt>
117 bool State::insert(InputIt first, InputIt last) {
118  // TODO(tmigimatsu): More efficient way of inserting
119  bool is_changed = false;
120  for (InputIt it = first; it != last; ++it) {
121  is_changed |= insert(*it);
122  }
123  return is_changed;
124 }
125 
127  public:
128  class UnknownEvaluation : public std::exception {
129  public:
130  explicit UnknownEvaluation(const PropositionBase& prop)
131  : prop_(prop), str_prop_(prop.to_string()) {}
132 
133  const char* what() const noexcept override { return str_prop_.c_str(); }
134 
135  const Proposition& proposition() const { return prop_; }
136 
137  private:
138  const Proposition prop_;
139  const std::string str_prop_;
140  };
141 
142  PartialState() = default;
143 
144  PartialState(const State& pos, const State& neg) : pos_(pos), neg_(neg) {}
145 
146  PartialState(State&& pos, State&& neg)
147  : pos_(std::move(pos)), neg_(std::move(neg)) {}
148 
149  PartialState(const Pddl& pddl, const std::unordered_set<std::string>& str_pos,
150  const std::unordered_set<std::string>& str_neg)
151  : pos_(pddl, str_pos), neg_(pddl, str_neg) {}
152 
153  const State& pos() const { return pos_; }
154  State& pos() { return pos_; }
155 
156  const State& neg() const { return neg_; }
157  State& neg() { return neg_; }
158 
159  bool empty() const { return pos_.empty() && neg_.empty(); }
160  size_t size() const { return pos_.size() + neg_.size(); }
161 
162  bool contains(const PropositionBase& prop) const;
163  bool does_not_contain(const PropositionBase& prop) const;
164 
171  int insert(const PropositionBase& prop);
172  int insert(Proposition&& prop);
173 
174  template <class... Args>
175  int emplace(Args&&... args) {
176  return insert(Proposition(args...));
177  }
178 
185  int erase(const PropositionBase& prop);
186  int erase(Proposition&& prop);
187 
191  bool IsConsistent() const;
192 
193  std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>
194  Stringify() const;
195 
196  friend bool operator==(const PartialState& lhs, const PartialState& rhs) {
197  return lhs.pos_ == rhs.pos_ && lhs.neg_ == rhs.neg_;
198  }
199 
200  friend bool operator!=(const PartialState& lhs, const PartialState& rhs) {
201  return lhs.pos_ != rhs.pos_ || lhs.neg_ != rhs.neg_;
202  }
203 
204  friend bool operator<(const PartialState& lhs, const PartialState& rhs) {
205  return std::tie(lhs.pos_, lhs.neg_) < std::tie(rhs.pos_, rhs.neg_);
206  }
207 
208  friend std::ostream& operator<<(std::ostream& os, const PartialState& state);
209 
210  private:
211  State pos_;
212  State neg_;
213 };
214 
220 class StateIndex {
221  public:
222  class iterator;
223  using IndexedState = Eigen::Array<bool, Eigen::Dynamic, 1>;
224 
231  explicit StateIndex(const std::vector<Predicate>& predicates,
232  bool use_cache = true);
233 
240  Proposition GetProposition(size_t idx_proposition) const;
241 
248  size_t GetPropositionIndex(const Proposition& prop) const;
249 
256  State GetState(Eigen::Ref<const IndexedState> indexed_state) const;
257 
264  IndexedState GetIndexedState(const State& state) const;
265 
269  size_t size() const { return idx_predicate_group_.back(); }
270 
271  // Iterators
272  iterator begin() const { return iterator(this, 0); };
273  iterator end() const { return iterator(this, size()); };
274 
275  const Pddl& pddl() const { return *pddl_; }
276 
277  private:
278  const Pddl* pddl_ = nullptr;
279 
280  // Predicates vector stored for portability
281  std::vector<Predicate> predicates_;
282 
283  // Sorted vector of predicate group indices in indexed state (beginning of
284  // predicate group)
285  std::vector<size_t> idx_predicate_group_;
286 
287  // Map from predicate to index in predicates vector
288  std::unordered_map<std::string, size_t> idx_predicates_;
289 
290  // Cache
291  mutable std::unordered_map<size_t, Proposition> cache_propositions_;
292  mutable std::unordered_map<std::string, size_t> cache_idx_propositions_;
293 
294  bool use_cache_;
295 
296  public:
297  class iterator {
298  public:
299  // Iterator traits
300  using iterator_category = std::random_access_iterator_tag;
301  using value_type = Proposition;
302  using difference_type = ptrdiff_t;
303  using pointer = const Proposition*;
304  using reference = const Proposition&;
305 
306  // Constructor
307  iterator(const StateIndex* state_index, int idx)
308  : state_index_(state_index), idx_(idx) {}
309 
310  // Forward iterator
311  iterator& operator++() { return operator+=(1); }
312 
313  iterator operator++(int) {
314  iterator it = *this;
315  operator++();
316  return it;
317  }
318 
319  reference operator*() const {
320  prop_ = state_index_->GetProposition(idx_);
321  return prop_;
322  };
323 
324  pointer operator->() const {
325  prop_ = state_index_->GetProposition(idx_);
326  return &prop_;
327  }
328 
329  bool operator==(const iterator& rhs) const {
330  return state_index_ == rhs.state_index_ && idx_ == rhs.idx_;
331  }
332 
333  bool operator!=(const iterator& rhs) const { return !(*this == rhs); }
334 
335  // Bidirectional iterator
336  iterator& operator--() { return operator+=(-1); }
337 
338  iterator operator--(int) {
339  iterator it = *this;
340  operator--();
341  return it;
342  }
343 
344  // Random access iterator
345  bool operator<(const iterator& rhs) const {
346  return state_index_ == rhs.state_index_ && idx_ < rhs.idx_;
347  }
348 
349  bool operator>(const iterator& rhs) const { return rhs < *this; };
350 
351  bool operator<=(const iterator& rhs) const {
352  return state_index_ == rhs.state_index_ && idx_ <= rhs.idx_;
353  }
354 
355  bool operator>=(const iterator& rhs) const { return rhs <= *this; };
356 
357  iterator& operator+=(difference_type n) {
358  idx_ += n;
359  return *this;
360  }
361 
362  iterator operator+(difference_type n) const {
363  iterator temp = *this;
364  return temp += n;
365  }
366 
367  friend iterator operator+(difference_type n, const iterator& it) {
368  return it + n;
369  }
370 
371  iterator& operator-=(difference_type n) { return operator+=(-n); }
372 
373  iterator operator-(difference_type n) const {
374  iterator temp = *this;
375  return temp -= n;
376  }
377 
378  difference_type operator-(const iterator& rhs) const {
379  return idx_ - rhs.idx_;
380  }
381 
382  value_type operator[](difference_type n) const { return *(operator+(n)); }
383 
384  private:
385  const StateIndex* state_index_ = nullptr;
386  int idx_ = 0;
387  mutable Proposition prop_;
388  };
389 };
390 
391 } // namespace symbolic
392 
393 namespace std {
394 
395 template <>
396 struct hash<symbolic::State> {
397  size_t operator()(const symbolic::State& state) const noexcept;
398 };
399 
400 } // namespace std
401 
402 #endif // SYMBOLIC_STATE_H_
symbolic::State::emplace
bool emplace(Args &&... args)
Definition: state.h:75
symbolic::PropositionBase
Definition: proposition.h:25
symbolic
Definition: action.cc:329
symbolic::StateIndex::size
size_t size() const
Definition: state.h:269
symbolic::State::insert
bool insert(const PropositionBase &prop)
Definition: state.h:64
symbolic::State::contains
bool contains(const PropositionBase &prop) const
Definition: state.h:56
symbolic::PartialState::UnknownEvaluation
Definition: state.h:128
symbolic::Pddl
Definition: pddl.h:40
symbolic::Proposition
Definition: proposition.h:67
symbolic::State
Definition: state.h:40
symbolic::State::erase
bool erase(const PropositionBase &prop)
Definition: state.h:83
symbolic::HashSet
Definition: hash_set.h:27
symbolic::PartialState
Definition: state.h:126
symbolic::StateIndex::iterator
Definition: state.h:297
symbolic::StateIndex
Definition: state.h:220