Bratmobile
focused.h
1 #ifndef FOCUSED_H
2 #define FOCUSED_H
3 #include "configurator.h"
4 
8 class FocusedConfigurator:public virtual Configurator{
9  protected:
10  StateMatcher* matcher=new StateMatcher();
18 ExecutionInfo package_info(vertexDescriptor gv=TransitionSystem::null_vertex(), bool been=false){
19  return ExecutionInfo(currentVertex, gv, currentTask, controlGoal, been, m_plan);
20 }
21 
35 virtual Disturbance getDisturbance(TransitionSystem&g, vertexDescriptor v, b2World & world, const Direction & dir, const b2Transform& start);
36 
48 virtual void backtrack(std::vector <vertexDescriptor>& evaluation_q, std::vector <vertexDescriptor>&priority_q, std::set<vertexDescriptor>& closed, std::vector <vertexDescriptor>& plan_prov, vertexDescriptor module_src=MOVING_VERTEX, vertexDescriptor startRecycle=MOVING_VERTEX);
49 
58 virtual std::vector <vertexDescriptor> splitTask(vertexDescriptor v, Direction d, vertexDescriptor src=TransitionSystem::null_vertex());
59 
70 virtual bool propagateD(vertexDescriptor v1, vertexDescriptor v0, std::set<vertexDescriptor>*closed=NULL, StateMatcher::MATCH_TYPE match=StateMatcher::_FALSE);
71 
72 //if in plan the vertex gets priority
73 void planPriority(TransitionSystem&, vertexDescriptor);
74 
75 
76 //adjust real-world task
77 void adjust_rw_task(const vertexDescriptor&, TransitionSystem &, Task*, const b2Transform &);
78 
79 
91 virtual VertexMatch hardMatch(State s, Direction dir=Direction::UNDEFINED, StateMatcher::MATCH_TYPE match_type=StateMatcher::_TRUE, StateDifference * _sd=NULL); //matches to most likely
92 
93 
105 virtual VertexMatch findMatch(State s, Direction dir=Direction::UNDEFINED, StateMatcher::MATCH_TYPE match_type=StateMatcher::_TRUE, StateDifference * _sd=NULL, vertexDescriptor src=TransitionSystem::null_vertex()); //matches to most likely
106 
116 virtual std::vector<vertexDescriptor> explorer(vertexDescriptor v, TransitionSystem&g, b2World &w); //evaluates only after DEFAULT, internal one step lookahead
117 
121 void resetPhi();
122 
132 std::pair <edgeDescriptor, bool> add_vertex_now(const vertexDescriptor & src, vertexDescriptor & v1, Disturbance obs,Edge edge=Edge(), bool topDown=0);
133 
142 std::pair <edgeDescriptor, bool> add_vertex_retro(vertexDescriptor &src, vertexDescriptor &v1, Edge edge=Edge(), bool topDown=0);
143 
145 std::vector <Direction> getExploredDirections(vertexDescriptor v, const std::vector<Direction>& directions);
146 
151 virtual bool preventTransition(vertexDescriptor v){
152  return v==currentVertex && (!m_plan.empty()||!currentTask.is_over());
153 }
154 
156 virtual void removeExploredTransitions(vertexDescriptor v);
157 
164 virtual void transitionMatrix(vertexDescriptor v, Direction d, vertexDescriptor src);
165 
175 void applyTransitionMatrix(vertexDescriptor v0, Direction d, bool ended, vertexDescriptor src, std::vector<vertexDescriptor>& plan_prov);
176 
185 void addToPriorityQueue(vertexDescriptor v, std::vector<vertexDescriptor>& queue, const std::set <vertexDescriptor>& closed);
186 
187 
188 //removes singleton vertices and self-edges
189 void ts_cleanup();
190 
191 //apply affine transformation to states (e.g. if the same situation encountered in the past is reencountered)
192 void shift_states(TransitionSystem &, const std::vector<vertexDescriptor>&, const b2Transform &); //shifts a sequence of states by a certain transform
193 
194 //return vertex from which exploration of the environment starts (explorer)
195 vertexDescriptor get_explore_start(TransitionSystem &);
196 
202 void pre_explore();
203 
204 
205 //std::vector <State> output_plan(const std::vector<vertexDescriptor> &, const TransitionSystem &);
206 
207 void explore_plan(b2World&)override;
208 
219 std::vector<Direction>::iterator get_next_option(vertexDescriptor v, vertexDescriptor src, std::vector<vertexDescriptor> full_plan);
220 
236 virtual bool recycle_plan(vertexDescriptor v, vertexDescriptor &v0, vertexDescriptor & task_start, StateMatcher::MATCH_TYPE &matchType,
237  b2Transform & shift_start, b2Transform& sk_first_start, std::pair<edgeDescriptor, bool>&edge,
238  std::vector<vertexDescriptor> &plan_prov, Direction t_get_direction){
239  return false;
240  }
241 
242 
254 std::pair<State, Edge> simulation_setup(b2World& w, Task & t, vertexDescriptor v0, b2Transform shift, b2Transform &start, std::vector<Direction>v0_options);
255 
262 void reassign_direction(vertexDescriptor bestNext, Direction& direction);
263 
264 // /**
265 // * @brief if the match is a crashed task
266 // *
267 // * @param match
268 // * @param other_matches
269 // * @return true if changes match
270 // */
271 // bool matchToSafe(VertexMatch &match,const std::vector<VertexMatch> &other_matches=std::vector<VertexMatch>());
272 
285 std::pair<edgeDescriptor, bool> setup_match_edge(VertexMatch &match, vertexDescriptor &v0, vertexDescriptor & v1,const Edge& k, Direction direction, bool changedMatch);
286 
293 virtual std::vector <vertexDescriptor> task_vertices(vertexDescriptor v, std::pair<bool, edgeDescriptor>* ep=NULL);
294 
295 // /**
296 // * @brief Returns a visited edge if present, or if the current
297 // *
298 // * @param es
299 // * @param g
300 // * @param cv
301 // * @return std::vector <vertexDescriptor>
302 // */
303 // std::vector <vertexDescriptor> visitedOrVisitingEdge(const std::vector <edgeDescriptor>& es, TransitionSystem& g, vertexDescriptor cv=TransitionSystem::null_vertex());
304 
313 vertexDescriptor getRecyclingStart(vertexDescriptor v, vertexDescriptor v1, vertexDescriptor taskStart);
314 
320 std::vector <edgeDescriptor> inEdges(vertexDescriptor v, Direction d = UNDEFINED); //returns a vector containing all the in-edges of a vertex which have the specified direction
321 
322 virtual bool closeVertex(std::set<vertexDescriptor> & closed, vertexDescriptor v);
323 
334 std::pair<edgeDescriptor, bool> addEdgeRetrospectively(vertexDescriptor v, vertexDescriptor &v1, const State & s_tmp,std::pair<edgeDescriptor, bool> first_edge, Direction d, float linearSpeed);
335 
346 void correctQueue(std::vector<vertexDescriptor>& queue, vertexDescriptor v, vertexDescriptor startRecycle, int planProvSize);
347 
353 void adjustProbability(const edgeDescriptor &e);
354 
360 void abandonPlan(std::vector<vertexDescriptor>& planProv, vertexDescriptor v0, vertexDescriptor v1);
361 
368 virtual std::vector<Direction> partiallyExplorativeOptions(std::pair<bool, edgeDescriptor> ve);
369 
370 virtual Robot makeRobot(b2World & w, const Task & task);
376  protected:
377  vertexDescriptor lastAdded=TransitionSystem::null_vertex(); //last vertex added to the evaluation queue
378  public:
379 
389  void addToEvaluationQueue(std::vector <vertexDescriptor>& evaluationQueue, vertexDescriptor v1, TransitionSystem & g, vertexDescriptor v);
390 
391  void reset(vertexDescriptor v=TransitionSystem::null_vertex()){
392  lastAdded=v;
393  }
394 };
395 
402 void visitedDirectionsPushBack( vertexDescriptor v, std::vector<Direction> & visitedDirections, Direction direction);
403 
404 virtual StateMatcher::MATCH_TYPE desiredMatch() {
405  return StateMatcher::MATCH_TYPE::ABSTRACT;
406 }
407 
412 void enforce_edge();
413 
417 virtual bool shouldPartiallyExplore(const std::vector<edgeDescriptor>& oe, std::pair<bool, edgeDescriptor> ve);
418 
423 virtual bool canPropagate(vertexDescriptor v);
424 
425 virtual bool canReassignOutcome(vertexDescriptor v);
426 
427 virtual float customSimulationStep(vertexDescriptor v=TransitionSystem::null_vertex());
428 
429 
430 bool hasPlanFinished(){
431  return m_plan.empty() && currentTask.is_over();
432 }
433 
434 public:
435 
436 void adjust_goal_expectation()override{}
437 
439 
441  init(_task);
442 }
443 
444 virtual ~FocusedConfigurator(){
445  delete matcher;
446  matcher=NULL;
447 }
448 
449 
450 
451 };
452 
453 #endif
Definition: configurator.h:17
virtual void init(Task _task=Task())
Initialises configurator.
Definition: configurator.cpp:19
Information about what the configurator is doing (current Task, current vertex), what it wants to do ...
Definition: planner.h:48
Adds and removes vertices from the evaluation queue.
Definition: focused.h:375
void addToEvaluationQueue(std::vector< vertexDescriptor > &evaluationQueue, vertexDescriptor v1, TransitionSystem &g, vertexDescriptor v)
Adds to evaluation queue: if the vertex represents a successful task, it is added to the end of the q...
Definition: focused.cpp:816
Configurator that only replans if the current task fails.
Definition: focused.h:8
std::pair< edgeDescriptor, bool > addEdgeRetrospectively(vertexDescriptor v, vertexDescriptor &v1, const State &s_tmp, std::pair< edgeDescriptor, bool > first_edge, Direction d, float linearSpeed)
Adds edge retrospectively (used in split task)
Definition: focused.cpp:741
virtual void transitionMatrix(vertexDescriptor v, Direction d, vertexDescriptor src)
Definition: focused.cpp:311
virtual std::vector< vertexDescriptor > explorer(vertexDescriptor v, TransitionSystem &g, b2World &w)
Constructs transition system using a Box2D simulation combined with an A* graph expansion algorithm.
Definition: focused.cpp:78
void adjustProbability(const edgeDescriptor &e)
Adjusts the probability that a continuous state (edge target) will occur after taking a discrete stat...
Definition: focused.cpp:764
std::pair< edgeDescriptor, bool > add_vertex_retro(vertexDescriptor &src, vertexDescriptor &v1, Edge edge=Edge(), bool topDown=0)
Adds vertices retroactively (e.g. after a state is split)
Definition: focused.cpp:12
void explore_plan(b2World &) override
Explores and plan.
Definition: focused.cpp:569
virtual std::vector< vertexDescriptor > splitTask(vertexDescriptor v, Direction d, vertexDescriptor src=TransitionSystem::null_vertex())
Split tasks into sub-states of fixed length.
Definition: focused.cpp:157
void applyTransitionMatrix(vertexDescriptor v0, Direction d, bool ended, vertexDescriptor src, std::vector< vertexDescriptor > &plan_prov)
Sets permitted transitions out of a state, formally combines jump guard J and guard Psi.
Definition: focused.cpp:362
virtual bool recycle_plan(vertexDescriptor v, vertexDescriptor &v0, vertexDescriptor &task_start, StateMatcher::MATCH_TYPE &matchType, b2Transform &shift_start, b2Transform &sk_first_start, std::pair< edgeDescriptor, bool > &edge, std::vector< vertexDescriptor > &plan_prov, Direction t_get_direction)
Does not recycle.
Definition: focused.h:236
void enforce_edge()
Used to make sure that there is always a connection between DUMMY/MOVING to the next in plan - useful...
Definition: focused.cpp:830
void abandonPlan(std::vector< vertexDescriptor > &planProv, vertexDescriptor v0, vertexDescriptor v1)
Clears plan vector, sets current task to change and clears current vertices.
Definition: focused.cpp:781
void visitedDirectionsPushBack(vertexDescriptor v, std::vector< Direction > &visitedDirections, Direction direction)
Pushes directions visited at this iteration (not using phi) to input vector.
Definition: focused.cpp:282
virtual VertexMatch findMatch(State s, Direction dir=Direction::UNDEFINED, StateMatcher::MATCH_TYPE match_type=StateMatcher::_TRUE, StateDifference *_sd=NULL, vertexDescriptor src=TransitionSystem::null_vertex())
Searches transition System for a match to a state provided. Continuous states are matched,...
Definition: focused.cpp:503
std::vector< Direction > getExploredDirections(vertexDescriptor v, const std::vector< Direction > &directions)
Definition: focused.cpp:291
vertexDescriptor getRecyclingStart(vertexDescriptor v, vertexDescriptor v1, vertexDescriptor taskStart)
Returns the vertex from which to start recycling plan.
Definition: focused.cpp:713
std::pair< State, Edge > simulation_setup(b2World &w, Task &t, vertexDescriptor v0, b2Transform shift, b2Transform &start, std::vector< Direction >v0_options)
Sets up for simulation.
Definition: focused.cpp:596
void resetPhi()
Resets all vertices evaluation function phi to a default unitialised value of 10.
Definition: focused.cpp:20
ExecutionInfo package_info(vertexDescriptor gv=TransitionSystem::null_vertex(), bool been=false)
Package task/goal execution details into an Execution Info instance.
Definition: focused.h:18
void addToPriorityQueue(vertexDescriptor v, std::vector< vertexDescriptor > &queue, const std::set< vertexDescriptor > &closed)
Adds vertexDescriptor to priority queue according to a custom heuristic.
Definition: focused.cpp:414
virtual void removeExploredTransitions(vertexDescriptor v)
Definition: focused.cpp:301
virtual void backtrack(std::vector< vertexDescriptor > &evaluation_q, std::vector< vertexDescriptor > &priority_q, std::set< vertexDescriptor > &closed, std::vector< vertexDescriptor > &plan_prov, vertexDescriptor module_src=MOVING_VERTEX, vertexDescriptor startRecycle=MOVING_VERTEX)
Iterates through vertices, if they result in crash, it splits the tasks and recalculates evaluation f...
Definition: focused.cpp:217
virtual bool canPropagate(vertexDescriptor v)
propagates only if the previous vertex
Definition: focused.cpp:255
std::pair< edgeDescriptor, bool > setup_match_edge(VertexMatch &match, vertexDescriptor &v0, vertexDescriptor &v1, const Edge &k, Direction direction, bool changedMatch)
Given a valid match, sets up the edge with the previous vertex Creates new edge if it doesn't exist,...
Definition: focused.cpp:635
virtual std::vector< Direction > partiallyExplorativeOptions(std::pair< bool, edgeDescriptor > ve)
Assigns options to vertex which has in previous iteration been expanded. Assigns direction of success...
Definition: focused.cpp:791
virtual bool propagateD(vertexDescriptor v1, vertexDescriptor v0, std::set< vertexDescriptor > *closed=NULL, StateMatcher::MATCH_TYPE match=StateMatcher::_FALSE)
Propagate a disturbance backwards to all states representing the same task (max 1)
Definition: focused.cpp:264
virtual Disturbance getDisturbance(TransitionSystem &g, vertexDescriptor v, b2World &world, const Direction &dir, const b2Transform &start)
Use attention window to find if any previously avoided obstacle is in the way of the goal,...
Definition: focused.cpp:37
virtual std::vector< vertexDescriptor > task_vertices(vertexDescriptor v, std::pair< bool, edgeDescriptor > *ep=NULL)
Rerturns all the vertices making up a task.
Definition: focused.cpp:644
std::pair< edgeDescriptor, bool > add_vertex_now(const vertexDescriptor &src, vertexDescriptor &v1, Disturbance obs, Edge edge=Edge(), bool topDown=0)
Adds state after discovering it in exploration.
Definition: focused.cpp:4
void adjust_goal_expectation() override
Uses tracking information to adjust the position of the goal relative to the robot.
Definition: focused.h:436
void reassign_direction(vertexDescriptor bestNext, Direction &direction)
Reassigns direction as the direction of the bext task to expand next in explorer.
Definition: focused.cpp:606
std::vector< edgeDescriptor > inEdges(vertexDescriptor v, Direction d=UNDEFINED)
Returns a vector of all the in-edges of vertex.
Definition: focused.cpp:694
void correctQueue(std::vector< vertexDescriptor > &queue, vertexDescriptor v, vertexDescriptor startRecycle, int planProvSize)
Edits.
Definition: focused.cpp:755
virtual VertexMatch hardMatch(State s, Direction dir=Direction::UNDEFINED, StateMatcher::MATCH_TYPE match_type=StateMatcher::_TRUE, StateDifference *_sd=NULL)
Searches transition System for a match to a state provided. Continuous states are matched,...
Definition: focused.cpp:435
virtual bool shouldPartiallyExplore(const std::vector< edgeDescriptor > &oe, std::pair< bool, edgeDescriptor > ve)
Psi guard: should only current task be simulated?
Definition: focused.cpp:835
std::vector< Direction >::iterator get_next_option(vertexDescriptor v, vertexDescriptor src, std::vector< vertexDescriptor > full_plan)
Returns an iterator to the next option representing a discrete Task transition. If v is in the plan,...
virtual bool preventTransition(vertexDescriptor v)
Definition: focused.h:151
void pre_explore()
Prepares transitionsystem for exploration: clears all edges of q0 with states that aren't the current...
Definition: focused.cpp:555
virtual Robot makeRobot(b2World &w, const Task &task)
Makes the robot object in the world with the given start position and task.
Definition: focused.cpp:810
Robot class for Box2D simulation.
Definition: robot.h:18
Definition: graphTools.h:453
A closed hybrid control loop. It has an initial disturbance representing the continuous state and a d...
Definition: task.h:47
bool is_over()
If task has finished executing.
Definition: task.h:369
Definition: disturbance.h:113
Edges connecting states in the transition system.
Definition: graphTools.h:40
Contains the differences between the contiuous components of two hybrid states.
Definition: graphTools.h:143
Hybrid states in the transition system.
Definition: graphTools.h:69