# 现代软件工程 结对编程 (II) 电梯调度

<现代软件工程> 的结对编程作业,  作者: 邹欣

Design and implement an Elevator Scheduler to aim for both correctness and performance, in managed code.

Skills to practice:

a)Requirement Analysis

需求分析

b)High level design (interface, information hiding, loose coupling)

程序API 设计,  信息隐藏, 耦合

c) Test Framework Design

设计测试框架, 模拟测试数据

d)Implementation skills

设计的实现

e)Algorithm design

算法设计

## 1.Background

Imagine we’re building a tall office building,   We need to have design an efficient elevator system to carry people to their destinations.    the following is a example of the configuration about elevators:

• The Building has 21 floors, 4 elevators, many passengers use these elevators everyday (passenger weight: average 70kg. max 120kg, min 40g).
• Other constant data: Elevator speed, door open/close time, passenger time for going in/out of the elevator. We can make reasonable assumptions about these.
• The building has 21 floors, from floor 0, 1, ... to 20. Floor 0 is the underground parking level, floor 1 is the lobby level. Most people come in/out the building via these 2 floors.
 Elevator name Service floor list Passenger limit Weight limit 1 All floors 10 800 kg 2 floor 1..10 10 800 kg 3 floor 0,1,2..10 20 1600 kg 4 floor 0,1, 11-20 20 2000 kg

*note: in our test program, the configuration of elevators can be changed,  the scheduler need to read the configuraiton at the initialization time via the API.

## 2.Requirement to Student pairs

2.1Each pair of students will design a set of interface and class definition so that an algorithm provider can provide his/her implementation to the “elevator scheduler” class.

2.2 We will discuss the student’s submission in the class,  pick the best design.

2.3 after the API is decided,  we will focus on the design of test framework

2.4  1-2 volunteers will implment a “test framework” app,  and the rest student pairs will each pair will focus on the implementation of the “elevator scheduler” program.

consideration for the API:

a) how to keep it simple.

b) how to provide enough info for the scheduler to finish the scheduling work,  without knowing too much info?

c) which component is actually driving the elevator?

d) how to regulate proper passenger behavor?  (e.g. if a passenger needs to go to floor 3 from floor 20, but the current elevator can’t go there directly, what should the passenger do?)

consideration for the test framework:

a) how to make sure it generates the same result for the same test cases on a given scheduler?

b) how to check the correctness of the scheduler?

c) how to prevent “cheating” by the scheduler?

d) how to emulate the “real world” efficiently?  (e.g. if 2 passengers are 30 minutes away, does the test framework need to wait for 30 minutes?)

TA will come up with a consistent testing model to test your program according to the “rush hour” scenario (see below), and record the total travel time of all passengers.

You (student pair) have:

1)A set of API

2)A simple solution (Bus program)

3)A set of test cases to run

2.5Explanation of BUS program:

We can have a worst case algorithm called “bus”.This algorithm treats an elevator as a bus,it goes from bottom to top,stops at every floor, open the door, to let people in and out,then close the door and move on.After it reaches the top floor, it will go down.This algorithm can serve all requests, but it’s apparently not the fastest algorithm.

Your code is required to be managed code (C#, managed C++, etc).

• It has to generate 0 (zero) Code Analysis warnings and errors. ( link for Code Analysis in Visual Studio)
• It has to be correct,  all passengers can reach their destinations
• It should be as fast as possible.
• It should not have randomness in scheduling (this is to avoid randomness in testing).

Score guideline:TA will evaluate the “average total travel time” for all passengers in the same test case, the lower, the better. If your performance is lower than “bus” solution, you get 0 points; if your program can’t deliver any passenger to the correct destination, you get 0 points.

One hint about elevator scheduling: When total weight is within 40 kg of the max limit, or the number of passengers is already at maximum, the elevator doesn’t need to stop for more external requests.

The elevator scheduler program doesn’t know how many passengers are waiting on each floor,it doesn’t know how many passengers will show up either.This is the same with the real world situation.

## 3.Testing

TA will simulate a “rush hour” test.The “rush hour” test is to simulate the come-to-work and leave-work scenario in a business building, which has the following 2 parts (they can be run next to each other).

1)Simple test.20 passengers

20 people going thru random floors within 5 minutes.

2)Come-to-work. 1000 total passengers,  duration: 60 minutes

a)80% of them goes from floor 0 and 1 to all other floors, the destination is distributed evenly. The time each passenger arrives at the elevator can be emulated as a normal distribution.

b)20% of them are going between any 2 floors of [2, 20], very few people travel between 2 adjacent floors (e.g. from floor 5 to 4). Other than this, the distribution is also even.

3)Leave-work. 1000 total passengers, duration: 45 minutes

a)90% of them go from other floors to floor 1 or floor 0.

b)10% of them travel between floors [2, 20], again, very few people travel between 2 adjacent floors.

 作业 博客要求 (写1个博客,附加题的解法写另一个博客) 博客注明结对编程人员的名字/或学号后3位. 看教科书和其它参考书, 网站中关于结对编程的章节。例如: http://www.cnblogs.com/xinz/archive/2011/08/07/2130332.html 照至少一张照片, 展现两人在一起合作编程的情况。 说明结对编程的优点和缺点。 结对的每一个人的优点和缺点在哪里 (要列出至少三个优点和一个缺点)。 看教科书和其它资料中关于 Information Hiding, interface design, loose coupling 的章节 说明怎样利用这些好的设计方法。 看 Design by Contract, Code Contract 的内容: http://en.wikipedia.org/wiki/Design_by_contract http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx 描述这些做法的优缺点, 说明你是如何把它们融入你的作业中的。 看教科书中，网上有关 unit test 的内容 http://www.cnblogs.com/xinz/archive/2011/11/20/2255830.html 通过截屏显示你是如何用VS 的 unit test 来保证你写的类的质量的。显示unit test 对你的写的类 (class) 的覆盖率 阅读有关 UML 的内容 画出UML 图显示各个实体之间的关系 (画一个图即可) 实现你的算法 说明你的算法的关键 （不必列出源代码）, 以及独到之处。   不断地把你的代码签入源代码控制  (GitHub 或其他)。 不能只有最后的版本签入，而是应该每做好一个小功能，就签入代码。 [附加题]改进电梯调度的interface  设计, 让它更好地反映现实,更能让学生练习算法, 更好地实现信息隐藏和信息共享。 目前的设计有什么缺点, 你会如何改进它? Analyze the API design, and propose a better API so that Scheduler can have more freedom and students can do more realistic scheduling. [附加题] 目前的这个测试程序只有命令行界面, 请给它设计UI界面, 显示乘客/电梯的运动, 并实现之。 Implement a UI to show how people/elevator moves  (write  a  blog to show your code and UI) [附加题]  阅读有关 MVC 和  MVVM 设计模式的文章。 说明你写的电梯调度的UI /Algorithm/interface如何实现了MVC 或MVVM 的算法。 [附加题] 我们现在的题目是假设所有电梯到达所有的楼层。  在现实生活中,  多部电梯到达的楼层都不一样。如果是这样 (例如3号电梯能到达10 – 20 层,  4 号电梯能到达5-15 层)，整个程序框架和你的电梯调度模块要做什么改变? 请说明你的改进意见

-       老师：希望作业能真正锻炼同学们的软件工程能力

-       助教：希望作业好改，分数有区分度，容易分清好学生和差学生，很容易抓到抄袭

-       学生：希望作业容易，打分公平，有意思，能学到一些有用的技能

-       其他读者：希望能给自己的教学/工作/学习有一些参考

1. 老师宣布作业，讲解作业的背景，要求，软件工程的相关理论。
2. 助教把技术文档交给学生，把测试框架连同简单的测试数据也交给学生
3. 学生阅读技术文档，试运行测试框架，写自己的调度程序，上交自己的调度程序给助教。
4. 助教设计各种新的测试数据，测试学生的调度程序能否满足
1. 基本要求：所有乘客最后都能送到目的地
2. 效能要求：测试程序的KPI,

并以上面练歌结果给学生作业成绩排序。

5. 学生可以做进一步作业（例如实现一个GUI 的电梯运行显示界面），写博客描述自己程序的特点以及学到的软件工程技能。

6. 助教给学生打分，并通过博客留言等方式交流。

7. 老师总结并让同学互相交流

-       等候时间最短？

-       在电梯里的时间最短？

-       …?

-       公共汽车（bus）： 像公共汽车一样，每站都停，到头了，就向反方向走.

-       出租车模式（taxi）: 只接受一个请求，然后就直奔目标，中途顺路的请求都一概不理。

-       正常的电梯：比出租车能接受更多顺路请求，但是不像公共汽车每站都停，来回奔忙。

楼层的情况，电梯的数量，每个电梯运行速度，载客重量和人数的上限，电梯能到达的楼层

当目前这一批乘客都送到目的地后两分钟，如果没有乘客来， 就把电梯排到某个位置。

或者，在 8 点钟把所有电梯都派到一楼等待。

posted @ 2011-03-20 20:09  SoftwareTeacher  阅读(2717)  评论(3编辑  收藏  举报