DataEvaluator.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <dbService/ClassSet.h>
  8. #include <dbService/NodeMatrixService.h>
  9. #include <dbService/CNodeDataService.h>
  10. #include <dbService/SchemeProcessService.h>
  11. #include <QMap>
  12. #include <QDebug>
  13. DataEvaluator::DataEvaluator(QObject *parent) : QObject(parent) { }
  14. void DataEvaluator::setProcess(SchemePlanManager::SchemeProcessInfo process)
  15. {
  16. m_process = process;
  17. }
  18. SchemePlanManager::SchemeProcessInfo DataEvaluator::process() const
  19. {
  20. return m_process;
  21. }
  22. void DataEvaluator::setGatherType(DataEvaluator::GatherType type)
  23. {
  24. m_gatherType = type;
  25. }
  26. DataEvaluator::GatherType DataEvaluator::gatherType() const
  27. {
  28. return m_gatherType;
  29. }
  30. bool DataEvaluator::evaluate()
  31. {
  32. if (m_process.type == SchemePlanManager::ImportWeightData) {
  33. if (m_process.dSource == SchemePlanManager::FromExpert) {
  34. return evaluateWeightFromExpert();
  35. } else if (m_process.dSource == SchemePlanManager::FromMeasurement) {
  36. return evaluateWeightFromMeasure();
  37. }
  38. } else if (m_process.type == SchemePlanManager::ImportEvalData) {
  39. if (m_process.indexType == ProjectManager::TechIndex) {
  40. if (m_process.dSource == SchemePlanManager::FromExpert) {
  41. return evaluateTechFromExpert();
  42. } else if (m_process.dSource == SchemePlanManager::FromMeasurement) {
  43. return evaluateTechFromMeasure();
  44. }
  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. return false;
  56. }
  57. bool DataEvaluator::evaluateWeightFromMeasure()
  58. {
  59. /// 获取权重分析数据
  60. /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引
  61. QMap<QString, QMap<QString, NodeMatrixInfo *>> nodeData;
  62. bool dataRet = getNodeData(nodeData);
  63. SchemePlanManager::Algorithm algorithm;
  64. bool algRet = getAlgorithm(algorithm);
  65. /// 获取指标体系
  66. QList<CNodeData> nodeList;
  67. bool mindRet = CNodeDataService().QueryAll(nodeList, m_process.projectId, m_process.indexType);
  68. CMind *mind = new CMind(this);
  69. mind->setNodeList(nodeList);
  70. if (!(dataRet && algRet && mindRet)) {
  71. return false;
  72. }
  73. /// 各个指标的权重值
  74. /// 外层 QString 是 uuid, 内层 QString 是指标名称, double 是指标权重
  75. QMap<QString, double> allWeights;
  76. if (algorithm == SchemePlanManager::Entropy) { // 熵值法
  77. /// 根据指标体系层级, 构造算法需要的数据, 逐层计算权重值并保存
  78. for (int i = 1; i < mind->levels(); i++) {
  79. for (CNodeData node : mind->nodesInLevel(i)) {
  80. QList<CNodeData> subNodes = mind->subNodes(node);
  81. EntropyMat mat;
  82. for (int j = 0; j < subNodes.size(); j++) {
  83. QVector<double> values;
  84. for (QString uuid : nodeData.keys()) {
  85. NodeMatrixInfo *info = nodeData[uuid][subNodes[j].name];
  86. if (info == nullptr) {
  87. break;
  88. }
  89. double value = nodeData[uuid][subNodes[j].name]->nodeValue.toDouble();
  90. values.append(value);
  91. }
  92. mat.append({ values });
  93. }
  94. if (mat.size() <= 0) {
  95. continue;
  96. }
  97. // 计算权重
  98. QScopedPointer<EntropyWeights> ew(new EntropyWeights(mat));
  99. QVector<double> weights, scores;
  100. ew->compute(weights, scores);
  101. // 结合父节点指标权重计算指标最终权重
  102. for (int k = 0; k < subNodes.size(); k++) {
  103. double w = weights[k];
  104. if (std::_Is_nan(w)) {
  105. w = 1 / subNodes.size();
  106. }
  107. CNodeData pNode = mind->node(subNodes[k].pNumber);
  108. if (allWeights.keys().contains(pNode.name)) {
  109. allWeights[subNodes[k].name] = allWeights[pNode.name] * w;
  110. } else {
  111. allWeights[subNodes[k].name] = w;
  112. }
  113. }
  114. }
  115. }
  116. } else if (algorithm == SchemePlanManager::PrincipalComponents) { // 主成分分析法
  117. for (int i = 1; i < mind->levels(); i++) {
  118. for (CNodeData node : mind->nodesInLevel(i)) {
  119. QList<CNodeData> subNodes = mind->subNodes(node);
  120. QVector<QVector<double>> mat;
  121. for (QString uuid : nodeData.keys()) {
  122. QVector<double> values;
  123. for (int j = 0; j < subNodes.size(); j++) {
  124. NodeMatrixInfo *info = nodeData[uuid][subNodes[j].name];
  125. if (info == nullptr) {
  126. break;
  127. }
  128. double value = nodeData[uuid][subNodes[j].name]->nodeValue.toDouble();
  129. values.append(value);
  130. }
  131. mat.append({ values });
  132. }
  133. if (mat.size() <= 0) {
  134. continue;
  135. }
  136. QScopedPointer<PCA> pca(new PCA(mat));
  137. pca->compute();
  138. // 结合父节点指标权重计算指标最终权重
  139. for (int k = 0; k < subNodes.size(); k++) {
  140. double w = pca->weights()[k];
  141. if (std::_Is_nan(w)) {
  142. w = 1 / subNodes.size();
  143. }
  144. CNodeData pNode = mind->node(subNodes[k].pNumber);
  145. if (allWeights.keys().contains(pNode.name)) {
  146. allWeights[subNodes[k].name] = allWeights[pNode.name] * w;
  147. } else {
  148. allWeights[subNodes[k].name] = w;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. qDebug() << __FUNCTION__ << __LINE__ << allWeights << endl;
  155. return false;
  156. }
  157. bool DataEvaluator::evaluateTechFromExpert()
  158. {
  159. return false;
  160. }
  161. bool DataEvaluator::evaluateTechFromMeasure()
  162. {
  163. return false;
  164. }
  165. bool DataEvaluator::evaluateScheme()
  166. {
  167. return false;
  168. }
  169. bool DataEvaluator::evaluateEfficiencyMEA()
  170. {
  171. return false;
  172. }
  173. bool DataEvaluator::evaluateEfficiencyGCE()
  174. {
  175. return false;
  176. }
  177. bool DataEvaluator::getNodeData(QMap<QString, QMap<QString, NodeMatrixInfo *>> &nodeData) const
  178. {
  179. /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引
  180. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType);
  181. QList<NodeMatrixInfo *> dataList;
  182. bool ret = NodeMatrixService().QueryDataByProjectAndIndex(&dataList, indexName, m_process.projectId,
  183. m_process.dSource);
  184. if (ret == false) {
  185. return false;
  186. }
  187. for (NodeMatrixInfo *info : dataList) {
  188. if (nodeData.keys().contains(info->strUuid) == false) {
  189. nodeData[info->strUuid] = QMap<QString, NodeMatrixInfo *>();
  190. }
  191. nodeData[info->strUuid][nodeDataKey(info)] = info;
  192. }
  193. return true;
  194. }
  195. bool DataEvaluator::getAlgorithm(SchemePlanManager::Algorithm &algorithm) const
  196. {
  197. QList<SchemePlanManager::SchemeProcessInfo> processList;
  198. bool ret = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_process.projectId,
  199. m_process.indexType);
  200. if (ret == false) {
  201. return false;
  202. }
  203. for (auto process : processList) {
  204. if (process.type == SchemePlanManager::CalculateWeight) {
  205. algorithm = process.algorithm;
  206. break;
  207. }
  208. }
  209. return true;
  210. }
  211. QString DataEvaluator::nodeDataKey(NodeMatrixInfo *data) const
  212. {
  213. QString key = data->abscissa;
  214. if (data->ordinate.length() > 0) {
  215. key += ("-" + data->ordinate);
  216. }
  217. return key;
  218. }