Parasol Planning Library (PPL)
Weight.h
Go to the documentation of this file.
1 #ifndef WEIGHT_H_
2 #define WEIGHT_H_
3 
9 #include "Utilities/MPUtils.h"
10 
11 #ifdef _PARALLEL
12 #include "views/proxy.h"
13 #endif
14 
15 #include "nonstd/numerics.h"
16 
17 #include <iostream>
18 #include <numeric>
19 #include <string>
20 #include <vector>
21 
22 
35 template <class CfgType>
37 
38  public:
39 
42 
47  DefaultWeight(const std::string& _label = "", const double _w = 0,
48  const std::vector<CfgType>& _intermediates = std::vector<CfgType>());
49 
50  virtual ~DefaultWeight() = default;
51 
55 
58  virtual const DefaultWeight& operator=(const DefaultWeight& _w);
59 
63 
67  virtual bool operator==(const DefaultWeight& _w) const noexcept;
68 
72  virtual bool operator!=(const DefaultWeight& _w) const noexcept;
73 
78  virtual bool operator<(const DefaultWeight& _other) const noexcept;
79 
83 
85  const std::string& GetLPLabel() const noexcept;
87  void SetLPLabel(const std::string& _lpLabel) noexcept;
88 
91  std::vector<CfgType>& GetIntermediates() noexcept;
94  const std::vector<CfgType>& GetIntermediates() const noexcept;
95 
97  void SetIntermediates(const std::vector<CfgType>& _intermediates) noexcept;
99  void SetIntermediates(std::vector<CfgType>&& _intermediates) noexcept;
100 
102  double GetWeight() const noexcept;
104  void SetWeight(const double _w) noexcept;
105 
107  void SetControl(const Control& _c) noexcept;
109  const ControlSet& GetControlSet() const noexcept;
111  ControlSet& GetControlSet() noexcept;
113  void SetControlSet(const ControlSet& _c) noexcept;
114 
116  bool IsChecked(const int _mult) const noexcept;
119  void SetChecked(const int _mult) noexcept;
120 
121  bool HasClearance() const noexcept;
123  double GetClearance() const noexcept;
126  void SetClearance(const double _c) noexcept;
127 
128  // The number of timesteps that the local plan on this edge cares about.
131  size_t GetTimeSteps() const noexcept;
134  void SetTimeSteps(std::size_t _steps) noexcept;
135 
136 
140 
143  virtual void Read(std::istream& _is);
144 
147  virtual void Write(std::ostream& _os) const;
148 
151  static Robot* inputRobot;
152 
154  void Clear();
155 
159 
161  virtual DefaultWeight operator+(const DefaultWeight& _other) const ;
162 
163  double Weight() const noexcept; // For GraphAlgo interface
164  static DefaultWeight MaxWeight() noexcept; // For Dijkstra's Alg
165 
167 
168  protected:
169 
172 
173  std::string m_lpLabel;
174 
175  double m_weight{0.};
176  std::vector<CfgType> m_intermediates;
177 
179  int m_checkedMult{std::numeric_limits<int>::max()};
180 
182  double m_clearance{std::numeric_limits<double>::infinity()};
183 
184  // For nonholonomic robots.
186  size_t m_timeSteps{0};
187 
189 
190  public:
191 
192 #ifdef _PARALLEL
193  void define_type(stapl::typer &t)
194  {
195  t.member(m_weight);
196  t.member(m_lpLabel);
197  t.member(m_intermediates);
198  }
199 #endif
200 };
201 
202 /*------------------------------ Construction --------------------------------*/
203 
204 template <typename CfgType>
206 DefaultWeight(const std::string& _label, const double _w,
207  const std::vector<CfgType>& _intermediates) :
208  m_lpLabel(_label), m_weight(_w), m_intermediates(_intermediates) {
209 }
210 
211 /*------------------------------- Assignment ---------------------------------*/
212 
213 template <typename CfgType>
217  if(this != &_w) {
218  m_lpLabel = _w.GetLPLabel();
219  m_weight = _w.GetWeight();
220  m_intermediates = _w.GetIntermediates();
221  m_checkedMult = _w.m_checkedMult;
222  m_clearance = _w.GetClearance();
223  m_controls = _w.GetControlSet();
224  m_timeSteps = _w.GetTimeSteps();
225  }
226  return *this;
227 }
228 
229 /*-------------------------- Equality and Ordering ---------------------------*/
230 
231 template <typename CfgType>
232 bool
234 operator==(const DefaultWeight& _w) const noexcept {
235  static constexpr double tolerance = 100
236  * std::numeric_limits<double>::epsilon();
237 
238  return nonstd::approx(m_weight, _w.GetWeight(), tolerance)
239  and m_intermediates == _w.GetIntermediates();
240 }
241 
242 
243 template <typename CfgType>
244 bool
246 operator!=(const DefaultWeight& _w) const noexcept {
247  return !(*this == _w);
248 }
249 
250 
251 template <typename CfgType>
252 bool
254 operator<(const DefaultWeight& _w) const noexcept {
255  return m_weight < _w.m_weight;
256 }
257 
258 /*----------------------------------------------------------------------------*/
259 
260 template <typename CfgType>
261 const std::string&
263 GetLPLabel() const noexcept {
264  return m_lpLabel;
265 }
266 
267 
268 template <typename CfgType>
269 void
271 SetLPLabel(const std::string& _label) noexcept {
272  m_lpLabel = _label;
273 }
274 
275 
276 template <typename CfgType>
277 std::vector<CfgType>&
279 GetIntermediates() noexcept {
280  return m_intermediates;
281 }
282 
283 
284 template <typename CfgType>
285 const std::vector<CfgType>&
287 GetIntermediates() const noexcept {
288  return m_intermediates;
289 }
290 
291 
292 template <typename CfgType>
293 void
295 SetIntermediates(const std::vector<CfgType>& _intermediates) noexcept {
296  m_intermediates = _intermediates;
297 }
298 
299 
300 template <typename CfgType>
301 void
303 SetIntermediates(std::vector<CfgType>&& _intermediates) noexcept {
304  m_intermediates = std::move(_intermediates);
305 }
306 
307 
308 template <typename CfgType>
309 double
311 GetWeight() const noexcept {
312  return m_weight;
313 }
314 
315 
316 template <typename CfgType>
317 void
319 SetWeight(const double _w) noexcept {
320  m_weight = _w;
321 }
322 
323 
324 //------------------------- Control Set Modifiers ------------------------------
325 
326 //This is a wrapper for SetControlSet(), because right now the only use case is
327 // single controls:
328 template <typename CfgType>
329 void
331 SetControl(const Control& _c) noexcept {
332  SetControlSet({_c});
333 }
334 
335 template <typename CfgType>
336 const ControlSet&
338 GetControlSet() const noexcept {
339  return m_controls;
340 }
341 
342 template <typename CfgType>
343 ControlSet&
345 GetControlSet() noexcept {
346  return m_controls;
347 }
348 
349 
350 // We can potentially have multiple controls to get from one configuration to
351 // the next, so the entire set of those controls can be set here.
352 template <typename CfgType>
353 void
355 SetControlSet(const ControlSet& _c) noexcept {
356  m_controls = _c;
357 }
358 
359 
360 template <typename CfgType>
361 std::size_t
363 GetTimeSteps() const noexcept {
364  return m_timeSteps;
365 }
366 
367 template <typename CfgType>
368 void
370 SetTimeSteps(std::size_t _steps) noexcept {
371  m_timeSteps = _steps;
372 }
373 
374 
375 template <typename CfgType>
376 bool
378 IsChecked(const int _mult) const noexcept {
379  return m_checkedMult <= _mult;
380 }
381 
382 
383 template <typename CfgType>
384 void
386 SetChecked(const int _mult) noexcept {
387  m_checkedMult = std::min(m_checkedMult, _mult);
388 }
389 
390 
391 template <typename CfgType>
392 double
394 GetClearance() const noexcept {
395  return m_clearance;
396 }
397 
398 
399 template <typename CfgType>
400 void
402 SetClearance(const double _c) noexcept {
403  m_clearance = _c;
404 }
405 
406 /*----------------------------------- I/O ------------------------------------*/
407 
411 template <typename CfgType>
412 void
414 Clear() {
415  // Reset the initial state variables of this object:
416  m_lpLabel.clear();
417  m_intermediates.clear();
418  m_controls.clear();
419  m_weight = 0.;
420  m_checkedMult = std::numeric_limits<int>::max();
421  m_clearance = std::numeric_limits<double>::infinity();
422  m_timeSteps = 0;
423 }
424 
425 // Initialize the input robot (a placeholder for not knowing the robot when
426 // reading in cfgs from a file) to nullptr, as it is to be set externally.
427 template <typename CfgType>
429 
430 
431 // Reads in an edge from a standard input stream, including
432 // control signals for nonholonomic robots.
433 // This function is symmetric with Write().
434 template <typename CfgType>
435 void
437 Read(std::istream& _is) {
438  if(!inputRobot)
439  throw RunTimeException(WHERE) << "Need to tell the weight class what robot "
440  << "we are reading for. Use inputRobot "
441  << "member to specify.";
442 
443  Clear(); //Necessary, as stapl seems to use the same temp weight object
444 
445  // Read the data for robots that are either holonomic or not:
446  size_t numIntermediates;
447  _is >> numIntermediates;
448  m_intermediates.clear();
449 
450  CfgType tmp(inputRobot);
451  for(size_t i = 0; i < numIntermediates; ++i) {
452  _is >> tmp;
453  m_intermediates.push_back(tmp);
454  }
455  _is >> m_weight;
456 
457  #ifndef VIZMO_MAP
458  // Read the data specifically for nonholonomic robots if necessary:
459  if(inputRobot->IsNonholonomic()) {
460  //read the # timesteps used for this edge and the # actuators/controls
461  _is >> m_timeSteps;
462  std::size_t numControls;
463  _is >> numControls;
464 
465  //Now loop through each individual control, outputting the full signal
466  // and label for that control:
467  for(std::size_t i = 0; i < numControls; i++) {
468  Control con;
469  std::string label;
470  _is >> label;
471 
474 
475  //If the label is "Coast" then we need to put a nullptr for the actuator
476  // since that's what is used for a coast control.
477  if(label.compare("Coast") == 0) // Note compare() returns 0 if equal.
478  con.actuator = nullptr;
479  else
480  con.actuator = inputRobot->GetActuator(label);
481 
482  //Loop through and get all of the control signal's values.
483  //The number of values in the signal is equal to the robot's DOF.
484  std::size_t signalLength = inputRobot->GetMultiBody()->DOF();
485  con.signal.resize(signalLength, 0);
486  for(std::size_t j = 0; j < signalLength; j++) {
487  double val;
488  _is >> val;
489  con.signal[j] = val;
490  }
491 
492  //It wouldn't be a bad idea to put in a check here to ensure that if
493  // the actuator is a nullptr, then all of the signal is 0.
494 
495  //finally push back the read-in control signal:
496  m_controls.push_back(con);
497  }
498  }
499  #endif
500 }
501 
502 
503 // Writes to a standard output stream all of the data for the edge, including
504 // control signals for nonholonomic robots.
505 // This function is symmetric with Write().
506 template <typename CfgType>
507 void
509 Write(std::ostream& _os) const {
512 
513  //Write the data that's needed whether it's a holonomic robot or not:
514  _os << m_intermediates.size() << " ";
515  for(auto& cfg : m_intermediates)
516  _os << cfg;
517  _os << std::scientific << std::setprecision(16) << m_weight;
518 
519  #ifndef VIZMO_MAP
520  //If nonholonomic, print extra data needed (holonomic will have no controls)
521  if(!m_controls.empty()) {
522  //output the # timesteps used for this edge and the # actuators/controls
523  _os << " " << m_timeSteps << " " << m_controls.size() << " ";
524 
525  //Now loop through each individual control, outputting the full signal
526  // and label for that control:
527  for(auto control : m_controls) {
528  //Output the actuator label:
529  if(control.actuator)
530  _os << control.actuator->GetLabel() << " ";
531  else
532  _os << "Coast ";// if actuator is nullptr, it's a coast control.
533 
536  //Write each of the control signal's values:
537  for (double& val : control.signal)
538  _os << val << " ";
539  }
540  }
541  #endif
542 
543  // Clear scientific/precision options.
544  _os.unsetf(std::ios_base::floatfield);
545 }
546 
547 
548 
549 template <typename CfgType>
550 std::ostream&
551 operator<<(std::ostream& _os, const DefaultWeight<CfgType>& _w) {
552  _w.Write(_os);
553  return _os;
554 }
555 
556 
557 template <typename CfgType>
558 std::istream&
559 operator>>(std::istream& _is, DefaultWeight<CfgType>& _w) {
560  _w.Read(_is);
561  return _is;
562 }
563 
564 /*---------------------- stapl graph interface helpers -----------------------*/
565 
566 template <typename CfgType>
569 operator+(const DefaultWeight& _other) const {
570  return DefaultWeight(m_lpLabel, m_weight + _other.m_weight);
571 }
572 
573 
574 template <typename CfgType>
575 double
577 Weight() const noexcept {
578  return GetWeight();
579 }
580 
581 
582 template <typename CfgType>
585 MaxWeight() noexcept {
586  static constexpr double max = std::numeric_limits<double>::max();
587  return DefaultWeight("INVALID", max);
588 }
589 
590 /*----------------------------------------------------------------------------*/
591 
592 #endif
std::vector< Control > ControlSet
A discrete set of controls can be defined by a sequence of allowed controls.
Definition: Control.h:91
#define WHERE
Macro for retrieving info about file, function, and line number.
Definition: RuntimeUtils.h:32
std::ostream & operator<<(std::ostream &_os, const DefaultWeight< CfgType > &_w)
Definition: Weight.h:551
std::istream & operator>>(std::istream &_is, DefaultWeight< CfgType > &_w)
Definition: Weight.h:559
Definition: Weight.h:36
void SetLPLabel(const std::string &_lpLabel) noexcept
Set the string label value.
Definition: Weight.h:271
void SetTimeSteps(std::size_t _steps) noexcept
Definition: Weight.h:370
void SetControl(const Control &_c) noexcept
Set a singular control corresponding to the path given by the weight.
Definition: Weight.h:331
static Robot * inputRobot
Definition: Weight.h:151
virtual bool operator<(const DefaultWeight &_other) const noexcept
Definition: Weight.h:254
void SetChecked(const int _mult) noexcept
Definition: Weight.h:386
std::vector< CfgType > & GetIntermediates() noexcept
Definition: Weight.h:279
virtual const DefaultWeight & operator=(const DefaultWeight &_w)
Definition: Weight.h:216
virtual void Read(std::istream &_is)
Definition: Weight.h:437
std::string m_lpLabel
Label of local planner that built this edge.
Definition: Weight.h:173
double m_clearance
The clearance value, or inf if not evaluated.
Definition: Weight.h:182
void SetWeight(const double _w) noexcept
Set the numerical weight value.
Definition: Weight.h:319
DefaultWeight(const std::string &_label="", const double _w=0, const std::vector< CfgType > &_intermediates=std::vector< CfgType >())
Definition: Weight.h:206
bool IsChecked(const int _mult) const noexcept
Is the checked resolution multiple at most _mult?
Definition: Weight.h:378
const ControlSet & GetControlSet() const noexcept
Get a vector of controls corresponding to the path given by the weight.
Definition: Weight.h:338
const std::string & GetLPLabel() const noexcept
Get the string label value.
Definition: Weight.h:263
void SetIntermediates(const std::vector< CfgType > &_intermediates) noexcept
Set the intermediate configurations corresponding to the current weight.
Definition: Weight.h:295
virtual bool operator!=(const DefaultWeight &_w) const noexcept
Definition: Weight.h:246
virtual DefaultWeight operator+(const DefaultWeight &_other) const
This only adds weights, it doesn't take intermediates into account.
Definition: Weight.h:569
double Weight() const noexcept
Definition: Weight.h:577
virtual bool operator==(const DefaultWeight &_w) const noexcept
Definition: Weight.h:234
double GetWeight() const noexcept
Get the numerical weight value.
Definition: Weight.h:311
double GetClearance() const noexcept
Get the clearance value of the current weight.
Definition: Weight.h:394
void SetControlSet(const ControlSet &_c) noexcept
Set a vector of controls corresponding to the path given by the weight.
Definition: Weight.h:355
int m_checkedMult
The checked resolution multiple (for lazy query), or max if none.
Definition: Weight.h:179
void Clear()
Clear all of the contents of the object to a reinitialized state.
Definition: Weight.h:414
bool HasClearance() const noexcept
ControlSet m_controls
The controls used.
Definition: Weight.h:185
size_t GetTimeSteps() const noexcept
Definition: Weight.h:363
std::vector< CfgType > m_intermediates
Intermediate configurations.
Definition: Weight.h:176
size_t m_timeSteps
The number of timesteps to apply the controls.
Definition: Weight.h:186
void SetClearance(const double _c) noexcept
Definition: Weight.h:402
virtual ~DefaultWeight()=default
double m_weight
The edge length.
Definition: Weight.h:175
virtual void Write(std::ostream &_os) const
Definition: Weight.h:509
static DefaultWeight MaxWeight() noexcept
Definition: Weight.h:585
Definition: Robot.h:31
Definition: Control.h:23
Actuator * actuator
The actuator for this control.
Definition: Control.h:34
Signal signal
The signal for this control.
Definition: Control.h:35
Definition: PMPLExceptions.h:62