Permalink
Browse files

premium fcfs elevator

  • Loading branch information...
1 parent a2d9552 commit 8b8c3dbf45f74477c08f8697af37bbd7eb439882 @ranxian committed Oct 9, 2012
Showing with 77 additions and 15 deletions.
  1. +2 −2 FcfsFlash.cpp
  2. +1 −0 fcfs.cpp
  3. +28 −8 fcfs_elevator.cpp
  4. +28 −0 floor.cpp
  5. +10 −0 floor.h
  6. +3 −3 passenger.h
  7. +5 −2 thor.cpp
View
4 FcfsFlash.cpp
@@ -46,17 +46,17 @@ void PFcfsFlash() {
int nPassenger = PoissRand(lambda);
for(int j = 0; j < nPassenger; j++) {
Passenger *pass = new Passenger(i, floor(Rand(1, K)));
- FcfsEleIter iter;
floors[i].PushPassenger(pass);
+ if (floors[i].Ordered()) continue;
+
FcfsElevator *idle = Idlest();
if(idle) {
idle->PushOrder(new Order(&floors[i], pass));
}
}
-
floors[i].LetThemWait();
}
}
View
1 fcfs.cpp
@@ -12,6 +12,7 @@ FcfsElevator* Idlest() {
minNPass = iter->Get_nPassenger();
idle = &(*iter);
}
+ if(iter->Get_dir() == '-') return &(*iter);
}
if(minNPass < Elevator::Get_capacity()) return idle;
View
36 fcfs_elevator.cpp
@@ -6,7 +6,7 @@ FcfsElevator::FcfsElevator(int capacity_): Elevator(capacity_) {
};
void FcfsElevator::Move() {
- if(passenger == NULL) {
+ if(nPassenger == 0) {
if(!orders.empty()) {
Order *order = orders.front();
@@ -49,6 +49,8 @@ void FcfsElevator::Move() {
}
return;
}
+
+ Passenger* passenger = passengers.front();
if(pos == passenger->Get_to()) {
if (doorStatus == 1) {
Letout();
@@ -87,16 +89,34 @@ void FcfsElevator::Move() {
}
void FcfsElevator::Letin(Order *order) {
- passenger = order->passenger;
- order->floor->PopPassenger();
- nPassenger++;
+ Floor* floor = order->floor;
+
+ while (floor->Get_nPassenger() && nPassenger < capacity) {
+ passengers.push_back(floor->PopPassenger());
+ nPassenger++;
+ }
+
+ floor->CheckOrder();
}
+struct to_is_pos
+{
+ int pos;
+ to_is_pos(int pos_) : pos(pos_){};
+ bool operator () (Passenger *p) {
+ return p->Get_to() == pos;
+ }
+};
+
void FcfsElevator::Letout() {
- passenger->Arrive();
- passenger = NULL;
- nPassenger--;
- throughput++;
+ for(list<Passenger*>::iterator iter = passengers.begin(); iter != passengers.end(); iter++) {
+ if((*iter)->Get_to() == pos) {
+ nPassenger--;
+ throughput++;
+ (*iter)->Arrive();
+ }
+ }
+ passengers.remove_if( to_is_pos(pos) );
}
void FcfsElevator::PushOrder(Order *order) {
View
28 floor.cpp
@@ -1,8 +1,12 @@
#include "floor.h"
#include "passenger.h"
+#include "fcfs_elevator.h"
+
+FcfsElevator* Idlest();
Floor::Floor() {
nPassenger = 0;
+ ordered = false;
}
void Floor::PushPassenger(Passenger *p) {
@@ -29,4 +33,28 @@ void Floor::LetThemWait() {
for(iter = passengers.begin(); iter != passengers.end(); iter++) {
(*iter)->Wait();
}
+}
+
+Passenger* Floor::FrontPassenger() {
+ if(passengers.empty()) {
+ return NULL;
+ } else {
+ return passengers.front();
+ }
+}
+
+void Floor::CheckOrder() {
+ if (ordered || nPassenger == 0) return;
+ Passenger* pass = passengers.front();
+ FcfsElevator *idle = Idlest();
+
+ if(idle) {
+ Order *order = new Order(this, pass);
+ idle->PushOrder(order);
+ ordered = true;
+ }
+}
+
+bool Floor::Ordered() {
+ return ordered;
}
View
10 floor.h
@@ -11,6 +11,7 @@ class Floor
private:
list<Passenger*> passengers;
int nPassenger;
+ bool ordered;
public:
Floor(); /* Construct funtion */
@@ -25,6 +26,15 @@ class Floor
* The popped(frontest) passenger pointer
*/
Passenger *PopPassenger();
+
+ Passenger *FrontPassenger();
+
void LetThemWait(); /* Let passengers wait for one unit time */
+
+ void CheckOrder();
+
+ bool Ordered();
+
+ friend class Passenger;
};
#endif
View
6 passenger.h
@@ -40,9 +40,9 @@ class Passenger
static int AverWaitTime(); /* Average wait time for all passengers */
- static int MaxDuration(); /* Max Duration of all passengers */
-
- static int MinDuration(); /* Min Duration of all passengers */
+ static int MaxDuration(); /* Max Duration of all passengers */
+
+ static int MinDuration(); /* Min Duration of all passengers */
static int AverDuration(); /* Average Duration of all passengers */
View
7 thor.cpp
@@ -7,6 +7,7 @@
using namespace std;
int FcfsFlash();
+int PFcfsFlash();
int Print();
int K;
@@ -30,19 +31,21 @@ Floor floors[30];
int main(int argc, const char * argv[])
{
+ int DUAL_MAX;
// lambda = atof(argv[1]);
srand(time(NULL));
cin >> K;
cin >> lambda;
cin >> nElevator;
+ cin >> DUAL_MAX;
int capacity = 20;
for(int i = 0; i < nElevator; i++) {
elevators.push_back(FcfsElevator(capacity));
}
- for(dual = 1; dual <= 100; dual++) {
+ for(dual = 1; dual <= DUAL_MAX; dual++) {
system("clear");
- FcfsFlash();
+ PFcfsFlash();
for(FcfsEleIter eleIter = elevators.begin(); eleIter != elevators.end(); eleIter++) {
eleIter->Move();

0 comments on commit 8b8c3db

Please sign in to comment.