cloudflight
2024-05-07 d750fc90db76b0c6aab2f99b987b7af84b831ce4
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AForge;
using System.Drawing;
using System.Text.RegularExpressions;
using AForge.Genetic;
//using NPOI.SS.Formula.Eval;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using System.Collections.Concurrent;
using Dapper;
 
namespace Hydro.CommonBase
{
    public class GeneticParams
    {
        static int Count = 0;
        public GeneticParams(int Level)
        {
            this.Level = Level;
            this.ID = Count;
            Count++;
        }
 
        public void Init(string ConfigPath)
        {
            if (Db == null) Db = new Db();
            if (!string.IsNullOrEmpty(ConfigPath)) Db.Init(ConfigPath);
            if (Quene!=null) Quene.Dispose();
            Quene = new Quene();
            this.Quene= Quene;
            //»ñȡȫ¾ÖĬÈÏÉèÖÃ
            var globalConfig = Db.Connection.QueryFirstOrDefault<string>("SELECT FValue FROM MyConfig WHERE FKey='GlobalConfig'");
            GlobalConfig = JsonConvert.DeserializeObject<GlobalConfig>(globalConfig) ?? new GlobalConfig();
            this.GlobalConfig = GlobalConfig;
            isInited = true;
            //return gParam;
        }
        public void Dispose()
        {
            onReportProgress = null;
            Db.Close();
            Db = null;
 
        }
        
 
        public List<double> list_BestEnerge;
        public List<double> list_AvgHz;
        public List<DRange> ranges;
        public List<int> DLengths;
        public List<double?> accuracys;
        public List<int> Dependencies;
        public double[] list_AtemtpArr;
 
        public string SolutionSaveName;
        public int populationSize;
        public int iterations;
        public int populationSize_full;
        public int iterations_full;
        public List<ulong> Upopulations = null;
 
 
        public int selectionMethod;
        public int optimizationMode;
        public bool Ç¿ÖÆÖؼÆËã;
 
        public int iterations_min;
        public double accuracyNum;
        //public int MultiTimes=0;
 
        public int MaxRobot;
        public int RuningRobot;
 
        public int num;
        public double BestOptValue;
        public DRange tolerance;
        public FinishEvent onFinished;
        public FinishEvent onError;
        public FinishEvent onReportProgress;
        public SetEvent setVars;
 
        public Db Db;
        public dynamic SolutionDBHelper = null;
        public GlobalConfig GlobalConfig;
        public bool isInited=false;
        public Quene Quene;
        public int Level;
        public int ID;
 
        public bool isNeedOutput = true;
 
        public double CurrentTotalDemand = -1;
    }
    public class Result
    {
        public double[] arrayTryValue = null;
        public float Demand;
        public double ObjFunctionValue;
        public int it_Times = 0;
        public int[] unitNum = null;
    }
    public class Quene
    {
 
        public System.Collections.Generic.Queue<WdnmoParam> RequestQueues;
        public HashSet<Guid> RequestFinished;
        public Dictionary<ulong, double> ResultHash;
        public int Level;
        public Dictionary<Guid, double?> ResultDictionary;
        public Dictionary<Guid, ChromosomeBase> ResultChrome;
        public int MissionQueneCount;
        public List<calcParam> MissionQuene;
        public DateTime ScadaTime;
        //public List<TimePoint> TimePoints;
        public double BestResult=double.MaxValue;
        public void Dispose()
        {
            if (RequestQueues != null)
            {
                RequestQueues.Clear();
                RequestQueues = null;
            }
            if (RequestFinished != null)
            {
                RequestFinished.Clear();
                RequestFinished = null;
            }
 
            if (ResultHash != null)
            {
                ResultHash.Clear();
                ResultHash = null;
            }
            if (ResultDictionary != null)
            {
                ResultDictionary.Clear();
                ResultDictionary = null;
            }
            if (ResultChrome != null)
            {
                ResultChrome.Clear();
                ResultChrome = null;
            }
            if (MissionQuene != null)
            {
                MissionQuene.Clear();
                MissionQuene = null;
            }
 
 
        }
    }
    [Serializable]
 
    public class WdnmoParam
    {
 
        public List<Result> results;
        public double DistriDemand;
 
 
        public Guid ID;
        public List<TimePoint> ScadaPoints { get; set; } = new List<TimePoint>();
        public List<TimePoint> ConfigPoints { get; set; } = new List<TimePoint>();
        public List<TimePoint> ResultPoints { get; set; } = new List<TimePoint>();
        [JsonIgnore]
        public Dictionary<string, double> dict_ScadaPoints = null;
        [JsonIgnore]
        public Dictionary<string, double> dict_ConfigPoints = null;
        [JsonIgnore]
        public Dictionary<string, double> dict_ResultPoints = null;
        [JsonIgnore]
        public Dictionary<string, double[]> dict_ScadaPointsPattern = null;
        public Dictionary<string, double[]> dict_ConfigPointsPattern = null;
        public Dictionary<string, double[]> dict_ResultPointsPattern = null;
 
        public bool isBuildDict = false;
        public void buildDict()
        {
            dict_ScadaPoints=new Dictionary<string, double>();
            dict_ScadaPointsPattern = new Dictionary<string, double[]>();
            ScadaPoints.ForEach(vv => 
            { 
                if (!string.IsNullOrEmpty(vv.Key) && !dict_ScadaPoints.ContainsKey(vv.Key)) 
                { 
                    dict_ScadaPoints.Add(vv.Key, vv.Value); 
                    if (vv.Pattern!=null)
                    {
                        dict_ScadaPointsPattern.Add(vv.Key, vv.Pattern);
                    }
                } 
            });
 
            dict_ConfigPoints = new Dictionary<string, double>();
            dict_ConfigPointsPattern = new Dictionary<string, double[]>();
            ConfigPoints.ForEach(vv =>
            {
                if (!string.IsNullOrEmpty(vv.Key) && !dict_ConfigPoints.ContainsKey(vv.Key))
                {
                    dict_ConfigPoints.Add(vv.Key, vv.Value);
                    if (vv.Pattern != null)
                    {
                        dict_ConfigPointsPattern.Add(vv.Key, vv.Pattern);
                    }
                }
            });
 
 
            dict_ResultPoints = new Dictionary<string, double>();
            dict_ResultPointsPattern = new Dictionary<string, double[]>();
            ResultPoints.ForEach(vv =>
            {
                if (!string.IsNullOrEmpty(vv.Key) && !dict_ResultPoints.ContainsKey(vv.Key))
                {
                    dict_ResultPoints.Add(vv.Key, vv.Value);
                    if (vv.Pattern != null)
                    {
                        dict_ResultPointsPattern.Add(vv.Key, vv.Pattern);
                    }
                }
            });
            isBuildDict = true;
        }
        public double Fitness = -1;
        public DateTime time { get; set; } = default(DateTime);
        //public int RunTimes = 0;
        public int Period { get; set; } = 0;
        public string ErrorMessage { get; set; } = null;
        
        public bool fullSearchMode = false;
        public FinishEvent onFinish;
        public bool isNeedOutPut = false;
        public Dictionary<string, double[]> ToSearchDict()
        {
            Dictionary<string, double[]> dict = new Dictionary<string, double[]>();
            foreach (var point in ScadaPoints)
            {
                if (!dict.ContainsKey(point.Key))
                    dict.Add(point.Key, new double[] { point.Value - point.SearchRange / 2, point.Value + point.SearchRange / 2 });
                else
                    dict[point.Key] = new double[] { point.Value - point.SearchRange / 2, point.Value + point.SearchRange / 2 };
            }
            if (!dict.ContainsKey("period"))
                dict.Add("period", new double[] { Period - 8, Period + 8 });
            else
                dict["period"] = new double[] { Period - 8, Period + 8 };
            return dict;
        }
        //public ulong Toulong(string childSaveName)
        //{
            
        //}
        public string ToSearchSring()
        {
            Dictionary<string, double> dict = new Dictionary<string, double>();
 
            foreach (var point in ScadaPoints)
            {
                if (!dict.ContainsKey(point.Key))
                    dict.Add(point.Key, point.Value);
                else
                    dict[point.Key] = point.Value;
            }
            if (!dict.ContainsKey("period"))
                dict.Add("period", Period);
            StringBuilder sb = new StringBuilder();
            foreach (var point in dict)
            {
                sb.AppendLine(point.Key + "\t" + point.Value);
            }
            return sb.ToString();
 
        }
        public Dictionary<string, double> ToResultDict()
        {
            Dictionary<string, double> dict = new Dictionary<string, double>();
            foreach (var point in ScadaPoints)
            {
                if (!dict.ContainsKey(point.Key))
                    dict.Add(point.Key, point.Value);
                else
                    dict[point.Key] = point.Value;
            }
            if (!dict.ContainsKey("period"))
                dict.Add("period", Period);
            else
                dict["period"] = Period;
            foreach (var point in ResultPoints)
            {
                if (!point.isNeedtoSave) continue;
                if (!CheckExpandoVariableName(point.SaveKey)) continue;
                if (!dict.ContainsKey(point.SaveKey))
                {
                    dict.Add(point.SaveKey, point.Value);
                }
                else
                    dict[point.SaveKey] = point.Value;
            }
            return dict;
            // ÖØÔØ == ÔËËã·û
 
        }
        private bool CheckExpandoVariableName(string name)
        {
            if (name == null) return false;
            // ÕýÔò±í´ïʽ£ºÒÔ×Öĸ»òÏ»®Ïß¿ªÍ·£¬ºóÃæ¸ú×ÅÈÎÒâ¸ö×Öĸ¡¢Êý×Ö»òÏ»®Ïß
            string pattern = @"^[a-zA-Z_]\w*$";
            return Regex.IsMatch(name, pattern);
        }
 
        public void Dispose()
        {
            results?.Clear();
            ScadaPoints?.Clear();
            ConfigPoints?.Clear();
            ResultPoints?.Clear();
            onFinish = null;
        }
 
        public WdnmoParam Copy()
        {
            //»ñȡһ¸öеÄʵÀý£¬½«Õâ¸öÀàµÄÊôÐÔÖµ¸³Öµ¸øÐÂʵÀý
            WdnmoParam v = new WdnmoParam();
            v.ID = this.ID;
            v.Fitness = this.Fitness;
            v.time = this.time;
            //v.RunTimes = this.RunTimes;
            v.Period = this.Period;
            v.ErrorMessage = this.ErrorMessage;
            v.fullSearchMode = this.fullSearchMode;
            v.onFinish = this.onFinish;
            v.isNeedOutPut = this.isNeedOutPut;
            v.results = this.results;
            v.ScadaPoints = new List<TimePoint>();
            foreach (var point in this.ScadaPoints)
            {
                v.ScadaPoints.Add(new TimePoint( point));
            }
            v.ConfigPoints = this.ConfigPoints;
            v.ResultPoints = new List<TimePoint>();
            foreach (var point in this.ResultPoints)
            {
                v.ResultPoints.Add(new TimePoint(point));
            }
           
            v.buildDict();
            return v;
        }
    }
 
    public class calcParam
    {
 
        public double[] vars;
 
        public Guid ID;
 
        public IChromosome chromosome;
 
        public List<SetVar> setVars = new List<SetVar>();
 
    }
    public class SetVar
    {
        public string ID;
        public bool isNode = true;
        public int type;
        public float value;
        public SetVar(string ID,bool isNode,int type,float value)
        {
            this.ID= ID;
            this.isNode = isNode;
            this.type = type;
            this.value = value;
        }
    }
 
    [Serializable]
 
    public class TimePoint
    {
        public TimePoint()
        {
 
        }
        public TimePoint(string key, double value)
        {
            Key = key;
            Value = value;
        }
        public TimePoint(TimePoint point)
        {
            this.Key= point.Key;
            this.Value = point.Value;
            this.Name = point.Name;
            this.ValueTime = point.ValueTime;
            this.SearchRange = point.SearchRange;
            this.isNeedtoSave = point.isNeedtoSave; 
            this.SaveKey = point.SaveKey;
            if (point.Pattern!=null) this.Pattern= point.Pattern.ToList().ToArray(); 
 
        }
        public string Name;
        public DateTime ValueTime { get; set; }
        public string Key { get; set; }
        public double Value { get; set; }
        public double[] Pattern { get; set; } = null;
        
        public double SearchRange = 0;
 
        public bool isNeedtoSave = false;
 
        public string SaveKey = null;       
 
        public override string ToString()
        {
            return $"{Name}\t[{Key}]\t{Value}";
        }
    }
 
 
    [Serializable]
    public delegate void FinishEvent(dynamic txt);
    public delegate void SetEvent(string name, WdnmoParam param);
    //public event FinishEvent ss;
 
 
    [Serializable]
 
    public enum SettingCalcType
    {
        Add = 0,
        Const = 1
    }
 
 
 
    [Serializable]
    public class LogicModelParams 
    {
 
 
        public Guid ID = new Guid();
        public List<variable> ¼¯ºÏ = new List<variable>();
        public List<variable> ³õʼÌõ¼þ = new List<variable>();
        public List<variable> Ï½׶ÎÌõ¼þ = new List<variable>();
        public List<variable> Ô¼Êø±äÁ¿ = new List<variable>();
        public List<variable> ÊÔËã±äÁ¿ = new List<variable>();
        public List<variable> Ë®Á¿·ÖÅä±äÁ¿ = new List<variable>();
        public List<variable> Ë®Á¿·ÖÅ伯ºÏ = new List<variable>();
        public List<variable> ±äÁ¿ = new List<variable>();
        public List<variable> ±í´ïʽ = new List<variable>();
        public List<variable> Ô¤´¦Àí²å¼þ = new List<variable>();
        public List<variable> ½Ó¿Ú±äÁ¿
        {
            get
            {
                List<variable> all = new List<variable>();
                all.AddRange(±äÁ¿);
                all.AddRange(³õʼÌõ¼þ);
                all.AddRange(Ͻ׶ÎÌõ¼þ);
                all.AddRange(±í´ïʽ);
                all.AddRange(Ë®Á¿·ÖÅä±äÁ¿);
                return all;
            }
        }
 
        public List<variable> É趨±äÁ¿
        {
            get
            {
                List<variable> all = new List<variable>();
                all.AddRange(³õʼÌõ¼þ);
                all.AddRange(Ͻ׶ÎÌõ¼þ);        
                return all;
            }
        }
        
        public List<variable> ±äÁ¿È«¼¯
        {
            get
            {
                List<variable> all = new List<variable>();
                all.AddRange(³õʼÌõ¼þ);
                all.AddRange(Ͻ׶ÎÌõ¼þ);
                all.AddRange(±äÁ¿);
                all.AddRange(ÊÔËã±äÁ¿);
                all.AddRange(¼¯ºÏ);
                all.AddRange(±í´ïʽ);
                all.AddRange(Ô¼Êø±äÁ¿);
                if (OFunction?.variable!=null) all.Add(OFunction.variable);
 
                return all;
            }
        }
        //[NonSerialized]
        [JsonIgnore]
        public Dictionary<string, variable> dict_Name { get; set; }
        //[NonSerialized]
        [JsonIgnore]
        public Dictionary<int, variable> dict_ID { get; set; }
        //[NonSerialized]
        [JsonIgnore] 
        public Dictionary<string, variable> dict_OutPut { get; set; }
        //public string inputFileString;
 
        [JsonIgnore]
        public dict<string,dict> map_node { get;set;}
        [JsonIgnore]
        public dict<string, dict> map_link { get; set; }
 
        public void buildDict()
        {
 
            //BookMark    £º½¨Á¢±äÁ¿×Öµä
            dict_Name = new Dictionary<string, variable>();
            dict_ID=new Dictionary<int, variable>();
            dict_OutPut = new Dictionary<string, variable>();
            ±äÁ¿È«¼¯.ForEach(vv => { if (!string.IsNullOrEmpty(vv.Name) && !dict_Name.ContainsKey(vv.Name)) dict_Name.Add(vv.Name, vv); });
            ±äÁ¿È«¼¯.ForEach(vv => {  if (!dict_ID.ContainsKey(vv.ID)) dict_ID.Add(vv.ID, vv); });
            ½Ó¿Ú±äÁ¿.ForEach(vv => { if (!string.IsNullOrEmpty(vv.Name) && !dict_OutPut.ContainsKey(vv.expressString)) dict_OutPut.Add(vv.expressString, vv); });
 
        }
 
        [NonSerialized]
        [JsonIgnore]
        public Dictionary<string, childSolutionParam> ChildParam = new Dictionary<string, childSolutionParam>();
 
        public OBJFunction OFunction;
        public EN_Times Ê±¼ä¿Ì¶È;
        //public List<PointF> results = new List<PointF>();
        public List<Result> results;
        //public int times;
 
        public LogicModelParams CloneResult()
        {
            var param_new = new LogicModelParams();
            ³õʼÌõ¼þ.ForEach(v => param_new.³õʼÌõ¼þ.Add(new variable {ID=v.ID,CalcValue_Set=v.CalcValue,LogicValue=v.LogicValue,objListString=v.objListString })) ;
            Ï½׶ÎÌõ¼þ.ForEach(v => param_new.Ͻ׶ÎÌõ¼þ.Add(new variable { ID = v.ID, CalcValue_Set = v.CalcValue, LogicValue = v.LogicValue, objListString = v.objListString }));
            Ô¼Êø±äÁ¿.ForEach(v => param_new.Ô¼Êø±äÁ¿.Add(new variable { ID = v.ID, CalcValue_Set = v.CalcValue, LogicValue = v.LogicValue, objListString = v.objListString }));
            ÊÔËã±äÁ¿.ForEach(v => param_new.ÊÔËã±äÁ¿.Add(new variable { ID = v.ID, CalcValue_Set = v.CalcValue, LogicValue = v.LogicValue, objListString = v.objListString }));
            Ë®Á¿·ÖÅä±äÁ¿.ForEach(v => param_new.Ë®Á¿·ÖÅä±äÁ¿.Add(new variable { ID = v.ID, CalcValue_Set = v.CalcValue, LogicValue = v.LogicValue, objListString = v.objListString }));
            Ë®Á¿·ÖÅ伯ºÏ.ForEach(v => param_new.Ë®Á¿·ÖÅ伯ºÏ.Add(new variable { ID = v.ID, CalcValue_Set = v.CalcValue, LogicValue = v.LogicValue, objListString = v.objListString }));
            ±äÁ¿.ForEach(v => param_new.±äÁ¿.Add(new variable { ID = v.ID, CalcValue_Set = v.CalcValue, LogicValue = v.LogicValue, objListString = v.objListString }));
            ¼¯ºÏ.ForEach(v => param_new.¼¯ºÏ.Add(new variable { ID = v.ID, CalcValue_Set = v.CalcValue, LogicValue = v.LogicValue, objListString = v.objListString }));
            ±í´ïʽ.ForEach(v => param_new.±í´ïʽ.Add(new variable { ID = v.ID, CalcValue_Set = v.CalcValue, LogicValue = v.LogicValue, objListString = v.objListString }));
            param_new.OFunction = new OBJFunction();
            param_new.OFunction.value = OFunction.value;
            param_new.buildDict();
            return param_new;
        }
 
    }
    /// <summary>
    /// ×Ó·½°¸²ÎÊý¼¯
    /// </summary>
    public class childSolutionParam
    {
        /// <summary>
        ///¼ÆËã²ÎÊý
        /// </summary>
        public WdnmoParam wParam;
        public List<variable> childVars_In;
        public List<variable> childVars_Out;
        public string childSaveName;
        public variable childFitness_Out;
        //public SaveSettings saveSettings;
        //public string savePath;
    }
    public class EN_Times
    {
        public int duration;
        public int step;
        public int Ntimes;
    }
    
   
    [Serializable]
    public class variable
    {
        public static List<string> listUsedInitValue = new List<string>() { "³õʼ¼ÆËãÖµ", "±ä»¯Öµ", "¾ø¶Ô±ä»¯Öµ" };
        public static List<string> list_ÊäÈëÖµ = new List<string>() { "»ù×¼Öµ(½çÃæ)", "¶¨ÒåÖµ", "»ù×¼Öµ(½Ó¿Ú)",  "ģʽϵÊý(½çÃæ)", "ģʽϵÊý(½Ó¿Ú)" };
        public static List<string> listUsedScadaValue = new List<string> { "¶¨ÒåÖµ", "»ù×¼Öµ(½Ó¿Ú)" };
 
        public static List<string> list_½Ó¿ÚÊä³öÖµ = new List<string>() { "±í´ïʽ¼ÆËã", "¼ÆËãÖµ", "±ä»¯Öµ", "³õʼ¼ÆËãÖµ", "×Ó·½°¸Öµ" };
        public static List<string> list_½Ó¿ÚÊäÈëÖµ = new List<string>() { "»ù×¼Öµ(½Ó¿Ú)", "ģʽϵÊý(½Ó¿Ú)" };
 
        public static List<string> list_NoNeedModelIndex = new List<string>() { "»ù×¼Öµ(½çÃæ)", "¶¨ÒåÖµ", "»ù×¼Öµ(½Ó¿Ú)", "ģʽϵÊý(½çÃæ)",  "ģʽϵÊý(½Ó¿Ú)" };
        public int ID;
        public string modelObjectID;
        public string Name;
        public int modelIndex;
        public int maxTimes = 1;
        
 
        //CFÐÞ¶© 2023Äê7ÔÂ10ÈÕ ´¦ÀíÐòÁл¯Ê§°ÜµÄÎÊÌâ
        [NonSerialized]
        public List<variable> Vars = new List<variable>();
        [NonSerialized]
        public Dictionary<int, int> VarIndexs = new Dictionary<int, int>();
 
        [NonSerialized]
        public List<variable> PatternVars = new List<variable>();
 
        // public int ParentID;
 
        public bool isNode;
        //public bool? isOutput;
       
        public dynamic Type;      
        public dynamic resultType;
        public DRange doubleRange;      
        
        public double? accuracy = null;
        public string expressString;
        public string expressType;
        public string defineType;
        public string IndicatorType;
        public int ObjType;
        public string logicType;
        /// <summary>
        /// É趨ֵ
        /// </summary>
        public double? LogicValue = null;
        /// <summary>
        /// É趨ģʽ
        /// </summary>
        public double[] LogicPattern = null;
        /// <summary>
        /// ³õʼ״̬¼ÆËãÖµ
        /// </summary>
        public double CalcValue_Init;
        ///// <summary>
        ///// Ä¿±ê״̬¼ÆËãÖµ
        ///// </summary>
        //public double? CalcValue_Calc = null;
 
 
        public double? CalcValue_Set
        {
            set 
            { 
                lock(this)
                {
                    this.CalcValue = value;
                }
            }
        }
        /// <summary>
        /// ÊýѧģÐÍÄ¿±êÖµ
        /// </summary>
        public double? CalcValue { get;private set; }=null;
 
        /// <summary>
        /// ÊýѧģÐÍÄ¿±êÖµ¶àʱ¿Ì
        /// </summary>
        public double[] CalcValue_Arr = null;
 
        /// <summary>
        /// ÏÔʾµÄСÊýλÊý
        /// </summary>
        public int Ð¡ÊýλÊý = 3;
 
        /// <summary>
        /// ±ê¸ß
        /// </summary>
        public double elevation = -9099;
 
        public int sort = 0;
 
        public string childSolution = "";
 
        public string childTag = "";
 
        public string childType = "´«Èë";
 
        public double SearchRange = -1;
 
        public bool isNeedToSave = false;
 
        public string SaveKey = null;
 
 
 
        /// <summary>
        /// ÊÇ·ñÊÇÐéÄâ±äÁ¿£¨Óɼ¯ºÏ²úÉú£©
        /// </summary>
        public bool virturl = false;
 
 
        [NonSerialized]
        public dynamic Eval = null;
 
        /// <summary>
        /// ¶ÔÏóÇåµ¥
        /// </summary>
        public string objListString;
 
        /// <summary>
        /// ¶ÔÏó±àºÅ£¨×îÓÅ£©
        /// </summary>
        public string obj_Best;
 
        public static double ErrNum = -9999999;
 
        private int _level = 0;
 
        [JsonIgnore]
        public int Level
        {
            get 
            {
                if (_level!=0) return _level;
                int tempLevel = 1;
                if (Vars!=null)
                foreach (var v in Vars) 
                {
                    if (tempLevel < v.Level + 1) tempLevel = v.Level + 1;
                }
                _level= tempLevel;
                return _level;
            }
            
        }
 
        public override string ToString()
        {
            string value = CalcValue == null ? LogicValue.ToString() : CalcValue.ToString();
            return $"[{Name}]{value}";
        }
 
        public double GetValue(Func<variable, double> GetCalcValue = null, int? period = 0, bool isÇå³ý»º´æ = false)
        {
            double Value;
            var v = this;
            // is double dd
            if (v.expressType == "×Ó·½°¸Öµ"|| v.logicType=="Ä¿±êº¯Êý" )
            {
                if (!v.CalcValue.HasValue)
                    Value = ErrNum; //throw (new Exception("Òì³£"));
                else
                    Value = (double)v.CalcValue;//v.GetCalcValue(GetCalcValue, period, false);
            }
            else if(v.logicType=="ÊÔËã²ÎÊý")//²»Çå³ý»º´æ
            {
                if (!v.CalcValue.HasValue)
                    Value = variable.ErrNum;
                else
                    Value = (double)v.CalcValue;//v.GetCalcValue(GetCalcValue, period, false);
                
            }
            
            else if (variable.list_ÊäÈëÖµ.IndexOf(v.expressType) < 0)
            {
                Value = v.GetCalcValue(GetCalcValue, period, isÇå³ý»º´æ);
            }
            else
            {
                double? d;
                if ((d = v.GetLogicValue()) == null)
                {
                    //result = $"±äÁ¿[{v.Name}]ÅäÖôíÎó,±àºÅ[{v.modelObjectID}]ȡֵʧ°Ü";
                    Value = variable.ErrNum;
                }
                else
                {
                    Value = (double)(v.GetLogicValue() ?? (variable.ErrNum));
                }
            }
            return Value;
        }
        public double GetCalcValue(Func<variable, double> GetCalcValue = null, int? period = 0, bool isÇå³ý»º´æ = false)
        {
            
            
            if (isÇå³ý»º´æ)
            {
                CalcValue = null;               
            }
            else
            {
                if (CalcValue!=null) return (double)CalcValue;
            }
 
 
 
            double CalcValue_Calc;
            double result;
            if (expressType == listUsedInitValue[0])//³õʼ¼ÆËãÖµ
            {
                result = CalcValue_Init;
            }
            else if (expressType == listUsedInitValue[1])//±ä»¯Öµ
            {
                CalcValue_Calc = GetCalcValue(this);
                result = CalcValue_Calc - CalcValue_Init;
            }
            else if (expressType == listUsedInitValue[2])//¾ø¶Ô±ä»¯Öµ
            {
                CalcValue_Calc = GetCalcValue(this);
                result = CalcValue_Calc - CalcValue_Init;
                if (result < 0) result *= -1;
            }
            else
            {
                CalcValue_Calc = GetCalcValue(this);
                result = CalcValue_Calc;
            }//¼ÆËãÖµ
 
            CalcValue = result;
            return result;
        }
        public double? GetLogicValue()
        {
            if (LogicValue != null) return (double)LogicValue;
            return null;
        }
 
        public double[] GetShowValue_Arr()
        {
 
            if (list_ÊäÈëÖµ.IndexOf(expressType) >= 0)
                //return LogicPattern;
                return CalcValue_Arr;
            else
                return CalcValue_Arr;
        }
 
        /// <summary>
        /// »ñÈ¡Ò³ÃæÏÔʾµÄ½á¹ûÖµ¼°Ñùʽ
        /// </summary>
        /// <returns></returns>
        public string GetShowValue(bool showString = false)
        {
            //,Color.Red,Color.Blue,Color.Orange
 
            double? result;
            string Result;
            if (expressType!=null && expressType.Contains("ģʽ"))
            {
                if (LogicPattern!=null)
                {
                    int LengthLimit = 16; //GlobalConfig.LengthLimit??16;
                    string txt= LogicPattern.ToList().Select(v=>v.ToString()).Aggregate((a, b) => a + "," + b);
                    //×±£Áô16¸ö×Ö·û
                    if (txt.Length > LengthLimit) txt = txt.Substring(0, LengthLimit) + "...";
                    return txt;
                }
                result = 0;
            }
            else if (list_ÊäÈëÖµ.IndexOf(expressType) >= 0)
                result = LogicValue;
            else
                result = CalcValue;
 
 
            Result = result == null ? "-" : Math.Round(Double.Parse(result.ToString()), Ð¡ÊýλÊý).ToString();
            if (showString)
            {
                if (!string.IsNullOrEmpty(obj_Best))
                {
                    Result += $" [{obj_Best}]";
                }
            }
            else
            if (Type.ToString().ToLower()== "initstatus")// is EPAModelNameSpace.EPAModelOutput.eType.INITSTATUS || Type is EPAModelNameSpace.EPAModelInput.eType.initStatus)
            {
                if (result == 0)
                    Result = "¹Ø|Yellow";
                else
                    Result = "¿ª|DarkGreen";
            }
            else
            {
                if (list_ÊäÈëÖµ.IndexOf(expressType) >= 0)
                    Result += "|Blue";
                else
                    Result += "|White";// Color.DarkGreen
            }
 
            return Result;
        }
        public variable Copy()
        {
            //»ñȡһ¸öеÄʵÀý£¬½«Õâ¸öÀàµÄÊôÐÔÖµ¸³Öµ¸øÐÂʵÀý
            variable v = new variable();
            v.ID = this.ID;
            v.IndicatorType = this.IndicatorType;
            v.Type = this.Type;
            v.expressType = this.expressType;
            v.expressString = this.expressString;
            v.defineType = this.defineType;
            v.isNode = this.isNode;
            v.ObjType = this.ObjType;
            v.logicType = this.logicType;
            v.LogicValue = this.LogicValue;
            v.LogicPattern = this.LogicPattern;
            v.СÊýλÊý = this.СÊýλÊý;
            v.virturl = this.virturl;
            v.doubleRange =new DRange( this.doubleRange);
            v.accuracy = this.accuracy;
            //v.Vars = this.Vars;
            v.Name = this.Name;
            v.modelObjectID = this.modelObjectID;
            v.modelIndex = this.modelIndex;
            v.maxTimes = this.maxTimes;
            v.childSolution = this.childSolution;
            v.childTag = this.childTag;
            v.childType = this.childType;
            v.SearchRange = this.SearchRange;
            v.SaveKey = this.SaveKey;
            v.objListString = this.objListString;
            v.obj_Best=this.obj_Best;
            v._level=this._level;
            return v;
        }
    }
 
    [Serializable]
    public class OBJFunction
    {
        public string Text_origin;
        public string Text_change;
        public string Text;
        public string ExpressType;
        public string Name;
 
        public double value;
        public DRange tolerance;
        public variable variable;
        //public List<variable> paramVariables;
        //public List<double> paramValues;
        [NonSerialized]
        public dynamic eval;
        public bool isUseOriginValue = false;
        /// <summary>
        /// true£ºÊ¹Óñí´ïʽģʽ£¬false£ºÊ¹ÓñàºÅģʽ
        /// </summary>
        public bool? isUseExpression = true;
        public bool isUseScadaValue = false;
 
    }
 
 
 
 
}