123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- #include "DataEvaluator.h"
- #include <ProjectManager.h>
- #include <SchemePlanManager.h>
- #include <CMind.h>
- #include "algorithm/EntropyWeights.h"
- #include "algorithm/PCA.h"
- #include "algorithm/HierarchicalAnalysis.h"
- #include <dbService/ClassSet.h>
- #include <dbService/NodeMatrixService.h>
- #include <dbService/CNodeDataService.h>
- #include <dbService/SchemeProcessService.h>
- #include <dbService/UserConfigService.h>
- #include <dbService/MindWeightService.h>
- #include <QMap>
- #include <QDebug>
- 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()
- {
- QMap<QString, QMap<QString, NodeMatrixInfo *>> nodeData;
- bool dataRet = getNodeData(nodeData);
- /// 权重分析专家导入数据, 使用层次分析法
- SchemePlanManager::Algorithm algorithm = SchemePlanManager::AHP;
- /// 获取指标体系
- QList<CNodeData> nodeList;
- bool mindRet = CNodeDataService().QueryAll(nodeList, m_process.projectId, m_process.indexType);
- CMind *mind = new CMind(this);
- mind->setNodeList(nodeList);
- /// 获取专家配置
- QMap<QString, double> config;
- bool cfgRet = getUserConfig(config);
- if (!(dataRet && mindRet && cfgRet)) {
- return false;
- }
- QMap<QString, double> indexWeights;
- // 结果集结, 先计算各个专家的数据, 再取均值
- if (m_gatherType == Result) {
- QMap<QString, QMap<QString, double>> mWeights;
- for (QString expertId : nodeData.keys()) {
- for (int i = 1; i < mind->levels(); i++) {
- for (CNodeData node : mind->nodesInLevel(i)) {
- QList<CNodeData> subNodes = mind->subNodes(node);
- QVector<qreal> nxnValus; // n x n矩阵
- for (int j = 0; j < subNodes.size(); j++) {
- QString abs = subNodes[j].name;
- for (int k = 0; k < subNodes.size(); k++) {
- QString ord = subNodes[k].name;
- QString key = abs + "-" + ord;
- double v;
- QStringList nodeValue = nodeData[expertId][key]->nodeValue.split("/");
- if (nodeValue.size() == 1) {
- v = nodeValue[0].toDouble();
- } else {
- v = nodeValue[0].toDouble() / nodeValue[1].toDouble();
- }
- nxnValus.append(v);
- }
- }
- // 计算权重并存储
- QScopedPointer<HierarchicalAnalysis> ha(new HierarchicalAnalysis(subNodes.size(), nxnValus));
- QVector<qreal> weights = ha->getWeights();
- for (int l = 0; l < weights.size(); ++l) {
- if (mWeights.keys().contains(expertId) == false) {
- mWeights[expertId] = QMap<QString, double>();
- }
- CNodeData pNode = mind->node(subNodes[l].pNumber);
- if (mWeights[expertId].keys().contains(pNode.name)) {
- mWeights[expertId][subNodes[l].name] = mWeights[expertId][pNode.name] * weights[l];
- } else {
- mWeights[expertId][subNodes[l].name] = weights[l];
- }
- }
- }
- }
- }
- // 求平均权重
- for (int i = 1; i < mind->levels(); i++) {
- for (CNodeData node : mind->nodesInLevel(i)) {
- QList<CNodeData> subNodes = mind->subNodes(node);
- for (int j = 0; j < subNodes.size(); j++) {
- double sum = 0;
- for (QString expertId : nodeData.keys()) {
- sum += mWeights[expertId][subNodes[j].name];
- }
- indexWeights[subNodes[j].name] = sum / nodeData.keys().size();
- }
- }
- }
- } else { // 矩阵集结, 先计算各个专家数据的均值, 在求权重
- // 求专家数据均值
- QMap<QString, double> avgNodeValue;
- for (QString key : nodeData.values().first().keys()) {
- double sum = 0;
- for (QString expertId : nodeData.keys()) {
- double v;
- QStringList nodeValue = nodeData[expertId][key]->nodeValue.split("/");
- if (nodeValue.size() == 1) {
- v = nodeValue[0].toDouble();
- } else {
- v = nodeValue[0].toDouble() / nodeValue[1].toDouble();
- }
- sum += v;
- }
- avgNodeValue[key] = sum / nodeData.keys().size();
- }
- // 求权重
- for (int i = 1; i < mind->levels(); i++) {
- for (CNodeData node : mind->nodesInLevel(i)) {
- QList<CNodeData> subNodes = mind->subNodes(node);
- QVector<qreal> nxnValus; // n x n矩阵
- for (int j = 0; j < subNodes.size(); j++) {
- QString abs = subNodes[j].name;
- for (int k = 0; k < subNodes.size(); k++) {
- QString ord = subNodes[k].name;
- QString key = abs + "-" + ord;
- nxnValus.append(avgNodeValue[key]);
- }
- }
- // 计算权重并存储
- QScopedPointer<HierarchicalAnalysis> ha(new HierarchicalAnalysis(subNodes.size(), nxnValus));
- QVector<qreal> weights = ha->getWeights();
- for (int l = 0; l < weights.size(); ++l) {
- CNodeData pNode = mind->node(subNodes[l].pNumber);
- if (indexWeights.keys().contains(pNode.name)) {
- indexWeights[subNodes[l].name] = indexWeights[pNode.name] * weights[l];
- } else {
- indexWeights[subNodes[l].name] = weights[l];
- }
- }
- }
- }
- }
- QStringList valueList;
- for (QString key : indexWeights.keys()) {
- valueList.append(QString("%1:%2").arg(key).arg(indexWeights[key]));
- }
- QString valueStr = valueList.join(";");
- bool ret = MindWeightService().saveUniqueWeightData(m_process.projectId, m_process.indexType, m_process.dSource,
- algorithm, valueStr);
- qDebug() << __FUNCTION__ << __LINE__ << ret << valueStr << endl;
- return ret;
- }
- bool DataEvaluator::evaluateWeightFromMeasure()
- {
- QMap<QString, QMap<QString, NodeMatrixInfo *>> nodeData;
- bool dataRet = getNodeData(nodeData);
- SchemePlanManager::Algorithm algorithm;
- bool algRet = getAlgorithm(algorithm);
- /// 获取指标体系
- QList<CNodeData> 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<QString, double> allWeights;
- if (algorithm == SchemePlanManager::Entropy) { // 熵值法
- /// 根据指标体系层级, 构造算法需要的数据, 逐层计算权重值并保存
- for (int i = 1; i < mind->levels(); i++) {
- for (CNodeData node : mind->nodesInLevel(i)) {
- QList<CNodeData> subNodes = mind->subNodes(node);
- EntropyMat mat;
- for (int j = 0; j < subNodes.size(); j++) {
- QVector<double> 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<EntropyWeights> ew(new EntropyWeights(mat));
- QVector<double> 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<CNodeData> subNodes = mind->subNodes(node);
- QVector<QVector<double>> mat;
- for (QString uuid : nodeData.keys()) {
- QVector<double> 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> 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<QString, QMap<QString, NodeMatrixInfo *>> &nodeData) const
- {
- /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引
- QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType);
- QList<NodeMatrixInfo *> dataList;
- bool ret = NodeMatrixService().QueryDataByProjectAndIndex(&dataList, indexName, m_process.projectId,
- m_process.dSource);
- if (ret == false) {
- return false;
- }
- for (NodeMatrixInfo *info : dataList) {
- QString key = info->strUuid; // 实测数据的 key
- if (m_process.dSource == SchemePlanManager::FromExpert) {
- key = info->expertId; // 专家数据的 key
- }
- if (nodeData.keys().contains(key) == false) {
- nodeData[key] = QMap<QString, NodeMatrixInfo *>();
- }
- nodeData[key][nodeDataKey(info)] = info;
- }
- return true;
- }
- bool DataEvaluator::getAlgorithm(SchemePlanManager::Algorithm &algorithm) const
- {
- QList<SchemePlanManager::SchemeProcessInfo> 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;
- }
- }
- if (m_process.type == SchemePlanManager::ImportWeightData) {
- if (m_process.dSource == SchemePlanManager::FromMeasurement && algorithm == SchemePlanManager::AHP) {
- algorithm == SchemePlanManager::Entropy;
- }
- if (m_process.dSource == SchemePlanManager::FromExpert) {
- algorithm == SchemePlanManager::AHP;
- }
- }
- return true;
- }
- bool DataEvaluator::getUserConfig(QMap<QString, double> &cfg) const
- {
- QList<UserConfig *> userCfgList;
- bool ret = UserConfigService().QueryUserConfigListInfoByEngineerId(&userCfgList, m_process.projectId);
- if (ret == false) {
- return false;
- }
- for (UserConfig *config : userCfgList) {
- cfg[QString("%1").arg(config->userId)] = config->weight;
- }
- return true;
- }
- QString DataEvaluator::nodeDataKey(NodeMatrixInfo *data) const
- {
- QString key = data->abscissa;
- if (data->ordinate.length() > 0) {
- key += ("-" + data->ordinate);
- }
- return key;
- }
|