DataEvaluator.cpp 11 KB

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