BehaviorPrediction.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /// \file BehaviorPrediction.h
  2. /// \brief Predict detected vehicles's possible trajectories, these trajectories extracted from the vector map.
  3. /// \author Hatem Darweesh
  4. /// \date Jul 6, 2017
  5. #ifndef BEHAVIORPREDICTION_H_
  6. #define BEHAVIORPREDICTION_H_
  7. #include <boost/random.hpp>
  8. #include <boost/math/distributions/normal.hpp>
  9. #include "PlannerH.h"
  10. #include "op_utility/UtilityH.h"
  11. #include "PassiveDecisionMaker.h"
  12. namespace PlannerHNS
  13. {
  14. #define MOTION_POSE_ERROR 0.2 // 50 cm pose error
  15. #define MOTION_ANGLE_ERROR 0.01 // 0.05 rad angle error
  16. #define MOTION_VEL_ERROR 0.2
  17. #define MEASURE_POSE_ERROR 0.1
  18. #define MEASURE_ANGLE_ERROR 0.01
  19. #define MEASURE_VEL_ERROR 0.1
  20. #define MEASURE_IND_ERROR 0.1
  21. #define PREDICTION_DISTANCE_PERCENTAGE 0.25
  22. #define BEH_PARTICLES_NUM 25
  23. #define BEH_MIN_PARTICLE_NUM 0
  24. #define KEEP_PERCENTAGE 0.85
  25. #define MAX_PREDICTION_SPEED 10.0
  26. #define POSE_FACTOR 0.1
  27. #define DIRECTION_FACTOR 0.1
  28. #define VELOCITY_FACTOR 0.2
  29. #define ACCELERATE_FACTOR 0.1
  30. #define INDICATOR_FACTOR 0.5
  31. #define FIXED_PLANNING_DISTANCE 10
  32. #define MIN_PREDICTION_TIME 5
  33. #define USE_OPEN_PLANNER_MOVE 0
  34. #define ACCELERATION_CALC_TIME 0.25
  35. #define ACCELERATION_DECISION_VALUE 0.5
  36. #define ENABLE_STOP_BEHAVIOR_GEN 1
  37. typedef boost::mt19937 ENG;
  38. typedef boost::normal_distribution<double> NormalDIST;
  39. typedef boost::variate_generator<ENG, NormalDIST> VariatGEN;
  40. class TrajectoryTracker;
  41. class Particle
  42. {
  43. public:
  44. bool bDeleted;
  45. BEH_STATE_TYPE beh; //[Stop, Yielding, Forward, Branching]
  46. double vel; //[0 -> Stop,1 -> moving]
  47. double vel_prev_big;
  48. double prev_time_diff;
  49. int acc; //[-1 ->Slowing, 0, Stopping, 1 -> accelerating]
  50. double acc_raw;
  51. int indicator; //[0 -> No, 1 -> Left, 2 -> Right , 3 -> both]
  52. WayPoint pose;
  53. bool bStopLine;
  54. double w;
  55. double w_raw;
  56. double pose_w;
  57. double dir_w;
  58. double vel_w;
  59. double acl_w;
  60. double ind_w;
  61. TrajectoryTracker* pTraj;
  62. int original_index;
  63. Particle()
  64. {
  65. prev_time_diff = 0;
  66. vel_prev_big = 0;
  67. original_index = 0;
  68. bStopLine = false;
  69. bDeleted = false;
  70. pTraj = nullptr;
  71. w = 0;
  72. w_raw = 0;
  73. pose_w = 0;
  74. dir_w = 0;
  75. vel_w = 0;
  76. acl_w = 0;
  77. ind_w = 0;
  78. beh = BEH_STOPPING_STATE;
  79. vel = 0;
  80. acc = 0;
  81. acc_raw = 0;
  82. indicator = 0;
  83. }
  84. };
  85. class TrajectoryTracker
  86. {
  87. public:
  88. unsigned int index;
  89. BEH_STATE_TYPE beh;
  90. BEH_STATE_TYPE best_beh;
  91. double best_p;
  92. std::vector<int> ids;
  93. std::vector<int> path_ids;
  94. WayPoint path_last_pose;
  95. double rms_error;
  96. std::vector<WayPoint> trajectory;
  97. std::vector<Particle> m_ForwardPart;
  98. std::vector<Particle> m_StopPart;
  99. std::vector<Particle> m_YieldPart;
  100. std::vector<Particle> m_LeftPart;
  101. std::vector<Particle> m_RightPart;
  102. BehaviorState m_CurrBehavior;
  103. int nAliveStop;
  104. int nAliveYield;
  105. int nAliveForward;
  106. int nAliveLeft;
  107. int nAliveRight;
  108. double pStop;
  109. double pYield;
  110. double pForward;
  111. double pLeft;
  112. double pRight;
  113. double w_avg_forward;
  114. double w_avg_stop;
  115. double w_avg_yield;
  116. double w_avg_left;
  117. double w_avg_right;
  118. PassiveDecisionMaker m_SinglePathDecisionMaker;
  119. TrajectoryTracker()
  120. {
  121. beh = BEH_STOPPING_STATE;
  122. rms_error = 0;
  123. index = 0;
  124. nAliveStop = 0;
  125. nAliveYield = 0;
  126. nAliveForward = 0;
  127. nAliveLeft = 0;
  128. nAliveRight = 0;
  129. pStop = 0;
  130. pYield = 0;
  131. pForward = 0;
  132. pLeft = 0;
  133. pRight = 0;
  134. best_beh = PlannerHNS::BEH_STOPPING_STATE;
  135. best_p = 0;
  136. w_avg_forward = 0;
  137. w_avg_stop = 0;
  138. w_avg_yield = 0;
  139. w_avg_left = 0;
  140. w_avg_right = 0;
  141. }
  142. virtual ~TrajectoryTracker()
  143. {
  144. }
  145. void InitDecision()
  146. {
  147. }
  148. TrajectoryTracker(const TrajectoryTracker& obj)
  149. {
  150. rms_error = 0;
  151. ids = obj.ids;
  152. path_ids = obj.path_ids;
  153. path_last_pose = obj.path_last_pose;
  154. beh = obj.beh;
  155. index = obj.index;
  156. trajectory = obj.trajectory;
  157. nAliveStop = obj.nAliveStop;
  158. nAliveYield = obj.nAliveYield;
  159. nAliveForward = obj.nAliveForward;
  160. nAliveLeft = obj.nAliveLeft;
  161. nAliveRight = obj.nAliveRight;
  162. pStop = obj.pStop;
  163. pYield = obj.pYield;
  164. pForward = obj.pForward;
  165. pLeft = obj.pLeft;
  166. pRight = obj.pRight;
  167. best_beh = obj.best_beh;
  168. best_p = obj.best_p;
  169. m_SinglePathDecisionMaker = obj.m_SinglePathDecisionMaker;
  170. m_ForwardPart = obj.m_ForwardPart;
  171. m_StopPart = obj.m_StopPart;
  172. m_YieldPart = obj.m_YieldPart;
  173. m_LeftPart = obj.m_LeftPart;
  174. m_RightPart = obj.m_RightPart;
  175. m_CurrBehavior = obj.m_CurrBehavior;
  176. w_avg_forward = obj.w_avg_forward;
  177. w_avg_stop = obj.w_avg_stop;
  178. w_avg_yield = obj.w_avg_yield;
  179. w_avg_left = obj.w_avg_left;
  180. w_avg_right = obj.w_avg_right;
  181. }
  182. TrajectoryTracker(std::vector<PlannerHNS::WayPoint>& path, const unsigned int& _index)
  183. {
  184. if(path.size()>0)
  185. {
  186. beh = path.at(0).beh_state;
  187. //std::cout << "New Path: Beh: " << beh << ", index: " << _index << ", LaneID_0: " << path.at(0).laneId << ", LaneID_1: "<< path.at(1).laneId << std::endl;
  188. }
  189. index = _index;
  190. trajectory = path;
  191. int prev_id = -10;
  192. int curr_id = -10;
  193. ids.clear();
  194. path_ids.clear();
  195. for(unsigned int i = 0; i < path.size(); i++)
  196. {
  197. curr_id = path.at(i).laneId;
  198. path_ids.push_back(curr_id);
  199. if(curr_id != prev_id)
  200. {
  201. ids.push_back(curr_id);
  202. prev_id = curr_id;
  203. }
  204. }
  205. path_last_pose = path.at(path.size()-1);
  206. nAliveStop = 0;
  207. nAliveYield = 0;
  208. nAliveForward = 0;
  209. nAliveLeft = 0;
  210. nAliveRight = 0;
  211. pStop = 0;
  212. pYield = 0;
  213. pForward = 0;
  214. pLeft = 0;
  215. pRight = 0;
  216. best_beh = PlannerHNS::BEH_STOPPING_STATE;
  217. best_p = 0;
  218. InitDecision();
  219. }
  220. void UpdatePathAndIndex(std::vector<PlannerHNS::WayPoint>& _path, const unsigned int& _index)
  221. {
  222. if(_path.size() == 0) return;
  223. beh = _path.at(0).beh_state;
  224. index = _index;
  225. trajectory = _path;
  226. int prev_id = -10;
  227. int curr_id = -10;
  228. ids.clear();
  229. path_ids.clear();
  230. for(unsigned int i = 0; i < _path.size(); i++)
  231. {
  232. curr_id = _path.at(i).laneId;
  233. path_ids.push_back(curr_id);
  234. if(curr_id != prev_id)
  235. {
  236. ids.push_back(curr_id);
  237. prev_id = curr_id;
  238. }
  239. }
  240. path_last_pose = _path.at(_path.size()-1);
  241. }
  242. double CalcMatchingPercentage(const std::vector<PlannerHNS::WayPoint>& _path)
  243. {
  244. if(_path.size() == 0) return 0;
  245. if(beh != _path.at(0).beh_state) return 0;
  246. int nCount = 0, nIds = 0;
  247. int prev_id = -10;
  248. int curr_id = -10;
  249. std::vector<int> _ids;
  250. for(unsigned int i = 0; i < _path.size(); i++)
  251. {
  252. curr_id = _path.at(i).laneId;
  253. if(i < path_ids.size())
  254. {
  255. nCount++;
  256. if(curr_id == path_ids.at(i))
  257. nIds++;
  258. }
  259. if(curr_id != prev_id)
  260. {
  261. _ids.push_back(curr_id);
  262. prev_id = curr_id;
  263. }
  264. }
  265. int nEqualities = 0;
  266. for(unsigned int i=0; i < _ids.size(); i++)
  267. {
  268. for(unsigned int j=0; j < ids.size(); j++)
  269. {
  270. if(_ids.at(i) == ids.at(j))
  271. {
  272. nEqualities++;
  273. break;
  274. }
  275. }
  276. }
  277. double rms_val = 0;
  278. for(unsigned int i=0; i < _path.size(); i++)
  279. {
  280. if(i < trajectory.size())
  281. {
  282. rms_val += hypot(_path.at(i).pos.y - trajectory.at(i).pos.y, _path.at(i).pos.y - trajectory.at(i).pos.y);
  283. }
  284. }
  285. rms_error = rms_val;
  286. if(rms_error < 5.0)
  287. return 1;
  288. if(_ids.size() == ids.size() && ids.size() == nEqualities && rms_error < 5.0) // perfect match
  289. return 1;
  290. WayPoint curr_last_pose = _path.at(_path.size()-1);
  291. double nMatch = (double)nIds/(double)nCount;
  292. double _d = hypot(path_last_pose.pos.y-curr_last_pose.pos.y, path_last_pose.pos.x-curr_last_pose.pos.x);
  293. double dCost = _d/FIXED_PLANNING_DISTANCE;
  294. if(dCost > 1.0)
  295. dCost = 1.0;
  296. double dMatch = 1.0 - dCost;
  297. double _a_diff = UtilityHNS::UtilityH::AngleBetweenTwoAnglesPositive(path_last_pose.pos.a, curr_last_pose.pos.a);
  298. double aCost = _a_diff/M_PI;
  299. if(aCost > 1.0)
  300. aCost = 1.0;
  301. double aMatch = 1.0 - aCost;
  302. double totalMatch = (nMatch + dMatch + aMatch)/3.0;
  303. return totalMatch;
  304. }
  305. void InsertNewParticle(const Particle& p)
  306. {
  307. if(p.beh == PlannerHNS::BEH_STOPPING_STATE && nAliveStop < BEH_PARTICLES_NUM)
  308. {
  309. m_StopPart.push_back(p);
  310. m_StopPart.at(m_StopPart.size()-1).pTraj = this;
  311. nAliveStop++;
  312. }
  313. else if(p.beh == PlannerHNS::BEH_YIELDING_STATE && nAliveYield < BEH_PARTICLES_NUM)
  314. {
  315. m_YieldPart.push_back(p);
  316. m_YieldPart.at(m_YieldPart.size()-1).pTraj = this;
  317. nAliveYield++;
  318. }
  319. else if(p.beh == PlannerHNS::BEH_FORWARD_STATE && nAliveForward < BEH_PARTICLES_NUM)
  320. {
  321. m_ForwardPart.push_back(p);
  322. m_ForwardPart.at(m_ForwardPart.size()-1).pTraj = this;
  323. nAliveForward++;
  324. }
  325. else if(p.beh == PlannerHNS::BEH_BRANCH_LEFT_STATE && nAliveLeft < BEH_PARTICLES_NUM)
  326. {
  327. m_LeftPart.push_back(p);
  328. m_LeftPart.at(m_LeftPart.size()-1).pTraj = this;
  329. nAliveLeft++;
  330. }
  331. else if(p.beh == PlannerHNS::BEH_BRANCH_RIGHT_STATE && nAliveRight < BEH_PARTICLES_NUM)
  332. {
  333. m_RightPart.push_back(p);
  334. m_RightPart.at(m_RightPart.size()-1).pTraj = this;
  335. nAliveRight++;
  336. }
  337. }
  338. void DeleteParticle(const Particle& p, const int& _i)
  339. {
  340. if(p.beh == PlannerHNS::BEH_STOPPING_STATE && nAliveStop > BEH_MIN_PARTICLE_NUM)
  341. {
  342. m_StopPart.erase(m_StopPart.begin()+_i);
  343. nAliveStop--;
  344. }
  345. else if(p.beh == PlannerHNS::BEH_YIELDING_STATE && nAliveYield > BEH_MIN_PARTICLE_NUM)
  346. {
  347. m_YieldPart.erase(m_YieldPart.begin()+_i);
  348. nAliveYield--;
  349. }
  350. else if(p.beh == PlannerHNS::BEH_FORWARD_STATE && nAliveForward > BEH_MIN_PARTICLE_NUM)
  351. {
  352. m_ForwardPart.erase(m_ForwardPart.begin()+_i);
  353. nAliveForward--;
  354. }
  355. else if(p.beh == PlannerHNS::BEH_BRANCH_LEFT_STATE && nAliveLeft > BEH_MIN_PARTICLE_NUM)
  356. {
  357. m_LeftPart.erase(m_LeftPart.begin()+_i);
  358. nAliveLeft--;
  359. }
  360. else if(p.beh == PlannerHNS::BEH_BRANCH_RIGHT_STATE && nAliveRight > BEH_MIN_PARTICLE_NUM)
  361. {
  362. m_RightPart.erase(m_RightPart.begin()+_i);
  363. nAliveRight--;
  364. }
  365. }
  366. void CalcAverages()
  367. {
  368. w_avg_forward = 0;
  369. double avg_sum = 0;
  370. for(unsigned int i = 0; i < m_ForwardPart.size(); i++)
  371. {
  372. avg_sum += m_ForwardPart.at(i).w;
  373. }
  374. if(m_ForwardPart.size() > 0)
  375. w_avg_forward = avg_sum/(double)m_ForwardPart.size();
  376. w_avg_left = 0;
  377. avg_sum = 0;
  378. for(unsigned int i = 0; i < m_LeftPart.size(); i++)
  379. {
  380. avg_sum += m_LeftPart.at(i).w;
  381. }
  382. if(m_LeftPart.size() > 0)
  383. w_avg_left = avg_sum/(double)m_LeftPart.size();
  384. w_avg_right = 0;
  385. avg_sum = 0;
  386. for(unsigned int i = 0; i < m_RightPart.size(); i++)
  387. {
  388. avg_sum += m_RightPart.at(i).w;
  389. }
  390. if(m_RightPart.size() > 0)
  391. w_avg_right = avg_sum/(double)m_RightPart.size();
  392. w_avg_stop = 0;
  393. avg_sum = 0;
  394. for(unsigned int i = 0; i < m_StopPart.size(); i++)
  395. {
  396. avg_sum += m_StopPart.at(i).w;
  397. }
  398. if(m_StopPart.size() > 0)
  399. w_avg_stop = avg_sum/(double)m_StopPart.size();
  400. w_avg_yield = 0;
  401. avg_sum = 0;
  402. for(unsigned int i = 0; i < m_YieldPart.size(); i++)
  403. {
  404. avg_sum += m_YieldPart.at(i).w;
  405. }
  406. if(m_YieldPart.size() > 0)
  407. w_avg_yield = avg_sum/(double)m_YieldPart.size();
  408. }
  409. void CalcProbabilities()
  410. {
  411. best_beh = PlannerHNS::BEH_STOPPING_STATE;
  412. pStop = (double)nAliveStop/(double)BEH_PARTICLES_NUM;
  413. best_p = pStop;
  414. pYield = (double)nAliveYield/(double)BEH_PARTICLES_NUM;
  415. if(pYield > best_p)
  416. {
  417. best_p = pYield;
  418. best_beh = PlannerHNS::BEH_YIELDING_STATE;
  419. }
  420. pForward = (double)nAliveForward/(double)BEH_PARTICLES_NUM;
  421. if(pForward > best_p)
  422. {
  423. best_p = pForward;
  424. best_beh = PlannerHNS::BEH_FORWARD_STATE;
  425. }
  426. pLeft = (double)nAliveLeft/(double)BEH_PARTICLES_NUM;
  427. if(pLeft > best_p)
  428. {
  429. best_p = pLeft;
  430. best_beh = PlannerHNS::BEH_BRANCH_LEFT_STATE;
  431. }
  432. pRight = (double)nAliveRight/(double)BEH_PARTICLES_NUM;
  433. if(pRight > best_p)
  434. {
  435. best_p = pRight;
  436. best_beh = PlannerHNS::BEH_BRANCH_RIGHT_STATE;
  437. }
  438. }
  439. };
  440. class ObjParticles
  441. {
  442. public:
  443. DetectedObject obj;
  444. std::vector<TrajectoryTracker*> m_TrajectoryTracker;
  445. std::vector<TrajectoryTracker*> m_TrajectoryTracker_temp;
  446. std::vector<Particle*> m_AllParticles;
  447. TrajectoryTracker* best_beh_track;
  448. int i_best_track;
  449. PlannerHNS::BehaviorState m_beh;
  450. double m_PredictionTime;
  451. double all_w;
  452. double max_w;
  453. double min_w;
  454. double max_w_raw;
  455. double min_w_raw;
  456. double avg_w;
  457. double pose_w_t;
  458. double dir_w_t;
  459. double vel_w_t;
  460. double acl_w_t;
  461. double ind_w_t;
  462. double pose_w_max;
  463. double dir_w_max;
  464. double vel_w_max;
  465. double acl_w_max;
  466. double ind_w_max;
  467. double pose_w_min;
  468. double dir_w_min;
  469. double vel_w_min;
  470. double acl_w_min;
  471. double ind_w_min;
  472. int n_stop;
  473. int n_yield;
  474. int n_left_branch;
  475. int n_right_branch;
  476. double p_stop;
  477. double p_yield;
  478. double p_left_branch;
  479. double p_right_branch;
  480. virtual ~ObjParticles()
  481. {
  482. DeleteTheRest(m_TrajectoryTracker);
  483. m_TrajectoryTracker_temp.clear();
  484. }
  485. ObjParticles()
  486. {
  487. m_PredictionTime = 0;
  488. best_beh_track = nullptr;
  489. i_best_track = -1;
  490. all_w = 0;
  491. pose_w_t = 0;
  492. dir_w_t = 0;
  493. vel_w_t = 0;
  494. acl_w_t = 0;
  495. ind_w_t = 0;
  496. max_w = DBL_MIN;
  497. min_w = DBL_MAX;
  498. max_w_raw = DBL_MIN;
  499. min_w_raw = DBL_MAX;
  500. avg_w = 0;
  501. pose_w_max = -999999;
  502. dir_w_max=-999999;
  503. vel_w_max=-999999;
  504. acl_w_max=-999999;
  505. ind_w_max=-999999;
  506. pose_w_min=999999;
  507. dir_w_min=999999;
  508. vel_w_min=999999;
  509. acl_w_min=999999;
  510. ind_w_min=999999;
  511. n_stop = 0;
  512. n_yield = 0;
  513. n_left_branch = 0;
  514. n_right_branch = 0;
  515. p_stop = 0;
  516. p_yield = 0;
  517. p_left_branch = 0;
  518. p_right_branch = 0;
  519. }
  520. // void CalculateProbabilities()
  521. // {
  522. // for(unsigned int i = 0; i < m_TrajectoryTracker.size(); i++)
  523. // {
  524. // m_TrajectoryTracker.at(i)->CalcProbabilities();
  525. // }
  526. //
  527. // if(m_TrajectoryTracker.size() > 0)
  528. // {
  529. // best_beh_track = m_TrajectoryTracker.at(0);
  530. // i_best_track = 0;
  531. // }
  532. //
  533. // for(unsigned int i = 1; i < m_TrajectoryTracker.size(); i++)
  534. // {
  535. // if(m_TrajectoryTracker.at(i)->best_p > best_beh_track->best_p)
  536. // {
  537. // best_beh_track = m_TrajectoryTracker.at(i);
  538. // i_best_track = i;
  539. // }
  540. // }
  541. // }
  542. class LLP
  543. {
  544. public:
  545. int new_index;
  546. double match_percent;
  547. TrajectoryTracker* pTrack;
  548. LLP()
  549. {
  550. new_index = -1;
  551. match_percent = -1;
  552. pTrack = 0;
  553. }
  554. };
  555. void DeleteFromList(std::vector<TrajectoryTracker*>& delete_me_track, const TrajectoryTracker* track)
  556. {
  557. for(unsigned int k = 0; k < delete_me_track.size(); k++)
  558. {
  559. if(delete_me_track.at(k) == track)
  560. {
  561. delete_me_track.erase(delete_me_track.begin()+k);
  562. return;
  563. }
  564. }
  565. }
  566. void DeleteTheRest(std::vector<TrajectoryTracker*>& delete_me_track)
  567. {
  568. for(unsigned int k = 0; k < delete_me_track.size(); k++)
  569. {
  570. delete delete_me_track.at(k);
  571. }
  572. delete_me_track.clear();
  573. }
  574. static bool IsBeggerPercentage(const LLP& p1, const LLP& p2)
  575. {
  576. return p1.match_percent > p2.match_percent;
  577. }
  578. void MatchWithMax(std::vector<LLP>& matching_list, std::vector<TrajectoryTracker*>& delete_me_track, std::vector<TrajectoryTracker*>& check_list)
  579. {
  580. if(matching_list.size() == 0 ) return;
  581. std::sort(matching_list.begin(), matching_list.end(), IsBeggerPercentage);
  582. while(matching_list.size()>0)
  583. {
  584. LLP f = matching_list.at(0);
  585. f.pTrack->UpdatePathAndIndex(obj.predTrajectories.at(f.new_index), f.new_index);
  586. bool bFound = false;
  587. for(unsigned int k=0; k < check_list.size(); k++)
  588. {
  589. if(check_list.at(k) == f.pTrack)
  590. {
  591. bFound = true;
  592. break;
  593. }
  594. }
  595. if(!bFound)
  596. check_list.push_back(f.pTrack);
  597. DeleteFromList(delete_me_track, f.pTrack);
  598. for(int i=0; i < matching_list.size(); i++)
  599. {
  600. if(matching_list.at(i).new_index == f.new_index || matching_list.at(i).pTrack == f.pTrack)
  601. {
  602. matching_list.erase(matching_list.begin()+i);
  603. i--;
  604. }
  605. }
  606. }
  607. }
  608. void MatchTrajectories()
  609. {
  610. m_TrajectoryTracker_temp.clear();
  611. std::vector<LLP> matching_list;
  612. std::vector<TrajectoryTracker*> delete_me_track = m_TrajectoryTracker;
  613. for(unsigned int t = 0; t < obj.predTrajectories.size();t++)
  614. {
  615. bool bMatched = false;
  616. LLP match_item;
  617. match_item.new_index = t;
  618. for(int i = 0; i < m_TrajectoryTracker.size(); i++)
  619. {
  620. TrajectoryTracker* pTracker = m_TrajectoryTracker.at(i);
  621. double vMatch = pTracker->CalcMatchingPercentage(obj.predTrajectories.at(t));
  622. if(vMatch == 1.0) // perfect match
  623. {
  624. pTracker->UpdatePathAndIndex(obj.predTrajectories.at(t), t);
  625. bool bFound = false;
  626. for(unsigned int k=0; k < m_TrajectoryTracker_temp.size(); k++)
  627. {
  628. if(m_TrajectoryTracker_temp.at(k) == pTracker)
  629. {
  630. bFound = true;
  631. break;
  632. }
  633. }
  634. if(!bFound)
  635. m_TrajectoryTracker_temp.push_back(pTracker);
  636. DeleteFromList(delete_me_track, pTracker);
  637. for(unsigned int k=0; k < matching_list.size(); k++)
  638. {
  639. if(matching_list.at(k).pTrack == pTracker)
  640. {
  641. matching_list.erase(matching_list.begin()+k);
  642. break;
  643. }
  644. }
  645. m_TrajectoryTracker.erase(m_TrajectoryTracker.begin()+i);
  646. bMatched = true;
  647. i--;
  648. break;
  649. }
  650. else if(vMatch > 0.5) // any matching less than 50%, the trajectory will be considered new
  651. {
  652. bMatched = true;
  653. match_item.match_percent = vMatch;
  654. match_item.pTrack = pTracker;
  655. matching_list.push_back(match_item);
  656. }
  657. }
  658. if(!bMatched)
  659. {
  660. m_TrajectoryTracker_temp.push_back(new TrajectoryTracker(obj.predTrajectories.at(t), t));
  661. }
  662. }
  663. MatchWithMax(matching_list,delete_me_track, m_TrajectoryTracker_temp);
  664. m_TrajectoryTracker.clear();
  665. DeleteTheRest(delete_me_track);
  666. m_TrajectoryTracker = m_TrajectoryTracker_temp;
  667. }
  668. };
  669. class BehaviorPrediction
  670. {
  671. public:
  672. BehaviorPrediction();
  673. virtual ~BehaviorPrediction();
  674. void DoOneStep(const std::vector<DetectedObject>& obj_list, const WayPoint& currPose, const double& minSpeed, const double& maxDeceleration, RoadNetwork& map);
  675. public:
  676. std::vector<PassiveDecisionMaker*> m_d_makers;
  677. double m_MaxLaneDetectionDistance;
  678. double m_PredictionDistance;
  679. bool m_bGenerateBranches;
  680. bool m_bUseFixedPrediction;
  681. bool m_bStepByStep;
  682. bool m_bParticleFilter;
  683. //std::vector<DetectedObject> m_PredictedObjects;
  684. //std::vector<DetectedObject*> m_PredictedObjectsII;
  685. std::vector<ObjParticles> m_temp_list;
  686. std::vector<ObjParticles> m_ParticleInfo;
  687. std::vector<ObjParticles*> m_temp_list_ii;
  688. std::vector<ObjParticles*> m_ParticleInfo_II;
  689. struct timespec m_GenerationTimer;
  690. timespec m_ResamplingTimer;
  691. bool m_bCanDecide;
  692. bool m_bFirstMove;
  693. bool m_bDebugOut;
  694. protected:
  695. //int GetTrajectoryPredictedDirection(const std::vector<WayPoint>& path, const PlannerHNS::WayPoint& pose, const double& pred_distance);
  696. int FromIndicatorToNumber(const PlannerHNS::LIGHT_INDICATOR& ind);
  697. PlannerHNS::LIGHT_INDICATOR FromNumbertoIndicator(const int& num);
  698. double CalcIndicatorWeight(PlannerHNS::LIGHT_INDICATOR p_ind, PlannerHNS::LIGHT_INDICATOR obj_ind);
  699. double CalcAccelerationWeight(int p_acl, int obj_acl);
  700. void CalPredictionTimeForObject(ObjParticles* pCarPart);
  701. void PredictCurrentTrajectory(RoadNetwork& map, ObjParticles* pCarPart);
  702. void FilterObservations(const std::vector<DetectedObject>& obj_list, RoadNetwork& map, std::vector<DetectedObject>& filtered_list);
  703. void ExtractTrajectoriesFromMap(const std::vector<DetectedObject>& obj_list, RoadNetwork& map, std::vector<ObjParticles*>& old_list);
  704. void CalculateCollisionTimes(const double& minSpeed);
  705. void ParticleFilterSteps(std::vector<ObjParticles*>& part_info);
  706. void SamplesFreshParticles(ObjParticles* pParts);
  707. void MoveParticles(ObjParticles* parts);
  708. void CalculateWeights(ObjParticles* pParts);
  709. void CalOnePartWeight(ObjParticles* pParts,Particle& p);
  710. void NormalizeOnePartWeight(ObjParticles* pParts,Particle& p);
  711. void CollectParticles(ObjParticles* pParts);
  712. void RemoveWeakParticles(ObjParticles* pParts);
  713. void FindBest(ObjParticles* pParts);
  714. void CalculateAveragesAndProbabilities(ObjParticles* pParts);
  715. static bool sort_weights(const Particle* p1, const Particle* p2)
  716. {
  717. return p1->w > p2->w;
  718. }
  719. static bool sort_trajectories(const std::pair<int, double>& p1, const std::pair<int, double>& p2)
  720. {
  721. return p1.second > p2.second;
  722. }
  723. public:
  724. //move to CPP later
  725. void DeleteFromList(std::vector<ObjParticles*>& delete_me, const ObjParticles* pElement)
  726. {
  727. for(unsigned int k = 0; k < delete_me.size(); k++)
  728. {
  729. if(delete_me.at(k) == pElement)
  730. {
  731. delete_me.erase(delete_me.begin()+k);
  732. return;
  733. }
  734. }
  735. }
  736. void DeleteTheRest(std::vector<ObjParticles*>& delete_me)
  737. {
  738. for(unsigned int k = 0; k < delete_me.size(); k++)
  739. {
  740. delete delete_me.at(k);
  741. }
  742. delete_me.clear();
  743. }
  744. };
  745. } /* namespace PlannerHNS */
  746. #endif /* BEHAVIORPREDICTION_H_ */