瀏覽代碼

修改熵权法

Signed-off-by: codeClown <zhaomengshou@126.com>
codeClown 1 年之前
父節點
當前提交
18470675aa
共有 2 個文件被更改,包括 44 次插入69 次删除
  1. 34 59
      QFD/algorithm/EntropyWeights.cpp
  2. 10 10
      QFD/algorithm/EntropyWeights.h

+ 34 - 59
QFD/algorithm/EntropyWeights.cpp

@@ -1,4 +1,4 @@
-//
+//
 // Created by Austin on 2023/10/10.
 //
 
@@ -7,29 +7,22 @@
 #include <numeric>
 #include <cmath>
 
-EntropyWeights::EntropyWeights(const EntropyMat& mat,
-                               const QVector<bool>& direction)
-    : mat_(mat), direction_(direction), ymin_(0.002), ymax_(0.996)
+EntropyWeights::EntropyWeights(const EntropyMat &mat, const QVector<bool> &direction)
+    : ymin_(0.002), ymax_(0.996), mat_(mat), direction_(direction)
 {
     index_num_ = mat_.count();
-    if (index_num_ == 0)
-    {
+    if (index_num_ == 0) {
         sample_num_ = 0;
-    }
-    else
-    {
+    } else {
         sample_num_ = mat_.at(0).count();
     }
 }
 
 void EntropyWeights::setYMin(double ymin)
 {
-    if (ymin < 0.002)
-    {
+    if (ymin < 0.002) {
         ymin_ = 0.002;
-    }
-    else if (ymin > ymax_)
-    {
+    } else if (ymin > ymax_) {
         return;
     }
 
@@ -38,28 +31,23 @@ void EntropyWeights::setYMin(double ymin)
 
 void EntropyWeights::setYMax(double ymax)
 {
-    if (ymax > 0.996)
-    {
+    if (ymax > 0.996) {
         ymax_ = 0.996;
-    }
-    else if (ymax < ymin_)
-    {
+    } else if (ymax < ymin_) {
         return;
     }
 
     ymax_ = ymax;
 }
 
-void EntropyWeights::getWeights(QVector<double>& weights, QVector<double>& score)
+void EntropyWeights::getWeights(QVector<double> &weights, QVector<double> &score)
 {
     // 计算第j个指标下,第i个样本占该指标的比重p
     EntropyMat pMat;
-    for (int i = 0; i < mat_.count(); i++)
-    {
+    for (int i = 0; i < mat_.count(); i++) {
         pMat.append(QVector<double>(mat_.at(i).count(), 0));
         double sum = std::accumulate(mat_.at(i).begin(), mat_.at(i).end(), 0.0);
-        for (int j = 0; j < mat_.at(i).count(); j++)
-        {
+        for (int j = 0; j < mat_.at(i).count(); j++) {
             pMat[i][j] = mat_[i][j] / sum;
         }
     }
@@ -68,37 +56,31 @@ void EntropyWeights::getWeights(QVector<double>& weights, QVector<double>& score
 
     // 计算第j个指标熵值
     QVector<double> e(mat_.count(), 0);
-    for (int i = 0; i < mat_.count(); i++)
-    {
+    for (int i = 0; i < mat_.count(); i++) {
         //        QVector<double> f(mat_.at(i).count(), 0.0);
         double fSum = 0;
-        for (int j = 0; j < mat_.at(i).count(); j++)
-        {
+        for (int j = 0; j < mat_.at(i).count(); j++) {
             fSum += pMat[i][j] * std::log(pMat[i][j]);
         }
         e[i] = -k * fSum;
     }
-    //qDebug() << e;
+    //    qDebug() << e;
 
     // 计算信息熵冗余度
     QVector<double> d(mat_.count(), 0);
-    for (int i = 0; i < e.count(); i++)
-    {
+    for (int i = 0; i < e.count(); i++) {
         d[i] = 1 - e[i];
     }
 
     // 求权值
     double dSum = std::accumulate(d.begin(), d.end(), 0.0);
-    for (auto v : d)
-    {
+    for (auto v : d) {
         weights.append(v / dSum);
     }
 
-    for (int i = 0; i < sample_num_; ++i)
-    {
+    for (int i = 0; i < sample_num_; ++i) {
         double s = 0;
-        for (int j = 0; j < index_num_; j++)
-        {
+        for (int j = 0; j < index_num_; j++) {
             s += pMat[j][i] * weights[j];
         }
         score.append(s * 100);
@@ -110,51 +92,44 @@ void EntropyWeights::getWeights(QVector<double>& weights, QVector<double>& score
  */
 void EntropyWeights::normalization()
 {
-    for (int i = 0; i < mat_.count(); i++)
-    {
+    for (int i = 0; i < mat_.count(); i++) {
         double minValue = 0;
         double maxValue = 0;
 
         getMinMax(mat_.at(i), minValue, maxValue);
 
-        if (direction_.at(i))
-        {
-            for (int j = 0; j < mat_.at(i).count(); j++)
-            {
+        bool dir = true;
+        if (!direction_.isEmpty()) {
+            dir = direction_.at(i);
+        }
+        if (dir) {
+            for (int j = 0; j < mat_.at(i).count(); j++) {
                 mat_[i][j] = (ymax_ - ymin_) * (mat_.at(i).at(j) - minValue) / (maxValue - minValue) + ymin_;
             }
-        }
-        else
-        {
-            for (int j = 0; j < mat_.at(i).count(); j++)
-            {
+        } else {
+            for (int j = 0; j < mat_.at(i).count(); j++) {
                 mat_[i][j] = (ymax_ - ymin_) * (maxValue - mat_.at(i).at(j)) / (maxValue - minValue) + ymin_;
             }
         }
     }
 }
 
-void EntropyWeights::getMinMax(const QVector<double>& in, double& min, double& max)
+void EntropyWeights::getMinMax(const QVector<double> &in, double &min, double &max)
 {
-    if (in.count() > 0)
-    {
+    if (in.count() > 0) {
         min = max = in.at(0);
 
-        for (int i = 1; i < in.count(); i++)
-        {
-            if (in.at(i) < min)
-            {
+        for (int i = 1; i < in.count(); i++) {
+            if (in.at(i) < min) {
                 min = in.at(i);
-            }
-            else if (in.at(i) > max)
-            {
+            } else if (in.at(i) > max) {
                 max = in.at(i);
             }
         }
     }
 }
 
-void EntropyWeights::compute(QVector<double>& weights, QVector<double>& score)
+void EntropyWeights::compute(QVector<double> &weights, QVector<double> &score)
 {
     normalization();
     qDebug() << mat_;

+ 10 - 10
QFD/algorithm/EntropyWeights.h

@@ -1,4 +1,4 @@
-//
+//
 // Created by Austin on 2023/10/10.
 //
 
@@ -7,7 +7,7 @@
 
 #include <QVector>
 
-typedef QVector<QVector<double> > EntropyMat;
+typedef QVector<QVector<double>> EntropyMat;
 
 class EntropyWeights
 {
@@ -16,26 +16,26 @@ public:
      * @param mat: 输入矩阵,一行代表一个指标, 每列对应一个样本
      * @param direction,定正向指标true,负向指标false
      */
-    EntropyWeights(const EntropyMat& mat, const QVector<bool>& direction);
+    EntropyWeights(const EntropyMat &mat, const QVector<bool> &direction = QVector<bool>());
 
     void setYMin(double ymin);
     void setYMax(double ymax);
 
-    void compute(QVector<double>& weights, QVector<double>& score);
+    void compute(QVector<double> &weights, QVector<double> &score);
 
-    //若归一化到[0,1], 0会出问题
+    // 若归一化到[0,1], 0会出问题
     double ymin_;
     double ymax_;
-    int sample_num_; //样本数
-    int index_num_;  //指标数
+    int sample_num_;  // 样本数
+    int index_num_;   // 指标数
 
 private:
     void normalization();
-    void getMinMax(const QVector<double>& in, double& min, double& max);
-    void getWeights(QVector<double>& weights, QVector<double>& score);
+    void getMinMax(const QVector<double> &in, double &min, double &max);
+    void getWeights(QVector<double> &weights, QVector<double> &score);
 
     EntropyMat mat_;
     QVector<bool> direction_;
 };
 
-#endif //ALGORITHM__ENTROPYWEIGHTS_H_
+#endif  // ALGORITHM__ENTROPYWEIGHTS_H_