// return the best elevator To Assign public Elevator findBestElevator(int RequestedFloor, Column column) { // The priority is for the elevator in mvt !important List <Elevator> elevatorsInMvtList = new List <Elevator>(); // Second priority for the Idle List <Elevator> elevatorIdleList = new List <Elevator>(); // list of others if no much List <Elevator> canBeList = new List <Elevator>(); Elevator bestFitElevator = null; foreach (Elevator elevator in column.elevatorsList) { if (elevator.direction == "Idle" || elevator.position == 1) { elevatorIdleList.Add(elevator); } else { elevatorsInMvtList.Add(elevator); } } if (elevatorIdleList.Count > 0) { bestFitElevator = elevatorIdleList.OrderBy(elevator => Math.Abs(elevator.position - 1)).First(); } else { /** Every elevator have a rating (Score): the rating is based in the distance between elevator and the next destination + distance RC **/ int rating = 0; foreach (Elevator elevator in elevatorsInMvtList) { rating = Math.Abs(elevator.position - elevator.nextDestination) + Math.Abs(elevator.nextDestination - 1); elevator.rating = rating; } // we sort the list a take the first one bestFitElevator = elevatorsInMvtList.OrderBy(elevator => elevator.rating).First(); } return(bestFitElevator); }
//Comparing elevator to previous best public BestElevatorInfo checkBestElevator(int scoreToCheck, Elevator newElevator, BestElevatorInfo bestElevatorInfo, int floor) { //If elevators situation is more favourable, set to best elevator if (scoreToCheck < bestElevatorInfo.bestScore) { bestElevatorInfo.bestScore = scoreToCheck; bestElevatorInfo.bestElevator = newElevator; bestElevatorInfo.referenceGap = Math.Abs(newElevator.currentFloor - floor); //If elevators are in a similar situation, set the closest one to the best elevator } else if (bestElevatorInfo.bestScore == scoreToCheck) { int gap = Math.Abs(newElevator.currentFloor - floor); if (bestElevatorInfo.referenceGap > gap) { bestElevatorInfo.bestScore = scoreToCheck; bestElevatorInfo.bestElevator = newElevator; bestElevatorInfo.referenceGap = gap; } } return(bestElevatorInfo); }
public BestElevatorInfo(Elevator _bestElevator, int _bestScore, int _referanceGap) { this.bestElevator = _bestElevator; this.bestScore = _bestScore; this.referanceGap = _referanceGap; }
public Elevator bestElevator(int FloorNumber) { // 3 list for 3 category of elevators: on mvt , idle and others List <Elevator> elevatorsInMvtList = new List <Elevator>(); List <Elevator> elevatorIdleList = new List <Elevator>(); List <Elevator> canBeList = new List <Elevator>(); Elevator bestFit = null; foreach (Elevator elevator in this.elevatorsList) { // for the direction down if (FloorNumber > 1) { if (elevator.direction == "down" && FloorNumber < elevator.position) { elevatorsInMvtList.Add(elevator); } else if (elevator.direction == "Idle") { elevatorIdleList.Add(elevator); } else { canBeList.Add(elevator); } } // for the direction UP if (FloorNumber < 1) { if (elevator.direction == "up" && FloorNumber > elevator.position) { elevatorsInMvtList.Add(elevator); } else if (elevator.direction == "Idle") { elevatorIdleList.Add(elevator); } else { canBeList.Add(elevator); } } // Console.WriteLine("Elevator " + BestColumn.Id + E.Id + " Direction= " + E.Direction + "| CurrentFloor = " + E.ElevatorCurrentFloor + "| NextStop = " + E.ElevatorNextStop); } if (elevatorsInMvtList.Count > 0) { // Calculate rating(score) and sort the list // return the first one int rating = 0; foreach (Elevator elevator in elevatorsInMvtList) { rating = Math.Abs(elevator.nextDestination - elevator.position) + Math.Abs(elevator.nextDestination - FloorNumber); elevator.rating = rating; } bestFit = elevatorsInMvtList.OrderBy(elevator => elevator.rating).First(); } else if (elevatorIdleList.Count > 0) { // if no best elevator in mvt we take the best Idle int rating = 0; foreach (Elevator elevator in elevatorIdleList) { rating = Math.Abs(elevator.nextDestination - elevator.position) + Math.Abs(elevator.nextDestination - FloorNumber); elevator.rating = rating; } bestFit = elevatorIdleList.OrderBy(elevator => elevator.rating).First(); } else { /************************************************ * If no 1. best elevator on mvt * 2. best nearest Idle * ==> take the nearest elevator when he become availlable **/ int rating = 0; foreach (Elevator elevator in canBeList) { rating = Math.Abs(elevator.position - FloorNumber); elevator.rating = rating; } bestFit = canBeList.OrderBy(elevator => elevator.rating).First(); } return(bestFit); }
// find_Best_Elevator seeks for the best elevator from elevator list in column, it look at direction and distance between floors public Elevator find_Best_Elevator(int FloorNumber, string Direction) { Console.WriteLine("Searching for best elevator to go to floor " + FloorNumber + " in " + Direction + " direction."); if (FloorNumber == this.lowFloor) { Direction = "DOWN"; } else if (FloorNumber == 0) { Direction = "DOWN"; } else if (FloorNumber == this.highFloor) { Direction = "UP"; } List <int> resultArray = new List <int>(); List <int> scoreArray = new List <int>(); int b_elevator; Elevator bestelevator; // If direction is up, function search for "UP" direction elevators, after for "IDLE" one , after any elevator. if (Direction == "UP") { for (int i = 0; i < this.elevator_list.Count; i++) { Elevator elevator_i = this.elevator_list[i]; if (((elevator_i.elevator_direction == "UP") && (elevator_i.elevator_floor <= FloorNumber)) || (elevator_i.elevator_floor == FloorNumber && FloorNumber == 0)) { resultArray.Add(i); int score = Math.Abs(elevator_i.elevator_floor - FloorNumber); scoreArray.Add(score); } } if (scoreArray.Count == 0) { for (int i = 0; i < this.elevator_list.Count; i++) { Elevator elevator_i = this.elevator_list[i]; if (elevator_i.elevator_direction == "IDLE") { resultArray.Add(i); int score = Math.Abs(elevator_i.elevator_floor - FloorNumber); scoreArray.Add(score); } } } if (scoreArray.Count == 0) { for (int i = 0; i < this.elevator_list.Count; i++) { Elevator elevator_i = this.elevator_list[i]; resultArray.Add(i); int score = Math.Abs(elevator_i.elevator_floor - FloorNumber); scoreArray.Add(score); } } int minimum = scoreArray[0]; int location = 0; for (int i = 1; i < scoreArray.Count; i++) { if (scoreArray[i] < minimum) { minimum = scoreArray[i]; location = i; } } b_elevator = resultArray[location]; } else { for (int i = 0; i < this.elevator_list.Count; i++) { Elevator elevator_i = this.elevator_list[i]; if (((elevator_i.elevator_direction == "DOWN") && (elevator_i.elevator_floor >= FloorNumber)) || (elevator_i.elevator_floor == FloorNumber)) { resultArray.Add(i); int score = Math.Abs(elevator_i.elevator_floor - FloorNumber); scoreArray.Add(score); } } if (scoreArray.Count == 0) { for (int i = 0; i < this.elevator_list.Count; i++) { Elevator elevator_i = this.elevator_list[i]; if (elevator_i.elevator_direction == "IDLE") { resultArray.Add(i); int score = Math.Abs(elevator_i.elevator_floor - FloorNumber); scoreArray.Add(score); } } } if (scoreArray.Count == 0) { for (int i = 0; i < this.elevator_list.Count; i++) { Elevator elevator_i = this.elevator_list[i]; resultArray.Add(i); int score = Math.Abs(elevator_i.elevator_floor - FloorNumber); scoreArray.Add(score); } } int minimum = scoreArray[0]; int location = 0; for (int i = 1; i < scoreArray.Count; i++) { if (scoreArray[i] < minimum) { minimum = scoreArray[i]; location = i; } } b_elevator = resultArray[location]; } bestelevator = this.elevator_list[b_elevator]; return(bestelevator); }
public BestElevatorInfo(Elevator bestElevator, int bestScore, int referenceGap) { this.bestElevator = bestElevator; this.bestScore = bestScore; this.referenceGap = referenceGap; }
// Here's the steps to move the elevator once the user is in the elevator public void moveElevator(Elevator elevator) { while (elevator.requestList.Count > 0) { if (elevator.requestList[0] > elevator.currentFloor) { elevator.direction = "UP"; Console.WriteLine(); Console.WriteLine("6- The Elevator is moving to pick up the User and go to his destination"); while (elevator.currentFloor < elevator.requestList[0]) { elevator.currentFloor++; if (elevator.currentFloor != 0) { Console.WriteLine("Elevator " + elevator.ID + " is at floor " + elevator.currentFloor); } if (elevator.currentFloor == elevator.floorList.Last()) { elevator.direction = "IDLE"; } } elevator.door = "OPEN"; Console.WriteLine("Door is opened"); elevator.requestList.RemoveAt(0); } else { elevator.direction = "DOWN"; Console.WriteLine(); Console.WriteLine("7- The Elevator is moving to pick up the User and go to his destination"); while (elevator.currentFloor > elevator.requestList.Last()) { elevator.currentFloor--; if (elevator.currentFloor != 0) { Console.WriteLine("Elevator " + elevator.ID + " is at floor " + elevator.currentFloor); } if (elevator.currentFloor == elevator.floorList.First()) { elevator.direction = "IDLE"; } } elevator.door = "OPEN"; Console.WriteLine("Door is opened"); elevator.requestList.RemoveAt(elevator.requestList.Count - 1); } elevator.door = "CLOSED"; Console.WriteLine("Door is closed"); elevator.direction = "IDLE"; } if (elevator.BufferList.Count > 0) { elevator.requestList = elevator.BufferList; elevator.direction = elevator.BufferDirection; moveElevator(elevator); } else { elevator.direction = "IDLE"; } }
/* Method 2: AssignElevator (RequestedFloor) * This method will be used for the requests made on the first floor. * Example: someone is at 1st floor and requests the 20th floor, so an * elevator should be expected to be sent to the user position * and get him up to the 20th floor. */ public void AssignElevator(int RequestedFloor) { // Here is Finding the best column in columnList Column currentColumn = this.columnList[0]; Console.WriteLine("2- Finding the best columnID and choose the best on which is goind to the same direction as the User"); foreach (var column in this.columnList) { Console.WriteLine("Current columnID " + column.ID + " which is from floor " + column.floorList[1] + " is goind to floor " + column.floorList.Last()); if (RequestedFloor >= column.floorList[1] && RequestedFloor <= column.floorList.Last()) { currentColumn = column; break; } } Console.WriteLine(); Console.WriteLine("3- Here the column has been choosen. The first one which match to the same direction as the User"); Console.WriteLine("the columnID for floor " + RequestedFloor + " is " + currentColumn.ID + "\n"); int gap = 1000; int distance; Elevator BestElevator = currentColumn.elevatorList[0]; string UserDirection; if (RequestedFloor > 1) { UserDirection = "UP"; } else { UserDirection = "DOWN"; } // Finding the best elevator Console.WriteLine("4- Here the method findBestElevator is deployed, findind de best Elevator in column's elevatorList "); foreach (Elevator elevator in currentColumn.elevatorList) { int currentDestination; if (elevator.direction == "IDLE") { currentDestination = 0; } else { currentDestination = elevator.requestList.Last(); } Console.WriteLine("ElevatorID : " + elevator.ID + ", Elevator Position = " + elevator.currentFloor + ", Elevator direction = " + elevator.direction + " and current destination is " + currentDestination); distance = calculateGap(elevator, 1, UserDirection); if (distance <= gap) { gap = distance; BestElevator = elevator; } } Console.WriteLine(); Console.WriteLine("5- The best Column && the nearest Elevator has been found"); Console.WriteLine("Go and Take the column " + currentColumn.ID + ", and the nearest elevator " + BestElevator.ID); if (BestElevator.currentFloor == 1) { Console.WriteLine("The nearest ElevatorID is " + BestElevator.ID); UpdateList(BestElevator, BestElevator.requestList, RequestedFloor); } else { Console.WriteLine("The nearest ElevatorID is " + BestElevator.ID); UpdateList(BestElevator, BestElevator.BufferList, RequestedFloor); // Setting Buffer direction For Basements if (RequestedFloor > 1) { BestElevator.BufferDirection = "DOWN"; } // For other floors else { BestElevator.BufferDirection = "UP"; } } moveElevator(BestElevator); }
/* Method 1: RequestElevator (FloorNumber) * This method represents an elevator request on a floor or basement. * Example: someone is at 54th floor and requests the 1st floor, so an * elevator should be expected to pick the user up at his currentFloor * and bring him back to the 1st floor. */ public void RequestElevator(int FloorNumber) { Column currentColumn = this.columnList[0]; // Here is Finding the best column in columnList foreach (var column in this.columnList) { if (FloorNumber >= column.floorList[1] && FloorNumber <= column.floorList.Last()) { currentColumn = column; break; } } // The if clause determine the USER Direction string direction; if (FloorNumber < 1) { direction = "UP"; } else { direction = "DOWN"; } Console.WriteLine("Current columnID " + currentColumn.ID + " which is from floor " + currentColumn.floorList[1] + " is goind to floor " + currentColumn.floorList.Last()); // finding the nearest elevator by comparing gap AND elevator down: // 1- the moving elevator which is arriving to the user // 2- the IDLE elevator // 3- other elevators int gap = 1000; int distance; /* Define the best elevator which down and ready to serve in the * Elevator List and get the best one in the column of elevatorList */ List <Elevator> FirstElevatorDown = new List <Elevator>(); List <Elevator> SecondElevatorDown = new List <Elevator>(); List <Elevator> ThirdElevatorDown = new List <Elevator>(); Elevator BestElevator = currentColumn.elevatorList[0]; foreach (Elevator elevator in currentColumn.elevatorList) { int currentDestination; if (elevator.direction == "IDLE") { currentDestination = 0; } else { currentDestination = elevator.requestList.Last(); } Console.WriteLine("ElevatorID : " + elevator.ID + ", Elevator Position = " + elevator.currentFloor + ", Elevator direction = " + elevator.direction + " and current destination is " + currentDestination); distance = calculateGap(elevator, FloorNumber, direction); if (elevator.direction == direction) { if ((direction == "UP" && elevator.currentFloor <= FloorNumber) | (direction == "DOWN" && elevator.currentFloor >= FloorNumber)) { FirstElevatorDown.Add(elevator); } } else if (elevator.direction == "IDLE") { SecondElevatorDown.Add(elevator); } else { ThirdElevatorDown.Add(elevator); } } if (FirstElevatorDown.Count > 0) { foreach (Elevator elevator in FirstElevatorDown) { distance = calculateGap(elevator, FloorNumber, direction); if (distance <= gap) { gap = distance; BestElevator = elevator; } } } else if (SecondElevatorDown.Count > 0) { foreach (Elevator elevator in SecondElevatorDown) { distance = calculateGap(elevator, FloorNumber, direction); if (distance <= gap) { gap = distance; BestElevator = elevator; } } } else { foreach (Elevator elevator in ThirdElevatorDown) { distance = calculateGap(elevator, FloorNumber, direction); if (distance <= gap) { gap = distance; BestElevator = elevator; } } } Console.WriteLine("The best ElevatorID is " + BestElevator.ID); // Updating the RequestList of the selected elevator if (BestElevator.direction == direction && BestElevator.direction == "IDLE") { if (BestElevator.direction == "DOWN" && BestElevator.currentFloor >= FloorNumber) { Console.WriteLine("Take the column " + currentColumn.ID + " and ElevatorID: " + BestElevator.ID + " which is currently at floor " + BestElevator.currentFloor); UpdateList(BestElevator, BestElevator.requestList, FloorNumber); UpdateList(BestElevator, BestElevator.requestList, 1); Console.WriteLine("Go and Take the column " + currentColumn.ID + ", and the nearest elevator " + BestElevator.ID); moveElevator(BestElevator); } else if (BestElevator.direction == "UP" && BestElevator.currentFloor <= FloorNumber) { Console.WriteLine("Take the column " + currentColumn.ID + " and ElevatorID: " + BestElevator.ID + " which is currently at floor " + BestElevator.currentFloor); UpdateList(BestElevator, BestElevator.requestList, FloorNumber); UpdateList(BestElevator, BestElevator.requestList, 1); Console.WriteLine("Go and Take the column " + currentColumn.ID + ", and the nearest elevator " + BestElevator.ID); moveElevator(BestElevator); } else if (BestElevator.direction == "IDLE") { Console.WriteLine("Take the column " + currentColumn.ID + " and ElevatorID: " + BestElevator.ID + " which is currently at floor" + BestElevator.currentFloor); UpdateList(BestElevator, BestElevator.requestList, FloorNumber); UpdateList(BestElevator, BestElevator.requestList, 1); Console.WriteLine("Go and Take the column " + currentColumn.ID + ", and the nearest elevator " + BestElevator.ID); moveElevator(BestElevator); } } else { Console.WriteLine("Take the column " + currentColumn.ID + " and ElevatorID: " + BestElevator.ID + " which is currently at floor" + BestElevator.currentFloor); UpdateList(BestElevator, BestElevator.BufferList, FloorNumber); UpdateList(BestElevator, BestElevator.requestList, 1); Console.WriteLine("Go and Take the column " + currentColumn.ID + ", and the nearest elevator " + BestElevator.ID); // For Basements if (FloorNumber > 1) { BestElevator.BufferDirection = "DOWN"; moveElevator(BestElevator); } // For other floors else { BestElevator.BufferDirection = "UP"; moveElevator(BestElevator); } } }
public Elevator findElevator(int requestedFloor, int floorNumber, string direction, int userPosition, int elevatorCurrentFloor) { int bestCase = 0; Elevator bestElevator = null; if (Battery.findColumn(requestedFloor,floorNumber).bestColumn =[0]) { foreach (Elevator i in elevatorList) { if (requestedFloor < 7 && direction == "down" && elevatorCurrentFloor == 7 || floorNumber == 7 && direction == "up" && elevatorCurrentFloor <= userPosition) { bestCase = 4; bestElevator = i; } else if (requestedFloor < 7 && direction == "idle" && elevatorCurrentFloor == 7 || floorNumber == 7 && direction == "idle " && elevatorCurrentFloor == userPosition) { if (bestCase < 3) { bestCase = 3; bestElevator = i; } } else if (requestedFloor < 7 && direction == "idle " && elevatorCurrentFloor != 7 || floorNumber == 7 && direction == "idle" && elevatorCurrentFloor != userPosition) { if (bestCase < 2) { bestCase = 2; bestElevator = i; } } else { if (bestCase < 1) { bestCase = 1; bestElevator = i; } } } } else { foreach (Elevator i in elevatorList) { if (requestedFloor > 7 && direction == "up" && elevatorCurrentFloor == 7 || floorNumber == 7 && direction == "down" && elevatorCurrentFloor >= userPosition) { bestCase = 4; bestElevator = i; } else if (requestedFloor > 7 && direction == "idle" && elevatorCurrentFloor == 7 || floorNumber == 7 && direction == "idle " && elevatorCurrentFloor == userPosition) { if (bestCase < 3) { bestCase = 3; bestElevator = i; } } else if (requestedFloor > 7 && direction == "idle " && elevatorCurrentFloor != 7 || floorNumber == 7 && direction == "idle" && elevatorCurrentFloor != userPosition) { if (bestCase < 2) { bestCase = 2; bestElevator = i; } } else { if (bestCase < 1) { bestCase = 1; bestElevator = i; } } } } return bestElevator; }