fitk.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef __MONKEY_FIT__
  2. #define __MONKEY_FIT__
  3. #include <math.h>
  4. #include <iterator>
  5. #include <map>
  6. #include <deque>
  7. #include <algorithm>
  8. #include "point.h"
  9. struct fit_k
  10. {
  11. static const int KN=200;
  12. std::map<int,int> probab_k; //k值的分布
  13. uint64_t point_count=0;
  14. fit_k()
  15. {
  16. }
  17. void remove(const std::string&card_id)
  18. {
  19. points_.erase(card_id);
  20. }
  21. void push(const point&pt)
  22. {
  23. c.push(time_second,dist_meter);
  24. double k;
  25. for(int i=0,kc=c.k_count();i<kc;i++)
  26. {
  27. if(k=c.random_k())
  28. {
  29. ++point_count;
  30. ++probab_k[round(k*KN)];
  31. }
  32. }
  33. }
  34. double get_K()const
  35. {
  36. return get_probab_max(probab_k.begin(),probab_k.end(),point_count)/KN;
  37. }
  38. template<typename T>
  39. static double get_probab_max(T it,T ite,int sum,int N=3)
  40. {
  41. sum/=5;
  42. sum=max(3,sum);
  43. std::vector<std::tuple<T,T,float>> p;
  44. if(N==0)
  45. {
  46. auto it0=it;
  47. for(int i=0;it0!=ite;++it0,++i)
  48. {
  49. if(i%10==0) printf("\n");
  50. printf("%5d:%2d,",it0->first,it0->second);
  51. }
  52. printf("\n");
  53. }
  54. int s=0;
  55. for(auto i1=it;it!=ite;)
  56. {
  57. for(;s<sum && i1!=ite;++i1)
  58. s+=i1->second;
  59. if(s<sum) break;
  60. p.push_back(std::make_tuple(it,i1,1.*s/(std::prev(i1)->first-it->first+1)));
  61. s-=it->second; ++it;
  62. }
  63. std::sort(p.begin(),p.end(),[](const std::tuple<T,T,int>&l,const std::tuple<T,T,int>&r){
  64. return 1.*std::get<2>(l) > 1.*std::get<2>(r);
  65. });
  66. int i=1;
  67. auto it1=std::get<0>(p[0]);
  68. auto it2=std::prev(std::get<1>(p[0]));
  69. for(;i<p.size();++i)
  70. {
  71. if(std::get<2>(p[i])!=std::get<2>(p[i-1]))
  72. break;
  73. if(std::get<0>(p[i])->first < it1->first)
  74. it1=std::get<0>(p[i]);
  75. if(std::prev(std::get<1>(p[i]))->second > it2->second)
  76. it2=std::prev(std::get<1>(p[i]));
  77. }
  78. ++it2;
  79. double rc=0;
  80. int cc=0;
  81. int x=0;
  82. for(;it1!=it2;++it1)
  83. {
  84. rc+=it1->first*it1->second;
  85. cc+=it1->second;
  86. if(N==0)
  87. {
  88. if(x++%10==0) printf("\n");
  89. printf("%5d:%2d,",it1->first,it1->second);
  90. }
  91. }
  92. if(N==0)
  93. printf("\n");
  94. return rc/cc;
  95. }
  96. };
  97. #endif