Exemplo n.º 1
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();
                }
            }
        }
Exemplo n.º 2
0
        public void getWork(ref Floor[] floors)
        {
            if (posCounter == Program.FLOORTIME || posCounter == 0) // 电梯停在某一层
            {
                if (posCounter != 0)
                {
                    if (target != position)
                        position += (target > position ? 1 : -1);
                    posCounter = 0;
                }

                // 下客
                if (passenger[position].Count > 0)
                {
                    if (waitCounter == Program.DROPTIME)
                    {
                        while (passenger[position].Count > 0)
                        {
                            Person p = passenger[position].Dequeue();
                            p.dropDown();
                        }
                        waitCounter = 0;
                    }
                    else
                    {
                        waitCounter++;
                        return;
                    }
                }
                if (boardCounter != 0)
                {
                    if (boardCounter == Program.BOARDTIME)
                    {
                        boardCounter = 0;
                    }
                    else
                    {
                        boardCounter++;
                    }
                    return;
                }
                // 上客
                if (target > position) // 电梯继续上行
                {
                    if (floors[position].upQueue.Count > 0)
                    {
                        while (totalPassenger() < capacity)
                        {
                            if (floors[position].upQueue.Count > 0)
                            {
                                Person p = floors[position].upQueue.Dequeue();
                                target = target > p.targetFloor ? target : p.targetFloor;
                                passenger[p.targetFloor].Enqueue(p);
                                p.getOnboard();
                                boardCounter = 1;
                            }
                            else break;
                        }
                    }
                }
                else if ((target < position) && (target != -1)) // 电梯继续下行
                {
                    if (floors[position].downQueue.Count > 0)
                    {
                        while (totalPassenger() < capacity)
                        {
                            if (floors[position].downQueue.Count > 0)
                            {
                                Person p = floors[position].downQueue.Dequeue();
                                target = target < p.targetFloor ? target : p.targetFloor;
                                passenger[p.targetFloor].Enqueue(p);
                                p.getOnboard();
                                boardCounter = 1;
                            }
                            else break;
                        }
                    }
                }
                else if (target == position)
                {
                    if (direction == 1) // 电梯原来上行
                    {
                        if (floors[position].upQueue.Count > 0) // 本行有乘客需求
                        {
                            while (totalPassenger() < capacity)
                            {
                                if (floors[position].upQueue.Count > 0)
                                {
                                    Person p = floors[position].upQueue.Dequeue();
                                    target = target > p.targetFloor ? target : p.targetFloor;
                                    passenger[p.targetFloor].Enqueue(p);
                                    p.getOnboard();
                                    boardCounter = 1;
                                }
                                else break;
                            }
                        }
                        else // 本行无需求,继续找上行需求
                        {
                            for (int i = position; i < numOfFloors; i++)
                            {
                                if (floors[i].upQueue.Count > 0 || floors[i].downQueue.Count > 0)
                                {
                                    target = i;
                                    break;
                                }
                            }
                        }
                    }
                    else if (direction == -1) // 电梯原来下行
                    {
                        if (floors[position].downQueue.Count > 0) // 本行有乘客需求
                        {
                            while (totalPassenger() < capacity)
                            {
                                if (floors[position].downQueue.Count > 0)
                                {
                                    Person p = floors[position].downQueue.Dequeue();
                                    target = target < p.targetFloor ? target : p.targetFloor;
                                    passenger[p.targetFloor].Enqueue(p);
                                    p.getOnboard();
                                    boardCounter = 1;
                                }
                                else break;
                            }
                        }
                        else // 本行无需求,继续找下行需求
                        {
                            for (int i = position; i >= 0; i--)
                            {
                                if (floors[i].upQueue.Count > 0 || floors[i].downQueue.Count > 0)
                                {
                                    target = i;
                                    break;
                                }
                            }
                        }
                    }
                    if (target == position) // 没有需求
                    {
                        target = -1;
                        direction = 0;
                        stopCount++;
                    }
                }
                else if (target == -1) // 电梯原来静止
                {
                    if (stopCount % 2 == 0)
                    {
                        for (int i = position; i < numOfFloors; i++)
                        {
                            if (floors[i].upQueue.Count > 0 || floors[i].downQueue.Count > 0)
                            {
                                target = i;
                                direction = 1;
                                break;
                            }
                        }
                        if (target == -1)
                        {
                            for (int i = position; i >= 0; i--)
                            {
                                if (floors[i].upQueue.Count > 0 || floors[i].downQueue.Count > 0)
                                {
                                    target = i;
                                    direction = -1;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int i = position; i >= 0; i--)
                        {
                            if (floors[i].upQueue.Count > 0 || floors[i].downQueue.Count > 0)
                            {
                                target = i;
                                direction = -1;
                                break;
                            }
                        }
                        if (target == -1)
                        {
                            for (int i = position; i < numOfFloors; i++)
                            {
                                if (floors[i].upQueue.Count > 0 || floors[i].downQueue.Count > 0)
                                {
                                    target = i;
                                    direction = 1;
                                    break;
                                }
                            }
                        }
                    }
                }

            }
            if (direction != 0)
                posCounter++;
        }
Exemplo n.º 3
0
 public void requestElevator(ref Floor floor)
 {
     if (targetFloor > floor.position)
     {
         floor.upQueue.Enqueue(this);
     }
     else
     {
         floor.downQueue.Enqueue(this);
     }
 }