World.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2012, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Matt Maly */
36 
37 #include "ompl/control/planners/ltl/World.h"
38 #include "ompl/util/Console.h"
39 #include "ompl/util/Hash.h"
40 #include <unordered_map>
41 #include <string>
42 
43 ompl::control::World::World(unsigned int np) : numProps_(np)
44 {
45 }
46 
47 bool ompl::control::World::operator[](unsigned int i) const
48 {
49  auto p = props_.find(i);
50  if (p == props_.end())
51  OMPL_ERROR("Proposition %u is not set in world", i);
52  return p->second;
53 }
54 
55 bool &ompl::control::World::operator[](unsigned int i)
56 {
57  return props_[i];
58 }
59 
60 unsigned int ompl::control::World::numProps() const
61 {
62  return numProps_;
63 }
64 
66 {
67  std::unordered_map<unsigned int, bool>::const_iterator p, q;
68  for (p = w.props_.begin(); p != w.props_.end(); ++p)
69  {
70  q = props_.find(p->first);
71  if (q == props_.end() || *q != *p)
72  return false;
73  }
74  return true;
75 }
76 
77 std::string ompl::control::World::formula() const
78 {
79  if (props_.empty())
80  return "true";
81  auto p = props_.begin();
82  std::string f = std::string(p->second ? "p" : "!p") + std::to_string(p->first);
83  ++p;
84  for (; p != props_.end(); ++p)
85  f += std::string(p->second ? " & p" : " & !p") + std::to_string(p->first);
86  return f;
87 }
88 
89 const std::unordered_map<unsigned int, bool> &ompl::control::World::props() const
90 {
91  return props_;
92 }
93 
95 {
96  return numProps_ == w.numProps_ && props_ == w.props_;
97 }
98 
100 {
101  props_.clear();
102 }
103 
104 size_t std::hash<ompl::control::World>::operator()(const ompl::control::World &w) const
105 {
106  std::size_t hash = 0;
107  for (const auto &p : w.props_)
108  ompl::hash_combine(hash, p);
109  return hash;
110 }
World(unsigned int numProps)
Initializes a world with a given number of propositions.
Definition: World.cpp:43
A class to represent an assignment of boolean values to propositions. A World can be partially restri...
Definition: World.h:71
std::string formula() const
Returns a formatted string representation of this World, as a conjunction of literals.
Definition: World.cpp:77
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:94
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:65
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
const std::unordered_map< unsigned int, bool > & props() const
Returns this World&#39;s underlying proposition-to-boolean assignment map.
Definition: World.cpp:89
unsigned int numProps() const
Returns the number of propositions declared for this World. Not all of the propositions have necessar...
Definition: World.cpp:60
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:47
void clear()
Clears this world&#39;s truth assignment.
Definition: World.cpp:99