Permalink
Browse files

add costflow

  • Loading branch information...
1 parent df2ccef commit 6e1d433a142dae3e50397cc08de9ddecab4bdfca @ranxian committed Oct 19, 2012
Showing with 53 additions and 26 deletions.
  1. +6 −3 Headers/elevator.h
  2. +1 −0 Headers/qt/emulator.h
  3. +1 −1 Sources/costflow.cpp
  4. +18 −8 Sources/elevator.cpp
  5. +1 −0 Sources/fcfs.cpp
  6. +4 −7 Sources/main.cpp
  7. +22 −7 Sources/qt/emulator.cpp
View
@@ -11,6 +11,7 @@ class Elevator
friend class Emulator;
protected:
int pos;
+ double fpos;
bool doorStatus; /* 0: close, 1: open, 2: in toggle door */
int toggleDoorCounter; /* 计算开/关门已花费时间,用以控制门的状态过渡 */
@@ -25,15 +26,13 @@ class Elevator
char dir; /* 电梯运动方向 */
- int defaultDir; /* 电梯默认接客方向 */
-
static double acceleration; /* 电梯加速度 */
std::list<Passenger*> passengers; /* 电梯内乘客 */
std::list<Instruction*> instructions; /* 电梯现有指令栈 */
- int speed;
+ double speed;
void Letout();
@@ -48,6 +47,10 @@ class Elevator
Elevator(); /* Constructor */
int Get_pos(); /* Get current position */
+
+ double Get_fpos();
+
+ static int defaultDir; /* 电梯默认接客方向 */
int Get_nPassenger(); /* Get current passenger number */
View
@@ -18,6 +18,7 @@ class Emulator : public QWidget
Emulator(QWidget *parent = 0);
static int dual; /* dual: Time consumed with the whole duration */
+ int timer;
static int nElevator; /* Total number of all elevators */
View
@@ -99,7 +99,7 @@ void Controller::CostFLow()
flow[path[i]][i] = 0, flow[i][path[i]] = 1;
/* No need to calc the mincost */
}
- printf("%u %u\n", Emulator::orders.size(), tt);
+ printf("%lu %l u\n", Emulator::orders.size(), tt);
//if (! tt) return;
//memset(toDel, 0, sizeof(toDel));
View
@@ -1,12 +1,14 @@
#include "../Headers/elevator.h"
#include "../Headers/emulator.h"
+#include <cmath>
Elevator::Elevator() {
pos = 0;
doorStatus = 0;
- speed = 1;
- toggleDoorCounter = 1;
+ speed = 0;
+ toggleDoorCounter = 0;
dir = '-';
+ fpos = 0;
}
int Elevator::Get_pos() {
@@ -23,7 +25,7 @@ char Elevator::Dir() {
void Elevator::OpenDoor() {
toggleDoorCounter++;
- if (toggleDoorCounter == 2)
+ if (toggleDoorCounter == 100)
doorStatus = 1;
}
@@ -41,7 +43,9 @@ void Elevator::Move() {
} else {
inst = instructions.back()->dest;
}
- if (pos == inst) {
+ if (abs(fpos - inst) < 1e-2) {
+ pos = inst;
+ speed = 0;
dir = '-';
if (doorStatus == 0) OpenDoor();
else {
@@ -56,12 +60,14 @@ void Elevator::Move() {
} else {
if (doorStatus == 1) CloseDoor();
else {
+ if (abs(fpos - inst) < 1 && speed >= 0.005) speed -= acceleration;
+ else if (speed + acceleration <= 0.01) speed += acceleration;
if (pos > inst) {
- pos -= speed;
- dir = '^';
- } else {
+ fpos -= speed*2;
dir = 'v';
- pos += speed;
+ } else {
+ dir = '^';
+ fpos += speed*2;
}
}
}
@@ -149,4 +155,8 @@ int Elevator::Get_defaultDir() {
void Elevator::Set_defaultDir(int dir) {
defaultDir = dir;
+}
+
+double Elevator::Get_fpos() {
+ return fpos;
}
View
@@ -12,6 +12,7 @@ Elevator* Idlest() {
void Controller::Fcfs() {
+ Elevator::defaultDir = 1;
while(Idlest() != NULL) {
if (Emulator::orders.empty()) return;
Elevator* idle = Idlest();
View
@@ -33,7 +33,8 @@ double Emulator::lambda;
string Emulator::elevatorType;
Floor Emulator::floors[30];
-double Elevator::acceleration = 1;
+double Elevator::acceleration = 0.00004;
+int Elevator::defaultDir = 1;
int main(int argc, char *argv[])
{
@@ -45,16 +46,12 @@ int main(int argc, char *argv[])
/* Set runtime parameters */
Emulator::nFloor = 10;
Emulator::lambda = 0.1;
- Emulator::nElevator = 10;
+ Emulator::nElevator = 3;
Emulator::elevatorType = "PFCFS";
/* Initialize elevators */
for(int i = 0; i < Emulator::nElevator; i++) {
- Elevator *ele;
- if (Emulator::elevatorType == "PFCFS") {
- Elevator* elev = new Elevator();
- ele = elev;
- }
+ Elevator* ele = new Elevator();
Emulator::elevators.push_back(ele);
}
View
@@ -62,8 +62,9 @@ Emulator::Emulator(QWidget* parent) : QWidget(parent) {
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(start, SIGNAL(clicked()), this, SLOT(startEmulationSlots()));
+ timer = 0;
- startTimer(200);
+ startTimer(10);
}
/* 开始/暂停游戏 slots */
@@ -85,19 +86,25 @@ void Emulator::startEmulator()
/* 每计数器刷新事件,详见函数内注释 */
void Emulator::timerEvent(QTimerEvent *event)
{
+ if (START) {
+ timer = (timer + 1) % 100;
+ }
+
Q_UNUSED(event);
if(START) {
- Born(); /* 产生乘客,电梯请求 */
-
- Control(); /* 调用调度算法 */
+ if (timer == 0) {
+ Born(); /* 产生乘客,电梯请求 */
+ Control(); /* 调用调度算法 */
+ }
Move(); /* 电梯依据指令或默认逻辑移动 */
Update(); /* 更新统计数据 */
+
+ repaint();
} else {
// Do nothing
}
- repaint();
}
/* Qt::绘制图形界面 */
@@ -113,9 +120,9 @@ void Emulator::paintEvent(QPaintEvent *event)
/* 绘制电梯 */
for(EleIter iter = elevators.begin(); iter != elevators.end(); iter++) {
i++;
- painter.drawRect(150 + i * 50, height() - (128 + (*iter)->Get_pos() * 50), 40, 40);
+ painter.drawRect(150 + i * 50, height() - (128 + (*iter)->Get_fpos() * 50), 40, 40);
painter.setPen(Qt::white);
- painter.drawText(158 + i * 50, height() - (95 + (*iter)->Get_pos() * 50), QString::number((*iter)->Get_nPassenger()));
+ painter.drawText(150 + i * 50, height() - (95 + (*iter)->Get_fpos() * 50), QString::number((*iter)->Get_nPassenger()));//QString::number((*iter)->Get_inst()->size() ));
painter.setPen(QColor(Qt::black));
}
@@ -124,6 +131,12 @@ void Emulator::paintEvent(QPaintEvent *event)
painter.drawRect(60, height() - (84 + i * 50), 110, 4);
painter.setPen(QColor(Qt::gray));
painter.drawText(105, height() - (90 + i * 50), QString::number(floors[i].Get_nPassenger()));
+ painter.setPen(QColor(Qt::red));
+ if (floors[i].UpOrdered())
+ painter.drawText(145, height() - (90 + i * 50), "^");
+ if (floors[i].DownOrdered()) {
+ painter.drawText(145, height() - (90 + i * 50) + 5, "v");
+ }
painter.setPen(QColor(Qt::black));
}
@@ -180,6 +193,8 @@ void Emulator::Born() {
void Emulator::Control() {
if (elevatorType == "PFCFS") {
Controller::Fcfs();
+ } else if (elevatorType == "costflow") {
+ Controller::CostFLow();
}
}

0 comments on commit 6e1d433

Please sign in to comment.