python实现两个agent的随机运动
目前在学习强化学习算法q_learning算法,下面的代码还不能叫做agent,只是实现了两个点的随机运动和轨迹的记录,
#代码中存在的问题
agent运动仅分为上、下、左、右四个方向,还需要加四个斜的方向和运动方向的限制
‘’‘
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
fig=plt.figure()
#画出坐标轴
ax=plt.axes()
#先进行10X10
plt.xlim(0,10)
plt.ylim(0,10)
#定义四个方向
#0,1,2,3,分别表示为上下左右
agent=np.array([‘top’,‘down’,‘left’,‘right’])
#位置随机出现
#x1,y1表示红军单个无人机
#x2,y2表示蓝军单个无人机
x1=np.random.randint(0,10)
y1=np.random.randint(0,10)
x2=np.random.randint(0,10)
y2=np.random.randint(0,10)
plt.scatter(x1,y1,color=‘red’,s=1)
plt.scatter(x2,y2,color=‘blue’,s=1)
for i in range(1000):
#随机选择方向
a = np.random.choice(agent)
b =np.random.choice(agent)
#定义红军运动方向
if a==‘top’:
y1+=0.1
else:
if a==‘down’:
y1-=0.1
else:
if a==‘left’:
x1-=0.1
else:
x1+=0.1
if b==‘top’:
y2+=0.1
else:
if b==‘down’:
y2-=0.1
else:
if b==‘left’:
x2-=0.1
else:
x2+=0.1
#添加运动轨迹
plt.scatter(x1,y1,color=‘red’,s=1)
plt.scatter(x2, y2, color=‘blue’, s=1)
plt.pause(0.01)
‘’‘
’‘’
效果图
#陆续解决的问题
在运动中加入q_learning算法,以红军为例,在蓝军的尾部实现对应的正奖励值,蓝军的头部区域实现对应的负奖励值,改进莫烦中的q_learning中的例子,实现无人机的一对一对抗
设定游戏结束的条件