HierarchyWeighting.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // Created by lenovo on 2023/10/18.
  3. //
  4. #include "HierarchyWeighting.h"
  5. HierarchyWeighting::HierarchyWeighting(const SMat& smat)
  6. : smat_(smat)
  7. {
  8. sam_num_ = smat.count();
  9. }
  10. void HierarchyWeighting::evaluate()
  11. {
  12. for (auto& m : mat_)
  13. {
  14. weights_node_ << QVector<HWWeight>();
  15. EvaluateNodeWeight(m);
  16. }
  17. smat_cv_ << QVector<double>(sam_num_, 0);
  18. LastLevelWeight();
  19. BestSample();
  20. }
  21. void HierarchyWeighting::BestSample()
  22. {
  23. if (weights_.count() != smat_.at(0).count())
  24. return;
  25. for (int i = 0; i < sam_num_; ++i)
  26. {
  27. for (int j = 0; j < weights_.count(); ++j)
  28. {
  29. smat_cv_[i] = smat_cv_[i] + smat_.at(i).at(j) * weights_[j];
  30. }
  31. }
  32. //qDebug() << "smat_cv_" << smat_cv_;
  33. double best = 0;
  34. for (int i = 0; i < smat_cv_.count(); ++i)
  35. {
  36. if (best < smat_cv_.at(i))
  37. {
  38. best = smat_cv_.at(i);
  39. best_index_ = i + 1;
  40. }
  41. }
  42. //qDebug() << "best_index_" << best_index_;
  43. }
  44. void HierarchyWeighting::LastLevelWeight()
  45. {
  46. int level = 0;
  47. for (auto& m : weights_node_)
  48. {
  49. if (level < m.node_number.count())
  50. {
  51. level = m.node_number.count();
  52. }
  53. }
  54. //qDebug() << "level" << level;
  55. //计算每层累加权重
  56. for (int i = 1; i < level; ++i)
  57. {
  58. for (auto& m : weights_node_)
  59. {
  60. if (m.node_number.count() == i) //找到对应层
  61. {
  62. int child = 0;
  63. for (auto& n : weights_node_)
  64. {
  65. if ((n.node_number.count() == i + 1) && (n.node_number.at(i - 1) == m.node_number.at(i - 1)))
  66. {
  67. for (int j = 0; j < n.weights.count(); ++j)
  68. {
  69. n.weights[j] = n.weights[j] * m.weights[child];
  70. }
  71. child++;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. //得到最后一层权重
  78. for (auto& m : weights_node_)
  79. {
  80. if (m.node_number.count() == level)
  81. {
  82. weights_ << m.weights;
  83. }
  84. }
  85. //qDebug() << "weights_" << weights_;
  86. }
  87. void HierarchyWeighting::EvaluateNodeWeight(const HWNode& hwnode)
  88. {
  89. QVector<qreal> nodeMul(hwnode.mat.count(), 1);
  90. for (int m = 0; m < hwnode.mat.count(); ++m)
  91. {
  92. for (int n = 0; n < hwnode.mat.at(0).count(); ++n)
  93. {
  94. nodeMul[m] *= hwnode.mat.at(m).at(n);
  95. }
  96. }
  97. // qDebug() << "nodeMul" << nodeMul;
  98. qreal nSum = 0;
  99. QVector<qreal> m_sqartnValues(hwnode.mat.count(), 1);
  100. for (int n = 0; n < hwnode.mat.count(); ++n)
  101. {
  102. m_sqartnValues[n] = qPow(nodeMul[n], (qreal)1 / hwnode.mat.count());
  103. nSum += m_sqartnValues[n];
  104. }
  105. // qDebug() << "sqartnValues" << m_sqartnValues;
  106. QVector<qreal> m_weights(hwnode.mat.count(), 0);
  107. for (int w = 0; w < hwnode.mat.count(); ++w)
  108. {
  109. m_weights[w] = m_sqartnValues[w] / nSum;
  110. }
  111. //qDebug() << "node weights" << m_weights;
  112. weights_node_ << HWWeight{hwnode.node, hwnode.name, m_weights, hwnode.node_number};
  113. //qDebug() << "weights_node_" << weights_node_.count();
  114. }
  115. void HierarchyWeighting::push(const QString& node, const QString& name, const HWMat& value)
  116. {
  117. if (node.isEmpty()) { return; }
  118. bool found = false;
  119. for (auto& m : mat_)
  120. {
  121. if (m.node == node)
  122. {
  123. found = true;
  124. m.name = name;
  125. m.mat = value;
  126. break;
  127. }
  128. }
  129. if (!found)
  130. {
  131. auto s = node.split(".");
  132. QVector<int> val;
  133. for (auto& v : s)
  134. {
  135. val << v.toInt();
  136. }
  137. mat_ << HWNode{node, name, value, val};
  138. }
  139. }
  140. bool HierarchyWeighting::isFull() const //少输入或者乱序都会返回flase
  141. {
  142. QVector<QVector<QVector<int> > > allNodes; //归列层
  143. for (const auto& m : mat_)
  144. {
  145. while (m.node_number.size() > allNodes.size())
  146. {
  147. allNodes << QVector<QVector<int> >();
  148. }
  149. allNodes[m.node_number.size() - 1] << m.node_number;
  150. // qDebug() << "allNodes number" << allNodes[m.node_number.size() - 1];
  151. }
  152. //qDebug() << "allNodes number" << allNodes;
  153. //判断输入顺序错误或重复
  154. for (int i = 0; i < allNodes.count(); ++i)
  155. {
  156. int child = allNodes.at(i).count() - 1;
  157. while (child)
  158. {
  159. if (allNodes.at(i).at(child).at(i - 1) == allNodes.at(i).at(child - 1).at(i - 1))
  160. {
  161. int sal = allNodes.at(i).at(child).at(i) - allNodes.at(i).at(child - 1).at(i);
  162. // qDebug() << allNodes.at(i).at(child).at(i);
  163. //qDebug() << allNodes.at(i).at(child - 1).at(i);
  164. if (sal <= 0)
  165. {
  166. //qDebug() << "sal" << sal;
  167. return false;
  168. }
  169. }
  170. child--;
  171. }
  172. }
  173. //判断缺少
  174. for (int i = 0; i < allNodes.count(); ++i)
  175. {
  176. for (auto& m : mat_)
  177. {
  178. if (m.node_number.count() == i) //找到对应层
  179. {
  180. int child = 0;
  181. for (auto& n : mat_)
  182. {
  183. if ((n.node_number.count() == i + 1) && (n.node_number.at(i - 1) == m.node_number.at(i - 1)))
  184. {
  185. child++;
  186. }
  187. }
  188. qDebug() << "child" << child;
  189. if (m.mat.count() != child)
  190. {
  191. return false;
  192. }
  193. }
  194. }
  195. }
  196. return true;
  197. }