Browse Source

调试算法

chengxr 1 year ago
parent
commit
086950e773

+ 1 - 1
QFD/algorithm/EntropyWeights.cpp

@@ -155,7 +155,7 @@ void EntropyWeights::getMinMax(const QVector<double> &in, double &min, double &m
 void EntropyWeights::compute(QVector<double> &weights, QVector<double> &score)
 {
     normalization();
-    qDebug() << mat_;
+    //    qDebug() << mat_;
     getWeights(weights, score);
     //    qDebug() << "mat_" << mat_;
 }

+ 121 - 16
QFD/common/DataEvaluator.cpp

@@ -4,11 +4,15 @@
 #include <SchemePlanManager.h>
 #include <CMind.h>
 
+#include "algorithm/EntropyWeights.h"
+
 #include <dbService/ClassSet.h>
 #include <dbService/NodeMatrixService.h>
 #include <dbService/CNodeDataService.h>
+#include <dbService/SchemeProcessService.h>
 
 #include <QMap>
+#include <QDebug>
 
 DataEvaluator::DataEvaluator(QObject *parent) : QObject(parent) { }
 
@@ -65,28 +69,82 @@ bool DataEvaluator::evaluateWeightFromExpert()
 bool DataEvaluator::evaluateWeightFromMeasure()
 {
     /// 获取权重分析数据
-    QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType);
-    QList<NodeMatrixInfo *> dataList;
-    bool ret = NodeMatrixService().QueryMeaureDataByProjectAndIndex(&dataList, indexName, m_process.projectId);
-    if (ret == false) {
+    /// 整理数据, 使用 uuid 将数据分组, 使用指标名称索引
+
+    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;
     }
 
-    /// 使用 uuid 将数据分组
-    QStringList m_uuidList;
-    QMap<QString, QList<NodeMatrixInfo *>> m_nodeData;
-    for (NodeMatrixInfo *info : dataList) {
-        if (m_uuidList.contains(info->strUuid) == false) {
-            m_uuidList.append(info->strUuid);
-            QList<NodeMatrixInfo *> list;
-            m_nodeData[info->strUuid] = list;
+    /// 各个指标的权重值
+    /// 外层 QString 是 uuid, 内层 QString 是指标名称, double 是指标权重
+    QMap<QString, double> allWeights;
+
+    /// 根据指标体系层级, 构造算法需要的数据
+    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 });
+            }
+
+            //                pdata = { { 4.1 }, { 0.04 }, { 0.19 }, { 0.01 }, { 0.05 } };
+
+            //                pdata = {
+            //                    { 4.1, 3.9 }, { 0.04, 0.08 }, { 0.19, 0.38 }, { 0.01, 0.18 }, { 0.36, 0.19 }, {
+            //                    0.05, 0.04 }
+            //                };
+
+            if (mat.size() <= 0) {
+                break;
+            }
+
+            QScopedPointer<EntropyWeights> ew(new EntropyWeights(mat));
+            QVector<double> weights, scores;
+            ew->compute(weights, scores);
+            qDebug() << __FUNCTION__ << __LINE__ << weights << endl;
+
+            for (int j = 0; j < subNodes.size(); j++) {
+                double w = weights[j];
+                if (std::_Is_nan(w)) {
+                    w = 0;
+                }
+
+                CNodeData pNode = mind->node(subNodes[j].pNumber);
+                if (allWeights.keys().contains(pNode.name)) {
+                    allWeights[subNodes[j].name] = allWeights[pNode.name] * w;
+                } else {
+                    allWeights[subNodes[j].name] = w;
+                }
+            }
         }
-        m_nodeData[info->strUuid].append(info);
     }
 
-    /// 获取指标体系
-
-    /// 根据指标体系层级, 构造算法需要的数据
+    qDebug() << __FUNCTION__ << __LINE__ << allWeights << endl;
 
     return false;
 }
@@ -115,3 +173,50 @@ 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) {
+        if (nodeData.keys().contains(info->strUuid) == false) {
+            nodeData[info->strUuid] = QMap<QString, NodeMatrixInfo *>();
+        }
+        nodeData[info->strUuid][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) {
+            if (process.algorithm == SchemePlanManager::Entropy) {
+                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;
+}

+ 13 - 0
QFD/common/DataEvaluator.h

@@ -5,6 +5,8 @@
 
 #include "SchemePlanManager.h"
 
+class NodeMatrixInfo;
+
 class DataEvaluator : public QObject
 {
     Q_OBJECT
@@ -28,6 +30,8 @@ public:
     bool evaluate();
 
 private:
+    ///  运行算法
+
     bool evaluateWeightFromExpert();   // 权重分析,专家数据
     bool evaluateWeightFromMeasure();  // 权重分析,实测数据
 
@@ -39,6 +43,15 @@ private:
     bool evaluateEfficiencyMEA();  // 效能评估, 物元分析法
     bool evaluateEfficiencyGCE();  // 效能评估, 灰色聚类法
 
+    /// 准备数据
+    bool getNodeData(QMap<QString, QMap<QString, NodeMatrixInfo *>> &nodeData) const;
+
+    /// 获取算法
+    bool getAlgorithm(SchemePlanManager::Algorithm &algorithm) const;
+
+    /// 使用横坐标和纵坐标构造索引
+    QString nodeDataKey(NodeMatrixInfo *data) const;
+
 private:
     SchemePlanManager::SchemeProcessInfo m_process;
 

+ 14 - 2
QFD/dbService/NodeMatrixService.cpp

@@ -566,13 +566,25 @@ bool NodeMatrixService::DeleteNodeMatrixListByExpertId(int expertId)
 }
 
 bool NodeMatrixService::QueryMeaureDataByProjectAndIndex(QList<NodeMatrixInfo *> *dataList, QString index, int projId)
+{
+    return QueryDataByProjectAndIndex(dataList, index, projId, 1);
+}
+
+bool NodeMatrixService::QueryExpertDataByProjectAndIndex(QList<NodeMatrixInfo *> *dataList, QString index, int projId)
+{
+    return QueryDataByProjectAndIndex(dataList, index, projId, 0);
+}
+
+bool NodeMatrixService::QueryDataByProjectAndIndex(QList<NodeMatrixInfo *> *dataList, QString index, int projId,
+                                                   int dataSource)
 {
     QSqlDatabase db = SqlDBHelper::getDatabase();
     QSqlQuery query(db);
     bool ret          = false;
     QString selectSql = QString("select id,expert_name, engineer_id, node, abscissa, ordinate, "
                                 "node_value, expert_id,mind_id,write_date,mark,str_uuid,table_msg from "
-                                "t_node_matrix_info where engineer_id ='%2' and table_msg ='%3'")
+                                "t_node_matrix_info where mind_id = '%1' and engineer_id ='%2' and table_msg ='%3'")
+                                .arg(dataSource)
                                 .arg(QString::number(projId))
                                 .arg(index);
     //    qDebug() << "selectSql=" << selectSql;
@@ -611,7 +623,7 @@ bool NodeMatrixService::UpdateMeasureData(const NodeMatrixInfo &info)
         if (info.ordinate.length() > 0) {
             t.update("t_node_matrix_info")
                     .set("node_value", info.nodeValue)
-                    .where("engineer_id = ? and table_msg = ? and str_uuid = ? and abscissa = ? and "
+                    .where("mind_id = 1 and engineer_id = ? and table_msg = ? and str_uuid = ? and abscissa = ? and "
                            "ordinate = ?",
                            info.engineerId, info.tableMsg, info.strUuid, info.abscissa, info.ordinate);
         } else {

+ 7 - 0
QFD/dbService/NodeMatrixService.h

@@ -61,6 +61,13 @@ public:
     /*根据指标体系类型和工程ID查询实测数据信息*/
     bool QueryMeaureDataByProjectAndIndex(QList<NodeMatrixInfo *> *dataList, QString index, int projId);
 
+    /*根据指标体系类型和工程ID查询专家数据信息*/
+    bool QueryExpertDataByProjectAndIndex(QList<NodeMatrixInfo *> *dataList, QString index, int projId);
+
+    /*根据指标体系类型和工程ID查询专家数据信息*/
+    /* dataSource, 0:专家数据, 1:实测数据*/
+    bool QueryDataByProjectAndIndex(QList<NodeMatrixInfo *> *dataList, QString index, int projId, int dataSource);
+
     /*修改节点值*/
     bool UpdateMeasureData(const NodeMatrixInfo &info);
 };