GreyClusterEvaluation.cpp 4.3 KB

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