cloudflight
2024-07-06 0340a1bb6012a97de0e029b26b5c882472749050
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
#define ValDistinct
#define DEBUG_TIME1
using Hydro.CommonBase;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
//using WaterDistributioinManager;
using AForge.Genetic;
using System.Linq;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Numerics;
using System.Data;
using Newtonsoft.Json.Linq;
using Hydro.CodeProvider;
using Hydro.HydraulicHelperNS;
 
namespace Hydro.ParrelControl
{
    public class BigBinaryChromo : ChromosomeBase
    {
 
 
 
        public int Level;
 
        public LogicModelParams param;
 
        public WdnmoParam wParam;
 
        public GeneticParams gParam;
 
        public BigBinaryFunction fitnessFunction;
 
        public static BigInteger originVal;
 
       
        
 
        public int length;
 
 
 
        protected BigInteger val;
 
        public void setval(BigInteger v)
        {
            AttemptArray = null;
            fitness = null;
            _valid = false;
            //_value = -1;
            val = v;
        }
 
        public static int seed = 0;
        public int GetSeed()
        {
            //将
            seed += Environment.TickCount + DateTime.Now.Millisecond;
            return seed;
        }
        //protected static Random rand = new Random();
 
        public int Length => length;
 
        //private BigInteger _value=-1;
        public BigInteger Value
        {
            get
            {
                //var cmax = (BigBinaryNums.CMaxValue >> BigBinaryNums.CMaxLength - length);
 
                //if (_value == -1) _value = val & cmax;
                return val;
            }
        }
        //=> val & (BigBinaryNums.CMaxValue >> BigBinaryNums.CMaxLength - length);
 
        public BigInteger MaxValue => BigBinaryNums.CMaxValue >> BigBinaryNums.CMaxLength - length;
        public string str_From = null;
#if ArrDistinct
        public BigBinaryChromo(int length, int Level, HashSet<double[]> Hset, LogicModelParams param, WdnmoParam wParam, GeneticParams gParam, BigBinaryFunction fitnessFunction)
#elif ValDistinct
        public BigBinaryChromo(int length, int Level, HashSet<BigInteger> Hset, LogicModelParams param, WdnmoParam wParam, GeneticParams gParam, BigBinaryFunction fitnessFunction)
#endif
 
        {
            this.wParam = wParam;
            this.gParam = gParam;
            this.fitnessFunction = fitnessFunction;
            this.param = param;
            this.Level = Level;
            ID = Guid.NewGuid();
            this.length = System.Math.Max(2, System.Math.Min(BigBinaryNums.CMaxLength, length));
            this.distinctDict = Hset;
            Generate();
            str_From = "新建";
            //AttemptArray = fitnessFunction.Translate(this);
            if (distinctDict == null) throw new ArgumentNullException(nameof(distinctDict));
 
 
        }
        bool _is枚举 = false;
        /// <summary>
        /// 枚举时,使用此构造函数来指定值
        /// </summary>
        /// <param name="uVal"></param>
        /// <param name="length"></param>
        /// <param name="Level"></param>
        /// <param name="param"></param>
        /// <param name="wParam"></param>
        /// <param name="gParam"></param>
        /// <param name="fitnessFunction"></param>
#if ArrDistinct
        public BigBinaryChromo(BigInteger uVal, int length, int Level, LogicModelParams param, WdnmoParam wParam, GeneticParams gParam, BigBinaryFunction fitnessFunction, HashSet<double[]> distinctDict)
#elif ValDistinct
        public BigBinaryChromo(BigInteger uVal, int length, int Level, LogicModelParams param, WdnmoParam wParam, GeneticParams gParam, BigBinaryFunction fitnessFunction, HashSet<BigInteger> distinctDict)
#endif
 
        {
            this.wParam = wParam;
            this.gParam = gParam;
            this.fitnessFunction = fitnessFunction;
            this.param = param;
            this.Level = Level;
            this.distinctDict = distinctDict;
            ID = Guid.NewGuid();
            this.length = System.Math.Max(2, System.Math.Min(BigBinaryNums.CMaxLength, length));
            setval(uVal);
            AttemptArray = fitnessFunction.Translate(this);
            fitness = null;
            _is枚举 = true;
            str_From = "枚举生成";
            if (distinctDict == null) throw new ArgumentNullException(nameof(distinctDict));
        }
        protected BigBinaryChromo(BigBinaryChromo source)
        {
            this.fitnessFunction = source.fitnessFunction;
            this.wParam = source.wParam;
            this.gParam = source.gParam;
            this.Level = source.Level;
            this.param = source.param;
            ID = Guid.NewGuid();
            length = source.length;
            setval(source.val);
            if (source.AttemptArray!=null) this.AttemptArray = source.AttemptArray.ToArray();
 
            this.distinctDict = source.distinctDict;
            str_From = "克隆";
            //AttemptArray = fitnessFunction.Translate(this);
            if (distinctDict==null) throw new ArgumentNullException(nameof(distinctDict));
 
 
        }
 
 
 
 
        /// <summary>
        /// init=true后使用await方法
        /// </summary>
        public static bool tested = false;
 
        /// <summary>
        /// init=true后使用await方法
        /// </summary>
        public static bool isInited = false;
 
        public override async void Evaluate(IFitnessFunction function)
        {
            //Log.Add("请求评估");
            if (fitness != null) return;
 
 
            await Task<double>.Run(() => function.Evaluate(this));
            //Trace.WriteLine(val);
            //if (!distinctDict.ContainsKey(val)) distinctDict.TryAdd(val, true);
            //distinctDict.TryAdd(AttemptArray, true);
            return;
 
 
        }
        public async void Evaluate()
        {
 
            if (fitness != null) return;
            await Task<double>.Run(() => fitnessFunction.Evaluate(this));
            return;
        }
 
        public override string ToString()
        {
 
            string s = "";
            s += $"[{Value}]";
            s += fitness == null ? "[]" : $"[{fitness}]";
 
 
            if (AttemptArray != null)
            {
                for (int i = 0; i < AttemptArray.Length; i++)
                {
                    s += "," + Math.Round(AttemptArray[i], 2).ToString();
                }
            }
            return s;
        }
#if ArrDistinct
        public HashSet<double[]> distinctDict = null;
#elif ValDistinct
        public HashSet<BigInteger> distinctDict = null;
#endif
        public override void Generate()
        {
#if DEBUG_TIME
            DateTime time = DateTime.Now;
#endif
 
 
            //byte[] array = new byte[BigBinaryNums.CMaxLength/8];
            //[Cloudflight修改]2024-1-16 随机数生成器不是线程安全的,所以每次都new一个新的
            //rand = new Random();
            //lock (rand)
            //{
            //    rand.NextBytes(array);
            //}
 
            //Random rand = new Random();
            //Random rand = new Random(GetSeed());
            //if (distinctDict == null) distinctDict = new HashSet<double[]>();
            do
            {
                //rand.NextBytes(array);
                //val = ToBigInteger(array);
                ////var bInt = ToByteArray(val);
                //AttemptArray = null;
                //_value = -1;
                if (fitnessFunction.CreateRandomArray(param, wParam, gParam,out dynamic arr,out double[] result))
                {
 
#if ValDistinct
                    var v = fitnessFunction.DisTranslate(result);
                    setval(v);
                    AttemptArray = result;
#endif
 
 
                }
                else
                {
                    if (arr!=null)
                    {
                        wParam.ErrorMessage="随机生成方案错误,尝试信息如下:\r\n"+arr.ToString();
                        return;
                    }
                }
 
            }
            while (!isValid());
 
            
#if DEBUG_TIME
            HydraulicHelper.TimeSet["循环获取新数组"] += (DateTime.Now - time).TotalSeconds;
            HydraulicHelper.TimeSetCount["循环获取新数组"]++;
#endif
        }
 
        bool compareArr(double[] a,double[] b)
        {
            //对比a、b两个数组
            if (a.Length!=b.Length)
                return false;
            for (int i = 0; i < a.Length; i++) 
            {
                if (a[i] != b[i]) return false;
            }
 
            return true;
        }
        //定义一个将byte[]转换为BigInteger的方法
 
        public static BigInteger ToBigInteger(byte[] data)
        {
            if (data == null)
                throw new ArgumentNullException(nameof(data));
 
            Array.Reverse(data); // Reverse the array to match BigInteger's byte order
            return new BigInteger(data);
        }
        //定义一个将BigInteger转换为byte[]的方法
        public static byte[] ToByteArray(BigInteger value)
        {
            return value.ToByteArray();
        }
        public override bool isValid()
        {
            if (_valid) return true;
#if DEBUG_TIME
            var time = DateTime.Now;
#endif
            if (AttemptArray == null) return false;// throw new ArgumentNullException(nameof(AttemptArray));
 
            if (AttemptArray.Any(x => double.IsNaN(x))) return false;
#if ArrDistinct
        //枚举时,不检查重复
            lock(distinctDict)
            {
                if (!_is枚举 && distinctDict.Contains(AttemptArray)) return false;
            }
            lock (distinctDict)
            {
                distinctDict.Add(AttemptArray);
            }
#elif ValDistinct
            if (!_is枚举 && distinctDict.Contains(val)) return false;
            distinctDict.Add(val);
            ////枚举时,不检查重复
            //lock (distinctDict)
            //{
            //    if (!_is枚举 && distinctDict.Contains(val)) return false;
            //}
            //lock (distinctDict)
            //{
            //    distinctDict.Add(val);
            //}
#endif
 
 
            calcParam c = new calcParam();
            c.vars = AttemptArray.ToArray();
            //for (int i=0;i<c.vars.Length;i++)
            //{
            //    param.试算变量[i].Value_Set(0, (double)(c.vars[i]));
            //}
            
            bool flag = true;
            if (param.ChromeFilter!=null)
            {
                var v = param.ChromeFilter;
                if (!(v.Eval.ChangeParam(param, wParam, gParam, c) == 0)) flag = false;
            }
         
#if DEBUG_TIME
            HydraulicHelper.TimeSet["判断有效"] += (DateTime.Now - time).TotalSeconds;
            HydraulicHelper.TimeSetCount["判断有效"]++;
#endif
            if (!flag) return false;
            _valid = true;
            return true;
 
        }
 
        public override IChromosome CreateNew()
        {
            
            return new BigBinaryChromo(length, Level, distinctDict, param, wParam, gParam, fitnessFunction);
        }
 
        public BigBinaryChromo CreateNext(BigInteger value)
        {
            if (value == -1) value = Value;
            var val1 = (value + 1 > MaxValue - 1) ? MaxValue - 1 : value + 1;
            return new BigBinaryChromo(val1, length, Level, param, wParam, gParam, fitnessFunction, distinctDict);
        }
 
        public override IChromosome Clone()
        {
            return new BigBinaryChromo(this);
        }
 
        public override void Mutate()
        {
            //写一套遗传算法的突变逻辑
 
            //fitnessFunction.SetMutateArr(AttemptArray);
            if( fitnessFunction.CreateRandomArray(param, wParam, gParam, out dynamic arr,out double[] result,AttemptArray))
            {
                var v = fitnessFunction.DisTranslate(result);
                setval(v);
                AttemptArray = result;
                
            }
            
 
        }
 
        public override void Crossover(IChromosome pair)
        {
            BigBinaryChromo binaryChromosome = (BigBinaryChromo)pair;
            /*[Cloudflight修改]2024-6-21 
             * 重新定义交叉
*/
            //if (binaryChromosome != null && binaryChromosome.length == length)
            //{
            //    Random rand = new Random(GetSeed());
            //    int num = (BigBinaryNums.CMaxLength - 1) - rand.Next(length - 1);
            //    BigInteger num2 = BigBinaryNums.CMaxValue >> num;
            //    BigInteger num3 = ~num2;
            //    BigInteger num4 = val;
            //    BigInteger num5 = binaryChromosome.val;
            //    setval(((num4 & num2) | (num5 & num3)));
            //    AttemptArray = null;
            //    AttemptArray=fitnessFunction.Translate(this);
            //    binaryChromosome.setval(((num5 & num2) | (num4 & num3)));
            //    binaryChromosome.AttemptArray = fitnessFunction.Translate(this);
 
            //}
            //var arr = AttemptArray.ToArray();
            //var arr1= binaryChromosome.AttemptArray.ToArray();
            Random rand = new Random(GetSeed());
            int num = rand.Next(AttemptArray.Length-1)+1;
            for(int i= 0;i<AttemptArray.Length;i++)
            {
                if (rand.NextDouble()<0.5)
                {
                    var temp = AttemptArray[i];
                    AttemptArray[i] = binaryChromosome.AttemptArray[i];
                    binaryChromosome.AttemptArray[i] = temp;
                }
                //else if (rand.NextDouble()<0.5)
                //{
                //    AttemptArray[i]= AttemptArray[i]+ rand.NextDouble() * (binaryChromosome.AttemptArray[i]- AttemptArray[i]);
                //    binaryChromosome.AttemptArray[i] = AttemptArray[i] + rand.NextDouble() * (binaryChromosome.AttemptArray[i] - AttemptArray[i]);
                //}
 
            }
            var arr1 = AttemptArray.ToArray();
            var v1 = fitnessFunction.DisTranslate(arr1);
            this.setval(v1);
            this.AttemptArray = arr1;
            //AttemptArray = null;
            //AttemptArray = fitnessFunction.Translate(this);
            var arr2 = AttemptArray.ToArray();
            var v2 = fitnessFunction.DisTranslate(arr2);
            binaryChromosome.setval(v2);
            binaryChromosome.AttemptArray = arr2;
            //binaryChromosome.AttemptArray = null;
            //binaryChromosome.AttemptArray = fitnessFunction.Translate(binaryChromosome);
 
 
 
 
        }
 
        public BigInteger getval()
        {
            return val;
        }
    }
    public class BigBinaryNums : IDisposable
    {
        /// <summary>
        /// 当前数据的值
        /// </summary>
        public BigInteger ulongValue;
        /// <summary>
        /// 当前数据的长度
        /// </summary>
        public int length;
 
        /// <summary>
        /// 数据类型的最大值
        /// </summary>
        BigInteger MaxValue;
        /// <summary>
        /// 数据类型的最大长度
        /// </summary>
        int MaxLength;
 
        
 
        public const int CMaxLength = 128;
 
        private static BigInteger _CMaxValue = 0;
 
        public static BigInteger CMaxValue
        {
            get 
            {
                if (_CMaxValue == 0) _CMaxValue = BigInteger.Pow(2, CMaxLength)-1;
                return _CMaxValue;
            }
        } 
 
 
        public double GetDoubleValue1D(int length_D, DRange range, double? accuracy = null)
        {
            if (length - length_D < 0) throw new Exception($"出现long->Arr转换错误,剩余位数{length}不足以进行长度为{length_D}的转换");
            int length_Remain = length - length_D;//获取剩余的长度
            BigInteger ulongMaxValue_1D = MaxValue >> MaxLength - length_D;//num3是取8位二进制的最大值(得到每一个维度的最大长度)
            BigInteger bigValue_1D = And(ulongValue,ulongMaxValue_1D);//取最后8位二进制
            
           
            if (bigValue_1D > ulong.MaxValue) throw new Exception("单个维度超出最大长度(64位)");
            ulong ulongValue_1D = (ulong)bigValue_1D;
          
        
            BigInteger ulongValueRemain = ulongValue >> length_D;//取剩余位数的值
 
 
            length = length_Remain;
            ulongValue = ulongValueRemain;
 
            var acc=(double)accuracy;
            double value=ulongValue_1D * acc;
 
#if DEBUG
            if (value > range.Length)
            {
                throw new Exception($"出现long->Arr转换错误,[{value + range.Min}]超出范围[{range.Min},{range.Max}]");
                return double.NaN;
                
            }
           
#endif
            
            
            return range.Min+ value;
 
            /*[Cloudflight修改]2024-5-31 
*           */
            //将ulongValue_1D转换为double类型
 
            return range.Length / (double)ulongMaxValue_1D * (double)ulongValue_1D + range.Min;
            // getRoundByDouble(range.Length * ulongValue_1D / (double)ulongMaxValue_1D + range.Min,range,accuracy);
 
            /*[Cloudflight修改]2024-5-31 
             * 按照精度来取整数
             */
            //return getRoundByDouble(range.Length * ulongValue_1D / (double)ulongMaxValue_1D + range.Min, range, (double)accuracy);
 
 
        }
        //定义方法,两个BigInteger的位运算与
        public static BigInteger And(BigInteger a, BigInteger b)
        {
            //用其他方法实现与运算,不要用a & b
            byte[] aBytes = a.ToByteArray();
            byte[] bBytes = b.ToByteArray();
            byte[] resultBytes = new byte[Math.Max(aBytes.Length, bBytes.Length)];
            for (int i = 0; i < resultBytes.Length; i++)
            {
                byte aByte = i < aBytes.Length ? aBytes[i] : (byte)0;
                byte bByte = i < bBytes.Length ? bBytes[i] : (byte)0;
                resultBytes[i] = (byte)(aByte & bByte);
            }
            return new BigInteger(resultBytes);
        }
        
        public static double getRoundByDouble(double v, DRange range, double accuracy)
        {
            //精度为accuracy的倍数,且在range范围内,最小值为range.Min,取最接近v的值
            if (accuracy == 0) return v;
            if (v < range.Min) return range.Min;
            if (v > range.Max) return range.Max;
 
            var delta = v - range.Min;
            double x = 0;
            double yushu = delta % accuracy;
            //比较v - delta % accuracy和v+accuracy-delta % accuracy哪个接近v,且在range范围内
            if (yushu > accuracy / 2 && (x=v + accuracy - yushu) < range.Max)
            {
                return x;
            }
            else
            {
                return v - yushu;
            }
        }
 
        public BigBinaryNums(BigInteger value, int length, BigInteger MaxValue, int MaxLength)
        {
            this.length = length;
            this.MaxValue = MaxValue;
            this.ulongValue = value;
            this.MaxLength = MaxLength;
        }
        public void AddBinary(double value, int length_D, DRange range,double Accuray_D)
        {
            if (value>range.Max) throw new Exception($"出现Arr->long转换错误,[{value}]超出范围[{range.Min},{range.Max}]");
            BigInteger ulongMaxValue_1D = MaxValue >> MaxLength - length_D;//num3是取8位二进制的最大值(得到每一个维度的最大长度)
            if (double.IsNaN(value)) value =(int) (range.Min+range.Max)/2;
            ulong ulongValue_1D = (ulong)((value - range.Min) / Accuray_D); 
            BigInteger ulongValueRemain = (ulongValue << length_D) + ulongValue_1D;//取剩余位数的值
            int length_Remain = length + length_D;//获取剩余的长度
             
            length = length_Remain;
            ulongValue = ulongValueRemain;
 
        }
 
        public static BigInteger ArrToUlong(SaveSettings saveSettings, List<double> OriginValueList)
        {
            int COUNT_D = OriginValueList.Count;
 
            BigInteger value = 0;
            int length = 0;
            BigBinaryNums bNum = new BigBinaryNums(value, length, BigBinaryNums.CMaxValue, CMaxLength);
 
            double[] result = new double[COUNT_D];
            ///*[Cloudflight修改]2024-6-21 
            throw new NotImplementedException();
            //for (int i = COUNT_D - 1; i >= 0; i--)
            //{
            //    if (!saveSettings.list_Range[i].IsInside(OriginValueList[i])) return BigBinaryNums.CMaxValue;
            //    bNum.AddBinary(OriginValueList[i], saveSettings.Length_Ds[i], saveSettings.list_Range[i],saveSettings.);
            //};
            return bNum.ulongValue;
        }
 
        public static List<double> UlongToArr(SaveSettings saveSettings, BigInteger uValue)
        {
            List<double> result = new List<double>();
 
            BigInteger value = uValue;//获取整数值
            int length = saveSettings.COUNT_D;//长度
            BigBinaryNums bNum = new BigBinaryNums(value, saveSettings.SumLength, BigBinaryNums.CMaxValue, CMaxLength);
 
 
            for (int i = 1; i <= saveSettings.COUNT_D; i++)
            {
                double item = bNum.GetDoubleValue1D(saveSettings.Length_Ds[i - 1], saveSettings.list_Range[i - 1]);
                result.Add(item);
            };
            return result;
        }
 
        public void Dispose()
        {
 
        }
    }
}