主要介绍了Python模拟简单电梯调度算法,涉及Python线程、队列、时间延迟等相关操作技巧,需要的朋友可以参考下。
需求:
某栋楼16层楼,有2部电梯。基于线程思想,模拟一个电梯调度程序。
目的:
1、学习调度算法
2、通过实现电梯调度,体会操作系统调度过程
3、学习特定环境下多线程编程方法
实现过程:
定义people类:
#定义people类
pygame.init()
up = pygame.image.load("up.png")
down = pygame.image.load("down.png")
people = []
count = 5
floor_count = 16
def init_people():
for i in range(0, count):
people_in = random.randint(1, floor_count)
people_out = random.randint(1, floor_count)
if people_in > people_out:
up_down = 2
else:
up_down = 1
t = random.randint(1, 50)
people.append([people_in, people_out, up_down, t, 1])
init_people()
input_finish = False
arrive = 0
elevator_right = False
def people_start():
for i in range(0, count):
if people[i][3] == t:
people[i][4] = 1
输入小人进入电梯的楼层:
# 输入小人进入电梯的楼层
def people_wait():
for i in range(0, count):
if people[i][3] == t:
print(i, "号小人从", people[i][0], "楼按了电梯")
if direction[people[i][0]] == 1:
if people[i][2] == 2:
direction[people[i][0]] = 3
if direction[people[i][0]] == 2:
if people[i][2] == 1:
direction[people[i][0]] = 3
if direction[people[i][0]] == 0:
if people[i][2] == 1:
direction[people[i][0]] = 1
if people[i][2] == 2:
direction[people[i][0]] = 2
if running[people[i][0]] == 0:
running[people[i][0]] = 1
if running[people[i][0]] == 2:
running[people[i][0]] = 3
if running1[people[i][0]] == 0:
running1[people[i][0]] = 1
if running1[people[i][0]] == 2:
running1[people[i][0]] = 3
# 小人在电梯里按楼层
def people_sit():
for j in range(0, count):
if people[j][4] == 1: # 状态
if people[j][0] == i: # 楼层
people[j][4] = 2
print(j, "号小人进入了左边电梯并按了", people[j][1], "楼")
if running[people[j][1]] == 1:
running[people[j][1]] = 3
if running[people[j][1]] == 0:
running[people[j][1]] = 2
def people_sit1():
for j in range(0, count):
if people[j][4] == 1 and people[j][0] == i:
people[j][4] = 2
print(j, "号小人进入了右边电梯并按了", people[j][1], "楼")
if running1[people[j][1]] == 1:
running1[people[j][1]] = 3
if running1[people[j][1]] == 0:
running1[people[j][1]] = 2
# 小人离开电梯
def people_out():
for j in range(0, count):
if people[j][4] == 2 and people[j][1] == i:
people[j][4] = 3
print(j, "号小人离开电梯")
# 主程序
while True:
people_finish()
t = t + 1
people_start()
people_wait()
clock.tick(FPS) # 最大帧率控制
screen.fill([225, 224, 40 ])
# 创造电梯穿梭间
pygame.draw.rect(screen, [0, 0, 0], [387, 0, 3, 660], 0)
pygame.draw.rect(screen, [0, 0, 0], [440, 0, 3, 660], 0)
pygame.draw.rect(screen, [0, 0, 0], [307, 0, 3, 660], 0)
pygame.draw.rect(screen, [0, 0, 0], [360, 0, 3, 660], 0)
# 创造电梯
screen.blit(pygame.image.load("people.png"), (400, y))
screen.blit(pygame.image.load("people.png"), (320, y1))
# 绘制楼层
floor_font = pygame.font.Font(None, 40)
for floor in range(1, 17):
floor_text = floor_font.render(str(floor), True, (40, 225, 185))
screen.blit(floor_text, (450, floors[floor]))
screen.blit(floor_text, (250, floors[floor]))
# 楼层变色
for floor in range(1, 17):
if running[floor] == 1:
floor_text = floor_font.render(str(floor), True, (197, 43, 136))
screen.blit(floor_text, (450, floors[floor]))
if running[floor] == 2:
floor_text = floor_font.render(str(floor), True, (194, 197, 43))
screen.blit(floor_text, (450, floors[floor]))
if running[floor] == 3:
floor_text = floor_font.render(str(floor), True, (40, 225, 185))
screen.blit(floor_text, (450, floors[floor]))
if direction[floor] == 1:
screen.blit(up, (500, floors[floor]))
screen.blit(up, (210, floors[floor]))
if direction[floor] == 2:
screen.blit(down, (540, floors[floor]))
screen.blit(down, (170, floors[floor]))
if direction[floor] == 3:
screen.blit(up, (500, floors[floor]))
screen.blit(down, (540, floors[floor]))
screen.blit(up, (210, floors[floor]))
screen.blit(down, (170, floors[floor]))
if running1[floor] == 1:
floor_text = floor_font.render(str(floor), True, (197, 43, 136))
screen.blit(floor_text, (250, floors[floor]))
if running1[floor] == 2:
floor_text = floor_font.render(str(floor), True, (194, 197, 43))
screen.blit(floor_text, (250, floors[floor]))
if running1[floor] == 3:
floor_text = floor_font.render(str(floor), True, (40, 225, 185))
screen.blit(floor_text, (250, floors[floor]))
initial = 0
for i in range(1, 17):
if running[i] == 1:
initial = 1
elif running[i] == 2:
initial = 1
elif running[i] == 3:
initial = 1
else:
pass
if y < 482 and initial == 0:
y = y + speed
move_up = False
initial1 = 0
for i in range(1, 17):
if running1[i] == 1:
initial1 = 1
elif running1[i] == 2:
initial1 = 1
elif running1[i] == 3:
initial1 = 1
else:
pass
if y1 > 2 and initial1 == 0:
y1 = y1 - speed
move_up1 = True
# 电梯移动
priority = 0
if inertia == 1:
for i in range(1, 17):
if running[i] == 1 and y <= floors[i]:
inertia = 1
break
elif running[i] == 2 and y <= floors[i]:
inertia = 1
break
elif running[i] == 3 and y <= floors[i]:
inertia = 1
break
else:
inertia = 0
if inertia == 0:
for i in range(1, 17):
if running[i] == 1 and y >= floors[i]:
inertia = 0
break
elif running[i] == 2 and y >= floors[i]:
inertia = 0
break
elif running[i] == 3 and y >= floors[i]:
inertia = 0
break
else:
inertia = 1
for i in range(1, 17):
if running[i] == 1 and y >= floors[i] and inertia == 0:
y = y - speed
move_up = True
priority = 1
break
if running[i] == 3 and y >= floors[i] and inertia == 0:
y = y - speed
move_up = True
priority = 1
break
if running[i] == 1 and y <= floors[i] and inertia == 1:
y = y + speed
priority = 1
break
if running[i] == 3 and y <= floors[i] and inertia == 1:
y = y + speed
priority = 1
break
for i in range(1, 17):
if priority == 0:
if running[i] == 2 and y >= floors[i] and inertia == 0:
y = y - speed
move_up = True
break
if running[i] == 2 and y <= floors[i] and inertia == 1:
y = y + speed
break
priority1 = 0
if inertia1 == 1:
for i in range(1, 17):
if running1[i] == 1 and y1 <= floors[i]:
inertia1 = 1
break
elif running1[i] == 2 and y1 <= floors[i]:
inertia1 = 1
break
elif running1[i] == 3 and y1 <= floors[i]:
inertia1 = 1
break
else:
inertia1 = 0
if inertia1 == 0:
for i in range(1, 17):
if running1[i] == 1 and y1 >= floors[i]:
inertia1 = 0
break
elif running1[i] == 2 and y1 >= floors[i]:
inertia1 = 0
break
elif running1[i] == 3 and y1 >= floors[i]:
inertia1 = 0
break
else:
inertia1 = 1
for i in range(1, 17):
if running1[i] == 1 and y1 >= floors[i] and inertia1 == 0:
y1 = y1 - speed
move_up1 = True
priority1 = 1
break
if running1[i] == 3 and y1 >= floors[i] and inertia1 == 0:
y1 = y1 - speed
move_up1 = True
priority1 = 1
break
if running1[i] == 1 and y1 <= floors[i] and inertia1 == 1:
y1 = y1 + speed
priority1 = 1
break
if running1[i] == 3 and y1 <= floors[i] and inertia1 == 1:
y1 = y1 + speed
priority1 = 1
break
for i in range(1, 17):
if priority1 == 0:
if running1[i] == 2 and y1 >= floors[i] and inertia1 == 0:
y1 = y1 - speed
move_up1 = True
break
if running1[i] == 2 and y1 <= floors[i] and inertia1 == 1:
y1 = y1 + speed
break
# 电梯到达下客
for i in range(1, 17):
if y == floors[i] and running[i] == 2:
running[i] = 0
time2 = 0
people_out()
if y == floors[i] and running[i] == 3:
running[i] = 1
people_out()
if y1 == floors[i] and running1[i] == 2:
running1[i] = 0
time1 = 0
people_out()
if y1 == floors[i] and running1[i] == 3:
running1[i] = 1
people_out()
# 电梯到达上客
# 上升到达
if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 1 and inertia == 0:
running[i] = 0
running1[i] = 0
direction[i] = 0
time2 = 0
people_sit()
if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 3 and inertia == 0:
direction[i] = 2
time2 = 0
people_sit()
# 下降到达
if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 2 and inertia == 1:
running[i] = 0
time2 = 0
running1[i] = 0
direction[i] = 0
people_sit()
if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 3 and inertia == 1:
direction[i] = 1
time2 = 0
people_sit()
if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 1 and inertia1 == 0:
running1[i] = 0
running[i] = 0
direction[i] = 0
time1 = 0
people_sit1()
if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 3 and inertia1 == 0:
direction[i] = 2
time1 = 0
people_sit1()
# 下降到达
if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 2 and inertia1 == 1:
running1[i] = 0
running[i] = 0
direction[i] = 0
time1 = 0
people_sit1()
if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 3 and inertia1 == 1:
direction[i] = 1
time1 = 0
people_sit1()
# 电梯停止
if time2 <= 50: # 电梯停止时间
if move_up:
y = y + speed
else:
y = y - speed
time2 = time2 + 1
else:
time2 = 100
if time1 <= 50: # 电梯停止时间
if move_up1:
y1 = y1 + speed
else:
y1 = y1 - speed
time1 = time1 + 1
else:
time1 = 100
move_up = False
move_up1 = False
keyboard_get()
# 显示输入楼层
if len(key_floor) == 1:
floor_font = pygame.font.Font(None, 50)
floor_text = floor_font.render(str(key_floor[0]), True, (0, 0, 255))
screen.blit(floor_text, (150, 100))
if len(key_floor) == 2:
floor_font = pygame.font.Font(None, 50)
floor_text = floor_font.render(str(key_floor[0]), True, (0, 0, 255))
screen.blit(floor_text, (150, 100))
floor_font = pygame.font.Font(None, 50)
floor_text = floor_font.render(str(key_floor[1]), True, (0, 0, 255))
screen.blit(floor_text, (170, 100))
pygame.display.update() # 刷新显示
# 输入上客楼层
if arrive == 1:
if input_finish:
arrive = 0
input_finish = False
if len(key_floor) == 1:
for i in range(1, 11):
if key_floor[0] == i:
if direction[i] == 1:
if way == 2:
direction[i] = 3
if direction[i] == 2:
if way == 1:
direction[i] = 3
if direction[i] == 0:
if way == 1:
direction[i] = 1
if way == 2:
direction[i] = 2
if running[i] == 0:
running[i] = 1
if running[i] == 2:
running[i] = 3
if running1[i] == 0:
running1[i] = 1
if running1[i] == 2:
running1[i] = 3
if len(key_floor) == 2:
for i in range(0, 7):
if key_floor[1] == i:
if direction[i + 10] == 1:
if way == 2:
direction[i + 10] = 3
if direction[i + 10] == 2:
if way == 1:
direction[i + 10] = 3
if direction[i + 10] == 0:
if way == 1:
direction[i + 10] = 1
if way == 2:
direction[i + 10] = 2
if running[i + 10] == 0:
running[i + 10] = 1
if running[i + 10] == 2:
running[i + 10] = 3
if running1[i + 10] == 0:
running1[i + 10] = 1
if running1[i + 10] == 2:
running1[i + 10] = 3
while True:
if len(key_floor) >= 1:
del key_floor[0]
else:
break
# 输入下客楼层
if arrive == 0:
if input_finish:
input_finish = False
if elevator_right:
elevator_right = False
if len(key_floor) == 1:
for ii in range(1, 11):
if key_floor[0] == ii:
if running1[ii] == 1:
running1[ii] = 3
if running1[ii] == 0:
running1[ii] = 2
if len(key_floor) == 2:
for ii in range(0, 7):
if key_floor[1] == ii:
if running1[ii + 10] == 1:
running1[ii + 10] = 3
if running1[ii + 10] == 0:
running1[ii + 10] = 2
else:
if len(key_floor) == 1:
for ii in range(1, 11):
if key_floor[0] == ii:
if running[ii] == 1:
running[ii] = 3
if running[ii] == 0:
running[ii] = 2
if len(key_floor) == 2:
for ii in range(0, 7):
if key_floor[1] == ii:
if running[ii + 10] == 1:
running[ii + 10] = 3
if running[ii + 10] == 0:
running[ii + 10] = 2
while True:
if len(key_floor) >= 1:
del key_floor[0]
else:
break
实现效果:


需要完整源码可以关注公众号“小袁同学的随记”回复“电梯调度模拟程序”获取
4620



被折叠的 条评论
为什么被折叠?



