DataEvaluator.cpp 15 KB

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