Bratmobile
b2bconfigurator.h
1 #ifndef B2BCONFIGURATOR_H
2 #define B2BCONFIGURATOR_H
3 #include "attentive.h"
9  FrontierCrashed() = default;
10 
11  FrontierCrashed(TransitionSystem & ts, Direction d) : transitionSystem(ts), direction(d) {}
12 
13  bool operator()(const Frontier & f) const {
14  if (f.connecting.empty()) return false;
15  return transitionSystem[f.connecting[0]].direction==direction && transitionSystem[f.frontier].outcome==simResult::crashed;
16  }
17  private:
18  TransitionSystem & transitionSystem;
19  Direction direction=UNDEFINED;
20 };
21 
27 class B2BConfigurator : public virtual AttentiveConfigurator {
28 public:
29  B2BConfigurator() = default;
30 
31  B2BConfigurator(Task & task) {
32  init(task);
33  }
34 
35 protected:
36 
44  bool closeVertex(std::set<vertexDescriptor> & closed, vertexDescriptor v) override;
45 
46  //std::vector<Direction> partiallyExplorativeOptions(std::pair<bool, edgeDescriptor> ve) override;
47 
48  // /**
49  // * @brief Doesn't split the task, just returns the vertex of the task at hand; if Task fails or
50  // * it's currently executing, it returns the source vertex for the task aswell
51  // *
52  // * @param v vertex of the task
53  // * @param d direction of the task that are allowed to split (not used)
54  // * @param src source of the task
55  // * @return std::vector <vertexDescriptor>
56  // */
57  // std::vector <vertexDescriptor> splitTask(vertexDescriptor v, Direction d, vertexDescriptor src=TransitionSystem::null_vertex()) override;
58 
59 
60  virtual float customSimulationStep(vertexDescriptor v=TransitionSystem::null_vertex());
61 
62 
73  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)override;
74 
83  bool attentionWindowOverlaps(const Disturbance & Di, const State & q, b2World & world, const Disturbance & focus );
84 
90  int visitedEdgeCount(const std::vector <edgeDescriptor>& es);
91 
92  int minimumEdgesForClearvoyance(Direction direction);
93 
94  // bool canGoToClearVoyance(const std::vector <edgeDescriptor> &oe, Direction direction);
105  virtual Disturbance getDisturbance(TransitionSystem&g, vertexDescriptor v, b2World & world, const Direction & dir, const b2Transform& start) override;
106 
107  //virtual std::vector <vertexDescriptor> task_vertices(vertexDescriptor v, std::pair<bool, edgeDescriptor>* ep=NULL);
108 
117  template <typename P>
118  std::vector <Direction> transitionInHindsight(vertexDescriptor v, P predicate){
119  std::vector <Direction> result;
121  std::vector <Frontier> frontiers=frontierVertices(v, transitionSystem, info);
122  if (frontiers.size()<2){ //only explored
123  // result={DEFAULT, LEFT, RIGHT};
124  //erase_from_vector(result, transitionSystem[ve.second.m_target].direction);
125  // return result;
126  result= predicate(v);
127  }
128  else if (frontiers.size()<4){ //left right explored
129  auto fLeft= std::find_if(frontiers.begin(), frontiers.end(), FrontierCrashed(transitionSystem, LEFT));
130  auto fRight= std::find_if(frontiers.begin(), frontiers.end(), FrontierCrashed(transitionSystem, RIGHT));
131  if (fLeft!=frontiers.end()){
132  result.push_back(DEFAULT);
133  }
134  if (fRight!=frontiers.end()){
135  result.push_back(DEFAULT);
136  }
137  }
138  return result;
139  }
140 
141  // /**
142  // *Combines edges K and jump function: represents possible transitions out of a state
143  // *@param v the vertex to which transitions are being assigned
144  // *@param d state direction (redundant)
145  // *@param src source vertex of state
146  // */
147  // virtual void transitionMatrix(vertexDescriptor v, Direction d, vertexDescriptor src)override;
148 
149  // virtual void removeExploredTransitions(vertexDescriptor v);
150 
160 virtual std::vector<vertexDescriptor> explorer(vertexDescriptor v, TransitionSystem&g, b2World &w)override; //evaluates only after DEFAULT, internal one step lookahead
161 
162 virtual simResult simulate(Task t, b2World & world, vertexDescriptor v0);
163 
167 void applyTransitionMatrix(vertexDescriptor v0, Direction d, bool ended, vertexDescriptor src, std::vector<vertexDescriptor>& plan_prov);
168 
169 
170 // /**
171 // * @brief Overload of makeRobot, uses a disturbance which may be the goal of the hindsight disturbance to make the sensor
172 // *
173 // * @param t
174 // * @param world
175 // * @param focus disturbance focus of attention (used for making the sensor)
176 // * @return Robot
177 // */
178 // virtual Robot makeRobot( b2World & world, const b2Transform& start, const Disturbance & focus);
184  public:
186  std::vector<Disturbance> disturbances;
187  vertexDescriptor source; //vertex from which additional DEFAULT Tasks will be explored
188 
189  DisturbanceLookahead()=default;
190 
191  DisturbanceLookahead( vertexDescriptor v, const Disturbance & d) : source(v) {
192  disturbances.push_back(d);
193  }
194 
195  };
196 
197  virtual bool add(vertexDescriptor v, const Disturbance& d);
198 
205  virtual Disturbance query(vertexDescriptor v);
206 
210  void pop(vertexDescriptor v);
211 
212  void reset() {lookaheads.clear();}
213 
214  std::vector <DisturbanceLookahead> getLookaheads() const {
215  return lookaheads;
216  }
217 
218  void setRoot(vertexDescriptor v){
219  root=v;
220  }
221 
222  vertexDescriptor getRoot(){
223  return root;
224  }
225  protected:
226  vertexDescriptor root=TransitionSystem::null_vertex(); //root of modular expansion
227  std::vector<DisturbanceLookahead> lookaheads;
228 }clearvoyance;
229 
238 void addOptionsInHindsight(vertexDescriptor v, vertexDescriptor v0, vertexDescriptor v1);
239 
240 
241 };
242 
243 #endif
Configurator with long-range planning. It explores transitions out of a state until a DEFAULT Task is...
Definition: attentive.h:10
Stores disturbance lookaheads for alternative DEFAULT tasks (where the disturbance is backpropagated)
Definition: b2bconfigurator.h:183
void pop(vertexDescriptor v)
Pops the first disturbance off of the vector associated with the queried vertex.
Definition: b2bconfigurator.cpp:309
virtual Disturbance query(vertexDescriptor v)
Gets first disturbance predicted with a vertex.
Definition: b2bconfigurator.cpp:299
B2BConfigurator is a class that extends the AttentiveConfigurator to eliminate the Task splitting in ...
Definition: b2bconfigurator.h:27
void addOptionsInHindsight(vertexDescriptor v, vertexDescriptor v0, vertexDescriptor v1)
If the frontier is a default state and its source is a turning task, it adds an additional default op...
Definition: b2bconfigurator.cpp:272
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) override
Same as AttentiveConfigurator::backtrack, but also adds vertex to priority queue if the vertex is in ...
Definition: b2bconfigurator.cpp:60
virtual std::vector< vertexDescriptor > explorer(vertexDescriptor v, TransitionSystem &g, b2World &w) override
Constructs transition system using a Box2D simulation combined with an A* graph expansion algorithm.
Definition: b2bconfigurator.cpp:229
void applyTransitionMatrix(vertexDescriptor v0, Direction d, bool ended, vertexDescriptor src, std::vector< vertexDescriptor > &plan_prov)
same as Focused but adds additional default tasks in hindsight
Definition: b2bconfigurator.cpp:256
int visitedEdgeCount(const std::vector< edgeDescriptor > &es)
Counts the number of visited edges.
Definition: b2bconfigurator.cpp:117
virtual Disturbance getDisturbance(TransitionSystem &g, vertexDescriptor v, b2World &world, const Direction &dir, const b2Transform &start) override
Uses clearvoyance to get the disturbance for a vertex if needed.
Definition: b2bconfigurator.cpp:143
bool closeVertex(std::set< vertexDescriptor > &closed, vertexDescriptor v) override
Closes a vertex but the maximum out edges number for a default state to have is 5 instead of 3 Also a...
Definition: b2bconfigurator.cpp:3
std::vector< Direction > transitionInHindsight(vertexDescriptor v, P predicate)
Looks at the frontier to unlock transitions from a vertex.
Definition: b2bconfigurator.h:118
bool attentionWindowOverlaps(const Disturbance &Di, const State &q, b2World &world, const Disturbance &focus)
Returns true if attention window overlap with Di.
Definition: b2bconfigurator.cpp:105
virtual void init(Task _task=Task())
Initialises configurator.
Definition: configurator.cpp:20
Information about what the configurator is doing (current Task, current vertex), what it wants to do ...
Definition: planner.h:49
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
A closed hybrid control loop. It has an initial disturbance representing the continuous state and a d...
Definition: task.h:47
std::vector< Frontier > frontierVertices(vertexDescriptor v, TransitionSystem &g, ExecutionInfo &info)
Performs iterative deepening search to find the frontier.
Definition: planner.cpp:100
Definition: disturbance.h:113
Predicate assessing whether a frontier is a crashed state.
Definition: b2bconfigurator.h:8
Contains the frontier (first) and the connecting vertices.
Definition: planner.h:17
Hybrid states in the transition system.
Definition: graphTools.h:69
Definition: disturbance.h:269