monkey_fit.h.bak 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #ifndef __MONKEY_FIT__
  2. #define __MONKEY_FIT__
  3. #include <cmath>
  4. #include <iterator>
  5. #include <map>
  6. #include <deque>
  7. #include <algorithm>
  8. #include "base_data.h"
  9. //y=k*x+b
  10. //struct point_2
  11. //{
  12. // point_2(double x,double y)
  13. // :x_(x)
  14. // ,y_(y)
  15. // {}
  16. // double x_,y_;
  17. //};
  18. struct monkey_fit
  19. {
  20. virtual ~monkey_fit(){}
  21. template<typename T> static double get_probab_max(T it,T ite,int sum,int N=3)
  22. {
  23. sum/=5;
  24. sum=max(3,sum);
  25. std::vector<std::tuple<T,T,float>> p;
  26. if(N==0)
  27. {
  28. auto it0=it;
  29. for(int i=0;it0!=ite;++it0,++i)
  30. {
  31. if(i%10==0) printf("\n");
  32. printf("%5d:%2d,",it0->first,it0->second);
  33. }
  34. printf("\n");
  35. }
  36. int s=0;
  37. for(auto i1=it;it!=ite;)
  38. {
  39. for(;s<sum && i1!=ite;++i1)
  40. s+=i1->second;
  41. if(s<sum) break;
  42. p.push_back(std::make_tuple(it,i1,1.*s/(std::prev(i1)->first-it->first+1)));
  43. s-=it->second; ++it;
  44. }
  45. std::sort(p.begin(),p.end(),[](const std::tuple<T,T,int>&l,const std::tuple<T,T,int>&r){
  46. return 1.*std::get<2>(l) > 1.*std::get<2>(r);
  47. });
  48. int i=1;
  49. auto it1=std::get<0>(p[0]);
  50. auto it2=std::prev(std::get<1>(p[0]));
  51. for(;i<p.size();++i)
  52. {
  53. if(std::get<2>(p[i])!=std::get<2>(p[i-1]))
  54. break;
  55. if(std::get<0>(p[i])->first < it1->first)
  56. it1=std::get<0>(p[i]);
  57. if(std::prev(std::get<1>(p[i]))->second > it2->second)
  58. it2=std::prev(std::get<1>(p[i]));
  59. }
  60. ++it2;
  61. double rc=0;
  62. int cc=0;
  63. int x=0;
  64. for(;it1!=it2;++it1)
  65. {
  66. rc+=it1->first*it1->second;
  67. cc+=it1->second;
  68. if(N==0)
  69. {
  70. if(x++%10==0) printf("\n");
  71. printf("%5d:%2d,",it1->first,it1->second);
  72. }
  73. }
  74. if(N==0)
  75. printf("\n");
  76. return rc/cc;
  77. }
  78. int round(double value)
  79. {
  80. return (value > 0.0)?floor(value + 0.5):ceil(value - 0.5);
  81. }
  82. virtual void reset(double monkey_speed = 0){}
  83. virtual void push(double time_second,double dist_meter) {}
  84. };
  85. struct monkey_fit_b :monkey_fit
  86. {
  87. static const int min_points=60;
  88. static const int BN=20;
  89. double k_init_; //猴车本身的速度
  90. int count_; //该卡输入的点数量
  91. std::map<int,int> probab_b; //b值的分布
  92. monkey_fit_b()
  93. :k_init_(0)
  94. ,count_(0)
  95. {
  96. }
  97. monkey_fit_b(double monkey_speed) //米/秒的速度
  98. :k_init_(monkey_speed)
  99. ,count_(0)
  100. {
  101. }
  102. virtual void reset(double monkey_speed/*=0*/)
  103. {
  104. k_init_=monkey_speed;
  105. count_=0;
  106. probab_b.clear();
  107. }
  108. virtual void push(double time_second,double dist_meter)
  109. {
  110. double k=k_init_;
  111. ++count_;
  112. double b=(dist_meter-k*time_second)*BN;
  113. ++probab_b[round(b)];
  114. }
  115. double get_dist(double time_second)const
  116. {
  117. return k_init_*time_second + get_B();
  118. }
  119. double get_K()const
  120. {
  121. return k_init_;
  122. }
  123. double get_B()const
  124. {
  125. if(count_<min_points)
  126. return 0;
  127. if(probab_b.size()<3)
  128. return 0;
  129. return 1.*get_probab_max(probab_b.begin(),probab_b.end(),count_)/BN;
  130. }
  131. };
  132. struct monkey_fit_k:monkey_fit
  133. {
  134. static const int min_points=60;
  135. static const int KN=50;
  136. int step_;
  137. u32 count_;
  138. std::map<int,int> probab_k; //k值的分布
  139. std::deque<point_2> m_points;
  140. monkey_fit_k() //米/秒的速度
  141. :step_(0)
  142. ,count_(0)
  143. {
  144. }
  145. virtual void reset(double speed /*=0*/)
  146. {
  147. probab_k.clear();
  148. m_points.clear();
  149. step_=0;
  150. count_ = 0;
  151. }
  152. virtual void push(double time_second,double dist_meter)
  153. {
  154. int cc=m_points.size();
  155. if(cc<60)
  156. {
  157. m_points.push_back(point_2(time_second,dist_meter));
  158. }
  159. else if(step_++>2)
  160. {
  161. m_points.push_back(point_2(time_second,dist_meter));
  162. step_=0;
  163. }
  164. if(cc<10)
  165. return;
  166. cc*=9./10;
  167. for(int i=0;i<3;i++)
  168. {
  169. int index=rand()%cc;
  170. double k=(dist_meter-m_points[index].y_)/(time_second-m_points[index].x_);
  171. if((1<k && k<3) || (-3<k && k<-1))
  172. {
  173. count_++;
  174. ++probab_k[round(k*KN)];
  175. }
  176. }
  177. }
  178. double get_K()
  179. {
  180. if(m_points.size()<min_points)
  181. return 0;
  182. return round(get_probab_max(probab_k.begin(),probab_k.end(),count_)/KN*10000)/10000.;
  183. }
  184. };
  185. #endif