DataEvaluator.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #include "DataEvaluator.h"
  2. #include <ProjectManager.h>
  3. #include <SchemePlanManager.h>
  4. #include <CMind.h>
  5. #include "algorithm/EntropyWeights.h"
  6. #include "algorithm/PCA.h"
  7. #include "algorithm/HierarchicalAnalysis.h"
  8. #include <dbService/ClassSet.h>
  9. #include <dbService/NodeMatrixService.h>
  10. #include <dbService/CNodeDataService.h>
  11. #include <dbService/SchemeProcessService.h>
  12. #include <dbService/UserConfigService.h>
  13. #include <dbService/MindWeightService.h>
  14. #include <dbService/MindScoreService.h>
  15. #include <QMap>
  16. #include <QDebug>
  17. DataEvaluator::DataEvaluator(QObject *parent) : QObject(parent) { }
  18. void DataEvaluator::setProcess(SchemePlanManager::SchemeProcessInfo process)
  19. {
  20. m_process = process;
  21. }
  22. SchemePlanManager::SchemeProcessInfo DataEvaluator::process() const
  23. {
  24. return m_process;
  25. }
  26. void DataEvaluator::setGatherType(DataEvaluator::GatherType type)
  27. {
  28. m_gatherType = type;
  29. }
  30. DataEvaluator::GatherType DataEvaluator::gatherType() const
  31. {
  32. return m_gatherType;
  33. }
  34. bool DataEvaluator::evaluate()
  35. {
  36. if (m_process.type == SchemePlanManager::ImportWeightData) {
  37. if (m_process.dSource == SchemePlanManager::FromExpert) {
  38. return evaluateWeightFromExpert();
  39. } else if (m_process.dSource == SchemePlanManager::FromMeasurement) {
  40. return evaluateWeightFromMeasure();
  41. }
  42. } else if (m_process.type == SchemePlanManager::ImportEvalData) {
  43. if (m_process.indexType == ProjectManager::TechIndex) {
  44. return evaluateTech();
  45. } else if (m_process.indexType == ProjectManager::OptimalIndex) {
  46. return evaluateScheme();
  47. } else if (m_process.indexType == ProjectManager::EfficiencyIndex) {
  48. return evaluateEfficiencyMEA();
  49. }
  50. }
  51. return false;
  52. }
  53. bool DataEvaluator::evaluateWeightFromExpert()
  54. {
  55. QMap<QString, QMap<QString, NodeMatrixInfo *>> nodeData;
  56. bool dataRet = getNodeData(nodeData);
  57. /// 权重分析专家导入数据, 使用层次分析法
  58. SchemePlanManager::Algorithm algorithm = SchemePlanManager::AHP;
  59. /// 获取指标体系
  60. QList<CNodeData> nodeList;
  61. bool mindRet = CNodeDataService().QueryAll(nodeList, m_process.projectId, m_process.indexType);
  62. CMind *mind = new CMind(this);
  63. mind->setNodeList(nodeList);
  64. /// 获取专家配置
  65. QMap<QString, double> config;
  66. bool cfgRet = getUserConfig(config);
  67. if (!(dataRet && mindRet && cfgRet)) {
  68. return false;
  69. }
  70. QMap<QString, double> indexWeights;
  71. // 结果集结, 先计算各个专家的数据, 再取均值
  72. if (m_gatherType == Result) {
  73. QMap<QString, QMap<QString, double>> mWeights;
  74. for (QString expertId : nodeData.keys()) {
  75. for (int i = 1; i < mind->levels(); i++) {
  76. for (CNodeData node : mind->nodesInLevel(i)) {
  77. QList<CNodeData> subNodes = mind->subNodes(node);
  78. QVector<qreal> nxnValus; // n x n矩阵
  79. for (int j = 0; j < subNodes.size(); j++) {
  80. QString abs = subNodes[j].name;
  81. for (int k = 0; k < subNodes.size(); k++) {
  82. QString ord = subNodes[k].name;
  83. QString key = abs + "-" + ord;
  84. double v;
  85. QStringList nodeValue = nodeData[expertId][key]->nodeValue.split("/");
  86. if (nodeValue.size() == 1) {
  87. v = nodeValue[0].toDouble();
  88. } else {
  89. v = nodeValue[0].toDouble() / nodeValue[1].toDouble();
  90. }
  91. nxnValus.append(v);
  92. }
  93. }
  94. // 计算权重并存储
  95. QScopedPointer<HierarchicalAnalysis> ha(new HierarchicalAnalysis(subNodes.size(), nxnValus));
  96. QVector<qreal> weights = ha->getWeights();
  97. for (int l = 0; l < weights.size(); ++l) {
  98. if (mWeights.keys().contains(expertId) == false) {
  99. mWeights[expertId] = QMap<QString, double>();
  100. }
  101. CNodeData pNode = mind->node(subNodes[l].pNumber);
  102. if (mWeights[expertId].keys().contains(pNode.name)) {
  103. mWeights[expertId][subNodes[l].name] = mWeights[expertId][pNode.name] * weights[l];
  104. } else {
  105. mWeights[expertId][subNodes[l].name] = weights[l];
  106. }
  107. }
  108. }
  109. }
  110. }
  111. // 求平均权重
  112. for (int i = 1; i < mind->levels(); i++) {
  113. for (CNodeData node : mind->nodesInLevel(i)) {
  114. QList<CNodeData> subNodes = mind->subNodes(node);
  115. for (int j = 0; j < subNodes.size(); j++) {
  116. double sum = 0;
  117. for (QString expertId : nodeData.keys()) {
  118. sum += mWeights[expertId][subNodes[j].name] * config[expertId];
  119. }
  120. indexWeights[subNodes[j].name] = sum / nodeData.keys().size();
  121. }
  122. }
  123. }
  124. } else { // 矩阵集结, 先计算各个专家数据的均值, 在求权重
  125. // 求专家数据均值
  126. QMap<QString, double> avgNodeValue;
  127. for (QString key : nodeData.values().first().keys()) {
  128. double sum = 0;
  129. for (QString expertId : nodeData.keys()) {
  130. double v;
  131. QStringList nodeValue = nodeData[expertId][key]->nodeValue.split("/");
  132. if (nodeValue.size() == 1) {
  133. v = nodeValue[0].toDouble();
  134. } else {
  135. v = nodeValue[0].toDouble() / nodeValue[1].toDouble();
  136. }
  137. sum += v * config[expertId];
  138. }
  139. avgNodeValue[key] = sum / nodeData.keys().size();
  140. }
  141. // 求权重
  142. for (int i = 1; i < mind->levels(); i++) {
  143. for (CNodeData node : mind->nodesInLevel(i)) {
  144. QList<CNodeData> subNodes = mind->subNodes(node);
  145. QVector<qreal> nxnValus; // n x n矩阵
  146. for (int j = 0; j < subNodes.size(); j++) {
  147. QString abs = subNodes[j].name;
  148. for (int k = 0; k < subNodes.size(); k++) {
  149. QString ord = subNodes[k].name;
  150. QString key = abs + "-" + ord;
  151. nxnValus.append(avgNodeValue[key]);
  152. }
  153. }
  154. // 计算权重并存储
  155. QScopedPointer<HierarchicalAnalysis> ha(new HierarchicalAnalysis(subNodes.size(), nxnValus));
  156. QVector<qreal> weights = ha->getWeights();
  157. for (int l = 0; l < weights.size(); ++l) {
  158. CNodeData pNode = mind->node(subNodes[l].pNumber);
  159. if (indexWeights.keys().contains(pNode.name)) {
  160. indexWeights[subNodes[l].name] = indexWeights[pNode.name] * weights[l];
  161. } else {
  162. indexWeights[subNodes[l].name] = weights[l];
  163. }
  164. }
  165. }
  166. }
  167. }
  168. QStringList valueList;
  169. for (QString key : indexWeights.keys()) {
  170. valueList.append(QString("%1:%2").arg(key).arg(indexWeights[key]));
  171. }
  172. QString valueStr = valueList.join(";");
  173. bool ret = MindWeightService().saveUniqueWeightData(m_process.projectId, m_process.indexType, m_process.dSource,
  174. algorithm, valueStr);
  175. return ret;
  176. }
  177. bool DataEvaluator::evaluateWeightFromMeasure()
  178. {
  179. QMap<QString, QMap<QString, NodeMatrixInfo *>> nodeData;
  180. bool dataRet = getNodeData(nodeData);
  181. SchemePlanManager::Algorithm algorithm;
  182. bool algRet = getAlgorithm(algorithm);
  183. /// 获取指标体系
  184. QList<CNodeData> nodeList;
  185. bool mindRet = CNodeDataService().QueryAll(nodeList, m_process.projectId, m_process.indexType);
  186. CMind *mind = new CMind(this);
  187. mind->setNodeList(nodeList);
  188. if (!(dataRet && algRet && mindRet)) {
  189. return false;
  190. }
  191. /// 各个指标的权重值
  192. /// 外层 QString 是 uuid, 内层 QString 是指标名称, double 是指标权重
  193. QMap<QString, double> allWeights;
  194. if (algorithm == SchemePlanManager::Entropy) { // 熵值法
  195. /// 根据指标体系层级, 构造算法需要的数据, 逐层计算权重值并保存
  196. for (int i = 1; i < mind->levels(); i++) {
  197. for (CNodeData node : mind->nodesInLevel(i)) {
  198. QList<CNodeData> subNodes = mind->subNodes(node);
  199. EntropyMat mat;
  200. for (int j = 0; j < subNodes.size(); j++) {
  201. QVector<double> values;
  202. for (QString uuid : nodeData.keys()) {
  203. NodeMatrixInfo *info = nodeData[uuid][subNodes[j].name];
  204. if (info == nullptr) {
  205. break;
  206. }
  207. double value = nodeData[uuid][subNodes[j].name]->nodeValue.toDouble();
  208. values.append(value);
  209. }
  210. mat.append({ values });
  211. }
  212. if (mat.size() <= 0) {
  213. continue;
  214. }
  215. // 计算权重
  216. QScopedPointer<EntropyWeights> ew(new EntropyWeights(mat));
  217. QVector<double> weights, scores;
  218. ew->compute(weights, scores);
  219. // 结合父节点指标权重计算指标最终权重
  220. for (int k = 0; k < subNodes.size(); k++) {
  221. double w = weights[k];
  222. if (std::_Is_nan(w)) {
  223. w = 1 / subNodes.size();
  224. }
  225. CNodeData pNode = mind->node(subNodes[k].pNumber);
  226. if (allWeights.keys().contains(pNode.name)) {
  227. allWeights[subNodes[k].name] = allWeights[pNode.name] * w;
  228. } else {
  229. allWeights[subNodes[k].name] = w;
  230. }
  231. }
  232. }
  233. }
  234. } else if (algorithm == SchemePlanManager::PrincipalComponents) { // 主成分分析法
  235. for (int i = 1; i < mind->levels(); i++) {
  236. for (CNodeData node : mind->nodesInLevel(i)) {
  237. QList<CNodeData> subNodes = mind->subNodes(node);
  238. QVector<QVector<double>> mat;
  239. for (QString uuid : nodeData.keys()) {
  240. QVector<double> values;
  241. for (int j = 0; j < subNodes.size(); j++) {
  242. NodeMatrixInfo *info = nodeData[uuid][subNodes[j].name];
  243. if (info == nullptr) {
  244. break;
  245. }
  246. double value = nodeData[uuid][subNodes[j].name]->nodeValue.toDouble();
  247. values.append(value);
  248. }
  249. mat.append({ values });
  250. }
  251. if (mat.size() <= 0) {
  252. continue;
  253. }
  254. QScopedPointer<PCA> pca(new PCA(mat));
  255. pca->compute();
  256. // 结合父节点指标权重计算指标最终权重
  257. for (int k = 0; k < subNodes.size(); k++) {
  258. double w = pca->weights()[k];
  259. if (std::_Is_nan(w)) {
  260. w = 1 / subNodes.size();
  261. }
  262. CNodeData pNode = mind->node(subNodes[k].pNumber);
  263. if (allWeights.keys().contains(pNode.name)) {
  264. allWeights[subNodes[k].name] = allWeights[pNode.name] * w;
  265. } else {
  266. allWeights[subNodes[k].name] = w;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. QStringList valueList;
  273. for (QString key : allWeights.keys()) {
  274. valueList.append(QString("%1:%2").arg(key).arg(allWeights[key]));
  275. }
  276. QString valueStr = valueList.join(";");
  277. bool ret = MindWeightService().saveUniqueWeightData(m_process.projectId, m_process.indexType, m_process.dSource,
  278. algorithm, valueStr);
  279. return ret;
  280. }
  281. bool DataEvaluator::evaluateTech()
  282. {
  283. QMap<QString, double> weightData;
  284. bool weightRet = getWeight(weightData);
  285. QMap<QString, QMap<QString, NodeMatrixInfo *>> nodeData;
  286. bool dataRet = getNodeData(nodeData);
  287. /// 获取指标体系
  288. QList<CNodeData> nodeList;
  289. bool mindRet = CNodeDataService().QueryAll(nodeList, m_process.projectId, m_process.indexType);
  290. CMind *mind = new CMind(this);
  291. mind->setNodeList(nodeList);
  292. /// 获取权重配置
  293. QMap<QString, double> config;
  294. bool cfgRet = false;
  295. if (m_process.dSource == SchemePlanManager::FromExpert) {
  296. cfgRet = getUserConfig(config);
  297. } else {
  298. for (QString uuid : nodeData.keys()) {
  299. config[uuid] = 1.0 / nodeData.keys().size();
  300. }
  301. cfgRet = nodeData.size() > 0;
  302. }
  303. if (weightRet == false || dataRet == false || mindRet == false || nodeList.size() <= 0 || cfgRet == false) {
  304. qDebug() << __FUNCTION__ << __LINE__ << endl;
  305. return false;
  306. }
  307. QMap<QString, double> scoreData;
  308. for (int i = 1; i < mind->levels(); i++) {
  309. for (CNodeData node : mind->nodesInLevel(i)) {
  310. QList<CNodeData> subNodes = mind->subNodes(node);
  311. for (int j = 0; j < subNodes.size(); j++) {
  312. double score = 0;
  313. for (QString uuid : nodeData.keys()) {
  314. for (QString weightKey : weightData.keys()) {
  315. QString key = subNodes[j].name + "-" + weightKey;
  316. if (nodeData[uuid].keys().contains(key)) {
  317. score += nodeData[uuid][key]->nodeValue.toDouble() * weightData[weightKey] * config[uuid];
  318. }
  319. }
  320. }
  321. scoreData[subNodes[j].name] = score;
  322. }
  323. }
  324. }
  325. QStringList valueList;
  326. for (QString key : scoreData.keys()) {
  327. valueList.append(QString("%1:%2").arg(key).arg(scoreData[key]));
  328. }
  329. QString valueStr = valueList.join(";");
  330. bool ret = MindScoreService().saveUniqueScoreData(m_process.projectId, valueStr);
  331. qDebug() << __FUNCTION__ << __LINE__ << valueStr << endl;
  332. return ret;
  333. }
  334. bool DataEvaluator::evaluateScheme()
  335. {
  336. return false;
  337. }
  338. bool DataEvaluator::evaluateEfficiencyMEA()
  339. {
  340. return false;
  341. }
  342. bool DataEvaluator::evaluateEfficiencyGCE()
  343. {
  344. return false;
  345. }
  346. bool DataEvaluator::getNodeData(QMap<QString, QMap<QString, NodeMatrixInfo *>> &nodeData) const
  347. {
  348. /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引
  349. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType);
  350. QList<NodeMatrixInfo *> dataList;
  351. bool ret = NodeMatrixService().QueryDataByProjectAndIndex(&dataList, indexName, m_process.projectId,
  352. m_process.dSource);
  353. if (ret == false) {
  354. return false;
  355. }
  356. if (dataList.size() <= 0) {
  357. qDebug() << __FUNCTION__ << __LINE__ << "未录入评估数据" << endl;
  358. return false;
  359. }
  360. for (NodeMatrixInfo *info : dataList) {
  361. QString key = info->strUuid; // 实测数据的 key
  362. if (m_process.dSource == SchemePlanManager::FromExpert) {
  363. key = info->expertId; // 专家数据的 key
  364. }
  365. if (nodeData.keys().contains(key) == false) {
  366. nodeData[key] = QMap<QString, NodeMatrixInfo *>();
  367. }
  368. nodeData[key][nodeDataKey(info)] = info;
  369. }
  370. return true;
  371. }
  372. bool DataEvaluator::getAlgorithm(SchemePlanManager::Algorithm &algorithm) const
  373. {
  374. QList<SchemePlanManager::SchemeProcessInfo> processList;
  375. bool ret = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_process.projectId,
  376. m_process.indexType);
  377. if (ret == false) {
  378. return false;
  379. }
  380. for (auto process : processList) {
  381. if (process.type == SchemePlanManager::CalculateWeight) {
  382. algorithm = process.algorithm;
  383. break;
  384. }
  385. }
  386. if (m_process.type == SchemePlanManager::ImportWeightData) {
  387. if (m_process.dSource == SchemePlanManager::FromMeasurement && algorithm == SchemePlanManager::AHP) {
  388. algorithm == SchemePlanManager::Entropy;
  389. }
  390. if (m_process.dSource == SchemePlanManager::FromExpert) {
  391. algorithm == SchemePlanManager::AHP;
  392. }
  393. }
  394. return true;
  395. }
  396. bool DataEvaluator::getUserConfig(QMap<QString, double> &cfg) const
  397. {
  398. QList<UserConfig *> userCfgList;
  399. bool ret = UserConfigService().QueryUserConfigListInfoByEngineerId(&userCfgList, m_process.projectId);
  400. if (ret == false) {
  401. return false;
  402. }
  403. for (UserConfig *config : userCfgList) {
  404. cfg[QString("%1").arg(config->userId)] = config->weight / 100;
  405. }
  406. return true;
  407. }
  408. bool DataEvaluator::getWeight(QMap<QString, double> &weight) const
  409. {
  410. MindWeightInfo info;
  411. int indexType = m_process.indexType;
  412. if (indexType == ProjectManager::TechIndex) {
  413. indexType = ProjectManager::AbilityIndex;
  414. }
  415. bool ret = MindWeightService().queryWeightData(&info, m_process.projectId, indexType);
  416. if (ret == false) {
  417. return false;
  418. }
  419. if (info.id < 0) {
  420. qDebug() << __FUNCTION__ << __LINE__ << "未找到指标权重数据" << endl;
  421. return false;
  422. }
  423. QStringList weightList = info.weight.split(";");
  424. for (QString keyValueStr : weightList) {
  425. QStringList keyValue = keyValueStr.split(":");
  426. if (keyValue.size() == 2) {
  427. weight[keyValue.first()] = keyValue.last().toDouble();
  428. }
  429. }
  430. return true;
  431. }
  432. QString DataEvaluator::nodeDataKey(NodeMatrixInfo *data) const
  433. {
  434. QString key = data->abscissa;
  435. if (data->ordinate.length() > 0) {
  436. key += ("-" + data->ordinate);
  437. }
  438. return key;
  439. }