World.cpp
1 #include "ompl/control/planners/ltl/World.h"
2 #include "ompl/util/Console.h"
3 #include <boost/functional/hash.hpp>
4 #include <boost/lexical_cast.hpp>
5 #include <boost/unordered_map.hpp>
6 #include <string>
7 
8 ompl::control::World::World(unsigned int np) : numProps_(np)
9 {
10 }
11 
12 bool ompl::control::World::operator[](unsigned int i) const
13 {
14  boost::unordered_map<unsigned int, bool>::const_iterator p = props_.find(i);
15  if (p == props_.end())
16  OMPL_ERROR("Proposition %u is not set in world", i);
17  return p->second;
18 }
19 
20 bool& ompl::control::World::operator[](unsigned int i)
21 {
22  return props_[i];
23 }
24 
25 unsigned int ompl::control::World::numProps() const
26 {
27  return numProps_;
28 }
29 
31 {
32  boost::unordered_map<unsigned int, bool>::const_iterator p, q;
33  for (p = w.props_.begin(); p != w.props_.end(); ++p)
34  {
35  q = props_.find(p->first);
36  if (q == props_.end() || *q != *p)
37  return false;
38  }
39  return true;
40 }
41 
42 std::string ompl::control::World::formula(void) const
43 {
44  if (props_.empty())
45  return "true";
46  boost::unordered_map<unsigned int, bool>::const_iterator p = props_.begin();
47  std::string f = std::string(p->second ? "p" : "!p") + boost::lexical_cast<std::string>(p->first);
48  ++p;
49  for (; p != props_.end(); ++p)
50  f += std::string(p->second ? " & p" : " & !p") + boost::lexical_cast<std::string>(p->first);
51  return f;
52 }
53 
54 const boost::unordered_map<unsigned int, bool>& ompl::control::World::props(void) const
55 {
56  return props_;
57 }
58 
60 {
61  return numProps_ == w.numProps_ && props_ == w.props_;
62 }
63 
65 {
66  props_.clear();
67 }
68 
69 namespace ompl
70 {
71  namespace control
72  {
73  std::size_t hash_value(const ompl::control::World& w)
74  {
75  std::size_t hash = 0;
76  boost::unordered_map<unsigned int, bool>::const_iterator p;
77  for (p = w.props_.begin(); p != w.props_.end(); ++p)
78  boost::hash_combine(hash, *p);
79  return hash;
80  }
81  }
82 }
World(unsigned int numProps)
Initializes a world with a given number of propositions.
Definition: World.cpp:8
A class to represent an assignment of boolean values to propositions. A World can be partially restri...
Definition: World.h:51
void clear(void)
Clears this world's truth assignment.
Definition: World.cpp:64
bool operator==(const World &w) const
Returns whether this World is equivalent to a given World, by comparing their truth assignment maps...
Definition: World.cpp:59
Main namespace. Contains everything in this library.
Definition: Cost.h:42
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
const boost::unordered_map< unsigned int, bool > & props(void) const
Returns this World's underlying proposition-to-boolean assignment map.
Definition: World.cpp:54
bool satisfies(const World &w) const
Returns whether this World propositionally satisfies a given World w. Specifically, returns true iff for every proposition p assigned in w, p is assigned in this World and this[p] == w[p].
Definition: World.cpp:30
unsigned int numProps(void) const
Returns the number of propositions declared for this World. Not all of the propositions have necessar...
Definition: World.cpp:25
std::string formula(void) const
Returns a formatted string representation of this World, as a conjunction of literals.
Definition: World.cpp:42
bool operator[](unsigned int i) const
Returns the boolean value of a given proposition in this World. Reports an error if the proposition h...
Definition: World.cpp:12