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 <devel@irasnyder.com>
/* Find all idle Elevators */
for (it = elevators_.begin(); it != elevators_.end(); it++)
if (it->is_idle ())
/* 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));
idle_elevators.push_back (&(*it));
if (idle_elevators.size() > 0)
{
if (idle_elevators.size() > 0)
{
+ std::vector<Elevator*>::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);
int num = choose_random_number_in_range (0, idle_elevators.size());
idle_elevators.at (num) -> stop_at (requested_stop);