X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=elevatorcontroller.cpp;h=9134efbdd557550efb54a8f9770fc364b89ca1e0;hb=f95be7131a6221491745f0a001f45589aa6acca3;hp=034d4d9941a14b37be2ec6a36089cb5cad8655fd;hpb=ebce8052bac9ec200fab30773230bb69e5c988af;p=cs356-p1-elevator.git diff --git a/elevatorcontroller.cpp b/elevatorcontroller.cpp index 034d4d9..9134efb 100644 --- a/elevatorcontroller.cpp +++ b/elevatorcontroller.cpp @@ -26,6 +26,64 @@ void ElevatorController::call_elevator_to (int floor, Direction direction) if (floor < 0 || floor > number_of_floors_) throw bad_floor (); + ElevatorList::iterator it; + std::vector low_load_elevators; + Stop requested_stop (floor, direction); + const int max_load = number_of_floors_ / 3; + + std::cout << "Using max load: " << max_load << std::endl; + + /* Find all elevators with "low load" */ + for (it=elevators_.begin(); it != elevators_.end(); it++) + { + if (it->getLoad() <= max_load) + low_load_elevators.push_back (&(*it)); + } + + std::vector::iterator lle_it; + bool found = false; + float distance = INT_MAX; + Elevator *e; + int num; + + /* Make sure there are lightly loaded elevators */ + if (low_load_elevators.size() > 0) + { + /* Find the closest lightly loaded elevator */ + for (lle_it = low_load_elevators.begin(); lle_it != low_load_elevators.end(); lle_it++) + { + if ((*lle_it)->distance_from (requested_stop) < distance) + { + found = true; + distance = (*lle_it)->distance_from (requested_stop); + e = *lle_it; + } + } + + /* Found a closest one, send it */ + if (found) + { + std::cout << "Found closest LLE" << std::endl; + e->stop_at (requested_stop); + return; + } + + /* No closest one, so send randomly */ + std::cout << "No closest LLE: choosing randomly" << std::endl; + num = choose_random_number_in_range (0, low_load_elevators.size()); + low_load_elevators.at (num) -> stop_at (requested_stop); + return; + } + + /* There were no lightly loaded elevators, so just choose randomly */ + std::cout << "No LLE at all, choosing randomly" << std::endl; + num = choose_random_number_in_range (0, elevators_.size()); + Elevator &e_ref = elevators_.at (num); + + e_ref.stop_at (requested_stop); + return; + +#ifdef ALGORITHM_IDLE ElevatorList::iterator it; std::vector idle_elevators; Stop requested_stop(floor, direction); @@ -105,6 +163,7 @@ void ElevatorController::call_elevator_to (int floor, Direction direction) Elevator &e_ref = elevators_.at (num); e_ref.stop_at (requested_stop); +#endif } void ElevatorController::elevator_request (int elevator_number, int floor) @@ -136,4 +195,15 @@ void ElevatorController::move_elevators () it->move(); } +bool ElevatorController::oneElevatorWillStillStopAt (int floor, Direction direction) const +{ + ElevatorList::const_iterator it; + + for (it=elevators_.begin(); it!=elevators_.end(); it++) + if (it->willStopAt (floor, direction)) + return true; + + return false; +} + /* vim: set ts=4 sts=4 sw=4 noet tw=112: */