GreyClusterEvaluation.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. cvtMat.append(QVector<GCEmat>(level_num_, { 0.0, 0.0 }));
  17. ranges_cv_.append(cvtMat);
  18. }
  19. QVector<GCEmat> cvrang_weights(level_num_, { 0.0, 0.0 });
  20. ranges_weight_.append(cvrang_weights);
  21. }
  22. void GreyClusterEvaluation::evaluate(const QVector<double> &weights)
  23. {
  24. for (int s = 0; s < sample_num_; ++s) {
  25. double val_front = mat_.at(s).front_val;
  26. double val_back = mat_.at(s).back_val;
  27. triangularwhiteningweight(s, val_front, val_back);
  28. }
  29. // QVector<double> weights = {0.2, 0.15, 0.05, 0.4, 0.2};
  30. evaluationlevel(weights);
  31. }
  32. void GreyClusterEvaluation::triangularwhiteningweight(int sam, double front_val, double back_val)
  33. {
  34. for (int i = 0; i < level_num_; ++i) {
  35. Q_ASSERT(ranges_.at(sam).at(i).level == i);
  36. // 计算前
  37. double a1, a2, a3, a4;
  38. a1 = ranges_.at(sam).at(i).left_left;
  39. a2 = ranges_.at(sam).at(i).left_val;
  40. a3 = ranges_.at(sam).at(i).right_val;
  41. a4 = ranges_.at(sam).at(i).right_right;
  42. double lambda_k = (a2 + a3) / 2;
  43. if (front_val > a4 || front_val < a1) {
  44. ranges_cv_[sam][i].front_val = 0;
  45. } else if ((front_val >= a1) && (front_val < lambda_k)) {
  46. ranges_cv_[sam][i].front_val = (front_val - a1) / (lambda_k - a1);
  47. } else if ((front_val >= lambda_k) && (front_val <= a4)) {
  48. ranges_cv_[sam][i].front_val = (a4 - front_val) / (a4 - lambda_k);
  49. }
  50. if (back_val > a4 || back_val < a1) {
  51. ranges_cv_[sam][i].back_val = 0;
  52. } else if ((back_val >= a1) && (back_val < lambda_k)) {
  53. ranges_cv_[sam][i].back_val = (back_val - a1) / (lambda_k - a1);
  54. } else if ((back_val >= lambda_k) && (back_val <= a4)) {
  55. ranges_cv_[sam][i].back_val = (a4 - back_val) / (a4 - lambda_k);
  56. }
  57. }
  58. }
  59. void GreyClusterEvaluation::evaluationlevel(const QVector<double> &weights)
  60. {
  61. for (int i = 0; i < level_num_; ++i) {
  62. double front_val = 0;
  63. double back_val = 0;
  64. for (int j = 0; j < sample_num_; ++j) {
  65. front_val = front_val + ranges_cv_.at(j).at(i).front_val * weights[j];
  66. back_val = back_val + ranges_cv_.at(j).at(i).back_val * weights[j];
  67. }
  68. ranges_weight_[i].front_val = front_val;
  69. ranges_weight_[i].back_val = back_val;
  70. }
  71. }
  72. BestIndex GreyClusterEvaluation::getBestIndex() const
  73. {
  74. BestIndex index = { 0, 0 };
  75. double maxf_val = ranges_weight_.at(0).front_val;
  76. double maxb_val = ranges_weight_.at(0).back_val;
  77. for (int i = 1; i < ranges_weight_.count(); ++i) {
  78. if (ranges_weight_.at(i).front_val > maxf_val) {
  79. maxf_val = ranges_weight_.at(i).front_val;
  80. index.front_index = i;
  81. }
  82. if (ranges_weight_.at(i).back_val > maxb_val) {
  83. maxb_val = ranges_weight_.at(i).back_val;
  84. index.back_index = i;
  85. }
  86. }
  87. index.front_index = index.front_index + 1;
  88. index.back_index = index.back_index + 1;
  89. // for (auto &item : ranges_weight_) {
  90. // qDebug() << QString("前 %1 : 后 %2").arg(item.front_val).arg(item.back_val);
  91. // }
  92. for (int i = 0; i < ranges_cv_.size(); i++) {
  93. qDebug() << "指标" << i << " 深度" << ranges_cv_.at(i).count();
  94. int n = 0;
  95. for (auto &item : ranges_cv_.at(i)) {
  96. qDebug() << QString(" 前 %1 : 后 %2").arg(item.front_val).arg(item.back_val);
  97. n++;
  98. if (n >= level_num_)
  99. break;
  100. }
  101. }
  102. return index;
  103. }
  104. const QVector<GCEmat> &GreyClusterEvaluation::getRangeWeights() const
  105. {
  106. return ranges_weight_;
  107. }
  108. const QVector<GCEMat> &GreyClusterEvaluation::getRangeCVT() const
  109. {
  110. return ranges_cv_;
  111. }