GreyClusterEvaluation.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Created by lenovo on 2023/10/16.
  3. //
  4. #include "GreyClusterEvaluation.h"
  5. GreyClusterEvaluation::GreyClusterEvaluation(const GCEMat& mat, const GCERangeMat& ranges)
  6. : mat_(mat),
  7. ranges_(ranges)
  8. {
  9. sample_num_ = mat_.count();
  10. if (sample_num_ == 0)
  11. {
  12. level_num_ = 0;
  13. }
  14. else
  15. {
  16. level_num_ = ranges_.at(0).count();
  17. }
  18. for (int i = 0; i < sample_num_; ++i)
  19. {
  20. GCEMat cvtMat;
  21. for (int j = 0; j < level_num_; ++j)
  22. {
  23. cvtMat.append(QVector<GCEmat>(level_num_, {0.0, 0.0}));
  24. }
  25. ranges_cv_.append(cvtMat);
  26. }
  27. QVector<GCEmat> cvrang_weights(level_num_, {0.0, 0.0});
  28. ranges_weight_.append(cvrang_weights);
  29. }
  30. void GreyClusterEvaluation::evaluate()
  31. {
  32. for (int s = 0; s < sample_num_; ++s)
  33. {
  34. double val_front = mat_.at(s).front_val;
  35. double val_back = mat_.at(s).back_val;
  36. triangularwhiteningweight(s, val_front, val_back);
  37. }
  38. QVector<double> weights = {0.2, 0.15, 0.05, 0.4, 0.2};
  39. evaluationlevel(weights);
  40. }
  41. void GreyClusterEvaluation::triangularwhiteningweight(int sam, double front_val, double back_val)
  42. {
  43. auto pFunc1 = [](double left, double right) { return (left + right) / 2; };
  44. auto pFunc2 = [](double v, double left_left, double left_right) {
  45. return (v - left_left) / (left_right - left_left);
  46. };
  47. auto pFunc3 = [](double v, double left_right, double right_right) {
  48. return (right_right - v) / (right_right - left_right);
  49. };
  50. for (int i = 0; i < level_num_; ++i)
  51. {
  52. Q_ASSERT(ranges_.at(sam).at(i).level == i);
  53. double r1 = pFunc1(ranges_.at(sam).at(i).left_val, ranges_.at(sam).at(i).right_val);
  54. double r2 = pFunc2(front_val, ranges_.at(sam).at(i).left_left, r1);
  55. double r3 = pFunc3(front_val, r1, ranges_.at(sam).at(i).right_right);
  56. if ((front_val >= ranges_.at(sam).at(i).left_left) && (front_val <= r1))
  57. ranges_cv_[sam][i].front_val = r2;
  58. else if ((front_val >= r1) && (front_val <= ranges_.at(sam).at(i).right_right))
  59. ranges_cv_[sam][i].front_val = r3;
  60. else
  61. ranges_cv_[sam][i].front_val = 0;
  62. r2 = pFunc2(back_val, ranges_.at(sam).at(i).left_left, r1);
  63. r3 = pFunc3(back_val, r1, ranges_.at(sam).at(i).right_right);
  64. if ((back_val >= ranges_.at(sam).at(i).left_left) && (back_val <= r1))
  65. ranges_cv_[sam][i].back_val = r2;
  66. else if ((back_val >= r1) && (back_val <= ranges_.at(sam).at(i).right_right))
  67. ranges_cv_[sam][i].back_val = r3;
  68. else
  69. ranges_cv_[sam][i].back_val = 0;
  70. }
  71. }
  72. void GreyClusterEvaluation::evaluationlevel(const QVector<double>& weights)
  73. {
  74. for (int i = 0; i < level_num_; ++i)
  75. {
  76. double front_val = 0;
  77. double back_val = 0;
  78. for (int j = 0; j < sample_num_; ++j)
  79. {
  80. front_val = front_val + ranges_cv_.at(j).at(i).front_val * weights[j];
  81. back_val = back_val + ranges_cv_.at(j).at(i).back_val * weights[j];
  82. }
  83. ranges_weight_[i].front_val = front_val;
  84. ranges_weight_[i].back_val = back_val;
  85. }
  86. }
  87. BestIndex GreyClusterEvaluation::getBestIndex() const
  88. {
  89. BestIndex index = {0, 0};
  90. double maxf_val = ranges_weight_.at(0).front_val;
  91. double maxb_val = ranges_weight_.at(0).back_val;
  92. for (int i = 1; i < ranges_weight_.count(); ++i)
  93. {
  94. if (ranges_weight_.at(i).front_val > maxf_val)
  95. {
  96. maxf_val = ranges_weight_.at(i).front_val;
  97. index.front_index = i;
  98. }
  99. if (ranges_weight_.at(i).back_val > maxb_val)
  100. {
  101. maxb_val = ranges_weight_.at(i).back_val;
  102. index.back_index = i;
  103. }
  104. }
  105. index.front_index = index.front_index + 1;
  106. index.back_index = index.back_index + 1;
  107. return index;
  108. }