Exemplo n.º 1
0
        private static void testPossion()
        {
            double total = 0;

            for (int i = 0; i < 10000; i++)
            {
                total += Possion.exprand(6);
            }
            // Expected output: 10
            Console.WriteLine(total / 10000);
            Console.ReadLine();
        }
Exemplo n.º 2
0
        private static void emulate()
        {
            double start;
            Random randTarget = new Random(System.DateTime.Now.Millisecond);

            Queue <Person>[] ALL = new Queue <Person> [numOfFloors];
            // 初始化各楼层的乘客信息
            for (int i = 0; i < numOfFloors; i++)
            {
                floors[i] = new Floor(i);
                ALL[i]    = new Queue <Person>();
                ALL[i].Clear();
                start = 0;
                for (int j = 0; j < TOTAL; j++)
                {
                    start += Possion.exprand(lamda);
                    Person p = new Person();
                    p.startFloor = i;
                    p.startTime  = start;
                    ALL[i].Enqueue(p);
                }
            }

            // 每秒模拟
            for (time = 0; ; time++)
            {
                // 检查ALL矩阵, 乘客请求电梯
                for (int i = 0; i < numOfFloors; i++)
                {
                    Person p;
                    if (ALL[i].Count > 0)
                    {
                        p = ALL[i].Peek();
                    }
                    else
                    {
                        p = null;
                    }
                    while (p != null && p.startTime < time)
                    {
                        ALL[i].Dequeue();
                        int target = i;
                        // 随机目标楼层
                        while (target == i || target == numOfFloors)
                        {
                            target = randTarget.Next(numOfFloors);
                        }
                        p.targetFloor = target;
                        p.requestElevator(ref floors[i]);
                        if (ALL[i].Count > 0)
                        {
                            p = ALL[i].Peek();
                        }
                        else
                        {
                            p = null;
                        }
                    }
                }

                // 更新楼层参数
                // 不用了,Floor自己完成

                // 触发电梯
                for (int i = 0; i < numOfElevator; i++)
                {
                    elevators[i].getWork(ref floors);
                }
                if (bOver(ref ALL))
                {
                    break;
                }
                if (realTime)
                {
                    System.Threading.Thread.Sleep(200);
                    printResult();
                }
            }
        }