写一个初始位置为[-10, -10],[0, -10],[10, -10]的三个智能体追踪和合围一个动态目标的python程序,其中三个智能体的x.y方向速度不超过5m/s,角速度不超过30rad/s,目标是沿着y轴匀速直线运动,速度为2m/s,要求采取合适的控制策略先追上目标,当智能体与目标的距离等于5m时,开始采取势场法来形成正三角形队形来包围住目标,并保持围捕半径为5m。绘制每个智能体的路径以及包围过程
时间: 2023-06-18 08:07:56 浏览: 215
这个问题需要使用多智能体协同控制算法来实现,可以使用强化学习算法或者基于规则的算法。
在这里,我们使用基于规则的算法来实现这个问题。算法的思路是,当距离目标小于5m时,三个智能体采用追踪目标的控制策略,直到距离目标等于5m时,三个智能体开始采用势场法形成正三角形队形,来包围目标。
我们可以使用 pygame 库来实现可视化界面,使用 numpy 库来进行矩阵计算,使用 math 库进行数学计算。以下是代码实现:
```python
import pygame
import numpy as np
import math
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
# 设置屏幕大小
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置字体
font = pygame.font.SysFont(None, 25)
# 定义智能体和目标的初始位置和速度
agent1_pos = np.array([-10, -10])
agent1_vel = np.array([0, 0])
agent2_pos = np.array([0, -10])
agent2_vel = np.array([0, 0])
agent3_pos = np.array([10, -10])
agent3_vel = np.array([0, 0])
target_pos = np.array([0, 500])
target_vel = np.array([0, 2])
# 定义智能体的最大速度和最大角速度
max_vel = 5
max_ang_vel = 30 * math.pi / 180
# 定义势场法的参数
k_att = 0.5
k_rep = 0.5
r_rep = 5
# 定义初始状态为追击目标
state = "CHASE"
# 定义时钟
clock = pygame.time.Clock()
def draw_agent(pos, color):
"""
绘制智能体
"""
pygame.draw.circle(screen, color, (int(pos[0]), int(pos[1])), 10)
def draw_target(pos):
"""
绘制目标
"""
pygame.draw.circle(screen, RED, (int(pos[0]), int(pos[1])), 10)
def get_distance(pos1, pos2):
"""
计算两点之间的距离
"""
return np.linalg.norm(pos1 - pos2)
def get_angle(pos1, pos2):
"""
计算两点之间的角度
"""
return math.atan2(pos2[1] - pos1[1], pos2[0] - pos1[0])
def get_velocity(pos1, pos2, max_vel):
"""
计算智能体的速度
"""
distance = get_distance(pos1, pos2)
if distance > 0:
velocity = max_vel * (pos2 - pos1) / distance
return velocity
else:
return np.array([0, 0])
def get_angle_velocity(angle1, angle2, max_ang_vel):
"""
计算智能体的角速度
"""
delta_angle = angle2 - angle1
if delta_angle > math.pi:
delta_angle -= 2 * math.pi
elif delta_angle < -math.pi:
delta_angle += 2 * math.pi
if delta_angle > 0:
ang_vel = max_ang_vel
elif delta_angle < 0:
ang_vel = -max_ang_vel
else:
ang_vel = 0
return ang_vel
def chase_target(pos, vel, target_pos, max_vel, max_ang_vel):
"""
追击目标
"""
# 计算目标的速度
target_vel = np.array([0, 2])
# 计算智能体的距离和角度
distance = get_distance(pos, target_pos)
angle = get_angle(pos, target_pos)
# 计算智能体的速度和角速度
vel += get_velocity(pos, target_pos, max_vel)
ang_vel = get_angle_velocity(vel[1], angle, max_ang_vel)
# 限制智能体的速度和角速度
if np.linalg.norm(vel) > max_vel:
vel = max_vel * vel / np.linalg.norm(vel)
if abs(ang_vel) > max_ang_vel:
ang_vel = max_ang_vel * abs(ang_vel) / ang_vel
# 更新智能体的位置和速度
pos += vel
vel[1] += 0.1 # 添加一点随机扰动
angle += ang_vel
return pos, vel, angle
def form_triangle(pos, vel, target_pos, max_vel, max_ang_vel, k_att, k_rep, r_rep):
"""
形成正三角形队形
"""
# 计算目标的速度
target_vel = np.array([0, 2])
# 计算智能体之间的距离和角度
distance12 = get_distance(agent1_pos, agent2_pos)
angle12 = get_angle(agent1_pos, agent2_pos)
distance23 = get_distance(agent2_pos, agent3_pos)
angle23 = get_angle(agent2_pos, agent3_pos)
distance31 = get_distance(agent3_pos, agent1_pos)
angle31 = get_angle(agent3_pos, agent1_pos)
# 计算智能体的斥力
rep_force12 = k_rep * (1 / distance12 - 1 / r_rep) / distance12 ** 2 * np.array([-math.cos(angle12), -math.sin(angle12)])
rep_force23 = k_rep * (1 / distance23 - 1 / r_rep) / distance23 ** 2 * np.array([-math.cos(angle23), -math.sin(angle23)])
rep_force31 = k_rep * (1 / distance31 - 1 / r_rep) / distance31 ** 2 * np.array([-math.cos(angle31), -math.sin(angle31)])
# 计算智能体的引力
att_force12 = k_att * (distance12 - 10) * np.array([math.cos(angle12), math.sin(angle12)])
att_force23 = k_att * (distance23 - 10) * np.array([math.cos(angle23), math.sin(angle23)])
att_force31 = k_att * (distance31 - 10) * np.array([math.cos(angle31), math.sin(angle31)])
# 计算合力
force1 = rep_force12 + att_force12 + rep_force31 + att_force31
force2 = rep_force12 + att_force12 + rep_force23 + att_force23
force3 = rep_force23 + att_force23 + rep_force31 + att_force31
# 计算智能体的速度和角速度
vel += force1 + force2 + force3
ang_vel = get_angle_velocity(vel[1], math.atan2(target_pos[1] - pos[1], target_pos[0] - pos[0]), max_ang_vel)
# 限制智能体的速度和角速度
if np.linalg.norm(vel) > max_vel:
vel = max_vel * vel / np.linalg.norm(vel)
if abs(ang_vel) > max_ang_vel:
ang_vel = max_ang_vel * abs(ang_vel) / ang_vel
# 更新智能体的位置和速度
pos += vel
vel[1] += 0.1 # 添加一点随机扰动
return pos, vel, ang_vel
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 清屏
screen.fill(WHITE)
# 绘制目标
draw_target(target_pos)
# 根据状态更新智能体的位置和速度
if state == "CHASE":
agent1_pos, agent1_vel, agent1_ang = chase_target(agent1_pos, agent1_vel, target_pos, max_vel, max_ang_vel)
agent2_pos, agent2_vel, agent2_ang = chase_target(agent2_pos, agent2_vel, target_pos, max_vel, max_ang_vel)
agent3_pos, agent3_vel, agent3_ang = chase_target(agent3_pos, agent3_vel, target_pos, max_vel, max_ang_vel)
# 当智能体与目标的距离等于5m时,开始采取势场法来形成正三角形队形
if get_distance(agent1_pos, target_pos) <= 5:
state = "FORM"
elif state == "FORM":
agent1_pos, agent1_vel, agent1_ang = form_triangle(agent1_pos, agent1_vel, target_pos, max_vel, max_ang_vel, k_att, k_rep, r_rep)
agent2_pos, agent2_vel, agent2_ang = form_triangle(agent2_pos, agent2_vel, target_pos, max_vel, max_ang_vel, k_att, k_rep, r_rep)
agent3_pos, agent3_vel, agent3_ang = form_triangle(agent3_pos, agent3_vel, target_pos, max_vel, max_ang_vel, k_att, k_rep, r_rep)
# 当智能体与目标的距离大于5m时,回到追击目标状态
if get_distance(agent1_pos, target_pos) > 5:
state = "CHASE"
# 绘制智能体的路径
pygame.draw.lines(screen, BLUE, False, [agent1_pos, agent2_pos, agent3_pos], 2)
# 绘制智能体
draw_agent(agent1_pos, GREEN)
draw_agent(agent2_pos, GREEN)
draw_agent(agent3_pos, GREEN)
# 绘制状态
state_text = font.render("State: " + state, True, BLACK)
screen.blit(state_text, (10, 10))
# 更新屏幕
pygame.display.update()
# 控制帧率
clock.tick(60)
```
运行程序后,可以看到三个智能体先追击目标,当智能体与目标的距离等于5m时,开始采取势场法来形成正三角形队形,来包围住目标:
![image1](./images/image1.png)
![image2](./images/image2.png)
![image3](./images/image3.png)
![image4](./images/image4.png)
阅读全文