//schedules elevators based on random input generated.
        public void ScheduleElevators()
        {
            bool             foundPassenger = false;
            PassengerRequest outValue       = new PassengerRequest();
            int requestingFloor             = -1;

            for (int floorIndex = StartFloor; floorIndex < Floors.Length; floorIndex++)
            {
                var floor = Floors[floorIndex];
                if (floor.PassengerRequests.Count > 0)
                {
                    floor.PassengerRequests.TryDequeue(out outValue);
                    if (outValue != null)
                    {
                        if (floorIndex == Floors.Length - 1)
                        {
                            StartFloor = 0;
                        }
                        else
                        {
                            StartFloor = floorIndex + 1;
                        }

                        foundPassenger  = true;
                        requestingFloor = floorIndex;
                        break;
                    }
                }
                if (floorIndex == Floors.Length - 1)
                {
                    StartFloor = 0;
                }
                else
                {
                    StartFloor = floorIndex + 1;
                }
            }
            if (foundPassenger)
            {
                var optimumElevatorAssigned = -1;
                var minimumSteps            = int.MinValue;
                for (int elevatorIndex = 0; elevatorIndex < Elevators.Length; elevatorIndex++)
                {
                    // get the closest elevator.
                    var distance = Elevators[elevatorIndex].GetDistanceToNearestElevator(outValue);
                    if (minimumSteps < distance)
                    {
                        minimumSteps            = distance;
                        optimumElevatorAssigned = elevatorIndex;
                    }
                }
                if (optimumElevatorAssigned != -1)
                {
                    Console.WriteLine($"For the following request {outValue.SourceFloorIndex}-->{outValue.TargetFloorIndex} the elevator # {optimumElevatorAssigned} is assigned");
                    //assigning the request to the elevator.
                    Elevators[optimumElevatorAssigned].AssignJob(outValue);
                }
            }
        }
        private List <PassengerRequest> GetStream()
        {
            int numberOfFloors = Floors.Length;
            int count          = 0;
            List <PassengerRequest> requests = new List <PassengerRequest>();

            while (count < numberOfFloors)
            {
                Random    random        = new Random();
                int       floorIndex    = random.Next(0, Floors.Length);
                int       directionFlag = random.Next(0, 2);
                Direction direction     = Direction.None;

                if (directionFlag == 0)
                {
                    direction = Direction.Up;
                }
                else if (directionFlag == 1)
                {
                    direction = Direction.Down;
                }

                if (floorIndex == 0)
                {
                    direction = Direction.Up;
                }
                else if (floorIndex == numberOfFloors - 1)
                {
                    direction = Direction.Down;
                }

                int exitFloorIndex = -1;
                if (direction == Direction.Up)
                {
                    exitFloorIndex = random.Next(numberOfFloors);
                    while (exitFloorIndex <= floorIndex)
                    {
                        exitFloorIndex = random.Next(numberOfFloors);
                    }
                }
                else
                {
                    exitFloorIndex = random.Next(numberOfFloors);
                    while (exitFloorIndex >= floorIndex)
                    {
                        exitFloorIndex = random.Next(numberOfFloors);
                    }
                }
                count++;
                PassengerRequest request = new PassengerRequest(floorIndex, exitFloorIndex, true);
                Console.WriteLine($"Created request(stream) with {request.SourceFloorIndex}-->{request.TargetFloorIndex} and TimeStamp {request.RequestDateTime}");
                requests.Add(request);
            }
            return(requests);
        }
示例#3
0
        public int GetDistanceToNearestElevator(PassengerRequest request)
        {
            int optDistance = -1;
            int distance    = Math.Abs(CurrentFloor - request.SourceFloorIndex);

            if (CurrentElevatorStatus == ElevatorStatus.Idle)
            {
                optDistance = NumberOfFloors + 1 - distance;
            }
            else if (ElevatorDirection == Direction.Down)
            {
                if (request.SourceFloorIndex > CurrentFloor)
                {
                    optDistance = 1;
                }
                else if (request.SourceFloorIndex < CurrentFloor)
                {
                    if (request.PassengerRequestDirection == ElevatorDirection)
                    {
                        optDistance = NumberOfFloors + 2 - distance;
                    }
                    else
                    {
                        optDistance = NumberOfFloors + 1 - distance;
                    }
                }
            }
            else if (ElevatorDirection == Direction.Up)
            {
                if (request.SourceFloorIndex < CurrentFloor)
                {
                    optDistance = 1;
                }
                else if (request.SourceFloorIndex > CurrentFloor)
                {
                    if (request.PassengerRequestDirection == ElevatorDirection)
                    {
                        optDistance = NumberOfFloors + 2 - distance;
                    }
                    else
                    {
                        optDistance = NumberOfFloors + 1 - distance;
                    }
                }
            }
            return(optDistance);
        }
示例#4
0
 public void AssignJob(PassengerRequest passengerRequest)
 {
     PassengerRequests.Enqueue(passengerRequest);
 }