GreyClusterEvaluation.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (left_left == left_right) {
  39. return 0.0;
  40. }
  41. return (v - left_left) / (left_right - left_left);
  42. };
  43. auto pFunc3 = [](double v, double left_right, double right_right) {
  44. if (right_right == left_right) {
  45. return 0.0;
  46. }
  47. return (right_right - v) / (right_right - left_right);
  48. };
  49. for (int i = 0; i < level_num_; ++i) {
  50. Q_ASSERT(ranges_.at(sam).at(i).level == i);
  51. double r1 = pFunc1(ranges_.at(sam).at(i).left_val, ranges_.at(sam).at(i).right_val);
  52. double r2 = pFunc2(front_val, ranges_.at(sam).at(i).left_left, r1);
  53. double r3 = pFunc3(front_val, r1, ranges_.at(sam).at(i).right_right);
  54. if ((front_val >= ranges_.at(sam).at(i).left_left) && (front_val <= r1)) {
  55. ranges_cv_[sam][i].front_val = r2;
  56. } else if ((front_val >= r1) && (front_val <= ranges_.at(sam).at(i).right_right)) {
  57. ranges_cv_[sam][i].front_val = r3;
  58. } else {
  59. ranges_cv_[sam][i].front_val = 0;
  60. }
  61. r2 = pFunc2(back_val, ranges_.at(sam).at(i).left_left, r1);
  62. r3 = pFunc3(back_val, r1, ranges_.at(sam).at(i).right_right);
  63. if ((back_val >= ranges_.at(sam).at(i).left_left) && (back_val <= r1)) {
  64. ranges_cv_[sam][i].back_val = r2;
  65. } else if ((back_val >= r1) && (back_val <= ranges_.at(sam).at(i).right_right)) {
  66. ranges_cv_[sam][i].back_val = r3;
  67. } else {
  68. ranges_cv_[sam][i].back_val = 0;
  69. }
  70. }
  71. }
  72. void GreyClusterEvaluation::evaluationlevel(const QVector<double> &weights)
  73. {
  74. for (int i = 0; i < level_num_; ++i) {
  75. double front_val = 0;
  76. double back_val = 0;
  77. for (int j = 0; j < sample_num_; ++j) {
  78. front_val = front_val + ranges_cv_.at(j).at(i).front_val * weights[j];
  79. back_val = back_val + ranges_cv_.at(j).at(i).back_val * weights[j];
  80. }
  81. ranges_weight_[i].front_val = front_val;
  82. ranges_weight_[i].back_val = back_val;
  83. }
  84. }
  85. BestIndex GreyClusterEvaluation::getBestIndex() const
  86. {
  87. BestIndex index = { 0, 0 };
  88. double maxf_val = ranges_weight_.at(0).front_val;
  89. double maxb_val = ranges_weight_.at(0).back_val;
  90. for (int i = 1; i < ranges_weight_.count(); ++i) {
  91. if (ranges_weight_.at(i).front_val > maxf_val) {
  92. maxf_val = ranges_weight_.at(i).front_val;
  93. index.front_index = i;
  94. }
  95. if (ranges_weight_.at(i).back_val > maxb_val) {
  96. maxb_val = ranges_weight_.at(i).back_val;
  97. index.back_index = i;
  98. }
  99. }
  100. index.front_index = index.front_index + 1;
  101. index.back_index = index.back_index + 1;
  102. // for (auto &item : ranges_weight_) {
  103. // qDebug() << QString("前 %1 : 后 %2").arg(item.front_val).arg(item.back_val);
  104. // }
  105. for (int i = 0; i < ranges_cv_.size(); i++) {
  106. qDebug() << "指标" << i;
  107. int n = 0;
  108. for (auto &item : ranges_cv_.at(i)) {
  109. qDebug() << QString(" 前 %1 : 后 %2").arg(item.front_val).arg(item.back_val);
  110. n++;
  111. if (n >= 4)
  112. break;
  113. }
  114. }
  115. return index;
  116. }
  117. const QVector<GCEmat> &GreyClusterEvaluation::getRangeWeights() const
  118. {
  119. return ranges_weight_;
  120. }
  121. const QVector<GCEMat> &GreyClusterEvaluation::getRangeCVT() const
  122. {
  123. return ranges_cv_;
  124. }