這篇是對(duì)之前0109編程-基于Plotly實(shí)現(xiàn)的簡單智能體思路文章
掃地bot智能體的修改和改進(jìn):
- 使用tkiner生成窗體契耿,每按空格鍵行走一步螃征。
- 實(shí)時(shí)顯示行走步數(shù)和剩余灰塵數(shù)量盯滚。
- 使用了面向?qū)ο蟮姆绞蕉x灰塵和機(jī)器人淌山。
- 添加了far參數(shù)用來增強(qiáng)智能體在上下左右四個(gè)方向的視野泼疑,修改這個(gè)參數(shù)可以有效增強(qiáng)掃地效率退渗。
以下是完整代碼(部分代碼和思路來自蔡媛媛同學(xué))个粱。
from tkinter import *
import random
import time
dusts_li = [] #灰塵實(shí)例的列表,dust{x,y,id,color,canvas,update()}
#定義Robot類
class Robot:
def __init__(self, canvas, color,x,y):
self.canvas = canvas
self.color=color
self.id = canvas.create_oval(
x - 25, y - 25, x + 25, y + 25, fill=color, outline=color)
self.x = x
self.y = y
def update(self): #更新自身的位置都许。刪除舊圖形嫂冻,添加新圖形。
self.canvas.delete(self.id)
self.id = canvas.create_oval(
self.x - 25, self.y - 25, self.x + 25, self.y + 25, fill=self.color, outline=self.color)
#定義Dust類
class Dust:
def __init__(self, canvas, color, x, y):
self.canvas = canvas
self.id = canvas.create_oval(
x - 5, y - 5, x + 5, y + 5, fill=color, outline=color)
self.x = x
self.y = y
#獲取近鄰四格的灰塵索引
def getNearDusts(bot):
res=[[] for i in range(5)] #左上右下中
far=200 #robot的視野長度
for d in dusts_li: #對(duì)100個(gè)灰塵實(shí)例分別放入左上右下中四個(gè)列表
if bot.x-25-far<=d.x<bot.x-25 and bot.y-25<=d.y<bot.y+25:
res[0].append(d)
elif bot.x-25<=d.x<bot.x+25 and bot.y+25<=d.y<bot.y+25+far:
res[1].append(d)
elif bot.x+25<=d.x<bot.x+25+far and bot.y-25<=d.y<bot.y+25:
res[2].append(d)
elif bot.x-25<=d.x<bot.x+25 and bot.y-25-far<=d.y<bot.y-25:
res[3].append(d)
elif bot.x-25<=d.x<bot.x+25 and bot.y-25<=d.y<bot.y+25:
res[4].append(d)
return res #返回分組后的列表[[d11,d4],[],[d2],[],[d5]]
#定義reset方法避免出范圍
def reset(n):
if n<25:
n=25
if n>475:
n=475
return n
step=0
#移動(dòng)bot方法
def moveBot(event):
idli = getNearDusts(robot) #獲得分組后實(shí)例列表[[d11,d4],[],[d2,d7],[],[d5]]
countli = [len(d) for d in idli[:4]] #獲得分組計(jì)數(shù)列表[2,0,2,0]
maxcount = max(countli) #最高值2
#尋找方向睛低,多個(gè)方向上數(shù)量相同情況下隨機(jī),否則可能卡在死角不動(dòng)
nli = [n for n in range(len(countli)) if countli[n] == maxcount] #最高值索引的列表[0,2]
n = nli[random.randint(0, len(nli) - 1)] #隨機(jī)一個(gè),0或者2钱雷,與idli匹配骂铁,表示左上右下中
px=robot.x
py=robot.y
#移動(dòng)robot數(shù)據(jù)
if n == 0:
robot.x = reset(robot.x - 50)
elif n == 1:
robot.y = reset(robot.y + 50)
elif n == 2:
robot.x = reset(robot.x + 50)
elif n == 3:
robot.y = reset(robot.y - 50)
#吸取中位置的塵土
for dst in idli[4]:
dusts_li.remove(dst)
canvas.delete(dst.id)
#移動(dòng)robot
robot.update()
#計(jì)數(shù)
global step
if robot.x!=px or robot.y!=py:
step+=1
retext()
#tkiner初始化
root = Tk()
root.title("Robot")
root.geometry("500x500+1000+100")
canvas = Canvas(root, width=500, height=500, bd=0, highlightthickness=0)
#生成灰塵
for i in range(100):
dust = Dust(canvas, "grey", random.randint(0, 490), random.randint(0, 490))
botpos = canvas.coords(dust.id)
dusts_li.append(dust)
#初始化bot
robot = Robot(canvas, "red", random.randint(0, 475), random.randint(0, 475))
#狀態(tài)文字
state = {}
def retext():
if 'id' in state:
canvas.delete(state['id'])
state['id'] = canvas.create_text(
(100, 20), text='STEP:{} LEFT:{}'.format(step, len(dusts_li)))
retext()
#創(chuàng)建界面
canvas.bind_all("<space>", moveBot)
canvas.pack()
root.mainloop()
每個(gè)人的智能新時(shí)代
如果您發(fā)現(xiàn)文章錯(cuò)誤罩抗,請(qǐng)不吝留言指正拉庵;
如果您覺得有用,請(qǐng)點(diǎn)喜歡套蒂;
如果您覺得很有用名段,歡迎轉(zhuǎn)載~
END