4 #include <opencv2/calib3d.hpp>
8 #include <boost/graph/graph_traits.hpp>
9 #include <boost/graph/adjacency_list.hpp>
10 #include <boost/graph/filtered_graph.hpp>
11 #include <boost/graph/graph_utility.hpp>
13 #include <boost/property_map/property_map.hpp>
14 #include <boost/graph/copy.hpp>
16 #include "disturbance.h"
17 #include "box2d_helpers.h"
19 const float NAIVE_PHI=10.0;
22 enum VERTEX_LABEL {UNLABELED, MOVING, ESCAPE, ESCAPE2};
30 template <
class V,
class M>
31 bool operator()(
const std::tuple<V,M, float> & p1,
const std::tuple<V, M, float> &p2)
const{
32 return std::get<2>(p1)< std::get<2>(p2);
41 float probability=1.0;
44 bool overrideZeroSteps=
false;
48 float weighted_probability(
int it){
51 result=probability*float(it_observed)/float(it);
72 b2Transform endPose = b2Transform_zero, start = b2Transform_zero;
73 simResult::resultType outcome;
74 std::vector <Direction> options;
78 VERTEX_LABEL label=VERTEX_LABEL::UNLABELED;
79 Direction direction=DEFAULT;
85 State(
const b2Transform &_start): start(_start){}
87 State(
const b2Transform &_start,
const Disturbance& di,
const Direction & dir): start(_start), Di(di), direction(dir){}
126 b2Transform travel_transform();
129 return direction==LEFT || direction==RIGHT;
132 bool isGoingStraight(){
133 return direction==DEFAULT || direction==STOP;
143 b2Transform pose=b2Transform_zero;
145 enum WHAT_D_FLAG{DI, DN};
161 return fabs(pose.p.x)+fabs(pose.p.y)+fabs(pose.q.GetAngle());
170 return fabs(bf.pose.p.x)+fabs(bf.pose.p.y)+bf.pose.q.GetAngle();
179 return fabs(bf.width())+fabs(bf.length());
225 typedef boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, State, Edge> TransitionSystem;
227 typedef boost::graph_traits<TransitionSystem>::vertex_iterator vertexIterator;
228 typedef boost::graph_traits<TransitionSystem>::vertex_descriptor vertexDescriptor;
229 typedef boost::graph_traits<TransitionSystem>::edge_descriptor edgeDescriptor;
230 typedef boost::graph_traits<TransitionSystem>::edge_iterator edgeIterator;
233 const vertexDescriptor DUMMY=1;
241 const vertexDescriptor MOVING_VERTEX=0;
256 bool operator()(edgeDescriptor e){
257 return e.m_target!=cv;
261 vertexDescriptor cv=0;
270 Connected(TransitionSystem * ts): g(ts){}
272 bool operator()(
const vertexDescriptor& v)
const{
273 bool in= boost::in_degree(v, *g)>0;
274 bool out =boost::out_degree(v, *g)>0;
275 return (in || out) || v==0 ;
278 TransitionSystem * g=NULL;
297 int simToMotorStep(
int simstep);
306 int distanceToSimStep(
const float& s,
const float& ds);
317 void update(edgeDescriptor e, std::pair <State, Edge> sk, TransitionSystem& g,
bool current,
int it);
327 void set(edgeDescriptor e, std::pair <State, Edge>sk, TransitionSystem&,
bool current,
int it);
330 std::pair< bool, edgeDescriptor> getMostLikely(TransitionSystem&,std::vector<edgeDescriptor>,
int);
332 std::vector <edgeDescriptor> outEdges(TransitionSystem&, vertexDescriptor, Direction);
334 Disturbance getExpectedDisturbance(TransitionSystem&, vertexDescriptor, Direction,
int);
344 std::pair <bool,edgeDescriptor> visitedEdge(
const std::vector <edgeDescriptor>& es, TransitionSystem& g, vertexDescriptor cv=TransitionSystem::null_vertex());
347 std::pair <edgeDescriptor, bool> add_edge(
const vertexDescriptor&,
const vertexDescriptor &, TransitionSystem&,
const int &, Direction d=UNDEFINED);
355 bool check_edge_direction(
const std::pair<edgeDescriptor, bool> &, TransitionSystem&, Direction);
366 std::vector<vertexDescriptor>::iterator to_task_end(edgeDescriptor &e, TransitionSystem &g,
const std::vector<vertexDescriptor> & plan, std::vector<vertexDescriptor>::iterator it);
375 InPlan(std::vector <vertexDescriptor>* _p):plan(_p){}
377 bool operator()(
const edgeDescriptor & e)
const{
378 return (check_vector_for(*plan, e.m_target)!=plan->end());
382 std::vector<vertexDescriptor> *plan;
391 bool operator()(
const edgeDescriptor & e)
const {
392 bool not_self= e.m_source!=e.m_target && (*g)[e].step!=0 ;
400 TransitionSystem * g=NULL;
413 bool operator()(
const edgeDescriptor & e)
const {
415 bool not_self= nse(e) || e.m_source==e.m_target && (*g)[e].step!=0;
419 TransitionSystem * g=NULL;
437 SameIteration(TransitionSystem & _g,
int _i): g(_g), iteration(_i){}
439 bool operator()(
const edgeDescriptor & e)
const {
440 return g[e].it_observed==iteration;
443 TransitionSystem & g;
449 typedef boost::filtered_graph<TransitionSystem, ViableEdge, Connected> FilteredTS;
456 enum MATCH_TYPE {_FALSE, D_NEW, DN_POSE, _TRUE, ANY, D_INIT, ABSTRACT, DI_POSE, DN_SHAPE, DI_SHAPE, POSE};
464 return pose() &&
abstract();
468 return position && angle;
472 return Di_exact() && Dn_exact();
476 return Di_position && Di_angle && Di_shape;
480 return Dn_position && Dn_angle && Dn_shape;
484 return Di_position && Di_angle;
488 return Dn_position && Dn_angle;
499 return Dn_shape && Dn_pose();
503 return Di_shape && Di_pose();
509 position = sd.pose.p.Length()<(threshold.for_robot_position()*coefficient);
510 angle=fabs(sd.pose.q.GetAngle())<threshold.for_robot_angle();
512 Dn_position= sd.Dn.pose.p.Length()<(Dn.radius()*coefficient);
513 Di_position= sd.Di.pose.p.Length()<(Di.radius()*coefficient);
514 Dn_angle=fabs(sd.Dn.pose.q.GetAngle())<Dn.get_angle();
515 Di_angle=fabs(sd.Di.pose.q.GetAngle())<Di.get_angle();
516 bool Dn_below_threshold_w=fabs(sd.Dn.width())<(Dn.get_width()*coefficient*2);
517 bool Dn_below_threshold_l=fabs(sd.Dn.length())<(Dn.get_length()*coefficient*2);
518 bool Di_below_threshold_w=fabs(sd.Di.width())<(Dn.get_width()*coefficient*2);
519 bool Di_below_threshold_l=fabs(sd.Di.length())<(Dn.get_length()*coefficient*2);
520 Dn_shape= Dn_below_threshold_l && Dn_below_threshold_w;
521 Di_shape= Di_below_threshold_l && Di_below_threshold_w;
525 StateMatcher::MATCH_TYPE what(){
529 else if (
abstract()){
532 else if (Dn_exact()){
535 else if (Di_exact()){
544 else if (shape_Dn()){
547 else if (shape_Di()){
559 bool Di_position=
false;
562 bool Dn_position=
false;
567 bool match_equal(
const MATCH_TYPE& candidate,
const MATCH_TYPE& desired);
575 float get_coefficient(
const float &);
579 const float COEFFICIENT_INCREASE_THRESHOLD=0.0;
582 typedef std::pair<StateMatcher::MATCH_TYPE, vertexDescriptor> VertexMatch;
Definition: disturbance.h:45
Definition: threshold.h:10
Definition: graphTools.h:453
Definition: threshold.h:91
Bundle for_Dn() const
Returns a bundle of thresholds for the initial disturbance.
Definition: threshold.h:121
Bundle for_Di() const
Returns a bundle of thresholds for the initial disturbance.
Definition: threshold.h:114
Compares the last float in a tuple.
Definition: graphTools.h:28
Predicate: gives info on whether a vertex has connections or is a singleton.
Definition: graphTools.h:268
Definition: disturbance.h:112
Edges connecting states in the transition system.
Definition: graphTools.h:40
bool enableOverride()
If this edge has zero steps, it sets override to true.
Definition: graphTools.cpp:40
Definition: graphTools.h:372
Definition: graphTools.h:387
Definition: graphTools.h:435
Contains the differences between the contiuous components of two hybrid states.
Definition: graphTools.h:142
float sum_D(const BodyFeatures &bf)
Total difference in disturbance pose and shape combined.
Definition: graphTools.h:187
void fill_valid_bodyfeatures(BodyFeatures &bf, const State &s1, const State &s2, WHAT_D_FLAG flag)
Fills the bodyfeatures match with differences between disturbances in the two states....
Definition: graphTools.cpp:100
void init(const State &s1, const State &s2)
Initialises the difference object using two states we want to compare.
Definition: graphTools.cpp:73
float get_sum(int mt)
Returns difference in certain aspects we want to match between states.
Definition: graphTools.cpp:52
float sum_D_pos(const BodyFeatures &bf)
Total difference in disturbance pose.
Definition: graphTools.h:169
void fill_invalid_bodyfeatures(BodyFeatures &)
Fills bf match with large values indicating no match.
Definition: graphTools.cpp:92
float sum_D_shape(const BodyFeatures &bf)
Total difference in disturbance shape.
Definition: graphTools.h:178
float sum_r()
Total difference in "global" robot pose.
Definition: graphTools.h:160
Definition: graphTools.h:461
Hybrid states in the transition system.
Definition: graphTools.h:69
bool visited()
Whether this state has been visited in exploration using evaluation function phi as proxy.
Definition: graphTools.h:92
void resetVisited()
Sets the phi value to its naive value (call to visited() will return false)
Definition: graphTools.h:100
b2Transform end_from_Dn() const
Return transformation from end to Di position.
Definition: graphTools.cpp:17
b2Transform end_from_Di() const
Return transformation from end to Di position.
Definition: graphTools.cpp:25
b2Transform start_from_Dn() const
Return transformation from start to Dn position.
Definition: graphTools.cpp:10
b2Transform start_from_Di() const
Return transformation from start to Di position.
Definition: graphTools.cpp:3
Predicate: gives info on whether an edge is worth keeping. Namely, the edge is either not a self-edge...
Definition: graphTools.h:409
Used as a predicate, gives info on whether a vertex is the current vertex.
Definition: graphTools.h:247
is_not_v(vertexDescriptor _cv)
Constructor assigns current vertex.
Definition: graphTools.h:254
Definition: disturbance.h:268