DataEvaluator.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "DataEvaluator.h"
  2. #include <ProjectManager.h>
  3. #include <SchemePlanManager.h>
  4. #include <CMind.h>
  5. #include "algorithm/EntropyWeights.h"
  6. #include <dbService/ClassSet.h>
  7. #include <dbService/NodeMatrixService.h>
  8. #include <dbService/CNodeDataService.h>
  9. #include <dbService/SchemeProcessService.h>
  10. #include <QMap>
  11. #include <QDebug>
  12. DataEvaluator::DataEvaluator(QObject *parent) : QObject(parent) { }
  13. void DataEvaluator::setProcess(SchemePlanManager::SchemeProcessInfo process)
  14. {
  15. m_process = process;
  16. }
  17. SchemePlanManager::SchemeProcessInfo DataEvaluator::process() const
  18. {
  19. return m_process;
  20. }
  21. void DataEvaluator::setGatherType(DataEvaluator::GatherType type)
  22. {
  23. m_gatherType = type;
  24. }
  25. DataEvaluator::GatherType DataEvaluator::gatherType() const
  26. {
  27. return m_gatherType;
  28. }
  29. bool DataEvaluator::evaluate()
  30. {
  31. if (m_process.type == SchemePlanManager::ImportWeightData) {
  32. if (m_process.dSource == SchemePlanManager::FromExpert) {
  33. return evaluateWeightFromExpert();
  34. } else if (m_process.dSource == SchemePlanManager::FromMeasurement) {
  35. return evaluateWeightFromMeasure();
  36. }
  37. } else if (m_process.type == SchemePlanManager::ImportEvalData) {
  38. if (m_process.indexType == ProjectManager::TechIndex) {
  39. if (m_process.dSource == SchemePlanManager::FromExpert) {
  40. return evaluateTechFromExpert();
  41. } else if (m_process.dSource == SchemePlanManager::FromMeasurement) {
  42. return evaluateTechFromMeasure();
  43. }
  44. } else if (m_process.indexType == ProjectManager::OptimalIndex) {
  45. return evaluateScheme();
  46. } else if (m_process.indexType == ProjectManager::EfficiencyIndex) {
  47. return evaluateEfficiencyMEA();
  48. }
  49. }
  50. return false;
  51. }
  52. bool DataEvaluator::evaluateWeightFromExpert()
  53. {
  54. return false;
  55. }
  56. bool DataEvaluator::evaluateWeightFromMeasure()
  57. {
  58. /// 获取权重分析数据
  59. /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引
  60. QMap<QString, QMap<QString, NodeMatrixInfo *>> nodeData;
  61. bool dataRet = getNodeData(nodeData);
  62. SchemePlanManager::Algorithm algorithm;
  63. bool algRet = getAlgorithm(algorithm);
  64. /// 获取指标体系
  65. QList<CNodeData> nodeList;
  66. bool mindRet = CNodeDataService().QueryAll(nodeList, m_process.projectId, m_process.indexType);
  67. CMind *mind = new CMind(this);
  68. mind->setNodeList(nodeList);
  69. if (!(dataRet && algRet && mindRet)) {
  70. return false;
  71. }
  72. /// 各个指标的权重值
  73. /// 外层 QString 是 uuid, 内层 QString 是指标名称, double 是指标权重
  74. QMap<QString, double> allWeights;
  75. /// 根据指标体系层级, 构造算法需要的数据
  76. for (int i = 1; i < mind->levels(); i++) {
  77. for (CNodeData node : mind->nodesInLevel(i)) {
  78. QList<CNodeData> subNodes = mind->subNodes(node);
  79. EntropyMat mat;
  80. for (int j = 0; j < subNodes.size(); j++) {
  81. QVector<double> values;
  82. for (QString uuid : nodeData.keys()) {
  83. NodeMatrixInfo *info = nodeData[uuid][subNodes[j].name];
  84. if (info == nullptr) {
  85. break;
  86. }
  87. double value = nodeData[uuid][subNodes[j].name]->nodeValue.toDouble();
  88. values.append(value);
  89. }
  90. mat.append({ values });
  91. }
  92. // pdata = { { 4.1 }, { 0.04 }, { 0.19 }, { 0.01 }, { 0.05 } };
  93. // pdata = {
  94. // { 4.1, 3.9 }, { 0.04, 0.08 }, { 0.19, 0.38 }, { 0.01, 0.18 }, { 0.36, 0.19 }, {
  95. // 0.05, 0.04 }
  96. // };
  97. if (mat.size() <= 0) {
  98. break;
  99. }
  100. QScopedPointer<EntropyWeights> ew(new EntropyWeights(mat));
  101. QVector<double> weights, scores;
  102. ew->compute(weights, scores);
  103. qDebug() << __FUNCTION__ << __LINE__ << weights << endl;
  104. for (int j = 0; j < subNodes.size(); j++) {
  105. double w = weights[j];
  106. if (std::_Is_nan(w)) {
  107. w = 0;
  108. }
  109. CNodeData pNode = mind->node(subNodes[j].pNumber);
  110. if (allWeights.keys().contains(pNode.name)) {
  111. allWeights[subNodes[j].name] = allWeights[pNode.name] * w;
  112. } else {
  113. allWeights[subNodes[j].name] = w;
  114. }
  115. }
  116. }
  117. }
  118. qDebug() << __FUNCTION__ << __LINE__ << allWeights << endl;
  119. return false;
  120. }
  121. bool DataEvaluator::evaluateTechFromExpert()
  122. {
  123. return false;
  124. }
  125. bool DataEvaluator::evaluateTechFromMeasure()
  126. {
  127. return false;
  128. }
  129. bool DataEvaluator::evaluateScheme()
  130. {
  131. return false;
  132. }
  133. bool DataEvaluator::evaluateEfficiencyMEA()
  134. {
  135. return false;
  136. }
  137. bool DataEvaluator::evaluateEfficiencyGCE()
  138. {
  139. return false;
  140. }
  141. bool DataEvaluator::getNodeData(QMap<QString, QMap<QString, NodeMatrixInfo *>> &nodeData) const
  142. {
  143. /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引
  144. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType);
  145. QList<NodeMatrixInfo *> dataList;
  146. bool ret = NodeMatrixService().QueryDataByProjectAndIndex(&dataList, indexName, m_process.projectId,
  147. m_process.dSource);
  148. if (ret == false) {
  149. return false;
  150. }
  151. for (NodeMatrixInfo *info : dataList) {
  152. if (nodeData.keys().contains(info->strUuid) == false) {
  153. nodeData[info->strUuid] = QMap<QString, NodeMatrixInfo *>();
  154. }
  155. nodeData[info->strUuid][nodeDataKey(info)] = info;
  156. }
  157. return true;
  158. }
  159. bool DataEvaluator::getAlgorithm(SchemePlanManager::Algorithm &algorithm) const
  160. {
  161. QList<SchemePlanManager::SchemeProcessInfo> processList;
  162. bool ret = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_process.projectId,
  163. m_process.indexType);
  164. if (ret == false) {
  165. return false;
  166. }
  167. for (auto process : processList) {
  168. if (process.type == SchemePlanManager::CalculateWeight) {
  169. if (process.algorithm == SchemePlanManager::Entropy) {
  170. algorithm = process.algorithm;
  171. }
  172. break;
  173. }
  174. }
  175. return true;
  176. }
  177. QString DataEvaluator::nodeDataKey(NodeMatrixInfo *data) const
  178. {
  179. QString key = data->abscissa;
  180. if (data->ordinate.length() > 0) {
  181. key += ("-" + data->ordinate);
  182. }
  183. return key;
  184. }