duheng
2025-03-28 dfe7e1653f8309e23e4c314cd58ac4ff7ce49dbc
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
namespace PBS.Console.Test
{
    public class Helper
    {
        private readonly int _numNodes;
        private readonly double[] _baseDemandValues;
        private readonly double[][] _precomputedDemands;
 
        #region 核心算法实现
        private double[][] PrecomputeDemands(int totalRuns)
        {
            var rand = new Random();
            var demandPool = new double[totalRuns][];
 
            for (int i = 0; i < totalRuns; i++)
            {
                demandPool[i] = new double[_numNodes];
                for (int n = 0; n < _numNodes; n++)
                {
                    // 遗传算法式选择
                    int idx = rand.Next(_baseDemandValues.Length);
 
                    // 20%概率变异
                    if (rand.NextDouble() < 0.2)
                        idx = (idx + 1) % _baseDemandValues.Length;
 
                    demandPool[i][n] = _baseDemandValues[idx];
                }
            }
            return demandPool;
        }
 
        private double[][] PrecomputeDemands1(int totalRuns)
        {
            var rand = new Random();
            var demandPool = new double[totalRuns][];
 
            for (int i = 0; i < totalRuns; i++)
            {
                demandPool[i] = new double[_numNodes];
                for (int n = 0; n < _numNodes; n++)
                {
                    // 遗传算法式选择
                    int idx = rand.Next(_baseDemandValues.Length);
 
                    // 20%概率变异
                    if (rand.NextDouble() < 0.2)
                        idx = (idx + 1) % _baseDemandValues.Length;
 
                    demandPool[i][n] = _baseDemandValues[idx];
                }
            }
            return demandPool;
        }
 
 
 
 
        private double[,] GenerateAllDemands(int nodeCount, int totalIterations, double[] weValues)
        {
 
            double[,] demands = new double[totalIterations, nodeCount];
            Parallel.For(0, totalIterations, iter =>
            {
                var rand = new Random();
                for (int node = 0; node < nodeCount; node++)
                {
                    // 遗传算法式选择
                    int idx = rand.Next(weValues.Length);
 
                    // 20%概率变异
                    if (rand.NextDouble() < 0.2)
                        idx = (idx + 1) % weValues.Length;
                    demands[iter, node] = weValues[idx];
                }
            });
            return demands;
        }
 
        #endregion
 
 
 
 
 
        #region 辅助类定义
        public class SimulationResult
        {
            public int RunId { get; set; }
            public DateTime TimeStamp { get; set; }
            public double[] Pressures { get; }
            public double[] Flows { get; }
 
            public SimulationResult(int nodeCount)
            {
                Pressures = new double[nodeCount];
                Flows = new double[nodeCount];
            }
        }
 
        public class SensitivityResult
        {
            public double[] FirstOrderIndices { get; }
            public double[] TotalOrderIndices { get; }
 
            public SensitivityResult(int parameterCount)
            {
                FirstOrderIndices = new double[parameterCount];
                TotalOrderIndices = new double[parameterCount];
            }
        }
 
        private class SobolSequence
        {
            // Sobol序列生成实现(需根据具体数学库实现)
            public double[][] Generate(int samples) => throw new NotImplementedException();
        }
 
        private SensitivityResult CalculateSobolIndices(double[][] inputs, List<double[]> outputs)
        {
            // Sobol指数计算实现
            throw new NotImplementedException();
        }
        #endregion
    }
}