关于目标的【预测】和POD的【控制】,如果控制得准确又及时,那么画面中的目标始终处于中心,不存在roi轨迹
如果控制不够好,不能将目标锁定在画面中心,那么此时利用预测来辅助控制的话,会导致两者相互掣肘,越控越差、越预测越离谱
因此需要拿目标相对地面的运动轨迹来做预测。
假定:
- 飞机高度H=3000米
- 目标初始化区域为半径R=9000米的圆C
- 目标速度变化范围为0-72km/h(0-20m/s)
- 暂时不考虑目标的高度变化
- 读取10s,预测10s
生成数据
- 每个训练回合开始,目标在随机位置初始化,方向随机,速度随机,运动方式随机(静止/匀速/变速/混合,直线/左转/右转/混合)
- 每个训练回合中,目标在C中的运动步数STEPS需要结合论文和代码以进一步确认
- 间隔0.01秒保存目标位置,每组数据采样60s
网页
DeepSeek-Coder
感觉每天生成的代码都不一样,看样子每天都在训练
prompt
py写程序:
1、生成一个随机整数A,A只能为1或2或3,打印A
2、定义一个小车B,B前向速度为0m/s到20m/s之间的随机大小S
3、当A为1,B作直线运动、且初始方向与X轴的夹角随机;
4、当A为2,前进方向与X轴的夹角的角速度为5°/s到30°/s之间的随机大小,B初始方向与X轴的夹角随机
5、当A为3,前进方向与X轴的夹角的角速度为-5°/s到-30°/s之间的随机大小,B初始方向与X轴的夹角随机
6、B运行10秒,取样间隔为0.01秒
7、将以上过程重复1000遍,将B的所有运动轨迹点在同一张图里画出来
生成代码
import random
import matplotlib.pyplot as plt
import numpy as np
# Function to simulate the car's motion
def simulate_car_motion(S, A, total_time=10, time_step=0.01):
# Initialize the car's position and direction
x, y, theta = 0, 0, 0 # Initial position at the origin, initial direction along the X-axis
# Determine the car's motion based on A
if A == 1:
# Straight line motion
theta = random.uniform(0, 2 * np.pi) # Random initial direction
elif A == 2:
# Left turn
theta = random.uniform(0, 2 * np.pi) # Random initial direction
theta_dot = np.deg2rad(random.uniform(5, 30)) # Random angular velocity for left turn
elif A == 3:
# Right turn
theta = random.uniform(0, 2 * np.pi) # Random initial direction
theta_dot = np.deg2rad(random.uniform(-30, -5)) # Random angular velocity for right turn
# Simulate the car's motion
positions = [(x, y)]
num_steps = int(total_time / time_step)
for _ in range(num_steps):
x += S * time_step * np.cos(theta)
y += S * time_step * np.sin(theta)
positions.append((x, y))
if A == 2 or A == 3:
theta += theta_dot * time_step
return positions
# Set up the plot
plt.figure(figsize=(10, 8))
# Repeat the simulation 1000 times
for _ in range(1000):
# Generate a random integer A, which can be 1, 2, or 3
A = random.choice([1, 2, 3])
# Define the car's forward speed S
S = random.uniform(0, 20)
# Simulate the car's motion
positions = simulate_car_motion(S, A)
# Plot the car's trajectory
x_values, y_values = zip(*positions)
plt.plot(x_values, y_values, linewidth=0.5, color='blue', alpha=0.1)
# Set plot properties
plt.title('Trajectories of Car B')
plt.xlabel('X Position')
plt.ylabel('Y Position')
plt.grid(True)
plt.axis('equal') # Set the aspect ratio of x and y axes to be equal
plt.tight_layout()
plt.show()
修改后
import random
import matplotlib.pyplot as plt
import numpy as np
# Function to simulate the car's motion
def simulate_car_motion(S, A, total_time=10, time_step=0.01):
# Initialize the car's position and direction
x, y, theta = 0, 0, 0 # Initial position at the origin, initial direction along the X-axis
# Determine the car's motion based on A
if A == 1:
# Straight line motion
theta = random.uniform(0, 2 * np.pi) # Random initial direction
elif A == 2:
# Left turn
theta = random.uniform(0, 2 * np.pi) # Random initial direction
theta_dot = np.deg2rad(random.uniform(5, 30)) # Random angular velocity for left turn
elif A == 3:
# Right turn
theta = random.uniform(0, 2 * np.pi) # Random initial direction
theta_dot = np.deg2rad(random.uniform(-30, -5)) # Random angular velocity for right turn
# Simulate the car's motion
positions = [(x, y)]
num_steps = int(total_time / time_step)
for _ in range(num_steps):
x += S * time_step * np.cos(theta)
y += S * time_step * np.sin(theta)
positions.append((x, y))
if A == 2 or A == 3:
theta += theta_dot * time_step
return positions
# Set up the plot
plt.figure(figsize=(10, 8))
# Repeat the simulation 1000 times
for _ in range(1000):
# Generate a random integer A, which can be 1, 2, or 3
A = random.choice([1, 2, 3])
# Define the car's forward speed S
S = random.uniform(0, 20)
# Simulate the car's motion
positions = simulate_car_motion(S, A)
# Plot the car's trajectory
x_values, y_values = zip(*positions)
plt.plot(x_values, y_values, linewidth=0.5, color='blue', alpha=0.1)
# Set plot properties
plt.title('Trajectories of Car B')
plt.xlabel('X Position')
plt.ylabel('Y Position')
plt.grid(True)
plt.axis('equal') # Set the aspect ratio of x and y axes to be equal
plt.tight_layout()
plt.show()
给预测模型之前的数据预处理
- 在生成数据中进行取样,相邻数据间的时间间隔为t,服从正态分布
- t取值范围为(20/60, 90/60),该范围外的忽略掉
- t的均值暂定0.5
升级版代码
import random
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# 定义小车类
class Car:
def __init__(self, speed, direction):
self.speed = speed # 速度以米/秒为单位
self.direction = np.radians(direction) # 将角度转换为弧度
self.x, self.y = 0,0
self.positions = [(self.x, self.y)]
def move(self, dt, steering_angle=0):
# 计算小车的位置变化
dx = self.speed * np.cos(self.direction) * dt
dy = self.speed * np.sin(self.direction) * dt
self.x += dx
self.y += dy
self.positions.append((self.x, self.y))
# 更新小车的方向(如果有转向)
if steering_angle != 0:
self.direction += np.radians(steering_angle * dt)
# 初始化轨迹图
fig, ax = plt.subplots()
# 用于保存所有轨迹点的DataFrame
all_positions = pd.DataFrame(columns=['x', 'y', 'mode'])
# 打开一个txt文件以写入轨迹点
with open('car_trajectories.txt', 'w') as file:
# 重复100遍模拟
for _ in range(5000):
# 小车B的初始速度和方向
speed = random.uniform(5, 20) # 速度以米/秒为单位
initial_direction = random.uniform(0, 360)
car_B = Car(speed, initial_direction)
print("-----: ",_)
# 模拟B运行10秒
dt = 0.01 # 取样间隔
step = 0
file.write(f'{0}\t{0}\t{0}\t{"first"}\n')
for t in np.arange(0, 20, dt):
step = step+1
# 随机选择运动模式
if t % 5 <= 0.01: # 使用0.01的容忍值来确保在整秒时重新生成A
# A = random.randint(1, 3)
mode = random.choice(['straight', 'curve1', 'curve2', 'mixed', 'circle'])
print("mode:", mode)
if mode == 'straight':
# 直线运动,不改变方向
car_B.move(dt)
elif mode == 'curve1':
# 曲线运动,改变方向
steering_angle = random.uniform(5, 20) # 角速度在-20°/s到20°/s之间
car_B.move(dt, steering_angle)
elif mode == 'curve2':
# 曲线运动,改变方向
steering_angle = random.uniform(-20, -5) # 角速度在-20°/s到20°/s之间
car_B.move(dt, steering_angle)
elif mode == 'mixed':
# 直线加曲线运动,改变方向
if random.random() < 0.5: # 50%的概率直线运动
car_B.move(dt)
else: # 50%的概率曲线运动
steering_angle = random.uniform(-20, 20)
car_B.move(dt, steering_angle)
elif mode == 'circle':
# 圆形运动,保持方向不变,但速度会变化
# 这里我们简化为匀速圆周运动
car_B.move(dt)
# 将轨迹点写入文件
file.write(f'{step}\t{car_B.positions[-1][0]}\t{car_B.positions[-1][1]}\t{mode}\n')
# 绘制小车的轨迹
x_coords = [pos[0] for pos in car_B.positions]
y_coords = [pos[1] for pos in car_B.positions]
ax.plot(x_coords, y_coords, linewidth=1, alpha=0.2)
# 显示轨迹图
ax.set_aspect('equal', 'datalim')
ax.set_title('Car Trajectories')
ax.set_xlabel('X Coordinate')
ax.set_ylabel('Y Coordinate')
ax.grid(True)
plt.show()
还需考虑的内容
- 任务泛化能力在哪里?如果设定了圆心位置、而目标在圆C之外该如何解决?
- 可以将每次扩散预测计算的原点定义在在输入数据的第一个点上面,这样就不必要训练训练一个像圆C那样超大的扩散地图!模型不需要学会目标在地图各个地方的运动样式,具备真正的泛化能力、同时训练成本小
- 将思路延伸到这里,似乎一般的神经网络已经能够胜任此工作,后面可以作出探索
- LSTM依然可用,随便举个例子
- 获得目标的位置会有极大的噪声,甚至目标的运动还不如噪声和误差显著!这个以后再处理
- 实际环境中,喂给预测模型的数据需要经过适当的滤波处理
- 预测模型需要具备噪声处理能力吗?可以一试,直接将目标的轨迹先加噪声后喂给预测模型
- 或者将目标的轨迹先加噪声、待滤波平滑后再发给预测模型?
- 输入数据和输出预测数据的长短应当可控,这个以后再处理
- 训练的时候可以用较长的输入数据,当输入数据较短时采用插值的方式?并无不妥
- 限制输出预测轨迹的长度是有意义的
- 用扩散模型的一个优势在于使用步数来预测未来轨迹,输入数据的步数和