Automaton.h
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 #ifndef OMPL_CONTROL_PLANNERS_LTL_AUTOMATON_
38 #define OMPL_CONTROL_PLANNERS_LTL_AUTOMATON_
39 
40 #include "ompl/control/planners/ltl/World.h"
41 #include "ompl/util/ClassForward.h"
42 #include <boost/unordered_map.hpp>
43 #include <limits>
44 #include <ostream>
45 #include <vector>
46 
47 namespace ompl
48 {
49  namespace control
50  {
52 
53  OMPL_CLASS_FORWARD(Automaton);
55 
69  class Automaton
70  {
71  public:
77  {
81  int eval(const World& w) const;
82 
83  TransitionMap& operator=(const TransitionMap& tm)
84  {
85  entries = tm.entries;
86  return *this;
87  }
88 
89  mutable boost::unordered_map<World, unsigned int> entries;
90  };
91 
93  Automaton(unsigned int numProps, unsigned int numStates = 0);
94 
96  unsigned int addState(bool accepting = false);
97 
99  void setAccepting(unsigned int s, bool a);
100 
102  bool isAccepting(unsigned int s) const;
103 
105  void setStartState(unsigned int s);
106 
109  int getStartState(void) const;
110 
112  void addTransition(unsigned int src, const World& w,
113  unsigned int dest);
114 
120  bool run(const std::vector<World>& trace) const;
121 
125  int step(int state, const World& w) const;
126 
128  TransitionMap& getTransitions(unsigned int src);
129 
131  unsigned int numStates(void) const;
132 
134  unsigned int numTransitions(void) const;
135 
137  unsigned int numProps(void) const;
138 
140  void print(std::ostream& out) const;
141 
144  unsigned int distFromAccepting(unsigned int s, unsigned int maxDist = std::numeric_limits<unsigned int>::max()) const;
145 
147  static AutomatonPtr AcceptingAutomaton(unsigned int numProps);
148 
151  static AutomatonPtr CoverageAutomaton(unsigned int numProps, const std::vector<unsigned int>& covProps);
152 
155  static AutomatonPtr SequenceAutomaton(unsigned int numProps, const std::vector<unsigned int>& seqProps);
156 
159  static AutomatonPtr DisjunctionAutomaton(unsigned int numProps, const std::vector<unsigned int>& disjProps);
160 
163  static AutomatonPtr AvoidanceAutomaton(unsigned int numProps, const std::vector<unsigned int>& avoidProps);
164 
168  static AutomatonPtr CoverageAutomaton(unsigned int numProps);
169 
173  static AutomatonPtr SequenceAutomaton(unsigned int numProps);
174 
177  static AutomatonPtr DisjunctionAutomaton(unsigned int numProps);
178 
179  protected:
180  unsigned int numProps_;
181  unsigned int numStates_;
182  int startState_;
183  std::vector<bool> accepting_;
184  std::vector<TransitionMap> transitions_;
185  mutable std::vector<unsigned int> distances_;
186  };
187  }
188 }
189 #endif
void print(std::ostream &out) const
Prints the automaton to a given output stream, in Graphviz dot format.
Definition: Automaton.cpp:123
unsigned int numTransitions(void) const
Returns the number of transitions in this automaton.
Definition: Automaton.cpp:109
void setAccepting(unsigned int s, bool a)
Sets the accepting status of a given state.
Definition: Automaton.cpp:51
A class to represent an assignment of boolean values to propositions. A World can be partially restri...
Definition: World.h:51
int step(int state, const World &w) const
Runs the automaton for one step from the given state, using the values of propositions from a given W...
Definition: Automaton.cpp:92
unsigned int distFromAccepting(unsigned int s, unsigned int maxDist=std::numeric_limits< unsigned int >::max()) const
Returns the shortest number of transitions from a given state to an accepting state.
Definition: Automaton.cpp:145
Automaton(unsigned int numProps, unsigned int numStates=0)
Creates an automaton with a given number of propositions and states.
Definition: Automaton.cpp:32
static AutomatonPtr SequenceAutomaton(unsigned int numProps, const std::vector< unsigned int > &seqProps)
Helper function to return a sequence automaton. Assumes all propositions are mutually exclusive...
Definition: Automaton.cpp:226
int getStartState(void) const
Returns the start state of the automaton. Returns -1 if no start state has been set.
Definition: Automaton.cpp:66
void setStartState(unsigned int s)
Sets the start state of the automaton.
Definition: Automaton.cpp:61
Main namespace. Contains everything in this library.
Definition: Cost.h:42
static AutomatonPtr DisjunctionAutomaton(unsigned int numProps, const std::vector< unsigned int > &disjProps)
Helper function to return a disjunction automaton, which accepts when one of the given propositions b...
Definition: Automaton.cpp:248
int eval(const World &w) const
Returns the automaton state corresponding to a given World in this transition map. Returns -1 if no such transition exists.
Definition: Automaton.cpp:13
Each automaton state has a transition map, which maps from a World to another automaton state...
Definition: Automaton.h:76
unsigned int addState(bool accepting=false)
Adds a new state to the automaton and returns an ID for it.
Definition: Automaton.cpp:42
void addTransition(unsigned int src, const World &w, unsigned int dest)
Adds a given transition to the automaton.
Definition: Automaton.cpp:71
unsigned int numProps(void) const
Returns the number of propositions used by this automaton.
Definition: Automaton.cpp:118
A boost shared pointer wrapper for ompl::control::Automaton.
static AutomatonPtr AcceptingAutomaton(unsigned int numProps)
Returns a single-state automaton that accepts on all inputs.
Definition: Automaton.cpp:183
static AutomatonPtr CoverageAutomaton(unsigned int numProps, const std::vector< unsigned int > &covProps)
Helper function to return a coverage automaton. Assumes all propositions are mutually exclusive...
Definition: Automaton.cpp:193
bool isAccepting(unsigned int s) const
Returns whether a given state of the automaton is accepting.
Definition: Automaton.cpp:56
static AutomatonPtr AvoidanceAutomaton(unsigned int numProps, const std::vector< unsigned int > &avoidProps)
Returns an avoidance automaton, which rejects when any one of the given list of propositions becomes ...
Definition: Automaton.cpp:266
A class to represent a deterministic finite automaton, each edge of which corresponds to a World...
Definition: Automaton.h:69
TransitionMap & getTransitions(unsigned int src)
Returns the outgoing transition map for a given automaton state.
Definition: Automaton.cpp:99
bool run(const std::vector< World > &trace) const
Runs the automaton from its start state, using the values of propositions from a given sequence of Wo...
Definition: Automaton.cpp:80
unsigned int numStates(void) const
Returns the number of states in this automaton.
Definition: Automaton.cpp:104