正态分布在物料需求分配中的应用,附C#实现代码
业务需要:某生产项目物料需求量,根据生产计划跨度,在生产周期中,不同月份物料需求量正态分布。
什么叫正态分布:有个哥们描述的特别好,引用一下。http://hi.baidu.com/snoopywang/blog/item/4d019b1602e6be51f3de3253.html
标准正态分布公式:
具体算法实现:(折叠了)
1private void NormalDistribution(DateTime dRealTimeA, DateTime dRealTimeB, double douQuantity, double douK)
2 {
3 Hashtable has = new Hashtable();
4 //是否开始时间大于结束时间
5 if (dRealTimeA >= dRealTimeB)
6 return;
7
8 //节点的实际延续时间
9 int iDuration = DateDiff(dRealTimeB, dRealTimeA);
10 //实物量正态分布加权总值
11 double douQuantityDaySum = 0;
12 double douQuantitySum = 0;
13
14 Hashtable hasPlanningMonth = new Hashtable();
15 //算各月份天数
16 for (int i = 0; i < iDuration; i++)
17 {
18 double dNum1 = Convert.ToDouble(i);
19 double dDuration = Convert.ToDouble(iDuration);
20
21 double dNum2 = dNum1 - dDuration / 2;
22 double dNum3 = (-1 * dNum2) * (dNum2) * douK;
23 double dNum4 = Math.Exp(dNum3);
24 douQuantityDaySum += dNum4;
25
26 DateTime dPlanningDate = dRealTimeA.AddDays(i);
27 string strMonth = dPlanningDate.ToString("yyyy") + dPlanningDate.ToString("MM");
28 if (!hasPlanningMonth.ContainsKey(strMonth))
29 {
30 hasPlanningMonth.Add(strMonth, 1 * dNum4);
31 }
32 else
33 {
34 //新日期
35 double dDays = Convert.ToDouble(hasPlanningMonth[strMonth].ToString()) + 1 * dNum4;
36 hasPlanningMonth[strMonth] = dDays;
37 }
38 }
39 int iMonthCount = 0;
40
41 foreach (object obj in hasPlanningMonth.Keys)
42 {
43 string strYear = obj.ToString().Substring(0, 4);
44 string strMonth = obj.ToString().Substring(4, 2);
45 //每天实物量
46 double douDayQuantity = douQuantity / douQuantityDaySum;
47 double dDays = Convert.ToDouble(hasPlanningMonth[obj.ToString()].ToString());
48
49 iMonthCount++;
50 int intMonthQuantity = (int)System.Math.Floor(douDayQuantity * dDays);
51 if (iMonthCount == hasPlanningMonth.Keys.Count)
52 {
53 intMonthQuantity = Convert.ToInt32(douQuantity - douQuantitySum);
54 }
55 else
56 {
57 douQuantitySum += intMonthQuantity;
58 }
59
60 has.Add(strYear + strMonth, intMonthQuantity.ToString());
61 }
62 listBox.Items.Clear();
63
64 foreach (object obj in has.Keys)
65 {
66 listBox.Items.Add(obj.ToString() + "---" + has[obj.ToString()] + "\r\n");
67 listBox.Sorted = true;
68 }
69 }
计算结果:
什么叫正态分布:有个哥们描述的特别好,引用一下。http://hi.baidu.com/snoopywang/blog/item/4d019b1602e6be51f3de3253.html
标准正态分布公式:
1private void NormalDistribution(DateTime dRealTimeA, DateTime dRealTimeB, double douQuantity, double douK)
2 {
3 Hashtable has = new Hashtable();
4 //是否开始时间大于结束时间
5 if (dRealTimeA >= dRealTimeB)
6 return;
7
8 //节点的实际延续时间
9 int iDuration = DateDiff(dRealTimeB, dRealTimeA);
10 //实物量正态分布加权总值
11 double douQuantityDaySum = 0;
12 double douQuantitySum = 0;
13
14 Hashtable hasPlanningMonth = new Hashtable();
15 //算各月份天数
16 for (int i = 0; i < iDuration; i++)
17 {
18 double dNum1 = Convert.ToDouble(i);
19 double dDuration = Convert.ToDouble(iDuration);
20
21 double dNum2 = dNum1 - dDuration / 2;
22 double dNum3 = (-1 * dNum2) * (dNum2) * douK;
23 double dNum4 = Math.Exp(dNum3);
24 douQuantityDaySum += dNum4;
25
26 DateTime dPlanningDate = dRealTimeA.AddDays(i);
27 string strMonth = dPlanningDate.ToString("yyyy") + dPlanningDate.ToString("MM");
28 if (!hasPlanningMonth.ContainsKey(strMonth))
29 {
30 hasPlanningMonth.Add(strMonth, 1 * dNum4);
31 }
32 else
33 {
34 //新日期
35 double dDays = Convert.ToDouble(hasPlanningMonth[strMonth].ToString()) + 1 * dNum4;
36 hasPlanningMonth[strMonth] = dDays;
37 }
38 }
39 int iMonthCount = 0;
40
41 foreach (object obj in hasPlanningMonth.Keys)
42 {
43 string strYear = obj.ToString().Substring(0, 4);
44 string strMonth = obj.ToString().Substring(4, 2);
45 //每天实物量
46 double douDayQuantity = douQuantity / douQuantityDaySum;
47 double dDays = Convert.ToDouble(hasPlanningMonth[obj.ToString()].ToString());
48
49 iMonthCount++;
50 int intMonthQuantity = (int)System.Math.Floor(douDayQuantity * dDays);
51 if (iMonthCount == hasPlanningMonth.Keys.Count)
52 {
53 intMonthQuantity = Convert.ToInt32(douQuantity - douQuantitySum);
54 }
55 else
56 {
57 douQuantitySum += intMonthQuantity;
58 }
59
60 has.Add(strYear + strMonth, intMonthQuantity.ToString());
61 }
62 listBox.Items.Clear();
63
64 foreach (object obj in has.Keys)
65 {
66 listBox.Items.Add(obj.ToString() + "---" + has[obj.ToString()] + "\r\n");
67 listBox.Sorted = true;
68 }
69 }
计算结果:
其它:为计算方便公式中的1/K,在编码过程中描术为K。