MindEvaluation.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #include "MindEvaluation.h"
  2. #include "CMind.h"
  3. #include "dbService/NodeMatrixService.h"
  4. #include "dbService/UserConfigService.h"
  5. #include "algorithm/HierarchicalAnalysis.h"
  6. #include "algorithm/EntropyWeights.h"
  7. #include "algorithm/PCA.h"
  8. #include <QMessageBox>
  9. #include <QDebug>
  10. MindEvaluation::MindEvaluation(CMind *mind, int expertId, const QString &tableType, int mindMatrix, QObject *parent)
  11. : QObject(parent),
  12. m_mind(mind),
  13. m_expertId(expertId),
  14. m_tableType(tableType),
  15. m_mindMatrix(mindMatrix),
  16. m_isAggregationOp(false)
  17. {
  18. init();
  19. }
  20. MindEvaluation::MindEvaluation(CMind *mind, MindEvaluation::AggregationType atype, const QString &tableType,
  21. int mindMatrix, QObject *parent)
  22. : QObject(parent),
  23. m_mind(mind),
  24. m_tableType(tableType),
  25. m_mindMatrix(mindMatrix),
  26. m_aggregationType(atype),
  27. m_isAggregationOp(true)
  28. {
  29. }
  30. MindEvaluation::~MindEvaluation() { }
  31. void MindEvaluation::updateSeqNodes()
  32. {
  33. m_mind->getSeqNodes(m_seqNodes);
  34. }
  35. static QList<QPair<QString, QString>> getMatrixString(const QStringList &in)
  36. {
  37. QList<QPair<QString, QString>> res;
  38. const int n = in.size();
  39. for (int i = 0; i < n; ++i) {
  40. for (int j = 0; j < n; ++j) {
  41. res << QPair<QString, QString>(in.at(i), in.at(j));
  42. }
  43. }
  44. return res;
  45. }
  46. void MindEvaluation::computeWeights()
  47. {
  48. if (!m_isAggregationOp) {
  49. computeSingleWeights();
  50. } else {
  51. if (m_aggregationType == NoMerge) {
  52. computeNoMergeWeights();
  53. } else {
  54. computeMergeWeights();
  55. }
  56. }
  57. }
  58. void MindEvaluation::init()
  59. {
  60. updateSeqNodes();
  61. }
  62. void MindEvaluation::computeSingleWeights()
  63. {
  64. QList<NodeMatrixInfo *> nmInfos;
  65. if (!NodeMatrixService().QueryNodeMatrixListByExpertIdAndEngineerId(&nmInfos, m_expertId, m_mind->root().projectId,
  66. m_tableType)) {
  67. QMessageBox::warning(nullptr, "警告", "数据库访问失败");
  68. return;
  69. }
  70. for (int sn = 0; sn < m_seqNodes.count(); sn++) {
  71. qDebug() << "第" << sn << "层";
  72. for (auto s : m_seqNodes.at(sn)) {
  73. qDebug() << s.name << s.childs;
  74. if (m_mindMatrix == 0) { // 层次分析法
  75. auto matrixStr = getMatrixString(s.childs);
  76. QVector<qreal> nxnValus(matrixStr.size(), 0.0001); // n x n矩阵
  77. for (const auto &nmInfo : nmInfos) {
  78. if (nmInfo->mindId == m_mindMatrix) {
  79. int loc = matrixStr.indexOf(QPair<QString, QString>(nmInfo->abscissa, nmInfo->ordinate));
  80. if (loc >= 0) {
  81. QStringList nodeValue = nmInfo->nodeValue.split("/");
  82. if (nodeValue.size() == 1) {
  83. nxnValus[loc] = nodeValue[0].toDouble();
  84. } else {
  85. nxnValus[loc] = nodeValue[0].toDouble() / nodeValue[1].toDouble();
  86. }
  87. }
  88. }
  89. }
  90. QScopedPointer<HierarchicalAnalysis> ha(new HierarchicalAnalysis(s.childs.size(), nxnValus));
  91. QVector<qreal> weights = ha->getWeights();
  92. for (int c = 0; c < s.childs.size(); ++c) {
  93. mindNodeWeights << MindNodeItem { s.name, s.childs.at(c), weights.at(c), 0 };
  94. }
  95. } else {
  96. // 熵权法
  97. if (1) {
  98. QMap<QString /*指标*/, QMap<QString /*uuid*/, double>> seqMap;
  99. for (const auto &nmInfo : nmInfos) {
  100. if (nmInfo->mindId == m_mindMatrix) {
  101. int loc = s.childs.indexOf(nmInfo->abscissa); // 指标索引
  102. if (loc >= 0) {
  103. QStringList nodeValue = nmInfo->nodeValue.split("/");
  104. double v;
  105. if (nodeValue.size() == 1) {
  106. v = nodeValue[0].toDouble();
  107. } else {
  108. v = nodeValue[0].toDouble() / nodeValue[1].toDouble();
  109. }
  110. if (!seqMap.contains(nmInfo->abscissa)) {
  111. seqMap.insert(nmInfo->abscissa, QMap<QString /*uuid*/, double>());
  112. }
  113. seqMap[nmInfo->abscissa].insert(nmInfo->strUuid, v);
  114. }
  115. }
  116. }
  117. EntropyMat eMat; // 一个指标一行数据
  118. for (const auto &c : s.childs) {
  119. if (seqMap.contains(c)) {
  120. QVector<double> indexVecs;
  121. QMapIterator<QString, double> iter(seqMap[c]);
  122. while (iter.hasNext()) {
  123. iter.next();
  124. indexVecs << iter.value();
  125. }
  126. eMat << indexVecs;
  127. } else {
  128. QMessageBox::warning(nullptr, "警告", QString("指标%1数据缺失").arg(c));
  129. }
  130. }
  131. QScopedPointer<EntropyWeights> ew(new EntropyWeights(eMat));
  132. QVector<qreal> weights, score;
  133. ew->compute(weights, score);
  134. for (int c = 0; c < s.childs.size(); ++c) {
  135. mindNodeWeights << MindNodeItem { s.name, s.childs.at(c), weights.at(c), score.at(c) };
  136. }
  137. } else { // pca
  138. QMap<QString /*uuid*/, QMap<QString /*指标*/, double>> seqMap;
  139. for (const auto &nmInfo : nmInfos) {
  140. if (nmInfo->mindId == m_mindMatrix) {
  141. int loc = s.childs.indexOf(nmInfo->abscissa); // 指标索引
  142. if (loc >= 0) {
  143. QStringList nodeValue = nmInfo->nodeValue.split("/");
  144. double v;
  145. if (nodeValue.size() == 1) {
  146. v = nodeValue[0].toDouble();
  147. } else {
  148. v = nodeValue[0].toDouble() / nodeValue[1].toDouble();
  149. }
  150. if (!seqMap.contains(nmInfo->strUuid)) {
  151. seqMap.insert(nmInfo->strUuid, QMap<QString /*指标*/, double>());
  152. }
  153. seqMap[nmInfo->strUuid].insert(nmInfo->abscissa, v);
  154. }
  155. }
  156. }
  157. QVector<QVector<double>> pMat; // 一个指标一列数据
  158. QMapIterator<QString, QMap<QString /*指标*/, double>> iter(seqMap);
  159. while (iter.hasNext()) {
  160. iter.next();
  161. QMap<QString /*指标*/, double> indexMap = iter.value();
  162. QVector<double> tmp;
  163. QMapIterator<QString /*指标*/, double> iterIndex(indexMap);
  164. while (iterIndex.hasNext()) {
  165. iterIndex.next();
  166. tmp << iterIndex.value();
  167. }
  168. pMat << tmp;
  169. }
  170. QScopedPointer<PCA> pca(new PCA(pMat));
  171. pca->compute();
  172. QVector<qreal> weights = pca->weights();
  173. QVector<qreal> score = pca->scores();
  174. for (int c = 0; c < s.childs.size(); ++c) {
  175. mindNodeWeights << MindNodeItem { s.name, s.childs.at(c), weights.at(c), score.at(c) };
  176. }
  177. }
  178. }
  179. }
  180. }
  181. qDeleteAll(nmInfos);
  182. }
  183. /// 数据集结,先计算每个专家的权重,在求均值
  184. void MindEvaluation::computeNoMergeWeights()
  185. {
  186. int enjId = m_mind->root().projectId;
  187. QList<UserConfig *> users;
  188. if (!UserConfigService().QueryUserConfigListInfoByEngineerId(&users, enjId)) {
  189. QMessageBox::warning(nullptr, "警告", "数据库访问失败");
  190. return;
  191. }
  192. bool QueryUserConfigListInfoByEngineerId(QList<UserConfig *> * userCfgList, int engineerId);
  193. QScopedPointer<QList<QList<MindNodeItem>>> allItems(new QList<QList<MindNodeItem>>());
  194. for (const auto &user : users) {
  195. QList<NodeMatrixInfo *> nmInfos;
  196. if (!NodeMatrixService().QueryNodeMatrixListByExpertIdAndEngineerId(&nmInfos, user->id, enjId, m_tableType)) {
  197. qDeleteAll(users);
  198. QMessageBox::warning(nullptr, "警告", "数据库访问失败");
  199. return;
  200. }
  201. QList<MindNodeItem> mnItem;
  202. computeOneEntry(nmInfos, mnItem);
  203. allItems->append(mnItem);
  204. qDeleteAll(nmInfos);
  205. }
  206. qDeleteAll(users);
  207. for (int i = 0; i < allItems->size(); ++i) {
  208. if (i == 0) {
  209. mindNodeWeights = allItems->at(0);
  210. } else {
  211. for (const auto &item : allItems->at(i)) {
  212. int id = mindNodeWeights.indexOf(item);
  213. mindNodeWeights[id].value += item.value;
  214. mindNodeWeights[id].score += item.score;
  215. }
  216. }
  217. }
  218. for (int id = 0; id < mindNodeWeights.size(); id++) {
  219. mindNodeWeights[id].value /= allItems->size();
  220. mindNodeWeights[id].score /= allItems->size();
  221. }
  222. }
  223. /// 数矩阵集结,先计算出一个综合专家,在计算权重
  224. void MindEvaluation::computeMergeWeights()
  225. {
  226. int enjId = m_mind->root().projectId;
  227. QList<UserConfig *> users;
  228. if (!UserConfigService().QueryUserConfigListInfoByEngineerId(&users, enjId)) {
  229. QMessageBox::warning(nullptr, "警告", "数据库访问失败");
  230. return;
  231. }
  232. bool QueryUserConfigListInfoByEngineerId(QList<UserConfig *> * userCfgList, int engineerId);
  233. QList<NodeMatrixInfo *> megerNMInfo;
  234. QVector<double> nodeValues;
  235. for (const auto &user : users) {
  236. QList<NodeMatrixInfo *> nmInfos;
  237. if (!NodeMatrixService().QueryNodeMatrixListByExpertIdAndEngineerId(&nmInfos, user->id, enjId, m_tableType)) {
  238. qDeleteAll(users);
  239. QMessageBox::warning(nullptr, "警告", "数据库访问失败");
  240. return;
  241. }
  242. if (megerNMInfo.size() == 0) {
  243. for (int nm = 0; nm < nmInfos.size(); ++nm) {
  244. NodeMatrixInfo *info = nmInfos.at(nm);
  245. megerNMInfo.append(new NodeMatrixInfo());
  246. *megerNMInfo[0] = *info;
  247. double v;
  248. QStringList nds = nmInfos.at(nm)->nodeValue.split("/");
  249. if (nds.size() == 1) {
  250. v = nds.at(0).toDouble();
  251. } else {
  252. v = nds.at(0).toDouble() / nds.at(1).toDouble();
  253. }
  254. nodeValues << v;
  255. }
  256. } else {
  257. for (int nm = 0; nm < nmInfos.size(); ++nm) {
  258. double v;
  259. QStringList nds = nmInfos.at(nm)->nodeValue.split("/");
  260. if (nds.size() == 1) {
  261. v = nds.at(0).toDouble();
  262. } else {
  263. v = nds.at(0).toDouble() / nds.at(1).toDouble();
  264. }
  265. nodeValues[nm] += v;
  266. }
  267. }
  268. qDeleteAll(nmInfos);
  269. }
  270. for (int nm = 0; nm < megerNMInfo.size(); ++nm) {
  271. megerNMInfo[nm]->nodeValue = QString::number(nodeValues[nm] / users.size());
  272. }
  273. computeOneEntry(megerNMInfo, mindNodeWeights);
  274. qDeleteAll(megerNMInfo);
  275. qDeleteAll(users);
  276. }
  277. void MindEvaluation::computeOneEntry(const QList<NodeMatrixInfo *> &nmInfos, QList<MindNodeItem> &mindNodeItem)
  278. {
  279. for (int sn = 0; sn < m_seqNodes.count(); sn++) {
  280. qDebug() << "第" << sn << "层";
  281. for (auto s : m_seqNodes.at(sn)) {
  282. qDebug() << s.name << s.childs;
  283. if (m_mindMatrix == 0) { // 层次分析法
  284. auto matrixStr = getMatrixString(s.childs);
  285. QVector<qreal> nxnValus(matrixStr.size(), 0.0001); // n x n矩阵
  286. for (const auto &nmInfo : nmInfos) {
  287. if (nmInfo->mindId == m_mindMatrix) {
  288. int loc = matrixStr.indexOf(QPair<QString, QString>(nmInfo->abscissa, nmInfo->ordinate));
  289. if (loc >= 0) {
  290. QStringList nodeValue = nmInfo->nodeValue.split("/");
  291. if (nodeValue.size() == 1) {
  292. nxnValus[loc] = nodeValue[0].toDouble();
  293. } else {
  294. nxnValus[loc] = nodeValue[0].toDouble() / nodeValue[1].toDouble();
  295. }
  296. }
  297. }
  298. }
  299. QScopedPointer<HierarchicalAnalysis> ha(new HierarchicalAnalysis(s.childs.size(), nxnValus));
  300. QVector<qreal> weights = ha->getWeights();
  301. for (int c = 0; c < s.childs.size(); ++c) {
  302. mindNodeItem << MindNodeItem { s.name, s.childs.at(c), weights.at(c), 0 };
  303. }
  304. } else {
  305. // 熵权法
  306. if (1) {
  307. QMap<QString /*指标*/, QMap<QString /*uuid*/, double>> seqMap;
  308. for (const auto &nmInfo : nmInfos) {
  309. if (nmInfo->mindId == m_mindMatrix) {
  310. int loc = s.childs.indexOf(nmInfo->abscissa); // 指标索引
  311. if (loc >= 0) {
  312. QStringList nodeValue = nmInfo->nodeValue.split("/");
  313. double v;
  314. if (nodeValue.size() == 1) {
  315. v = nodeValue[0].toDouble();
  316. } else {
  317. v = nodeValue[0].toDouble() / nodeValue[1].toDouble();
  318. }
  319. if (!seqMap.contains(nmInfo->abscissa)) {
  320. seqMap.insert(nmInfo->abscissa, QMap<QString /*uuid*/, double>());
  321. }
  322. seqMap[nmInfo->abscissa].insert(nmInfo->strUuid, v);
  323. }
  324. }
  325. }
  326. EntropyMat eMat; // 一个指标一行数据
  327. for (const auto &c : s.childs) {
  328. if (seqMap.contains(c)) {
  329. QVector<double> indexVecs;
  330. QMapIterator<QString, double> iter(seqMap[c]);
  331. while (iter.hasNext()) {
  332. iter.next();
  333. indexVecs << iter.value();
  334. }
  335. eMat << indexVecs;
  336. } else {
  337. QMessageBox::warning(nullptr, "警告", QString("指标%1数据缺失").arg(c));
  338. }
  339. }
  340. QScopedPointer<EntropyWeights> ew(new EntropyWeights(eMat));
  341. QVector<qreal> weights, score;
  342. ew->compute(weights, score);
  343. for (int c = 0; c < s.childs.size(); ++c) {
  344. mindNodeItem << MindNodeItem { s.name, s.childs.at(c), weights.at(c), score.at(c) };
  345. }
  346. } else { // pca
  347. QMap<QString /*uuid*/, QMap<QString /*指标*/, double>> seqMap;
  348. for (const auto &nmInfo : nmInfos) {
  349. if (nmInfo->mindId == m_mindMatrix) {
  350. int loc = s.childs.indexOf(nmInfo->abscissa); // 指标索引
  351. if (loc >= 0) {
  352. QStringList nodeValue = nmInfo->nodeValue.split("/");
  353. double v;
  354. if (nodeValue.size() == 1) {
  355. v = nodeValue[0].toDouble();
  356. } else {
  357. v = nodeValue[0].toDouble() / nodeValue[1].toDouble();
  358. }
  359. if (!seqMap.contains(nmInfo->strUuid)) {
  360. seqMap.insert(nmInfo->strUuid, QMap<QString /*指标*/, double>());
  361. }
  362. seqMap[nmInfo->strUuid].insert(nmInfo->abscissa, v);
  363. }
  364. }
  365. }
  366. QVector<QVector<double>> pMat; // 一个指标一列数据
  367. QMapIterator<QString, QMap<QString /*指标*/, double>> iter(seqMap);
  368. while (iter.hasNext()) {
  369. iter.next();
  370. QMap<QString /*指标*/, double> indexMap = iter.value();
  371. QVector<double> tmp;
  372. QMapIterator<QString /*指标*/, double> iterIndex(indexMap);
  373. while (iterIndex.hasNext()) {
  374. iterIndex.next();
  375. tmp << iterIndex.value();
  376. }
  377. pMat << tmp;
  378. }
  379. QScopedPointer<PCA> pca(new PCA(pMat));
  380. pca->compute();
  381. QVector<qreal> weights = pca->weights();
  382. QVector<qreal> score = pca->scores();
  383. for (int c = 0; c < s.childs.size(); ++c) {
  384. mindNodeItem << MindNodeItem { s.name, s.childs.at(c), weights.at(c), score.at(c) };
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }