结对项目开发-电梯调度

郑云飞--韩亚华

1 、背景 该大楼有21层,从地面0 , 1 ​​, ... 20 。地板0是地下停车场水平, 1楼是大堂的水平。大多数人都在

走出大楼通过这些2层。一个关于电梯调度提示:当总重量在45公斤的最大限制,或乘客的数量已经在最大,电梯不需

要更多的外部请求停止。 电梯调度程序不知道有多少乘客等待每层,它不知道有多少乘客将出现。这与现实世界的情

况是一样的。

 

   得到这个题目后,我和飞哥商议后决定用C#完成这个模拟程序,这样可以利用以利用它的可视化编程缩短时间,

于是我们花了四天时间了粗略解了C#的语法。然后要构思如何实现电梯调度。通过各种假设我们充分认识到了这个问

题的复杂性,如果各种情况都考虑到,我们感觉到对我们来说太有挑战性了,而且网上说的各种算法我们也看不懂。所

以我们把问题人为地简化,让它实现有限的调度和模拟。为了简化难度我们规定对电梯的调度时每个请求只进行一次最

优计算。也就是电梯不会动态的选择电梯,这样虽然降低了电梯的效率,但也会简化电梯调度的难度。而我们计算最优

解的方法是时间最短原则,既让顾客等的时间最短。同时遵守同向优先,而且顾客不会出现和自己目的相反的方向的运行

。至于重量限制和人数限制在电梯里设计个标志位即可。

时间 2014.3.4-2014.3.7 2014.3.8 2014.3.9
韩亚华 学习C#的简单语法和案例,了解我们可能用到的部分 思考电梯的时间方案,并上网了解相关资料。 共同讨人我们对电梯设计的看法,制定共同的方案
郑云飞 查阅资料了解各种电梯调度的算法

 

 

 

 

 

2014-03-17

     经过一周的时间我们终于完成了电梯的一部分功能,能实现电梯的外部电动。如下图:

部分代码:

 

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace text1
{
    public partial class elevator1 : Form
    {
        public elevator1()
        {
            InitializeComponent();
        }
        public void showMessage(Dianti eleavtor)
        {
            //eleavtor001=new Form5();
             textBox1.Text = "电梯1已到达" + eleavtor.getCurrentFloor().ToString() + "楼,到达的乘客请出楼梯。" + "\n";
             textBox1.AppendText("请选择要去的楼层。\n");
            Application.Run(Program.pElevator.elevator001);
            //eleavtor001.Show();
            //eleavtor001.Hide();

        }
        public void warnMessage()
        {
            textBox1.AppendText("不合法的请求!");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(6);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(5);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(9);
        }

        private void button20_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(13);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(17);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(1);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(10);
        }

        private void button19_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(14);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(18);
        }

        private void button22_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void button23_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(19);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(15);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(11);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(7);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(3);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(4);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(8);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(12);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(16);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(20);
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            label1.BackColor = Color.DarkGray;
            label2.BackColor = Color.DarkGray;
            label3.BackColor = Color.DarkGray;
            timer1.Interval = 500;
            timer1.Start();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button21_Click(object sender, EventArgs e)
        {
            Program.pElevator.scheduing1.getelevator1().getInMessage(0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           
            if (Program.pElevator.scheduing1.getelevator1().getInRequestNum() == 0&&Program.pElevator.scheduing1.getelevator1().getOutRequestNum()==0)
            {
                label1.BackColor = Color.DarkGray;
                label3.BackColor = Color.DarkGray;
                if (label2.BackColor == Color.DarkGray)
                {
                    label2.BackColor = Color.DarkOrange;
                }
                else 
                {
                    label2.BackColor = Color.DarkGray;
                }
            }
            else if (Program.pElevator.scheduing1.getelevator1().getStase() == elevatorstase.moveUp)
            {
                label2.BackColor = Color.DarkGray;
                label3.BackColor = Color.DarkGray;
                if (label1.BackColor == Color.DarkGray)
                {
                    label1.BackColor = Color.DarkOrange;
                }
                else
                {
                    label1.BackColor =Color.DarkGray;
                }
            }
            else if (Program.pElevator.scheduing1.getelevator1().getStase() == elevatorstase.moveDown)
            {
                label1.BackColor=Color.DarkGray;
                label2.BackColor=Color.DarkGray;
                if (label3.BackColor == Color.DarkGray)
                {
                    label3.BackColor = Color.DarkOrange;
                }
                else
                {
                    label3.BackColor = Color.DarkGray;
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace text1
{
   
    public class Scheduing
    {
        
        public Scheduing()
        {
        }
        public class message
        {
            public int requestFloor;
            public elevatorstase goDirection;
            public message()
            {
                requestFloor = 0;
                goDirection = elevatorstase.weit;
            }
        }
        //public struct[100] newMassage1=new massage();
        public bool chooseSstrt = false;
        //message lastMessgge=new message();
        private Queue<message> newMessage1 = new Queue<message>();
        private Dianti elevator1=new Dianti();
        private Dianti elevator2=new Dianti();
        private Dianti elevator3=new Dianti();
        private Dianti elevator4=new Dianti();
        public Dianti getelevator1()
        {
            return elevator1;
        }
        public Dianti getelevator2()
        {
            return elevator2;
        }
        public Dianti getelevator3()
        {
            return elevator3;
        }
        public Dianti getelevator4()
        {
            return elevator4;
        }
        public void getRequest(int floor,elevatorstase stase)
        {
            message New=new message();
            New.requestFloor=floor;
            New.goDirection=stase;
            //if(!(floor==lastMessgge.requestFloor&&lastMessgge.goDirection==stase))
            newMessage1.Enqueue(New);
            if (chooseSstrt == false)
            {
                chooseSstrt = true;
                choose();

             }
            
        }
        public void choose()
        {
            while (newMessage1.Count()!=0)
            {
                message get = new message();
                get = newMessage1.Dequeue();
                int min, choce = 1;
                min = compare(elevator1, get);
                if (min > compare(elevator2, get))
                {
                    min = compare(elevator2, get);
                    choce = 2;
                }
                if (min > compare(elevator3, get))
                {
                    min = compare(elevator3, get);
                    choce = 3;
                }
                if (min > compare(elevator4, get))
                {
                    min = compare(elevator4, get);
                    choce = 4;
                }
                if (min != 100)
                {
                    switch (choce)
                    {
                        case 1:
                            {
                                if (elevator1.getStase() == elevatorstase.weit)
                                {
                                    if (get.requestFloor > elevator1.getCurrentFloor())
                                    {
                                        if (get.goDirection == elevatorstase.moveDown)
                                        {
                                            elevator1.theBusyState.lastDirection = get.goDirection;
                                            elevator1.theBusyState.lastRequestFloor = get.requestFloor;
                                            elevator1.theBusyState.busy= true;
                                        }
                                        elevator1.setStase(elevatorstase.moveUp);
                                    }
                                    else if (get.requestFloor < elevator1.getCurrentFloor())
                                    {
                                        if (get.goDirection == elevatorstase.moveUp)
                                        {
                                            elevator1.theBusyState.lastDirection = get.goDirection;
                                            elevator1.theBusyState.lastRequestFloor = get.requestFloor;
                                            elevator1.theBusyState.busy = true;
                                        }
                                        elevator1.setStase(elevatorstase.moveDown);
                                    }
                                    /*else
                                    {
                                        elevator1.setStase(elevatorstase.weit);
                                    }*/
                                }
                                elevator1.getRequset(get.requestFloor, 1);
                            } break;
                        case 2:
                            {
                                if (elevator2.getStase() == elevatorstase.weit)
                                {
                                    if (get.requestFloor > elevator2.getCurrentFloor())
                                    {
                                        if (get.goDirection == elevatorstase.moveDown)
                                        {
                                            elevator2.theBusyState.lastDirection = get.goDirection;
                                            elevator2.theBusyState.lastRequestFloor = get.requestFloor;
                                            elevator2.theBusyState.busy = true;
                                        }
                                        elevator2.setStase(elevatorstase.moveUp);
                                    }
                                    else if(get.requestFloor<elevator2.getCurrentFloor())
                                    {
                                        if (get.goDirection == elevatorstase.moveUp)
                                        {
                                            elevator2.theBusyState.lastDirection = get.goDirection;
                                            elevator2.theBusyState.lastRequestFloor = get.requestFloor;
                                            elevator2.theBusyState.busy = true;
                                        }
                                        elevator2.setStase(elevatorstase.moveDown);
                                    }
                                }
                                elevator2.getRequset(get.requestFloor, 2);

                            } break;
                        case 3:
                            {
                                if (elevator3.getStase() == elevatorstase.weit)
                                {
                                    if (get.requestFloor > elevator3.getCurrentFloor())
                                    {
                                        if (get.goDirection == elevatorstase.moveDown)
                                        {
                                            elevator3.theBusyState.lastDirection = get.goDirection;
                                            elevator3.theBusyState.lastRequestFloor = get.requestFloor;
                                            elevator3.theBusyState.busy = true;
                                        }
                                        elevator3.setStase(elevatorstase.moveUp);
                                    }
                                    else if(get.requestFloor<elevator3.getCurrentFloor())
                                    {
                                        if (get.goDirection == elevatorstase.moveUp)
                                        {
                                            elevator3.theBusyState.lastDirection = get.goDirection;
                                            elevator3.theBusyState.lastRequestFloor = get.requestFloor;
                                            elevator3.theBusyState.busy = true;
                                        }
                                        elevator3.setStase(elevatorstase.moveDown);
                                    }
                                }
                                elevator3.getRequset(get.requestFloor, 3);

                            } break;
                        case 4:
                            {
                                if (elevator4.getStase() == elevatorstase.weit)
                                {
                                    if (get.requestFloor > elevator4.getCurrentFloor())
                                    {
                                        if (get.goDirection == elevatorstase.moveDown)
                                        {
                                            elevator4.theBusyState.lastDirection = get.goDirection;
                                            elevator4.theBusyState.lastRequestFloor = get.requestFloor;
                                            elevator4.theBusyState.busy = true;
                                        }
                                        elevator4.setStase(elevatorstase.moveUp);
                                    }
                                    else if(get.requestFloor<elevator4.getCurrentFloor())
                                    {
                                        if (get.goDirection == elevatorstase.moveUp)
                                        {
                                            elevator4.theBusyState.lastDirection = get.goDirection;
                                            elevator4.theBusyState.lastRequestFloor = get.requestFloor;
                                            elevator4.theBusyState.busy = true;
                                        }
                                        elevator4.setStase(elevatorstase.moveDown);
                                    }
                                }
                                elevator4.getRequset(get.requestFloor, 4);
                            } break;
                    }
                }
            }
            chooseSstrt = false;
        }
        public int compare(Dianti elevator,message Get)
        {
            if (elevator.theBusyState.busy == true)
            {
                if (Get.requestFloor == elevator.theBusyState.lastRequestFloor && elevator.theBusyState.lastDirection == Get.goDirection)
                {
                    return 0;
                }
                else
                {
                    return 100;
                }
            }
            else if (elevator.getStase() == Get.goDirection)
            {
                if (elevator.getStase() == elevatorstase.moveUp && elevator.getCurrentFloor() < Get.requestFloor)
                {
                    return Get.requestFloor - elevator.getCurrentFloor();
                }
                else if (elevator.getStase() == elevatorstase.moveDown && elevator.getCurrentFloor() > Get.requestFloor)
                {
                    return elevator.getCurrentFloor() - Get.requestFloor;
                }
                else
                    return 100;
            }
            else if (elevator.getStase() == elevatorstase.weit)
            {
                if (Get.goDirection == elevatorstase.moveUp)
                {
                    if (Get.requestFloor >= elevator.getCurrentFloor())
                    {
                        return Get.requestFloor - elevator.getCurrentFloor();
                    }
                    else
                    {
                        return elevator.getCurrentFloor() - Get.requestFloor;
                    }
                }
                else
                {
                    if (Get.requestFloor <= elevator.getCurrentFloor())
                    {
                        return elevator.getCurrentFloor() - Get.requestFloor;
                    }
                    else
                    {
                        return Get.requestFloor - elevator.getCurrentFloor();
                    }
                }


            }
            else
                return 100;
   
        }
    }
}

 

我们把电梯的请求分为了内部请求和外部请求,而我们的调度函数主要解决相对复杂的外部请求。同时为了实现四部电梯的同时

运作。我们用使用timer调用线程池里的线程解决,同时还用到异步调用。

参考相关文献

http://www.cnblogs.com/mashang/archive/2009/08/01/1536730.html

http://www.cnblogs.com/inforasc/archive/2009/10/12/1582110.html

http://www.csharpwin.com/csharpspace/6074r865.shtml

细细品味C#
——Timer 及多线程编程

 

posted @ 2014-03-09 20:13  南风咖啡  阅读(288)  评论(21编辑  收藏  举报