lixiaojun
2022-11-23 b5c20e5143555a1bdd450a1e660216b90c93b6f1
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
using Accord;
using Accord.Genetic; 
using Optimization1D;
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IStation
{
    public class EpanetHelper
    {  
        EPAFunctionGUI epaFunction;
 
        const float MAX_ENERGE = 99999999999;
 
 
        /// <summary>
        /// err问题 
        /// </summary>
        private string ReportQuestion(int err)
        {
            ErrorDefine errorDefine = new ErrorDefine();
            #region [秦白云飞修订]2021-4-30
            //新增目的:查看报错问题
            //新增
 
#if DEBUG
            throw (new Exception(errorDefine.getErrorTxt(err)));
#endif
            #endregion [秦白云飞修订]2021-4-30
 
            return errorDefine.getErrorTxt(err);
        }
 
        #region const_mode
        public const int EN_ELEVATION = 0;  //  { Mode parameters }
        public const int EN_BASEDEMAND = 1;
        public const int EN_PATTERN = 2;
        public const int EN_EMITTER = 3;
        public const int EN_INITQUAL = 4;
        public const int EN_SOURCEQUAL = 5;
        public const int EN_SOURCEPAT = 6;
        public const int EN_SOURCETYPE = 7;
        public const int EN_TANKLEVEL = 8;
        public const int EN_DEMAND = 9;
        public const int EN_HEAD = 10;
        public const int EN_PRESSURE = 11;
        public const int EN_QUALITY = 12;
        public const int EN_SOURCEMASS = 13;
        public const int EN_INITVOLUME = 14;
        public const int EN_MIXMODEL = 15;
        public const int EN_MIXZONEVOL = 16;
        #endregion
 
        #region type_model
        enum Type_EPAObject
        {
            Node = 0,
            Link = 1,
            Pump = 2,
            Tank = 3
        }
        #endregion
 
        #region other
        Dictionary<ModelInput.eType, Type_EPAObject> Typelist = new Dictionary<ModelInput.eType, Type_EPAObject>() { { ModelInput.eType.level, Type_EPAObject.Tank }, { ModelInput.eType.flow, Type_EPAObject.Node }, { ModelInput.eType.press_out, Type_EPAObject.Tank }, { ModelInput.eType.pump_run, Type_EPAObject.Pump }, { ModelInput.eType.press_in, Type_EPAObject.Tank }, { ModelInput.eType.valve_initstatus, Type_EPAObject.Link } };
 
        List<ModelOutput.eType> const_list_nodeType = new List<ModelOutput.eType> { ModelOutput.eType.EN_JUNCTION, ModelOutput.eType.EN_RESERVOIR, ModelOutput.eType.EN_TANK };
        List<ModelOutput.eType> const_list_linkType = new List<ModelOutput.eType> { ModelOutput.eType.EN_CVPIPE, ModelOutput.eType.EN_PIPE, ModelOutput.eType.EN_PUMP, ModelOutput.eType.EN_PRV, ModelOutput.eType.EN_PSV, ModelOutput.eType.EN_FCV, ModelOutput.eType.EN_TCV, ModelOutput.eType.EN_GPV };
 
        enum Method
        {
            Count = 0,
            Sum = 1,
            Avg = 2
        }
 
        string GetTypeValue(int typeCode, int ObjType, Method method, out float Value, int valueCode = -1)
        {
            Value = -1;
            List<float> listValue = new List<float>();
            float result = 0;
            int err = 0;
            if (ObjType == 0)
            {
                int NLink1 = 0;
                err = EpanetBase.ENgetcount(EpanetBase.Const_class.Const_Component.EN_NODECOUNT, ref NLink1);
                if (err > 9) return ReportQuestion(err);
                for (int i = 1; i < NLink1; i++)
                {
                    int type = -1;
                    err = EpanetBase.ENgetnodetype(i, ref type);
                    if (err > 9) return ReportQuestion(err);
                    if (type == typeCode)
                    {
                        if (method == Method.Count)
                            result++;
                        else
                        {
                            float value = -1;
                            err = EpanetBase.ENgetnodevalue(i, valueCode, ref value);
                            if (err > 9) return ReportQuestion(err);
                            listValue.Add(value);
                        }
                    }
                }
            }
            else
            {
                int NLink1 = 0; //int pump_count = 0;
                err = EpanetBase.ENgetcount(EpanetBase.Const_class.Const_Component.EN_LINKCOUNT, ref NLink1);
                if (err > 9) return ReportQuestion(err);
                for (int i = 1; i < NLink1; i++)
                {
                    int type = -1;
                    err = EpanetBase.ENgetlinktype(i, ref type);
                    if (err > 9) return ReportQuestion(err);
 
                    if (type == typeCode)
                    {
                        if (method == Method.Count)
                            result++;
                        else
                        {
                            float value = -1;
                            err = EpanetBase.ENgetlinkvalue(i, valueCode, ref value);
                            if (err > 9) return ReportQuestion(err);
                            listValue.Add(value);
                        }
                    }
                }
            }
            if (method == Method.Sum)
            {
                result = 0;
                for (int i = 0; i < listValue.Count; i++)
                    result += listValue[i];
            }
            else if (method == Method.Avg)
            {
                result = 0;
                for (int i = 0; i < listValue.Count; i++)
                    result += listValue[i];
                result /= listValue.Count;
            }
            Value = result;
 
            return null;
        }
        string SetPumpValue(List<bool> isfreq, float value, List<string> Combine)
        {
            int err = 0;
            if (value != -1)
                for (int i = 0; i < Combine.Count; i++)
                {
 
                    if (!isfreq[i]) //工频泵
                    {
                        if (value > 0)
                        {
                            err = SetItemValue(Combine[i], 1, Type_EPAObject.Pump);
                            if (err > 9) return ReportQuestion(err);
                            return null;
 
 
                        }
                        else
                        {
                            err = SetItemValue(Combine[i], 0, Type_EPAObject.Pump);
                            if (err > 9) return ReportQuestion(err);
                            return null;
                        }
                    }
                    err = SetItemValue(Combine[i], value / 50.0, Type_EPAObject.Pump);
                    if (err > 9) return ReportQuestion(err);
                }
 
            return null;
        }
        string SetPumpValue(List<bool> isfreq, double[] value, List<string> Combine)
        {
            int err = 0;
            for (int i = 0; i < Combine.Count; i++)
            {
 
                if (!isfreq[i]) //工频泵
                {
                    if (value[i] > 0)
                    {
                        err = SetItemValue(Combine[i], 1, Type_EPAObject.Pump);
                        if (err > 9) return ReportQuestion(err);
                        return null;
 
 
                    }
                    else
                    {
                        err = SetItemValue(Combine[i], 0, Type_EPAObject.Pump);
                        if (err > 9) return ReportQuestion(err);
                        return null;
                    }
                }
                err = SetItemValue(Combine[i], value[i] / 50.0, Type_EPAObject.Pump);
                if (err > 9) return ReportQuestion(err);
            }
 
            return null;
        }
        int SetTypeValue(List<ModelInput> InputData, ModelInput.eType type, float value = -1, int Num = 0)
        {
            int err = 0;
            var InputData_type = (InputData as List<ModelInput>).FindAll(m => (m.Type == type));
            if (value == -1)
                for (int i = 0; i < InputData_type.Count; i++)
                {
                    err = SetItemValue(InputData_type[i].ObjectID, InputData_type[i].Value, Typelist[type]);
                    if (err > 9) return err;
                }
            if (value != -1)
                for (int i = 1; i <= Num; i++)
                {
                    err = SetItemValue(type + "_" + i.ToString(), value, Typelist[type]);
                    if (err > 9) return err;
                }
            return err;
        }
        int SetItemValue(string ID, dynamic value, Type_EPAObject type_Obj)
        {
            int Index = -1;
            int err = 0;
            switch (type_Obj)
            {
                case (Type_EPAObject.Tank):
 
                    err = EpanetBase.ENgetnodeindex(ID, ref Index);
                    err = EpanetBase.ENsetnodevalue(Index, EN_TANKLEVEL, value);  //源头压力基数
                    break;
                case (Type_EPAObject.Node):
                    err = EpanetBase.ENgetnodeindex(ID, ref Index);
                    err = EpanetBase.ENsetnodevalue(Index, EN_BASEDEMAND, value);  //源头压力基数
                    break;
                case (Type_EPAObject.Pump):
                    float speed = (float.Parse(value.ToString()));
                    if (speed < 0.1) speed = 0;
                    if (speed > 0)
                        err = EpanetBase.ENsetlinkvalue(Index, 4, 1);
                    else
                        err = EpanetBase.ENsetlinkvalue(Index, 4, 0);
                    //float speed = float.Parse(value.ToString());
                    err = EpanetBase.ENgetlinkindex(ID, ref Index);
                    //err = EpanetBase.ENsetlinkvalue(Index, 5, speed);
                    err = EpanetBase.ENsetinistatus(ID, speed.ToString());
                    //err = EpanetBase.ENsaveinpfile("C:\\EPA\\修改后" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".inp");
                    if (err > 9) return err;
 
 
                    break;
                case (Type_EPAObject.Link):
                    err = EpanetBase.ENgetlinkindex(ID, ref Index);
                    err = EpanetBase.ENsetlinkvalue(Index, EpanetBase.Const_class.Const_Link.EN_INITSTATUS, value);
                    break;
                default:
                    err = -1;
                    break;
            }
 
            return err;
 
 
 
 
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int GetOutputString(out List<ModelOutputNode> ListOutputNode, out List<ModelOutputLink> ListOutputLink)
        {
            int err = 0;
            //string result = null;
            ListOutputNode = new List<ModelOutputNode>();
            ListOutputLink = new List<ModelOutputLink>();
            int Njunction = 0;
            err = EpanetBase.ENgetcount(EpanetBase.Const_class.Const_Component.EN_NODECOUNT, ref Njunction);
            if (err > 9) return err;
 
            int NLink = 0;
            err = EpanetBase.ENgetcount(EpanetBase.Const_class.Const_Component.EN_LINKCOUNT, ref NLink);
            if (err > 9) return err;
            List<float> output = new List<float>() { Njunction, NLink };
 
            for (int i = 1; i <= Njunction; i++)
            {
                ModelOutputNode mOutput = new ModelOutputNode();
                StringBuilder ID = new StringBuilder();
 
                float Value = 0; int type = -1;
                err = EpanetBase.ENgetnodeid(i, ID);
                if (err > 9) return err;
 
                mOutput.ObjectID = ID.ToString();
                err = EpanetBase.ENgetnodetype(i, ref type);
                if (err > 9) return err;
                mOutput.Type = (ModelOutputNode.eType)type;
                var list_ValueIndex = mOutput.Get_list_ValueIndex();
                for (int j = 0; j < list_ValueIndex.Length; j++)
                {
 
                    err = EpanetBase.ENgetnodevalue(i, list_ValueIndex[j], ref Value);  //源头压力基数
                    if (err > 9) return err;
                    //if (Value <= 0.0001) Value = 0;
                    mOutput.SetValue(j, Value);
 
 
                }
                ListOutputNode.Add(mOutput);
            }
            for (int i = 1; i <= NLink; i++)
            {
                ModelOutputLink mOutput = new ModelOutputLink();
                StringBuilder idBuilder = new StringBuilder();
                //mOutput.Value = new List<float>();
                float Value = 0; int type = -1;
                err = EpanetBase.ENgetlinkid(i, idBuilder);  //源头压力基数
                if (err > 9) return err;
                mOutput.ObjectID = idBuilder.ToString();
                err = EpanetBase.ENgetlinktype(i, ref type);
                if (err > 9) return err;
                mOutput.Type = (ModelOutputLink.eType)(type + 10);
                var list_ValueIndex = mOutput.Get_list_ValueIndex();
                for (int j = 0; j < list_ValueIndex.Length; j++)
                {
                    err = EpanetBase.ENgetlinkvalue(i, list_ValueIndex[j], ref Value);  //源头压力基数
                    if (err > 9) return err;
                    //if (Value <= 0.0001) Value = 0;
                    mOutput.SetValue(j, Value);
                }
                ListOutputLink.Add(mOutput);
            }
            return err;
        }
        private string SetPumpStatus()
        {
            int NLink = 0; int err = 0;
            err = EpanetBase.ENgetcount(EpanetBase.Const_class.Const_Component.EN_LINKCOUNT, ref NLink);
            if (err > 9) return ReportQuestion(err);
 
 
            for (int i = 1; i <= NLink; i++)
            {
                //ModelOutputLink mOutput = new ModelOutputLink();
                //StringBuilder idBuilder = new StringBuilder();
                ////mOutput.Value = new List<float>();
                //float Value = 0; 
                //err = EpanetBase.ENgetlinkid(i, idBuilder);  //源头压力基数
                //if (err > 9) return ReportQuestion(err);
                //mOutput.ObjectID = idBuilder.ToString();
                int type = -1;
                err = EpanetBase.ENgetlinktype(i, ref type);
                if (err > 9) return ReportQuestion(err);
                if (type == EpanetBase.Const_class.Const_Link_types.EN_PUMP)
                {
 
                    err = EpanetBase.ENsetlinkvalue(i, EpanetBase.Const_class.Const_Link.EN_INITSTATUS, 0);
 
                }
 
            }
            return null;
        }
        #endregion
 
 
        private bool _isInitialSuccess = false;
 
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public string Initial(string file)
        {
 
            if (!System.IO.File.Exists(file))
                return "inp 不存在";
            var err = EpanetBase.ENopen(file, "0.rpt", "0.bin");
            if (err == 0)
            {
                _isInitialSuccess = true;
                return null;
            }
            return ReportQuestion(err);
        }
 
        public string GetResultByFlowAndPumpStatus(List<ModelInput> InputData, out List<ModelOutputNode> modelOutputNodes, out List<ModelOutputLink> modelOutputLinks)
        {
            #region 初始判断
 
            modelOutputLinks = null;
            modelOutputNodes = null;
            int err = 0;
            //string result = null;
            if (!_isInitialSuccess)
                return "初始化不成功";
            if (InputData == null)
                return "输入参数为空";
            #endregion
            #region 传参计算
            err = SetTypeValue(InputData, ModelInput.eType.press_in);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.flow);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.pump_run);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.level);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.valve_initstatus);
            if (err > 9) return ReportQuestion(err);
 
            err = EpanetBase.ENsolveH();
            if (err > 9) return ReportQuestion(err);
            #endregion
          /*  var path = System.IO.Directory.GetCurrentDirectory() + @"\model\out.inp";
            err = EpanetBase.ENsaveinpfile(path);
            if (err > 9) return ReportQuestion(err);*/
            #region 返回结果
            err = GetOutputString(out modelOutputNodes, out modelOutputLinks);
            if (err > 9) return ReportQuestion(err);
            return null;
            #endregion 
 
        }
 
        public string GetResultByPressAndPumpStatus(List<ModelInput> InputData, out List<ModelOutputNode> modelOutputNodes, out List<ModelOutputLink> modelOutputLinks)
        {
 
 
            #region 初始判断
 
            modelOutputLinks = null;
            modelOutputNodes = null;
            //string result = null;
            int err = 0;
            if (!_isInitialSuccess)
                return "初始化不成功";
            if (InputData == null)
                return "输入参数为空";
            #endregion
 
            #region 传参计算
            err = SetTypeValue(InputData, ModelInput.eType.press_in);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.press_out);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.pump_run);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.level);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.valve_initstatus);
            if (err > 9) return ReportQuestion(err);
 
            err = EpanetBase.ENsolveH();
            if (err > 9) return ReportQuestion(err);
            #endregion
           /* var path = System.IO.Directory.GetCurrentDirectory() + @"\model\out.inp";
            err = EpanetBase.ENsaveinpfile(path);
            if (err > 9) return ReportQuestion(err);*/
            #region 返回结果
            err = GetOutputString(out modelOutputNodes, out modelOutputLinks);
            if (err > 9) return ReportQuestion(err);
            return null;
            #endregion
        }
 
        /// <summary>
        /// 获取水泵转速比组合
        /// </summary>
        /// <param name="InputData"></param>
        /// <param name="List_Combine"></param>
        /// <param name="List_modelOutputNodes"></param>
        /// <param name="List_modelOutputLinks"></param>
        /// <param name="opt_combines"></param>
        /// <param name="opt_energes"></param>
        /// <param name="populationSize"></param>
        /// <param name="iterations"></param>
        /// <param name="range"></param>
        /// <param name="accuracy"></param>
        /// <returns></returns>
        public string GetResultByFlowAndPress(List<ModelInput> InputData, List<List<string>> List_Combine,
            out List<List<ModelOutputNode>> List_modelOutputNodes, out List<List<ModelOutputLink>> List_modelOutputLinks,
            out List<List<double>> opt_combines, out List<double> opt_energes, double rangeMin = 0.8, double rangeMax = 1,
            int populationSize = 40, int iterations = 20, double accuracy = 0.1)
        {
 
            #region 初始判断
            List_modelOutputNodes = new List<List<ModelOutputNode>>();
            List_modelOutputLinks = new List<List<ModelOutputLink>>();
            opt_combines = new List<List<double>>();
            opt_energes = new List<double>();
            string result = null;
            var range = new DoubleRange(rangeMin, rangeMax);
 
            if (!_isInitialSuccess)
                return "初始化不成功";
            if (InputData == null)
                return "输入参数为空";
            #endregion
 
            //float min_energe = MAX_ENERGE;
            for (int i = 0; i < List_Combine.Count; i++)
            {
                List<ModelOutputNode> modelOutputNodes;
                List<ModelOutputLink> modelOutputLinks;
                List<double> opt_combine = new List<double>();
                double energe;
                calcParms parms;
                var dr = new DoubleRange(range.Min * 50, range.Max * 50);
                result = InputInit(InputData, List_Combine[i], out parms, dr.Min, dr.Max);
                if (result != null)
                    return result;
                string result0 = _GetResultByFlowAndPress(InputData, List_Combine[i], out modelOutputNodes, out modelOutputLinks, out opt_combine, out energe, dr.Min, dr.Max, populationSize, iterations, accuracy);
                List_modelOutputNodes.Add(modelOutputNodes);
                List_modelOutputLinks.Add(modelOutputLinks);
                for (var j = 0; j < opt_combine.Count; j++) opt_combine[j] /= 50.0;
                opt_combines.Add(opt_combine);
                opt_energes.Add(energe);
            }
            return result;
        }
 
        #region other
        public void GetResult(double[] list_bestresult)
        {
            List<ModelOutputNode> nodes = null;
            List<ModelOutputLink> links = null;
            List<double> pumpflows = null;
            //double out_energe = 999999999f;
            double[] li = new double[list_bestresult.Length];
            for (int i = 0; i < li.Length; i++) li[i] = list_bestresult[i];
            this.epaFunction.GetResult(li, out nodes, out links, out pumpflows, true);
        }
        private string _GetResultByFlowAndPress(List<ModelInput> InputData, List<string> combine, out List<ModelOutputNode> modelOutputNodes, out List<ModelOutputLink> modelOutputLinks, out List<double> List_Hz, out double energe, double rangeMin = 0.8, double rangeMax = 1, int populationSize = 40, int iterations = 20, double accuracy = 0.1)
        {
 
 
            #region 初始判断
            modelOutputNodes = null;
            modelOutputLinks = null;
            //string result = null;
            //int err = 0;
            List_Hz = new List<double>();
            energe = 9999999999;
            #endregion
            #region 初始设置
            SetPumpStatus();
            #endregion
            var range = new DoubleRange(rangeMin, rangeMax);
            SearchSolution(combine, out List_Hz, out modelOutputNodes, out modelOutputLinks, out energe, rangeMin, rangeMax, populationSize, iterations, accuracy);
            return "";
        }
 
        private void SearchSolution(List<string> combine, out List<double> list_BestHzs, out List<ModelOutputNode> modelOutputNodes, out List<ModelOutputLink> modelOutputLinks, out double energe, double rangeMin = 0.8, double rangeMax = 1, int populationSize = 40, int iterations = 20, double accuracy = 0.1)
        {
            var list_BestEnerge = new List<double>();
            list_BestHzs = new List<double>();
            var list_AvgHz = new List<double>();
 
            modelOutputNodes = null;
            modelOutputLinks = null;
            energe = 999999999f;
 
 
            var range = new DoubleRange(rangeMin, rangeMax);
            var num_Total = range.Length / accuracy;
 
 
            var LENGTH_PUMP = 0;
            var value_LENGTH_PUMP = 1;
            var i = 0;
            while (value_LENGTH_PUMP < num_Total && i < 1000)
            {
                value_LENGTH_PUMP *= 2;
                LENGTH_PUMP += 1;
                i++;
            }
 
            var chromosomeLength = combine.Count * LENGTH_PUMP;
            BinaryChromosome ancestor = new BinaryChromosome(chromosomeLength);
 
 
 
            EPAFunctionGUI fitnessFunction = this.epaFunction;
            var IntselectionMethod = 0;
            object obj;
            if (IntselectionMethod != 0)
            {
                if (IntselectionMethod != 1)
                {
                    ISelectionMethod selectionMethod = new RouletteWheelSelection();
                    obj = selectionMethod;
                }
                else
                {
                    obj = new RankSelection();
                }
            }
            else
            {
                obj = new EliteSelection();
            }
 
            var optimizationMode = 1;
            epaFunction.Mode = ((optimizationMode != 0) ? OptimizationFunctionXD.Modes.Minimization : OptimizationFunctionXD.Modes.Maximization);
            Population population = new Population(populationSize, ancestor, fitnessFunction, (ISelectionMethod)obj);
 
            int num = 1;
            //double[,] array = new double[showOnlyBest ? 1 : populationSize, 2];
            List<ResultCombine> list_result = new List<ResultCombine>();
            //float[,] array = new double[showOnlyBest ? 1 : populationSize,combine.Count+1];
            bool needToStop = false;
            bool showOnlyBest = true;
            while (!needToStop)
            {
                population.RunEpoch();
                list_result.Clear();
                if (showOnlyBest)
                {
                    list_result.Add(new ResultCombine());
                    list_result[0].arrayHz = epaFunction.Translate(population.BestChromosome);
                    list_result[0].totalEnerge = epaFunction.OptimizationFunction(list_result[0].arrayHz);
                    Console.WriteLine($"迭代次数{num},总能耗{list_result[0].totalEnerge}");
 
                }
                else
                {
                    for (int j = 0; j < populationSize; j++)
                    {
                        list_result.Add(new ResultCombine());
                        list_result[j].arrayHz = epaFunction.Translate(population[j]);
                        list_result[j].totalEnerge = epaFunction.OptimizationFunction(list_result[j].arrayHz);
                    }
                }
                //chart.UpdateDataSeries("avg", array);
                //SetText(currentIterationBox, num.ToString());
 
                double[] list_bestresult = (epaFunction.Translate(population.BestChromosome));
                double bestEnerge = epaFunction.OptimizationFunction(list_bestresult);
 
 
 
 
                //SetText(currentValueBox, bestEnerge.ToString());
                List<double> pumpflows = null;
 
                this.epaFunction.GetResult(list_bestresult, out modelOutputNodes, out modelOutputLinks, out pumpflows, false);
 
 
                double sum = 0;
 
                energe = bestEnerge;
 
                sum /= list_bestresult.Length;
                list_BestEnerge.Add(bestEnerge);
                list_AvgHz.Add(sum);
                list_BestHzs = list_bestresult.ToList();
 
 
 
                num++;
                if (iterations != 0 && num > iterations)
                {
 
                    break;
                }
 
            }
        }
 
        public string InputInit(List<ModelInput> InputData, List<string> combine, out calcParms parms, double rangeMin = 0.8, double rangeMax = 1)
        {
            string result = null;
            #region 初始判断
            parms = null;
            //string result = null;
            int err = 0;
 
            var doubleRange = new DoubleRange(rangeMin, rangeMax);
 
            #endregion
 
 
 
            err = SetTypeValue(InputData, ModelInput.eType.press_in);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.flow);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.level);
            if (err > 9) return ReportQuestion(err);
            err = SetTypeValue(InputData, ModelInput.eType.valve_initstatus);
            if (err > 9) return ReportQuestion(err);
 
            List<bool> list_isfrequence = new List<bool>();
            combine.ForEach(ID => { bool isFre = InputData.Find(data => data.ObjectID == ID).Value == 1; list_isfrequence.Add(isFre); });
            var pressOutPoit = InputData.FindAll((m) => m.Type == ModelInput.eType.press_out).Select(p => (double)p.Value).ToList();
            var flowPointID = InputData.FindAll((m) => m.Type == ModelInput.eType.flow).Select(f => f.ObjectID).ToList();
            List<int> flowPointIndex = new List<int>();
 
 
            flowPointID.ForEach(FID =>
            {
                if (result != null) return;
                int index = -1;
                err = EpanetBase.ENgetnodeindex(FID, ref index);
                flowPointIndex.Add(index);
                if (err > 9) result = ReportQuestion(err);
            });
            if (result != null) return result;
 
            parms = new calcParms
            {
                list_isfrequence = list_isfrequence,
                flowPointIndex = flowPointIndex,
                demandPress = pressOutPoit
 
            };
            #region 计算单泵的长度
 
            var range = doubleRange;
 
            double accuracy = 0.1;
            var num_Total = range.Length / accuracy;
 
 
            var LENGTH_PUMP = 0;
            var value_LENGTH_PUMP = 1;
            var i = 0;
            while (value_LENGTH_PUMP < num_Total && i < 1000)
            {
                value_LENGTH_PUMP *= 2;
                LENGTH_PUMP += 1;
                i++;
            }
 
            #endregion
            List<DoubleRange> drs = new List<DoubleRange>();
            combine.ForEach(m => { drs.Add(range); });
 
            epaFunction = new EPAFunctionGUI(drs.ToArray(), LENGTH_PUMP, parms, this, combine);
            return null;
        }
 
        float accur = 0.1f;
        // <summary>
        /// 二分查找
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="low">开始索引 0</param>
        /// <param name="high">结束索引 </param>
        /// <param name="key">要查找的对象</param>
        /// <returns></returns>
        private float BinarySearch(List<string> combine, List<bool> list_isfrequence, int flowPointIndex, float lowHz, float highHz, float pressure)
        {
 
            string result = null;
            float mid = (lowHz + highHz) / 2;
            if (lowHz > highHz)
                return -1;
            else
            {
                if (highHz - lowHz <= accur)
                {
 
                    return mid;
                }
 
            }
            float press_out;
            float energe;
            result = getPressByPumps(combine, list_isfrequence, mid, flowPointIndex, out press_out, out energe);
            if (result != null) return -1;
 
 
            if (press_out > pressure)
                return BinarySearch(combine, list_isfrequence, flowPointIndex, lowHz, mid, pressure);
            else
                return BinarySearch(combine, list_isfrequence, flowPointIndex, mid, highHz, pressure);
 
        }
 
        private string getPressByPumps(List<string> combine, List<bool> isfrequence, float Hz, int flowPointIndex, out float press, out float energe)
        {
            int err = 0;
 
            #region 初始判断
 
            press = 0;
            energe = 99999999f;
            string result = null;
            #endregion
 
 
 
            var PressNodeID = -1;
            result = SetPumpValue(isfrequence, 0, combine);
            if (result != null) return result;
 
            result = SetPumpValue(isfrequence, ((float)Hz), combine);
            if (result != null) return result;
 
 
 
            //err = EpanetBase.ENsaveinpfile(@"D:\Desktop\C程序设计\项目源码\水力建模\eptools\out.inp");
 
            float PressValue = -1;
            int t = 0;
            //err = EpanetBase.ENinitH(0);
            //err = EpanetBase.ENrunH(ref t);
            err = EpanetBase.ENsolveH();
            if (err > 9) return ReportQuestion(err);
            err = EpanetBase.ENgetnodevalue(flowPointIndex, EpanetBase.Const_class.Const_Node.EN_HEAD, ref PressValue);
            if (err > 9) return ReportQuestion(err);
            press = PressValue;
 
            float TotalEnerge = 9999999999f;
 
            result = GetTypeValue(EpanetBase.Const_class.Const_Link_types.EN_PUMP, 1, Method.Sum, out TotalEnerge, EpanetBase.Const_class.Const_Link.EN_ENERGY);
            if (result != null) return result;
            energe = TotalEnerge;
 
 
 
            return result;
        }
 
        public string getPressByPumps(List<string> combine, List<bool> isfrequence, double[] list_Hz, List<int> flowPointIndex, out List<double> press, out double energe, out List<ModelOutputNode> modelOutputNodes, out List<ModelOutputLink> modelOutputLinks, out List<double> pumpflows, bool isNeedOutput = false, string path = null)
        {
            int err = 0;
 
            #region 初始判断
            modelOutputNodes = null;
            modelOutputLinks = null;
            pumpflows = null;
            press = new List<double>();
 
            energe = 99999999f;
            string result = null;
            #endregion
 
 
 
            var PressNodeID = -1;
            result = SetPumpValue(isfrequence, 0, combine);
            if (result != null) return result;
 
            result = SetPumpValue(isfrequence, list_Hz, combine);
            if (result != null) return result;
 
 
 
            //err = EpanetBase.ENsaveinpfile(@"D:\Desktop\C程序设计\项目源码\水力建模\eptools\out.inp");
 
            List<float> PressValue = new List<float>();
            int t = 0;
            //err = EpanetBase.ENinitH(0);
            //err = EpanetBase.ENrunH(ref t);
            //path = @"D:\Desktop\C程序设计\项目源码\水力建模\eptools_v2\Debug\model\out.inp";
            //EpanetBase.ENsaveinpfile(path);
            err = EpanetBase.ENsolveH();
            if (err > 9) return ReportQuestion(err);
            if (result != null) return result;
 
 
            var press0 = new List<double>();
            flowPointIndex.ForEach(FIndex =>
            {
                if (result != null) return;
                float PressValue0 = -1;
                err = EpanetBase.ENgetnodevalue(FIndex, EpanetBase.Const_class.Const_Node.EN_HEAD, ref PressValue0);
                press0.Add(PressValue0);
                if (err > 9) result = ReportQuestion(err);
            });
            press = press0;
 
 
 
            float TotalEnerge = 9999999999f;
 
            result = GetTypeValue(EpanetBase.Const_class.Const_Link_types.EN_PUMP, 1, Method.Sum, out TotalEnerge, EpanetBase.Const_class.Const_Link.EN_ENERGY);
            if (result != null) return result;
 
            bool isCannotDeliver = false;
 
            combine.ForEach(pump_id =>
            {
                if (isCannotDeliver) return;
                int pumpindex = -1;
                if ((err = EpanetBase.ENgetlinkindex(pump_id, ref pumpindex)) > 9) { result = ReportQuestion(err); return; }
                float pumpstatus = -1;
                if ((err = EpanetBase.ENgetlinkvalue(pumpindex, EpanetBase.Const_class.Const_Link.EN_STATUS, ref pumpstatus)) > 9) { result = ReportQuestion(err); return; }
                float pumpFlow = -1;
                err = EpanetBase.ENgetlinkvalue(pumpindex, EpanetBase.Const_class.Const_Link.EN_FLOW, ref pumpFlow);
                if (pumpstatus == 0 || pumpFlow <= 50) isCannotDeliver = true;
            });
            if (isCannotDeliver) return null;
            energe = TotalEnerge;
 
 
            if (isNeedOutput)
            {
                var pf = new List<double>();
                combine.ForEach(pump_id =>
                {
 
                    int pumpindex = -1;
                    err = EpanetBase.ENgetlinkindex(pump_id, ref pumpindex); if (err > 9) ;
                    float pumpFlow = -1;
                    err = EpanetBase.ENgetlinkvalue(pumpindex, EpanetBase.Const_class.Const_Link.EN_FLOW, ref pumpFlow);
                    pf.Add(pumpFlow);
                });
                pumpflows = pf;
                EpanetBase.ENsaveinpfile(path);
                GetOutputString(out modelOutputNodes, out modelOutputLinks);
            }
            return result;
        }
 
        public string GetPumpList(out List<Pump> pumps)
        {
            #region 初始判断
 
            //modelOutputLinks = null;
            //modelOutputNodes = null;
            string result = null;
            int err = 0;
            pumps = new List<Pump>();
            if (!_isInitialSuccess)
                return "初始化不成功";
            //if (InputData == null)
            //    return "输入参数为空";
            #endregion
 
 
 
            #region 获取
 
            int NLink1 = 0;
            err = EpanetBase.ENgetcount(EpanetBase.Const_class.Const_Component.EN_NODECOUNT, ref NLink1);
            if (err > 9) return ReportQuestion(err);
            for (int i = 1; i <= NLink1; i++)
            {
                int type = -1;
                err = EpanetBase.ENgetnodetype(i, ref type);
                if (err > 9) return ReportQuestion(err);
                if (type == EpanetBase.Const_class.Const_Link_types.EN_PUMP)
                {
                    Pump pump = new Pump();
 
                    err = EpanetBase.ENgetlinkvalue(i, EpanetBase.Const_class.Const_Link.EN_STATUS, ref pump.value);
                    if (err > 9) return ReportQuestion(err);
 
                    StringBuilder s = new StringBuilder();
                    err = EpanetBase.ENgetlinkid(i, s);
                    pump.ID = s.ToString();
                    if (err > 9) return ReportQuestion(err);
 
                    pump.index = i;
 
                    pumps.Add(pump);
 
                }
            }
 
 
 
 
            return null;
 
 
 
 
 
            #endregion
        }
    }
    public class Pump
    {
        public string ID = null;
        public int index;
        public float value;
 
    }
    public class calcParms
    {
        public List<bool> list_isfrequence = null;
        public List<int> flowPointIndex = null;
        public List<double> demandPress = null;
    }
    public class ResultCombine
    {
        public double[] arrayHz ;
        public double totalEnerge;
    }
    #endregion
 
}