Parasol Planning Library (PPL)
CBS.h
Go to the documentation of this file.
1 #ifndef PPL_CBS_H_
2 #define PPL_CBS_H_
3 
4 #include <functional>
5 #include <queue>
6 #include <unordered_set>
7 #include <unordered_map>
8 #include <vector>
9 
10 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
11 struct CBSNode {
12 
14  //std::unordered_map<IndividualTask*,std::unordered_set<ConstraintType>> constraintMap;
15  std::map<IndividualTask*,std::set<ConstraintType>> constraintMap;
16 
18  std::unordered_map<IndividualTask*,IndividualSolution*> solutionMap;
19 
21  double cost;
22 
23 
24  CBSNode() {
25  cost = 0;
26  }
27 
28  CBSNode(std::vector<IndividualTask*> _tasks) {
29  for(auto& task : _tasks) {
30  constraintMap[task] = {};
31  solutionMap[task] = nullptr;
32  }
33  cost = 0;
34  }
35 
36  CBSNode(const CBSNode& _node) {
38  solutionMap = _node.solutionMap;
39  cost = _node.cost;
40  }
41 
42  CBSNode& operator=(const CBSNode& _node) {
43  if(this != &_node) {
44  constraintMap.clear();
46  solutionMap.clear();
47  solutionMap = _node.solutionMap;
48  cost = _node.cost;
49  }
50  return *this;
51  }
52 
54  if(this != &_node) {
55  constraintMap.clear();
56  constraintMap = std::move(_node.constraintMap);
57  solutionMap.clear();
58  solutionMap = std::move(_node.solutionMap);
59  cost = std::move(_node.cost);
60  }
61  return *this;
62  }
63 
64  bool operator==(const CBSNode& _node) const {
65 
66  // Check if costs are equal
67  if(cost != _node.cost)
68  return false;
69 
70  for(auto& kv : constraintMap) {
71  auto task = kv.first;
72 
73  // Check if other node has the task in the constraint map
74  auto iter = _node.constraintMap.find(task);
75  if(iter == _node.constraintMap.end())
76  return false;
77 
78  // Check if constraint sets match
79  if(kv.second != *iter)
80  return false;
81  }
82 
83  for(auto& kv : solutionMap) {
84  auto task = kv.first;
85 
86  // Check if other node has the task in the solution map
87  auto iter = _node.solutionMap.find(task);
88  if(iter == _node.solutionMap.end())
89  return false;
90 
91  // Check if solutions match
92  if(kv.second != *(iter->second))
93  return false;
94  }
95  return true;
96  }
97 
98  bool operator>(const CBSNode& _node) const noexcept {
99  return cost > _node.cost;
100  }
101 
102 };
103 
104 
105 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
108  IndividualTask* _task)>;
109 
110 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
112  std::function<std::vector<std::pair<IndividualTask*, ConstraintType>>(
114 
115 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
118 
119 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
121  std::function<std::vector<CBSNode<IndividualTask,ConstraintType,IndividualSolution>>(
123  std::vector<std::pair<IndividualTask*, ConstraintType>> _constraints,
126 
127 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
129  std::function<void(
131  std::vector<IndividualTask*> _tasks,
134 
135 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
138  return [](
139  std::vector<CBSNode<IndividualTask, ConstraintType, IndividualSolution>>& _root,
140  std::vector<IndividualTask*> _tasks,
143 
145  for(auto task : _tasks) {
146  if (!_lowlevel(single, task))
147  return;
148  }
149  single.cost = _cost(single);
150  _root.push_back(single);
151  };
152 }
153 
154 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
157  std::vector<IndividualTask*> _tasks,
162 {
163  auto initial = CBSDefaultInitialFunction<IndividualTask, ConstraintType, IndividualSolution>();
164  return CBS(_tasks, _validate, _split, _lowlevel, _cost, initial);
165 }
166 
167 template <typename IndividualTask, typename ConstraintType, typename IndividualSolution>
170  std::vector<IndividualTask*> _tasks,
176 {
177 
179 
180  // Create the conflict tree
181  std::priority_queue<CBSNodeType,
182  std::vector<CBSNodeType>,
183  std::greater<CBSNodeType>> ct;
184 
185  // Create root node with initial plans
186  std::vector<CBSNodeType> root;
187 
188  _initial(root, _tasks, _lowlevel, _cost);
189 
190  for (auto node : root)
191  ct.push(node);
192 
193  // Search conflict tree
194  while(!ct.empty()) {
195 
196  // Grab minimum cost node
197  auto node = ct.top();
198  ct.pop();
199 
200  // Validate solution in node
201  auto constraints = _validate(node);
202 
203  // If there are no conflicts, return the valid solution
204  if(constraints.empty())
205  return node;
206 
207  // Create child nodes
208  auto children = _split(node, constraints, _lowlevel, _cost);
209 
210  // Add child nodes to the tree
211  for(const auto& child : children) {
212  ct.push(child);
213  }
214  }
215 
216  return CBSNodeType();
217 }
218 
219 
220 #endif
CBSInitialFunction< IndividualTask, ConstraintType, IndividualSolution > CBSDefaultInitialFunction()
Definition: CBS.h:137
std::function< std::vector< std::pair< IndividualTask *, ConstraintType > >(CBSNode< IndividualTask, ConstraintType, IndividualSolution > &_node)> CBSValidationFunction
Definition: CBS.h:113
std::function< void(std::vector< CBSNode< IndividualTask, ConstraintType, IndividualSolution > > &_root, std::vector< IndividualTask * > _tasks, CBSLowLevelPlanner< IndividualTask, ConstraintType, IndividualSolution > &_lowlevel, CBSCostFunction< IndividualTask, ConstraintType, IndividualSolution > &_cost)> CBSInitialFunction
Definition: CBS.h:133
std::function< std::vector< CBSNode< IndividualTask, ConstraintType, IndividualSolution > >(CBSNode< IndividualTask, ConstraintType, IndividualSolution > &_node, std::vector< std::pair< IndividualTask *, ConstraintType > > _constraints, CBSLowLevelPlanner< IndividualTask, ConstraintType, IndividualSolution > &_lowlevel, CBSCostFunction< IndividualTask, ConstraintType, IndividualSolution > &_cost)> CBSSplitNodeFunction
Definition: CBS.h:125
std::function< bool(CBSNode< IndividualTask, ConstraintType, IndividualSolution > &_node, IndividualTask *_task)> CBSLowLevelPlanner
Definition: CBS.h:108
std::function< double(CBSNode< IndividualTask, ConstraintType, IndividualSolution > &_node)> CBSCostFunction
Definition: CBS.h:117
CBSNode< IndividualTask, ConstraintType, IndividualSolution > CBS(std::vector< IndividualTask * > _tasks, CBSValidationFunction< IndividualTask, ConstraintType, IndividualSolution > &_validate, CBSSplitNodeFunction< IndividualTask, ConstraintType, IndividualSolution > &_split, CBSLowLevelPlanner< IndividualTask, ConstraintType, IndividualSolution > &_lowlevel, CBSCostFunction< IndividualTask, ConstraintType, IndividualSolution > &_cost)
Definition: CBS.h:156
Definition: CBS.h:11
CBSNode(const CBSNode &_node)
Definition: CBS.h:36
bool operator==(const CBSNode &_node) const
Definition: CBS.h:64
bool operator>(const CBSNode &_node) const noexcept
Definition: CBS.h:98
CBSNode & operator=(const CBSNode &_node)
Definition: CBS.h:42
CBSNode()
Definition: CBS.h:24
std::unordered_map< IndividualTask *, IndividualSolution * > solutionMap
Map of task to its solution.
Definition: CBS.h:18
CBSNode & operator=(CBSNode &&_node)
Definition: CBS.h:53
CBSNode(std::vector< IndividualTask * > _tasks)
Definition: CBS.h:28
double cost
Cost of current solution.
Definition: CBS.h:21
std::map< IndividualTask *, std::set< ConstraintType > > constraintMap
Map of task to its constraints.
Definition: CBS.h:15