From ebce8052bac9ec200fab30773230bb69e5c988af Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Sat, 6 Oct 2007 23:34:53 -0700 Subject: [PATCH] Improve ElevatorController's Dispatching Algorithm This improves the ElevatorController's Dispatching Algorithm so that it always sends the closest IDLE Elevator first, rather than just a random IDLE Elevator. Signed-off-by: Ira W. Snyder --- elevatorcontroller.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/elevatorcontroller.cpp b/elevatorcontroller.cpp index d5f760c..034d4d9 100644 --- a/elevatorcontroller.cpp +++ b/elevatorcontroller.cpp @@ -33,10 +33,44 @@ void ElevatorController::call_elevator_to (int floor, Direction direction) /* Find all idle Elevators */ for (it = elevators_.begin(); it != elevators_.end(); it++) if (it->is_idle ()) + { + /* If we have an IDLE elevator that is currently sitting at the + * requested floor, we should obviously send that. */ + if (it->distance_from (requested_stop) == 0) + { + it->stop_at (requested_stop); + return; + } + + /* Was not at the right floor, but still is a good cantidate */ idle_elevators.push_back (&(*it)); + } if (idle_elevators.size() > 0) { + std::vector::iterator idle_it; + bool found = false; + float distance = INT_MAX; + Elevator *e; + + /* Try to send the closest IDLE elevator */ + for (idle_it=idle_elevators.begin(); idle_it!=idle_elevators.end(); idle_it++) + { + if ((*idle_it)->distance_from (requested_stop) < distance) + { + found = true; + distance = (*idle_it)->distance_from (requested_stop); + e = *idle_it; + } + } + + if (found) + { + e->stop_at (requested_stop); + return; + } + + /* No closest IDLE elevator was found, so choose one randomly */ int num = choose_random_number_in_range (0, idle_elevators.size()); idle_elevators.at (num) -> stop_at (requested_stop); -- 2.25.1