#include "DataEvaluator.h" #include #include #include #include "algorithm/EntropyWeights.h" #include "algorithm/PCA.h" #include #include #include #include #include #include DataEvaluator::DataEvaluator(QObject *parent) : QObject(parent) { } void DataEvaluator::setProcess(SchemePlanManager::SchemeProcessInfo process) { m_process = process; } SchemePlanManager::SchemeProcessInfo DataEvaluator::process() const { return m_process; } void DataEvaluator::setGatherType(DataEvaluator::GatherType type) { m_gatherType = type; } DataEvaluator::GatherType DataEvaluator::gatherType() const { return m_gatherType; } bool DataEvaluator::evaluate() { if (m_process.type == SchemePlanManager::ImportWeightData) { if (m_process.dSource == SchemePlanManager::FromExpert) { return evaluateWeightFromExpert(); } else if (m_process.dSource == SchemePlanManager::FromMeasurement) { return evaluateWeightFromMeasure(); } } else if (m_process.type == SchemePlanManager::ImportEvalData) { if (m_process.indexType == ProjectManager::TechIndex) { if (m_process.dSource == SchemePlanManager::FromExpert) { return evaluateTechFromExpert(); } else if (m_process.dSource == SchemePlanManager::FromMeasurement) { return evaluateTechFromMeasure(); } } else if (m_process.indexType == ProjectManager::OptimalIndex) { return evaluateScheme(); } else if (m_process.indexType == ProjectManager::EfficiencyIndex) { return evaluateEfficiencyMEA(); } } return false; } bool DataEvaluator::evaluateWeightFromExpert() { return false; } bool DataEvaluator::evaluateWeightFromMeasure() { /// 获取权重分析数据 /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引 QMap> nodeData; bool dataRet = getNodeData(nodeData); SchemePlanManager::Algorithm algorithm; bool algRet = getAlgorithm(algorithm); /// 获取指标体系 QList nodeList; bool mindRet = CNodeDataService().QueryAll(nodeList, m_process.projectId, m_process.indexType); CMind *mind = new CMind(this); mind->setNodeList(nodeList); if (!(dataRet && algRet && mindRet)) { return false; } /// 各个指标的权重值 /// 外层 QString 是 uuid, 内层 QString 是指标名称, double 是指标权重 QMap allWeights; if (algorithm == SchemePlanManager::Entropy) { // 熵值法 /// 根据指标体系层级, 构造算法需要的数据, 逐层计算权重值并保存 for (int i = 1; i < mind->levels(); i++) { for (CNodeData node : mind->nodesInLevel(i)) { QList subNodes = mind->subNodes(node); EntropyMat mat; for (int j = 0; j < subNodes.size(); j++) { QVector values; for (QString uuid : nodeData.keys()) { NodeMatrixInfo *info = nodeData[uuid][subNodes[j].name]; if (info == nullptr) { break; } double value = nodeData[uuid][subNodes[j].name]->nodeValue.toDouble(); values.append(value); } mat.append({ values }); } if (mat.size() <= 0) { continue; } // 计算权重 QScopedPointer ew(new EntropyWeights(mat)); QVector weights, scores; ew->compute(weights, scores); // 结合父节点指标权重计算指标最终权重 for (int k = 0; k < subNodes.size(); k++) { double w = weights[k]; if (std::_Is_nan(w)) { w = 1 / subNodes.size(); } CNodeData pNode = mind->node(subNodes[k].pNumber); if (allWeights.keys().contains(pNode.name)) { allWeights[subNodes[k].name] = allWeights[pNode.name] * w; } else { allWeights[subNodes[k].name] = w; } } } } } else if (algorithm == SchemePlanManager::PrincipalComponents) { // 主成分分析法 for (int i = 1; i < mind->levels(); i++) { for (CNodeData node : mind->nodesInLevel(i)) { QList subNodes = mind->subNodes(node); QVector> mat; for (QString uuid : nodeData.keys()) { QVector values; for (int j = 0; j < subNodes.size(); j++) { NodeMatrixInfo *info = nodeData[uuid][subNodes[j].name]; if (info == nullptr) { break; } double value = nodeData[uuid][subNodes[j].name]->nodeValue.toDouble(); values.append(value); } mat.append({ values }); } if (mat.size() <= 0) { continue; } QScopedPointer pca(new PCA(mat)); pca->compute(); // 结合父节点指标权重计算指标最终权重 for (int k = 0; k < subNodes.size(); k++) { double w = pca->weights()[k]; if (std::_Is_nan(w)) { w = 1 / subNodes.size(); } CNodeData pNode = mind->node(subNodes[k].pNumber); if (allWeights.keys().contains(pNode.name)) { allWeights[subNodes[k].name] = allWeights[pNode.name] * w; } else { allWeights[subNodes[k].name] = w; } } } } } qDebug() << __FUNCTION__ << __LINE__ << allWeights << endl; return true; } bool DataEvaluator::evaluateTechFromExpert() { return false; } bool DataEvaluator::evaluateTechFromMeasure() { return false; } bool DataEvaluator::evaluateScheme() { return false; } bool DataEvaluator::evaluateEfficiencyMEA() { return false; } bool DataEvaluator::evaluateEfficiencyGCE() { return false; } bool DataEvaluator::getNodeData(QMap> &nodeData) const { /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引 QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType); QList dataList; bool ret = NodeMatrixService().QueryDataByProjectAndIndex(&dataList, indexName, m_process.projectId, m_process.dSource); if (ret == false) { return false; } for (NodeMatrixInfo *info : dataList) { if (nodeData.keys().contains(info->strUuid) == false) { nodeData[info->strUuid] = QMap(); } nodeData[info->strUuid][nodeDataKey(info)] = info; } return true; } bool DataEvaluator::getAlgorithm(SchemePlanManager::Algorithm &algorithm) const { QList processList; bool ret = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_process.projectId, m_process.indexType); if (ret == false) { return false; } for (auto process : processList) { if (process.type == SchemePlanManager::CalculateWeight) { algorithm = process.algorithm; break; } } return true; } QString DataEvaluator::nodeDataKey(NodeMatrixInfo *data) const { QString key = data->abscissa; if (data->ordinate.length() > 0) { key += ("-" + data->ordinate); } return key; }