电梯调度算法.docx
《电梯调度算法.docx》由会员分享,可在线阅读,更多相关《电梯调度算法.docx(15页珍藏版)》请在冰豆网上搜索。
电梯调度算法
一、算法设计
1.算法思想
本算法设计一部电梯,通过往返寻找方法,即先查询电梯运行方向的楼层是否存在有其他键被按下,有就继续往该方向运行,如果没有就查询电梯运行反方向的楼层是否有按键被按下,如果有电梯就改变方向,反方向运行。
如果没有电梯就停止在该楼层,30秒后如果没有任何键被按下,电梯就自动返回1楼停住。
同时,电梯乘客所去的楼层方向与电梯当前方向一致的话,则电梯优先搭载该乘客。
随后再搭载去反方向的乘客,以此实现电梯的升降操作。
2.运行环境
本程序用java语言、eclipse平台编写。
3.问题描述
电梯调度算法的基本原则就是如果在电梯运行方向上有人要使用电梯则继续往那个方向运动,如果电梯中的人还没有到达目的地则继续向原方向运动。
具体而言,如果电梯现在朝上运动,如果当前楼层的上方和下方都有请求,则先响应所有上方的请求,然后才向下响应下方的请求;反之。
二、实验步骤及方法
1.由于鼠标单击每个楼层按钮时,需要作出相应的动作,所以我们使用jbutton按钮类,通过jbutton的单击事件来模拟楼层按钮并通过改变颜色来表示按钮是否被按下
2.使用数组存储每个按钮的状态,1表示被按下,0表示未被按下。
用于电梯是否到达目标层的判断,及按钮颜色的刷新判断;
3.“电梯”也是一个jbutton类,通过改变颜色来模拟电梯的运行,需要在此类中设置一个方向值,用0,1,2分别表示电梯的停止向上向下运行。
三、源程序代码
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItemListener;
importjava.util.*;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.Timer;
publicclassLifta{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
Brickbrick=newBrick();
}}
classBrick{//方块类
publicintoctime=0;//计算开关门事件。
publicintj=0;
publicStringclosedoor="关门";
publicStringclose="关";
publicStringopen="开";
JFrameframe=newJFrame("电梯调度算法");
JPanelpan=newJPanel();
JButtonbut=null;
JButtonsign=newJButton(+j+"楼");
JButtonopenclose=newJButton(closedoor+"");
JButtonopenkey=newJButton(open+"");
JButtonclosekey=newJButton(close+"");
publicArrayListblackBrick;;//brick用来依次存放button,程序用button显示蓝色表示有电梯的当前位置。
publicArrayListcontrolBrick;//controlBrick用来依次存放button,表示每层楼电梯口的是上键和下键,程序用button显示绿色表示对应键被按下。
publicint[][]controlTable;//用二维数组controlTable与电梯口的上下键对应,其中如果值1表示button应显示绿色,值0表示button应显示灰色。
publicArrayListnumberBrick;//numberBrick用来依次存放button,程序用button显示桔黄色表示电梯内的数字键被按下。
publicint[]table;//用二维数组table与电梯内的数字键对应,其中如果值1表示button应显示桔黄色,值0表示button应显示灰色。
publicint[]upSignalTable;//纪录向上任务的任务数组,值为1表示相应位的上键被按下。
publicint[]downSignalTable;//纪录向下任务的任务数组,值为1表示相应位的下键被按下。
publicTimertimer;//计时器。
publicLiftThreadlift;//控制每个电梯的线程数组。
//==============构造方法(下)初始化========//
publicBrick(){
sign.setBounds(0,0,80,40);//起始位置
//开关门部分
openclose.setBounds(80,0,80,40);
openclose.setBackground(Color.yellow);
openkey.setBounds(160,0,80,40);
openkey.addActionListener(newOpencolseListener());//开门按钮添加事件
closekey.setBounds(240,0,80,40);
closekey.addActionListener(newOpencolseListener());//关门按钮添加事件
pan.setLayout(null);//画布绝对定位
Fontfnt=newFont("Serief",Font.ITALIC,15);//设置字体样式
//实例化数组
numberBrick=newArrayList(10);
blackBrick=newArrayList(10);
controlBrick=newArrayList(10);
controlTable=newint[10][2];
for(inti=0;i<10;i++)
for(intj=0;j<2;j++)
controlTable[i][j]=0;
table=newint[10];
for(inti=0;i<10;i++)
table[i]=0;
upSignalTable=newint[10];
downSignalTable=newint[10];
for(inti=0;i<10;i++){
upSignalTable[i]=0;
downSignalTable[i]=0;
}
//********************画图************************
for(inti=10;i>0;i--){
//最左边黑色列
but=newJButton();
but.setFont(fnt);
but.setBounds(0,(11-i)*40,80,40);
but.setBackground(Color.BLACK);
blackBrick.add(but);
pan.add(but);
//显示楼层
but=newJButton(+i+"楼");
but.setFont(fnt);
but.setBounds(80,(11-i)*40,80,40);
but.addActionListener(newNumberListener(i));
but.setBackground(Color.gray);
numberBrick.add(but);
pan.add(but);
//向上键
but=newJButton("上");
but.setFont(fnt);
but.setBounds(160,(11-i)*40,80,40);
if(i!
=10)
but.addActionListener(newUpListener(i));
but.setBackground(Color.gray);
controlBrick.add(but);
pan.add(but);
//向下键
but=newJButton("下");
but.setFont(fnt);
but.setBounds(240,(11-i)*40,80,40);
if(i!
=1)
but.addActionListener(newDownListener(i));
but.setBackground(Color.gray);
controlBrick.add(but);
pan.add(but);
}
//*******************运行部分*****************
pan.add(sign);
pan.add(openclose);
pan.add(openkey);
pan.add(closekey);
frame.add(pan);
frame.setSize(329,479);
lift=newLiftThread();
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
//====================================================构造方法(上)=======================================================//
//*************************************************填充方块和设置方法*******************************************************
publicvoidDrawBrick(){//重新刷新整个table,使每个button显示应该的颜色。
for(inti=1;i<=10;i++){
for(intj=0;j<10;j++)
((JButton)blackBrick.get(j)).setBackground(Color.black);
((JButton)blackBrick.get(10-lift.number)).setBackground(Color.blue);
}
}
publicvoidDrawControlBrick(){//将所电梯口的上下键全部重新按当前状态显示颜色。
for(inti=0;i<10;i++)
for(intj=0;j<2;j++)
if(controlTable[i][j]==1){
((JButton)controlBrick.get(i*2+j)).setBackground(Color.green);
}else{
((JButton)controlBrick.get(i*2+j)).setBackground(Color.gray);
}
}
publicvoidDrawNumberBrick(){//将所有电梯内的数字键重新按当前状态显示颜色。
for(inti=0;i<10;i++)
if(table[i]==1)
((JButton)numberBrick.get(i)).setBackground(Color.orange);
else
((JButton)numberBrick.get(i)).setBackground(Color.gray);
}
publicbooleanWorkState(){//判断当前电梯内的数字键是否被按下,如有键被按下,则值为1,如果没有任何键被按下,则值为0。
for(inti=0;i<10;i++){
if(table[i]==1)
returntrue;
}
returnfalse;
}
publicvoidsetState(intnumber){//如果电梯原来停,则改变电梯状态
lift.destination=number;
intx=lift.number;
if(lift.state==0){
if(number>x)
lift.state=1;
if(numberlift.state=2;
}
}
//*****************************************事件处理部分****************************************
publicclassUpListenerimplementsActionListener{//相应上键被按下的动作。
intL=1;//当前楼层。
UpListener(intn){
L=n;
}
publicvoidactionPerformed(ActionEvente){//重新显示上下键的颜色并将向上任务加入向上任务数组。
controlTable[10-L][0]=1;
setState(L);
DrawControlBrick();
}
}
publicclassDownListenerimplementsActionListener{//相应下键被按下的动作。
intL=0;
publicDownListener(intn){
L=n;
}
publicvoidactionPerformed(ActionEvente){
controlTable[10-L][1]=1;
setState(L);
DrawControlBrick();
}
}
publicclassNumberListenerimplementsActionListener{//相应数字键被按下的动作。
intnumber=0;//相应的数字键楼层。
NumberListener(intn){
number=n;
}
publicvoidactionPerformed(ActionEvente){
table[10-number]=1;
setState(number);
DrawNumberBrick();
}
}
publicclassOpencolseListenerimplementsActionListener{//开门关门按钮
//intnumber=0;
OpencolseListener(){
//number=n;
}
publicvoidactionPerformed(ActionEvente){
octime=500;
System.out.println("事件"+octime);
}
}
//***************************************主要监听器********************************************
publicclassTimeListenerimplementsActionListener{//电梯线程的监听器。
************************************
TimeListener(){}
publicvoidactionDown(){//检查向下运行的电梯是否还需向下,即检查在此层下还有没有数字键被按下,如果没有,则置电梯状态为停止,并使该电梯内所有数字键还原。
if(lift.state==2){
intcount=0;
for(inti=lift.number;i>0;i--)
if(table[10-i]==1||controlTable[10-i][1]==1||controlTable[10-i][0]==1)
count++;
if(count==0&&lift.number<=lift.destination){
count=0;
for(intj=lift.number;j<10;j++){
if(table[10-j]==1||controlTable[10-j][1]==1||controlTable[10-j][0]==1){
count++;
}}
if(count>0){
lift.state=1;
//System.out.println("改变方向:
向上");
}else{
lift.state=0;
}}}}
publicvoidactionUp(){//检查向上运行的电梯是否还需向上,即检查在此层上还有没有数字键被按下,如果没有,则置电梯状态为停止,并使该电梯内所有数字键还原。
if(lift.state==1){
intcount=0;
for(inti=lift.number;i<10;i++)
if(table[10-i]==1||controlTable[10-i][0]==1||controlTable[10-i][1]==1)
count++;
if(count==0&&lift.number>=lift.destination){
count=0;
for(intj=lift.number;j>0;j--){
if(table[10-j]==1||controlTable[10-j][1]==1||controlTable[10-j][0]==1){
count++;
}}
if(count>0){
lift.state=2;
//System.out.println("改变方向:
向下");
}else{
lift.state=0;
}}}}
publicvoidactionPerformed(ActionEventevent){
intstate=lift.state;//纪录电梯当前状态。
if(lift.state!
=0&&table[10-lift.number]==1){//如果电梯经过电梯内数字键显示的要到达的楼层,则该数字键状态恢复未按,并重新显示数字键颜色。
table[10-lift.number]=0;
DrawNumberBrick();
newopenclosThread();
}
if(state!
=0&&lift.number==lift.destination){//如果电梯属于逆向到达,则此任务完成,从任务数组中删除并重新显示上下键的颜色。
if(state==2&&controlTable[10-lift.number][state-1]==0){
controlTable[10-lift.number][2-state]=0;
DrawControlBrick();
newopenclosThread();
}
if(state==1&&controlTable[10-lift.number][state-1]==0){
controlTable[10-lift.number][2-state]=0;
newopenclosThread();
DrawControlBrick();
}}
if(state==1){//向上电梯经过的向上任务完成,从任务数组中删除并重新显示上下键的颜色。
if(controlTable[10-lift.number][0]==1){
controlTable[10-lift.number][0]=0;
newopenclosThread();
DrawControlBrick();
}}
if(state==2){//向下电梯经过的向下任务完成,从任务数组中删除并重新显示上下键的颜色。
if(controlTable[10-lift.number][1]==1){
controlTable[10-lift.number][1]=0;
newopenclosThread();
DrawControlBrick();
}}
actionUp();//判断是否继续向上
actionDown();//判断是否继续向下
state=lift.state;//如果电梯状态不为停止,则按照运行方向运行。
if(state==1){
lift.number++;
//System.