// // Created by lenovo on 2023/10/18. // #include "HierarchyWeighting.h" HierarchyWeighting::HierarchyWeighting(const SMat& smat) : smat_(smat) { sam_num_ = smat.count(); } void HierarchyWeighting::evaluate() { for (auto& m : mat_) { weights_node_ << QVector(); EvaluateNodeWeight(m); } smat_cv_ << QVector(sam_num_, 0); LastLevelWeight(); BestSample(); } void HierarchyWeighting::BestSample() { if (weights_.count() != smat_.at(0).count()) return; for (int i = 0; i < sam_num_; ++i) { for (int j = 0; j < weights_.count(); ++j) { smat_cv_[i] = smat_cv_[i] + smat_.at(i).at(j) * weights_[j]; } } //qDebug() << "smat_cv_" << smat_cv_; double best = 0; for (int i = 0; i < smat_cv_.count(); ++i) { if (best < smat_cv_.at(i)) { best = smat_cv_.at(i); best_index_ = i + 1; } } //qDebug() << "best_index_" << best_index_; } void HierarchyWeighting::LastLevelWeight() { int level = 0; for (auto& m : weights_node_) { if (level < m.node_number.count()) { level = m.node_number.count(); } } //qDebug() << "level" << level; //计算每层累加权重 for (int i = 1; i < level; ++i) { for (auto& m : weights_node_) { if (m.node_number.count() == i) //找到对应层 { int child = 0; for (auto& n : weights_node_) { if ((n.node_number.count() == i + 1) && (n.node_number.at(i - 1) == m.node_number.at(i - 1))) { for (int j = 0; j < n.weights.count(); ++j) { n.weights[j] = n.weights[j] * m.weights[child]; } child++; } } } } } //得到最后一层权重 for (auto& m : weights_node_) { if (m.node_number.count() == level) { weights_ << m.weights; } } //qDebug() << "weights_" << weights_; } void HierarchyWeighting::EvaluateNodeWeight(const HWNode& hwnode) { QVector nodeMul(hwnode.mat.count(), 1); for (int m = 0; m < hwnode.mat.count(); ++m) { for (int n = 0; n < hwnode.mat.at(0).count(); ++n) { nodeMul[m] *= hwnode.mat.at(m).at(n); } } // qDebug() << "nodeMul" << nodeMul; qreal nSum = 0; QVector m_sqartnValues(hwnode.mat.count(), 1); for (int n = 0; n < hwnode.mat.count(); ++n) { m_sqartnValues[n] = qPow(nodeMul[n], (qreal)1 / hwnode.mat.count()); nSum += m_sqartnValues[n]; } // qDebug() << "sqartnValues" << m_sqartnValues; QVector m_weights(hwnode.mat.count(), 0); for (int w = 0; w < hwnode.mat.count(); ++w) { m_weights[w] = m_sqartnValues[w] / nSum; } //qDebug() << "node weights" << m_weights; weights_node_ << HWWeight{hwnode.node, hwnode.name, m_weights, hwnode.node_number}; //qDebug() << "weights_node_" << weights_node_.count(); } void HierarchyWeighting::push(const QString& node, const QString& name, const HWMat& value) { if (node.isEmpty()) { return; } bool found = false; for (auto& m : mat_) { if (m.node == node) { found = true; m.name = name; m.mat = value; break; } } if (!found) { auto s = node.split("."); QVector val; for (auto& v : s) { val << v.toInt(); } mat_ << HWNode{node, name, value, val}; } } bool HierarchyWeighting::isFull() const //少输入或者乱序都会返回flase { QVector > > allNodes; //归列层 for (const auto& m : mat_) { while (m.node_number.size() > allNodes.size()) { allNodes << QVector >(); } allNodes[m.node_number.size() - 1] << m.node_number; // qDebug() << "allNodes number" << allNodes[m.node_number.size() - 1]; } //qDebug() << "allNodes number" << allNodes; //判断输入顺序错误或重复 for (int i = 0; i < allNodes.count(); ++i) { int child = allNodes.at(i).count() - 1; while (child) { if (allNodes.at(i).at(child).at(i - 1) == allNodes.at(i).at(child - 1).at(i - 1)) { int sal = allNodes.at(i).at(child).at(i) - allNodes.at(i).at(child - 1).at(i); // qDebug() << allNodes.at(i).at(child).at(i); //qDebug() << allNodes.at(i).at(child - 1).at(i); if (sal <= 0) { //qDebug() << "sal" << sal; return false; } } child--; } } //判断缺少 for (int i = 0; i < allNodes.count(); ++i) { for (auto& m : mat_) { if (m.node_number.count() == i) //找到对应层 { int child = 0; for (auto& n : mat_) { if ((n.node_number.count() == i + 1) && (n.node_number.at(i - 1) == m.node_number.at(i - 1))) { child++; } } qDebug() << "child" << child; if (m.mat.count() != child) { return false; } } } } return true; }