10 #ifndef SYMBOLIC_STATE_H_
11 #define SYMBOLIC_STATE_H_
13 #define SYMBOLIC_STATE_USE_SET
15 #include <Eigen/Eigen>
20 #include <unordered_map>
21 #include <unordered_set>
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
30 #include "symbolic/proposition.h"
36 #ifndef SYMBOLIC_STATE_USE_SET
37 class State :
private UniqueVector<Proposition> {
38 using Base = UniqueVector<Proposition>;
39 #else // SYMBOLIC_STATE_USE_SET
42 #endif // SYMBOLIC_STATE_USE_SET
45 using iterator = Base::iterator;
46 using const_iterator = Base::const_iterator;
49 State(std::initializer_list<Proposition> l) :
Base(l){};
51 State(
const Pddl& pddl,
const std::unordered_set<std::string>& str_state);
57 return Base::contains(prop);
67 template <
class InputIt>
68 bool insert(InputIt first, InputIt last);
74 template <
class... Args>
85 const_iterator begin()
const {
return Base::begin(); }
86 const_iterator end()
const {
return Base::end(); };
88 iterator begin() {
return Base::begin(); }
89 iterator end() {
return Base::end(); }
91 bool empty()
const {
return Base::empty(); }
92 size_t size()
const {
return Base::size(); }
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
100 std::unordered_set<std::string> Stringify()
const;
102 friend bool operator==(
const State& lhs,
const State& rhs) {
103 return static_cast<const Base&
>(lhs) ==
static_cast<const Base&
>(rhs);
105 friend bool operator!=(
const State& lhs,
const State& rhs) {
106 return !(lhs == rhs);
109 friend bool operator<(
const State& lhs,
const State& rhs) {
110 return static_cast<const Base&
>(lhs) <
static_cast<const Base&
>(rhs);
113 friend std::ostream& operator<<(std::ostream& os,
const State& state);
116 template <
class InputIt>
119 bool is_changed =
false;
120 for (InputIt it = first; it != last; ++it) {
121 is_changed |=
insert(*it);
131 : prop_(prop), str_prop_(prop.to_string()) {}
133 const char* what()
const noexcept
override {
return str_prop_.c_str(); }
135 const Proposition& proposition()
const {
return prop_; }
139 const std::string str_prop_;
147 : pos_(std::move(pos)), neg_(std::move(neg)) {}
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) {}
153 const State& pos()
const {
return pos_; }
154 State& pos() {
return pos_; }
156 const State& neg()
const {
return neg_; }
157 State& neg() {
return neg_; }
159 bool empty()
const {
return pos_.empty() && neg_.empty(); }
160 size_t size()
const {
return pos_.size() + neg_.size(); }
162 bool contains(
const PropositionBase& prop)
const;
163 bool does_not_contain(
const PropositionBase& prop)
const;
171 int insert(
const PropositionBase& prop);
172 int insert(Proposition&& prop);
174 template <
class... Args>
176 return insert(Proposition(args...));
185 int erase(
const PropositionBase& prop);
186 int erase(Proposition&& prop);
191 bool IsConsistent()
const;
193 std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>
196 friend bool operator==(
const PartialState& lhs,
const PartialState& rhs) {
197 return lhs.pos_ == rhs.pos_ && lhs.neg_ == rhs.neg_;
200 friend bool operator!=(
const PartialState& lhs,
const PartialState& rhs) {
201 return lhs.pos_ != rhs.pos_ || lhs.neg_ != rhs.neg_;
204 friend bool operator<(
const PartialState& lhs,
const PartialState& rhs) {
205 return std::tie(lhs.pos_, lhs.neg_) < std::tie(rhs.pos_, rhs.neg_);
208 friend std::ostream& operator<<(std::ostream& os,
const PartialState& state);
223 using IndexedState = Eigen::Array<bool, Eigen::Dynamic, 1>;
231 explicit StateIndex(
const std::vector<Predicate>& predicates,
232 bool use_cache =
true);
240 Proposition GetProposition(
size_t idx_proposition)
const;
248 size_t GetPropositionIndex(
const Proposition& prop)
const;
256 State GetState(Eigen::Ref<const IndexedState> indexed_state)
const;
264 IndexedState GetIndexedState(
const State& state)
const;
269 size_t size()
const {
return idx_predicate_group_.back(); }
272 iterator begin()
const {
return iterator(
this, 0); };
273 iterator end()
const {
return iterator(
this, size()); };
275 const Pddl& pddl()
const {
return *pddl_; }
278 const Pddl* pddl_ =
nullptr;
281 std::vector<Predicate> predicates_;
285 std::vector<size_t> idx_predicate_group_;
288 std::unordered_map<std::string, size_t> idx_predicates_;
291 mutable std::unordered_map<size_t, Proposition> cache_propositions_;
292 mutable std::unordered_map<std::string, size_t> cache_idx_propositions_;
300 using iterator_category = std::random_access_iterator_tag;
302 using difference_type = ptrdiff_t;
308 : state_index_(state_index), idx_(idx) {}
311 iterator& operator++() {
return operator+=(1); }
320 prop_ = state_index_->GetProposition(idx_);
325 prop_ = state_index_->GetProposition(idx_);
329 bool operator==(
const iterator& rhs)
const {
330 return state_index_ == rhs.state_index_ && idx_ == rhs.idx_;
333 bool operator!=(
const iterator& rhs)
const {
return !(*
this == rhs); }
336 iterator& operator--() {
return operator+=(-1); }
345 bool operator<(
const iterator& rhs)
const {
346 return state_index_ == rhs.state_index_ && idx_ < rhs.idx_;
349 bool operator>(
const iterator& rhs)
const {
return rhs < *
this; };
351 bool operator<=(
const iterator& rhs)
const {
352 return state_index_ == rhs.state_index_ && idx_ <= rhs.idx_;
355 bool operator>=(
const iterator& rhs)
const {
return rhs <= *
this; };
357 iterator& operator+=(difference_type n) {
362 iterator operator+(difference_type n)
const {
371 iterator& operator-=(difference_type n) {
return operator+=(-n); }
373 iterator operator-(difference_type n)
const {
378 difference_type operator-(
const iterator& rhs)
const {
379 return idx_ - rhs.idx_;
382 value_type operator[](difference_type n)
const {
return *(operator+(n)); }
402 #endif // SYMBOLIC_STATE_H_