123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- //
- // 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<HWWeight>();
- EvaluateNodeWeight(m);
- }
- smat_cv_ << QVector<double>(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<qreal> 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<qreal> 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<qreal> 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<int> val;
- for (auto& v : s)
- {
- val << v.toInt();
- }
- mat_ << HWNode{node, name, value, val};
- }
- }
- bool HierarchyWeighting::isFull() const //少输入或者乱序都会返回flase
- {
- QVector<QVector<QVector<int> > > allNodes; //归列层
- for (const auto& m : mat_)
- {
- while (m.node_number.size() > allNodes.size())
- {
- allNodes << QVector<QVector<int> >();
- }
- 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;
- }
|