From 8c68ab4be4bff7981e745cc2b41c7bba5257a2d3 Mon Sep 17 00:00:00 2001
From: ningshuxia <ningshuxia0927@outlook.com>
Date: 星期三, 16 四月 2025 20:37:38 +0800
Subject: [PATCH] 流量偏差 辅助类

---
 01-api/Application/IStation.Application/01-open-api/01-schedule/Schedule_Controller.cs |   17 ++-
 01-api/Service/IStation.Service/paras_schedule_settings.json                           |    3 
 01-api/Service/IStation.Service/07-helper/02-schedule/ScheduleHelper.cs                |   14 ++-
 01-api/_Expand/IStation.Win.Schedule/bjMain.cs                                         |   89 +++++++++++----------
 02-desktop/Desktop/IStation.Test/Program.cs                                            |    4 
 01-api/Service/IStation.Service/05-service/00-basic/FlowDeviation.cs                   |   59 ++++++++++++++
 01-api/Service/IStation.Service/03-settings/paras/Paras_LocalFile.cs                   |    8 +
 7 files changed, 137 insertions(+), 57 deletions(-)

diff --git a/01-api/Application/IStation.Application/01-open-api/01-schedule/Schedule_Controller.cs b/01-api/Application/IStation.Application/01-open-api/01-schedule/Schedule_Controller.cs
index f88e17d..e81e700 100644
--- a/01-api/Application/IStation.Application/01-open-api/01-schedule/Schedule_Controller.cs
+++ b/01-api/Application/IStation.Application/01-open-api/01-schedule/Schedule_Controller.cs
@@ -18,6 +18,7 @@
         private static readonly Service.ScheduleConfig _service_schedule_config = new();
 
         private static readonly Service.AnalysisDeviation _service_analysis_deviation = new();
+        private static readonly Service.FlowDeviation _service_flow_deviation = new();
 
         /// <summary>
         /// 璁$畻
@@ -56,7 +57,8 @@
             var is_monitor_record_debug = false;
 #if DEBUG
             is_monitor_record_debug = true;
-#endif
+#endif      
+
 
             log_title = "瀹炴椂ZyScada璇锋眰";
             var get_msg = GlobalHelper.GetMonitorRecordList(receipt_time, out List<Model.MonitorRecord> monitor_record_list, is_monitor_record_debug);
@@ -104,17 +106,20 @@
             }
 
 
-            log_title = "璋冨害杩斿洖";
-            var msg = "璁$畻缁撴潫!";
-            var helper = new Service.ScheduleHelper();
+            log_title = "娴侀噺琛ュ樊";
 
+            var helper = new Service.ScheduleHelper(); 
             helper.Initial(station1_open_flag_list, station1_schedule_config, analysis_deviation_list);
             var optimal_combine1 = helper.GetOptAnaCombine(station1, station1_flag_inlet_water_level_dict, target_flow1, target_pressure1);
 
 
-            helper.Initial(station2_open_flag_list, station2_schedule_config, analysis_deviation_list);
+            var station2_target_flow_diff=_service_flow_deviation.GetFlowDiff(target_pressure2);
+            ScheduleLog.Info(request_id, log_title, $"2杈撴按:{station2_target_flow_diff:N1}");
+            helper.Initial(station2_open_flag_list, station2_schedule_config, analysis_deviation_list,station2_target_flow_diff);
             var optimal_combine2 = helper.GetOptAnaCombine(station2, station2_flag_inlet_water_level_dict, target_flow2, target_pressure2);
-
+            
+            log_title = "璋冨害杩斿洖";
+            var msg = "璁$畻缁撴潫!";
             var schedule_request = new Model.ScheduleRequest
             {
                 ID = request_id,
diff --git a/01-api/Service/IStation.Service/03-settings/paras/Paras_LocalFile.cs b/01-api/Service/IStation.Service/03-settings/paras/Paras_LocalFile.cs
index 134e135..13d50f5 100644
--- a/01-api/Service/IStation.Service/03-settings/paras/Paras_LocalFile.cs
+++ b/01-api/Service/IStation.Service/03-settings/paras/Paras_LocalFile.cs
@@ -68,6 +68,12 @@
         /// 鍒嗘瀽鍋忓樊鏂囦欢
         /// </summary>
         public string AnalysisDeviationFile { get; set; } = "AnalysisDeviation.json";
-        
+         
+        /// <summary>
+        /// 娴侀噺鍋忓樊鏂囦欢
+        /// </summary>
+        public string FlowDeviationFile { get; set; } = "FlowDeviation.csv";
+
+
     }
 }
diff --git a/01-api/Service/IStation.Service/05-service/00-basic/FlowDeviation.cs b/01-api/Service/IStation.Service/05-service/00-basic/FlowDeviation.cs
new file mode 100644
index 0000000..c2b2c7d
--- /dev/null
+++ b/01-api/Service/IStation.Service/05-service/00-basic/FlowDeviation.cs
@@ -0,0 +1,59 @@
+锘縩amespace IStation.Service
+{
+    /// <summary>
+    /// 
+    /// </summary>
+    public partial class FlowDeviation
+    {
+        private readonly string _filePath = Path.Combine(
+                     Settings.ParasHelper.LocalFile.DataFolderDirectory,
+                     Settings.ParasHelper.LocalFile.FlowDeviationFile);
+        
+
+        private Curve.CurveExpress _flowDiffCurve = null;
+        private bool _isCurveLoaded = false;
+
+        /// <summary>
+        /// 鑾峰彇
+        /// </summary>
+        public double GetFlowDiff(double pressure)
+        {
+            if (_flowDiffCurve == null)
+            {
+                if (_isCurveLoaded)
+                    return 0;
+                _isCurveLoaded = true;
+                if (!File.Exists(_filePath))
+                    return default;
+                var ptList = new List<Curve.CurvePoint>();
+                using StreamReader reader = new StreamReader(_filePath);
+                reader.ReadLine();
+                string line;
+                while ((line = reader.ReadLine()) != null)
+                {
+
+                    string[] values = line.Split(',');
+                    var xStr = values[0];
+                    var yStr = values[1];
+
+                    var pX = double.Parse(xStr);
+                    var pY = double.Parse(yStr);
+
+                    pY = Math.Abs(pY);
+                    ptList.Add(new CurvePoint(pX, pY));
+                }
+                if (ptList.Any())
+                {
+                    _flowDiffCurve = Curve.FitHelper.BuildCurveExpress(ptList);
+                }
+
+            }
+
+            var flowDiff = _flowDiffCurve.GetFitPointY(pressure);
+            return flowDiff;
+        }
+
+         
+
+    }
+}
\ No newline at end of file
diff --git a/01-api/Service/IStation.Service/07-helper/02-schedule/ScheduleHelper.cs b/01-api/Service/IStation.Service/07-helper/02-schedule/ScheduleHelper.cs
index 1578b1a..5f6c9cc 100644
--- a/01-api/Service/IStation.Service/07-helper/02-schedule/ScheduleHelper.cs
+++ b/01-api/Service/IStation.Service/07-helper/02-schedule/ScheduleHelper.cs
@@ -25,12 +25,14 @@
         private readonly decimal _frequency_max = 50;
         private readonly decimal _frequency_space = 1;//棰戠巼闂撮殧 
 
-        private readonly double _start_stop_loss_coefficient = 0.95;//娉靛惎鍋滄崯澶辩郴鏁�
+        private readonly double _start_stop_loss_coefficient = 0.9;//娉靛惎鍋滄崯澶辩郴鏁�
 
         private double _sel_opt_flow_excess = 1;//鍙�夋柟妗堢殑娴侀噺浣欓噺
         private readonly double _sel_opt_pump_pressure_excess = 0;//鍙�夋柟妗堢殑鍗曟车鎵▼榛樿鎶崌浣欓噺
+        private double _station_target_flow_diff = 0; // 绔欑偣鐩爣娴侀噺宸��
 
-        private double _sel_opt_flow_deviation_ratio = 0.05;//鍙�夋柟妗堢殑娴侀噺鍋忓樊姣�
+
+        private readonly double _sel_opt_flow_deviation_ratio = 0.05;//鍙�夋柟妗堢殑娴侀噺鍋忓樊姣�
         private readonly double _sel_opt_reasonable_flow_deviation_ratio = 0.005;//鍚堢悊鐨勬柟妗堢殑娴侀噺鍋忓樊姣�
 
         private readonly Service.AnalysisParameter _service_analysis_parameter = new(); 
@@ -51,12 +53,14 @@
 
         private List<Model.AnalysisDeviation> _analysis_deviation_list = null;//鍒嗘瀽鍋忓樊
 
+
+
         #endregion
 
         /// <summary>
         /// 鍒濆鍖�
         /// </summary>
-        public void Initial(List<int> current_open_flag_list, Model.ScheduleConfig schedule_config, List<Model.AnalysisDeviation> analysis_deviation_list)
+        public void Initial(List<int> current_open_flag_list, Model.ScheduleConfig schedule_config, List<Model.AnalysisDeviation> analysis_deviation_list, double station_target_flow_diff = 0)
         {
             _current_open_flag_list = current_open_flag_list;
             _min_open_count = 1;
@@ -70,6 +74,7 @@
             _frequency_limit_list = null;
             _flag_cumulative_runtime_dict = null;
             _priority_open_flag_list = null;
+            _station_target_flow_diff = 0;
             if (schedule_config != null)
             {
                 _min_open_count = schedule_config.MinOpenCount;
@@ -86,7 +91,7 @@
             }
 
             _analysis_deviation_list = analysis_deviation_list;
-
+            _station_target_flow_diff = station_target_flow_diff;
         }
 
 
@@ -109,6 +114,7 @@
             )
         {
 
+            target_flow+=_station_target_flow_diff;
             #region 鍒濆鍖栧弬鏁� 
             if (pump_list == null || !pump_list.Any())
             {
diff --git a/01-api/Service/IStation.Service/paras_schedule_settings.json b/01-api/Service/IStation.Service/paras_schedule_settings.json
index 754fc56..026be9b 100644
--- a/01-api/Service/IStation.Service/paras_schedule_settings.json
+++ b/01-api/Service/IStation.Service/paras_schedule_settings.json
@@ -12,7 +12,8 @@
     "ScheduleConfigFile": "ScheduleConfig.json",
     "HydraulicModelFile": "HydraulicModel.inp",
     "AnalysisFactorFile": "AnalysisFactor.json",
-    "AnalysisDeviationFile": "AnalysisDeviation.json"
+    "AnalysisDeviationFile": "AnalysisDeviation.json",
+    "FlowDeviationFile": "FlowDeviation.csv"
   },
   "DataBase": {
     "DbType": "SQLite",
diff --git a/01-api/_Expand/IStation.Win.Schedule/bjMain.cs b/01-api/_Expand/IStation.Win.Schedule/bjMain.cs
index 8f2144d..3fda114 100644
--- a/01-api/_Expand/IStation.Win.Schedule/bjMain.cs
+++ b/01-api/_Expand/IStation.Win.Schedule/bjMain.cs
@@ -83,8 +83,8 @@
             {
                 return;
             }
-            var deline = new DateTime(2024,12,1);
-            var deline2 = new DateTime(2025,1,1);
+            var deline = new DateTime(2025,2,12);
+            var deline2 = new DateTime(2025,5,1);
             var time_list = _schedule_request_list.Where(x=>x.ReceptionTime>=deline&&x.ReceptionTime<= deline2).Select(x => x.ReceptionTime).OrderBy(x => x).ToList();
   
             foreach (var item in time_list)
@@ -157,57 +157,60 @@
                     }
                 }
 
+                double station2_target_flow_diff = 0;
                 if (this.barCekCalcFlowDiff.Checked)
                 {
-                    var ptList = new List<CurvePoint>();
-                    try
-                    {
-                        var filePath = @"D:\WorkCode\Project\ChDt1\Schedule.Ch.V1.0\02-desktop\Desktop\IStation.Test\bin\Debug\net6.0-windows\stationcsv\characteristic_curve.csv";
-                        using (StreamReader reader = new StreamReader(filePath))
-                        {
-                            reader.ReadLine();
-                            string line;
-                            while ((line = reader.ReadLine()) != null)
-                            {
-                                // 鎸夐�楀彿鍒嗛殧姣忎竴琛�
-                                string[] values = line.Split(',');
-                                var xStr = values[0];
-                                var yStr = values[1];
+                    station2_target_flow_diff = new Service.FlowDeviation().GetFlowDiff(target_pressure2);
+                    AlertTool.ShowInfo(this, "棰勬祴娴侀噺缂哄け", $"{station2_target_flow_diff:N1}");
+                    //var ptList = new List<CurvePoint>();
+                    //try
+                    //{
+                    //    var filePath = @"D:\WorkCode\Project\ChDt1\Schedule.Ch.V1.0\02-desktop\Desktop\IStation.Test\bin\Debug\net6.0-windows\stationcsv\characteristic_curve.csv";
+                    //    using (StreamReader reader = new StreamReader(filePath))
+                    //    {
+                    //        reader.ReadLine();
+                    //        string line;
+                    //        while ((line = reader.ReadLine()) != null)
+                    //        {
+                    //            // 鎸夐�楀彿鍒嗛殧姣忎竴琛�
+                    //            string[] values = line.Split(',');
+                    //            var xStr = values[0];
+                    //            var yStr = values[1];
 
-                                var pX = double.Parse(xStr);
-                                var pY = double.Parse(yStr);
+                    //            var pX = double.Parse(xStr);
+                    //            var pY = double.Parse(yStr);
 
-                                pY = Math.Abs(pY);
-                                ptList.Add(new CurvePoint(pX, pY));
+                    //            pY = Math.Abs(pY);
+                    //            ptList.Add(new CurvePoint(pX, pY));
 
-                            }
-                        }
-                    }
-                    catch (Exception ex)
-                    {
-                        Console.WriteLine("璇诲彇鏂囦欢鏃跺嚭閿�: " + ex.Message);
-                    }
+                    //        }
+                    //    }
+                    //}
+                    //catch (Exception ex)
+                    //{
+                    //    Console.WriteLine("璇诲彇鏂囦欢鏃跺嚭閿�: " + ex.Message);
+                    //}
 
-                    var x = ptList.Select(x => x.X).ToArray();
-                    var y = ptList.Select(x => x.Y).ToArray();
+                    //var x = ptList.Select(x => x.X).ToArray();
+                    //var y = ptList.Select(x => x.Y).ToArray();
 
-                    // 澶氶」寮忓洖褰掔殑闃舵暟锛堜緥濡傦紝浜屾澶氶」寮忥級
-                    int degree = 3;
+                    //// 澶氶」寮忓洖褰掔殑闃舵暟锛堜緥濡傦紝浜屾澶氶」寮忥級
+                    //int degree = 3;
 
-                    // 鎷熷悎澶氶」寮忓洖褰掓ā鍨�
-                    double[] coefficients = NonlinearRegressionHelper.FitPolynomial(x, y, degree);
+                    //// 鎷熷悎澶氶」寮忓洖褰掓ā鍨�
+                    //double[] coefficients = NonlinearRegressionHelper.FitPolynomial(x, y, degree);
 
-                    // 浣跨敤妯″瀷杩涜棰勬祴
-                    double xNew = target_pressure2;
-                    double yPredicted = NonlinearRegressionHelper.PredictPolynomial(xNew, coefficients);
+                    //// 浣跨敤妯″瀷杩涜棰勬祴
+                    //double xNew = target_pressure2;
+                    //double yPredicted = NonlinearRegressionHelper.PredictPolynomial(xNew, coefficients);
 
-                    target_flow2 += yPredicted;
-                    AlertTool.ShowInfo(this, "棰勬祴娴侀噺缂哄け", $"{yPredicted:N1}");
-                  
+                    //station2_target_flow_diff = yPredicted;
+                    //AlertTool.ShowInfo(this, "棰勬祴娴侀噺缂哄け", $"{yPredicted:N1}");
 
-                    // 璁$畻 R虏 鍜� MSE
-                    double rSquared = NonlinearRegressionHelper.CalculateRSquared(x, y, coefficients);
-                    double mse = NonlinearRegressionHelper.CalculateMSE(x, y, coefficients);
+
+                    //// 璁$畻 R虏 鍜� MSE
+                    //double rSquared = NonlinearRegressionHelper.CalculateRSquared(x, y, coefficients);
+                    //double mse = NonlinearRegressionHelper.CalculateMSE(x, y, coefficients);
 
                 }
 
@@ -221,7 +224,7 @@
                 helper.Initial(station1_open_flag_list, station1_schedule_config, analysis_deviation_list);
                 var optimal_combine1 = helper.GetOptAnaCombine(station1, station1_flag_inlet_water_level_dict, target_flow1, target_pressure1);
 
-                helper.Initial(station2_open_flag_list, station2_schedule_config, analysis_deviation_list);
+                helper.Initial(station2_open_flag_list, station2_schedule_config, analysis_deviation_list, station2_target_flow_diff);
                 var optimal_combine2 = helper.GetOptAnaCombine(station2, station2_flag_inlet_water_level_dict, target_flow2, target_pressure2);
 
                 conclusion_list = new List<Model.ScheduleConclusion>();
diff --git a/02-desktop/Desktop/IStation.Test/Program.cs b/02-desktop/Desktop/IStation.Test/Program.cs
index 3f8a0a0..f707e4e 100644
--- a/02-desktop/Desktop/IStation.Test/Program.cs
+++ b/02-desktop/Desktop/IStation.Test/Program.cs
@@ -14,7 +14,7 @@
         {
             //Station1Helper.Start();  //1 鏁版嵁淇
             //Station2Helper.Start();  //2 妯″瀷淇
-            // 3  python淇
+            // 3  python淇 ,鐩墠鏈変簺鏁版嵁闂杩橀渶瑕佽В鍐�
             //Completion();            //4 淇鍚庣浉浼兼崲绠椾慨姝�
 
             //StationCombineHelper.Start(1);// 5 缁勫悎鍋忓樊淇
@@ -22,7 +22,7 @@
 
             // 6 鍒嗘瀽娉甸璋辩郴鏁�
             // 7 
-            Station2TotalFlowDiffHelper.Start();// 8 琛ュ叏娴侀噺鍋忓樊
+           // Station2TotalFlowDiffHelper.Start();// 8 琛ュ叏娴侀噺鍋忓樊 + python
 
             Console.WriteLine();
             Console.WriteLine("ok");

--
Gitblit v1.9.3