AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
key_based_open_list.h
1#ifndef PROBFD_OPEN_LISTS_KEY_BASED_OPEN_LIST_H
2#define PROBFD_OPEN_LISTS_KEY_BASED_OPEN_LIST_H
3
4#include "probfd/algorithms/open_list.h"
5
6#include "probfd/fdr_types.h"
7
8#include <deque>
9#include <map>
10#include <vector>
11
12namespace probfd::open_lists {
13
14struct LifoPop {
15 template <typename C>
16 StateID operator()(C& c) const
17 {
18 auto it = c.rbegin();
19 const StateID res = *it;
20 c.erase(--(it.base()));
21 return res;
22 }
23};
24
25struct FifoPop {
26 template <typename C>
27 StateID operator()(C& c) const
28 {
29 auto it = c.begin();
30 const StateID res = *it;
31 c.erase(it);
32 return res;
33 }
34};
35
36template <
37 typename Key,
38 typename Pop = LifoPop,
39 typename Comparison = typename std::less<Key>>
40class KeyBasedOpenList : public FDROpenList {
41private:
42 using Bucket = std::vector<StateID>;
43 // using Buckets = std::vector<Bucket>;
44 using BucketMap = std::map<Key, Bucket, Comparison>;
45 using BucketRef = typename BucketMap::iterator;
46
47public:
48 explicit KeyBasedOpenList(const Comparison& comparison = Comparison())
49 : size_(0)
50 , bucket_map_(comparison)
51 {
52 }
53
54 virtual ~KeyBasedOpenList() = default;
55
56 virtual StateID pop() override
57 {
58 BucketRef b = bucket_map_.begin();
59 const StateID res = bucket_pop(b);
60 if (bucket_empty(b)) {
61 remove(b);
62 }
63 --size_;
64 return res;
65 }
66
67 virtual unsigned size() const override { return size_; }
68
69 virtual void clear() override
70 {
71 size_ = 0;
72 bucket_map_.clear();
73 }
74
75 virtual void push(StateID state_id) override
76 {
77 Key k = get_key(state_id);
78 push(k, state_id);
79 }
80
81 virtual void push(
82 StateID parent,
83 const ProbabilisticOperator* op,
84 const value_type::value_t& prob,
85 StateID state_id) override
86 {
87 Key k = get_key(parent, op, prob, state_id);
88 push(k, state_id);
89 }
90
91protected:
92 virtual Key get_key(StateID state_id) = 0;
93
94 virtual Key get_key(
95 StateID,
96 const ProbabilisticOperator*,
97 const value_type::value_t&,
98 StateID state_id)
99 {
100 return this->get_key(state_id);
101 }
102
103private:
104 void push(const Key& key, StateID stateid)
105 {
106 BucketRef b = bucket(key);
107 bucket_push(b, stateid);
108 ++size_;
109 }
110
111 inline BucketRef bucket(const Key& key)
112 {
113 return bucket_map_.emplace(key, Bucket()).first;
114 }
115
116 inline void remove(BucketRef ref) { bucket_map_.erase(ref); }
117
118 inline void bucket_push(BucketRef ref, StateID stateid)
119 {
120 ref->second.push_back(stateid);
121 }
122
123 inline StateID bucket_pop(BucketRef ref) { return Pop()(ref->second); }
124
125 inline bool bucket_empty(BucketRef ref) { return ref->second.empty(); }
126
127private:
128 unsigned size_;
129 BucketMap bucket_map_;
130};
131
132} // namespace probfd::open_lists
133
134#endif // PROBFD_OPEN_LISTS_KEY_BASED_OPEN_LIST_H
This namespace contains implementations of open lists.
Definition fifo_open_list.h:9
algorithms::OpenList< OperatorID > FDROpenList
Type alias for OpenLists for MDPs in FDR.
Definition fdr_types.h:26