GreyClusterEvaluation.cpp 3.8 KB

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