Parasol Planning Library (PPL)
GroupLocalPlan.h
Go to the documentation of this file.
1 #ifndef PPL_GROUP_LOCAL_PLAN_H_
2 #define PPL_GROUP_LOCAL_PLAN_H_
3 
10 
11 #include "containers/sequential/graph/graph_util.h"
12 
13 #include <algorithm>
14 #include <iostream>
15 #include <vector>
16 
17 template <typename GraphType> class GroupCfg;
18 
23 template <typename GraphType>
24 class GroupLocalPlan : public CompositeEdge<GraphType> {
25 
26  public:
27 
30 
33  typedef typename GraphType::CfgType CfgType;
34  typedef typename GraphType::EdgeType IndividualEdge;
35  typedef double EdgeWeight;
36 
39  typedef std::vector<GroupCfgType> GroupCfgPath;
41  typedef stapl::edge_descriptor_impl<size_t> ED;
42 
47  typedef std::vector<size_t> Formation;
48 
52 
58  GroupLocalPlan(GroupRoadmapType* const& _g = nullptr,
59  const std::string& _lpLabel = "",
60  const double _w = 0.0, const GroupCfgPath& _path = GroupCfgPath());
61 
67  GroupLocalPlan(RobotGroup* const& _g,
68  const std::string& _lpLabel = "",
69  const double _w = 0.0, const GroupCfgPath& _path = GroupCfgPath());
70 
71  virtual ~GroupLocalPlan() = default;
72 
76 
79  void SetGroupRoadmap(GroupRoadmapType* const& _g);
80 
83  void SetFormation(const Formation& _indices);
84 
87  const Formation& GetFormation() const noexcept;
88 
90  void Clear() noexcept;
91 
93  GroupCfgPath& GetIntermediates() noexcept;
94 
96  const GroupCfgPath& GetIntermediates() const noexcept;
97 
99  void SetIntermediates(const GroupCfgPath& _cfgs);
100 
102  const std::string& GetLPLabel() const noexcept;
103 
106  void SetLPLabel(const std::string _label) noexcept;
107 
111 
114  virtual GroupLocalPlan operator+(const GroupLocalPlan& _other) const;
115 
117 
118  private:
119 
122 
123  std::string m_lpLabel;
124 
127  std::vector<size_t> m_formation;
128 
130  GroupCfgPath m_cfgIntermediates;
131 
133 
134 };
135 
136 /*------------------------------- Construction -------------------------------*/
137 
138 template <typename GraphType>
139 GroupLocalPlan<GraphType>::
140 GroupLocalPlan(GroupRoadmapType* const & _g, const std::string& _lpLabel,
141  const double _w, const GroupCfgPath& _intermediates)
142  : CompositeEdge<GraphType>((GroupGraphType*)_g, _w, (CompositePath&)_intermediates),
143  m_lpLabel(_lpLabel) {}
144 
145 template <typename GraphType>
147 GroupLocalPlan(RobotGroup* const & _g, const std::string& _lpLabel,
148  const double _w, const GroupCfgPath& _intermediates)
149  : CompositeEdge<GraphType>(_g, _w, (CompositePath&)_intermediates),
150  m_lpLabel(_lpLabel) {}
151 
152 /*------------------------- Misc Interface Functions -------------------------*/
153 
154 template <typename GraphType>
155 void
157 SetGroupRoadmap(GroupRoadmapType* const& _g) {
158  // The new composite graph must have the same group.
159  if(_g->GetGroup() != this->m_group)
160  throw RunTimeException(WHERE) << "The new group roadmap must have the "
161  << "same robot group.";
162 
163  this->m_groupMap = (GroupGraphType*)_g;
164 
165  // Put all individual edges into the group local plan so that all are local:
166  for(size_t i = 0; i < this->GetNumRobots(); ++i)
167  this->SetEdge(i, IndividualEdge(this->GetEdge(i)));
168 }
169 
170 template <typename GraphType>
171 void
173 SetFormation(const Formation& _indices) {
174  m_formation = _indices;
175 }
176 
177 
178 template <typename GraphType>
181 GetFormation() const noexcept {
182  return m_formation;
183 }
184 
185 
186 template <typename GraphType>
187 void
189 Clear() noexcept {
190  // Reset the initial state variables of this object:
191  m_lpLabel.clear();
192  this->m_weight = 0.;
193  m_cfgIntermediates.clear();
194  m_formation.clear();
195 }
196 
197 
198 template <typename GraphType>
201 GetIntermediates() noexcept {
202  return m_cfgIntermediates;
203 }
204 
205 
206 template <typename GraphType>
209 GetIntermediates() const noexcept {
210  return m_cfgIntermediates;
211 }
212 
213 
214 template <typename GraphType>
215 void
217 SetIntermediates(const GroupCfgPath& _cfgs) {
218  m_cfgIntermediates = _cfgs;
219 }
220 
221 
222 template <typename GraphType>
223 const std::string&
225 GetLPLabel() const noexcept {
226  return m_lpLabel;
227 }
228 
229 
230 template <typename GraphType>
231 void
233 SetLPLabel(const std::string _label) noexcept {
234  m_lpLabel = _label;
235 }
236 
237 /*---------------------- stapl graph interface helpers -----------------------*/
238 
239 template <typename GraphType>
242 operator+(const GroupLocalPlan& _other) const {
243  if(!this->m_groupMap)
244  return GroupLocalPlan(this->m_group, m_lpLabel,
245  this->m_weight + _other.m_weight);
246  else
247  return GroupLocalPlan((GroupRoadmapType*)this->m_groupMap, m_lpLabel,
248  this->m_weight + _other.m_weight);
249 }
250 
251 /*------------------------------ Input/Output --------------------------------*/
252 
253 template<typename GraphType>
254 std::ostream&
255 operator<<(std::ostream& _os, const GroupLocalPlan<GraphType>& _groupLP) {
256  // For the group edges, the only caveat is that the intermediates need to line up.
257  // Each individual edge within a GroupLocalPlan should have the same number of
258  // intermediates (for now) so that we can do this easily. Then for a
259  // GroupLocalPlan with individual edges for i robots, you would print all i
260  // of the nth intermediates, then the (n+1)th, etc.
261 
262  // Make a vector of edges corresponding to each robot's edge to prevent from
263  // repeatedly retrieving each vector of cfgs.
264 #if 0
265  // TODO: when group intermediates are needed, use this code (but it's untested
266  // right now). Also it should really just populate m_intermediates and
267  // then print that vector (see DefaultWeight::Write() for analogous code).
268  std::vector< std::vector<CfgType> > edgeIntermediates;
269  const size_t numRobots = _groupLP.GetNumRobots();
270  size_t numIntermediates = 0;
271  const std::vector<size_t>& formation = _groupLP.GetFormation();
272  for(size_t i = 0; i < numRobots; ++i) {
273  // Check if the robot is inactive, if so, just duplicate the start cfg:
274  if(std::find(formation.begin(), formation.end(), i) ==
275  formation.end()) {
276  // Get the cfg that the robot is stationary at. Will be resized later to
277  // account for correct number of intermediates.
278  edgeIntermediates.push_back({_groupLP.GetRobotStartCfg(i)});
279  }
280  else {
281  edgeIntermediates.push_back(_groupLP.GetEdge(i)->GetIntermediates());
282  numIntermediates = edgeIntermediates.back().size();
283  }
284  }
285 
286  if(numIntermediates == 0)
287  throw RunTimeException(WHERE, "No active robots were detected in an edge!");
288 
289  // Now all intermediate vectors of size 1 need to have their cfgs duplicated
290  for(std::vector<CfgType>& intermediateVec : edgeIntermediates)
291  if(intermediateVec.size() == 1)
292  intermediateVec.resize(numIntermediates, intermediateVec[0]);
293  // TODO: this could be optimized by not duplicating but won't be for now.
294 
295  // Now loop through all intermediates so we construct each intermediate's
296  // composite cfg preserving the order of the robots in the group.
297  // Note: assuming the same number of intermediates in each edge.
298  for(size_t i = 0; i < numIntermediates; ++i) {
299  for(size_t robot = 0; i < numRobots; ++i) {
300  _os << edgeIntermediates[robot][i] << " ";
301  }
302  }
303 
304 #endif
305 
306  _os << 0 << " "; // 0 intermediates for now.
307  _os << _groupLP.Weight(); // Print out the weight.
308 
309  return _os;
310 }
311 
312 
313 #endif
std::ostream & operator<<(std::ostream &_os, const GroupLocalPlan< GraphType > &_groupLP)
Definition: GroupLocalPlan.h:255
#define WHERE
Macro for retrieving info about file, function, and line number.
Definition: RuntimeUtils.h:32
Definition: CompositeEdge.h:23
virtual size_t GetNumRobots() const noexcept
Get the number of robots given in this group local plan.
Definition: CompositeEdge.h:525
GraphType::EdgeType IndividualEdge
Definition: CompositeEdge.h:31
std::vector< CompositeStateType > CompositePath
Definition: CompositeEdge.h:38
double m_weight
The edge weight.
Definition: CompositeEdge.h:215
virtual double Weight() const noexcept
Get the weight of the plan.
Definition: CompositeEdge.h:583
virtual IndividualEdge * GetEdge(Robot *const _robot)
Definition: CompositeEdge.h:470
Definition: CompositeGraph.h:27
virtual RobotGroup * GetGroup()
Get the robot group.
Definition: CompositeGraph.h:190
Definition: GroupCfg.h:39
Definition: GroupLocalPlan.h:24
std::vector< size_t > Formation
Definition: GroupLocalPlan.h:47
GroupCfgPath & GetIntermediates() noexcept
Get the group configuration intermediates.
Definition: GroupLocalPlan.h:201
GroupCfg< GraphType > GroupCfgType
Definition: GroupLocalPlan.h:37
stapl::edge_descriptor_impl< size_t > ED
Definition: GroupLocalPlan.h:41
GroupLocalPlan(GroupRoadmapType *const &_g=nullptr, const std::string &_lpLabel="", const double _w=0.0, const GroupCfgPath &_path=GroupCfgPath())
Definition: GroupLocalPlan.h:140
BaseType::CompositePath CompositePath
Definition: GroupLocalPlan.h:40
double EdgeWeight
Definition: GroupLocalPlan.h:35
GroupRoadmap< GroupCfgType, GroupLocalPlan > GroupRoadmapType
Definition: GroupLocalPlan.h:38
void SetLPLabel(const std::string _label) noexcept
Definition: GroupLocalPlan.h:233
const Formation & GetFormation() const noexcept
Definition: GroupLocalPlan.h:181
CompositeEdge< GraphType > BaseType
Definition: GroupLocalPlan.h:31
void SetFormation(const Formation &_indices)
Definition: GroupLocalPlan.h:173
virtual ~GroupLocalPlan()=default
void SetGroupRoadmap(GroupRoadmapType *const &_g)
Definition: GroupLocalPlan.h:157
std::vector< GroupCfgType > GroupCfgPath
Definition: GroupLocalPlan.h:39
BaseType::GroupGraphType GroupGraphType
Definition: GroupLocalPlan.h:32
GraphType::EdgeType IndividualEdge
Definition: GroupLocalPlan.h:34
virtual GroupLocalPlan operator+(const GroupLocalPlan &_other) const
Definition: GroupLocalPlan.h:242
GraphType::CfgType CfgType
Definition: GroupLocalPlan.h:33
const std::string & GetLPLabel() const noexcept
Get the string label of this current local plan.
Definition: GroupLocalPlan.h:225
void SetIntermediates(const GroupCfgPath &_cfgs)
Set the group configuration intermediates.
Definition: GroupLocalPlan.h:217
void Clear() noexcept
Reset the states of this object.
Definition: GroupLocalPlan.h:189
Definition: GroupRoadmap.h:25
A group of one or more robots.
Definition: RobotGroup.h:17
Definition: PMPLExceptions.h:62