tangxu
2025-02-07 03a0d99de9c5fed6bea8bc83b49ce27786bda38c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using IStation.CalcModel;
using IStation.InternalModel;
using IStation.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static IStation.InternalModel.TimeData;
 
namespace IStation.Calc
{
    public partial class ErQuCalcHelper
    {
        
 
 
 
        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="dd_minute"></param>
        /// <returns></returns>
        private List<AnaPrj> CalcOptPrjsCoreCCC( )
        {
            _minTimeCountOpenPump = this.MinOpenPumpMinute / this.CalcSpaceMinute;
            _minTimeCountSwitch = this.MinSwitchPumpMinute / this.CalcSpaceMinute;
 
            
            //int rangeMinIndx = 0;
            //int rangeMaxIndx = TimeDataListCount - 1;
            //sum_Q = new double[5, TimeDataListCount, TimeDataListCount];
            //sum_P = new double[5, TimeDataListCount, TimeDataListCount];
 
            dictCalcCache = new Dictionary<string , RecSolutionBundle>[MaxPumpSwitchCount + 1];
            for (int i = 0; i < dictCalcCache.Length; i++)
            {
                dictCalcCache[i] = new Dictionary<string, RecSolutionBundle>();
            }
            //dict.Clear();
 
            var result = RecursionFuncCCC(this._targetFlowTotal, 0, timeDataListCount-1, MaxPumpSwitchCount);
 
       
            AnaPrj opt_prj = new AnaPrj();
            opt_prj.Items = new List<AnaPrjItem>();
 
            double sumPower = 0, sumFlow = 0, sumMoney = 0;
            foreach (var s in from x in result.Solutions orderby x.StartIndx select x)
            {
                if (s.PumpNum == 0)
                {
                    continue;
                }
                var itemSumDatas = new List<AnaPrjItemSumData>();
                var td = GetTimeDataBundle(s.PumpNum);
 
                for (int i = s.StartIndx; i < s.EndIndx; i++)
                {
                    itemSumDatas.Add(new AnaPrjItemSumData(_timeList[i],
                        td.TimeDatas[i]));
                    sumPower += td.TimeDatas[i].SumPower;
                    sumFlow += td.TimeDatas[i].SumFlow;
                    sumMoney += td.TimeDatas[i].SumMoney;
                }
 
                opt_prj.Items.Add(new AnaPrjItem()
                {
                    StartTime = _timeList[s.StartIndx],
                    EndTime = _timeList[s.EndIndx],
                    OpenPumpIndexs = (from x in td.OpenPumpInfos select x.PumpIndex).ToList(),
                    SumData = itemSumDatas
                });
            }
 
            opt_prj.SumMoney = sumMoney;
            opt_prj.SumFlow = sumFlow;
            opt_prj.SumPower = sumPower;
 
            //string txt = $"计算耗时{(DateTime.Now - dt).TotalSeconds}秒\r\n最少的电费为{result.BestPrice}\r\n计算次数为{TotalCalcCount}\r\n";
            //DateTime lastTime = default(DateTime);
            //foreach (var solution in solutions)
            //{
            //   // txt += $"{getTimebyInt(solution.IS):HH:mm}-{getTimebyInt(solution.I):HH:mm}\t开{solution.PumpNum}台\t流量{solution.Q}\t电费{solution.Price}\r\n";
            //   // lastTime = getTimebyInt(solution.I);
            //}
 
            var opt_prjs = new List<AnaPrj>();
            opt_prjs.Add(opt_prj);
            return opt_prjs;
        }
 
        int TotalCalcCount = 0;
        Dictionary<string , RecSolutionBundle>[] dictCalcCache;
 
        //List<List<double>> prices = new List<List<double>>();
        //List<List<double>> qs = new List<List<double>>();
        //double[,,] sum_Q = null;// new double[5, 288, 288];
        //double[,,] sum_P = null;//new double[5, 288, 288];
        class RecSolutionBundle
        {
            public List<RecSolution> Solutions = new List<RecSolution>();
            public double BestPara;//功率或者电费
            public int PumpSwitchCount;
        }
        class RecSolution
        {
            public int PumpNum;
            public int EndIndx;
            public int StartIndx;
            public double SumFlow;
            public double SumPower;
            public double SumMoney;
            //public Solution(int pumpNum, int startIndx, int endIndx )
            //{
            //    PumpNum = pumpNum;
            //    StartTimeIndx = startIndx;
            //    EndTimeIndx = endIndx;
            //}
                
            public RecSolution(int pumpNum, int startIndx, int endIndx, double sumFlow,double sumPower, double sumMoney)
            {
                PumpNum = pumpNum;
                StartIndx = startIndx;
                EndIndx = endIndx;
                SumFlow = sumFlow;
                SumPower = sumPower;
                SumMoney = sumMoney;
 
            }
        }
        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="TotalFlow"></param>
        /// <param name="startTimeIndx"></param>
        /// <param name="endTimeIndx"></param>
        /// <param name="maxPumpSwitchSize"></param>
        /// <returns></returns>
        RecSolutionBundle RecursionFuncCCC(double TotalFlow, int startTimeIndx, int endTimeIndx, int maxPumpSwitchSize)
        {
            RecSolutionBundle bestPumpStateSolution = new RecSolutionBundle();
 
 
            if (maxPumpSwitchSize < 0)
            {//不能再切换泵了
                TotalCalcCount++;
                bestPumpStateSolution.Solutions.Insert(0, new RecSolution(0, startTimeIndx, endTimeIndx, TotalFlow, double.MaxValue, double.MaxValue)); // 将当前泵和i的选择加入最优解
                bestPumpStateSolution.BestPara = double.MaxValue;
 
                return bestPumpStateSolution;
            }
 
            if (TotalFlow <= 0)
            {//已达到流量
                TotalCalcCount++;
                bestPumpStateSolution.Solutions.Insert(0, 
                    new RecSolution(0, startTimeIndx, endTimeIndx, TotalFlow, 0, 0)); // 将当前泵和i的选择加入最优解
                bestPumpStateSolution.BestPara = 0;
                return bestPumpStateSolution; 
            }
 
            //
            string key = string.Format("{0}.{1}.{2}",startTimeIndx , endTimeIndx, ((int)TotalFlow)/10000);
            if (dictCalcCache[maxPumpSwitchSize].ContainsKey(key))
            {
                TotalCalcCount++;
                return dictCalcCache[maxPumpSwitchSize][key];
            }
 
            //if (dict.ContainsKey(key) )
            //{
            //    TotalCalcCount++;
            //    return dict[key];
            //}
            double bestParas = double.MaxValue; 
            for (int iTime = startTimeIndx + 1; iTime <= endTimeIndx; iTime++)
            {
                for (int pumpNum = 0; pumpNum <= 4; pumpNum++)
                {
                    double sumPower = 0;
                    double sumMoney1 = 0;
                    double sumFlow = 0;////获取从开始时间,到结束时间,开pumpNum台泵的流量
                    if(pumpNum > 0)
                    {//获取从开始时间,到结束时间,开pumpNum台泵的费用
                        var td = GetTimeDataBundle(pumpNum);
                        td.GetSpanData(startTimeIndx, iTime, out sumFlow, out sumPower, out sumMoney1);
                    }
                    double best0 = double.MaxValue;
                    RecSolutionBundle solution = new RecSolutionBundle();
                    if (iTime < endTimeIndx)
                    {
                        solution = RecursionFuncCCC(TotalFlow - sumFlow, iTime, endTimeIndx, maxPumpSwitchSize - 1);
                        best0 = sumPower + solution.BestPara;
                    }
                    else if (TotalFlow - sumFlow <= 0)
                    {
                        best0 = sumPower;
                        solution.Solutions.Insert(0, new RecSolution(0, iTime, endTimeIndx, TotalFlow - sumFlow, 0, 0));
                    }
 
                    if (bestParas > best0)
                    {
                        bestParas = best0;
                        bestPumpStateSolution.Solutions = new List<RecSolution>(solution.Solutions); // 更新最优解
                        bestPumpStateSolution.Solutions.Insert(0, new RecSolution(pumpNum, startTimeIndx, iTime, sumFlow, sumPower, sumMoney1)); // 将当前泵和i的选择加入最优解                       
                    }
                }
            }
 
            bestPumpStateSolution.BestPara = bestParas;
            bestPumpStateSolution.PumpSwitchCount = maxPumpSwitchSize;
 
            dictCalcCache[maxPumpSwitchSize][key] = bestPumpStateSolution;
 
            TotalCalcCount++;
            return bestPumpStateSolution;
        }
 
 
 
 
 
    }
 
 
 
}