matlab 元胞自动机模型 模拟行人和机动车在人行横道处的交互行为
一直出错,数据出来也是空白的,要怎么改,sos
代码:
clc; clear ;
close all;
%定义模型参数
global road_length;
global road_width;
cell_size = 0.1; % 元胞大小,单位为m
crosswalk_width = 6; % 人行横道宽度,单位为m
crosswalk_length = 7; % 人行横道长度,单位为m
road_length = 30; % 道路长度,单位为m
road_width = 15;%道路宽度,单位为m
vehicle_speed = 5; % 车速,单位为m/s
vehicle_density = 0.2; % 车流密度,单位为车辆/元胞
pedestrian_density = 0.1; % 行人密度,单位为行人/元胞
vehicle_avoid_prob = 0.7; % 车辆避让概率
pedestrian_avoid_prob = 0.3; % 行人避让概率
minPedSpeed = 1;%行人最小速度
maxPedSpeed = 3;%行人最大速度
minCarSpeed = 0;
maxCarSpeed = 20;
% 定义交叉口网格的行数和列数
numRows = 10;
numCols = 30;
% 定义元胞长度
cellLength = 2;
% 定义交叉口矩阵,初始状态均为0(即空)
cellStates = false(numRows,numCols);
%定义元胞自动机模型
num_cells = road_length / cell_size; % 定义元胞数
road = zeros(num_cells, 1); % 初始化道路,0表示空车道,1表示有车辆,2表示行人
% 初始化车辆
for i = 1:num_cells
if rand() < vehicle_density
road(i) = 1;
end
end
% 初始化行人
pedestrian_arrival_rate = pedestrian_density * cell_size; % 计算行人到达率
t = 0; % 时间
while t < 3600 % 模拟1小时
if rand() < pedestrian_arrival_rate
% 行人到达人行横道的一侧
pos = round((crosswalk_length / cell_size) + rand() * ((road_length - crosswalk_length) / cell_size));
road(pos) = 2;
end
t = t + 1;
end
% 元胞自动机模拟循环
for t = 1:3600
% 道路循环
for i = 1:num_cells
% 行人或车辆存在
if road(i) > 0
if road(i) == 1 % 车辆移动
if i == num_cells % 车辆到达道路尽头
road(i) = 0;
else % 车辆向前移动一个元胞
if rand() > vehicle_avoid_prob % 车辆不避让
if road(i+1) == 0 % 没有车辆或行人
road(i) = 0;
road(i+1) = 1;
else % 有车辆或行人,继续停留
road(i) = 1;
end
else % 车辆避让
if i == num_cells-1 % 车辆即将到达道路尽头,无法避让
road(i) = 1;
elseif road(i+2) == 0 % 避让后没有车辆或行
end
end
end
end
end
end
end
edit SerialLink.plot
[myModifyForViewErrorA,myModifyForViewErrorB]=view(gca);
if isequal([myModifyForViewErrorA,myModifyForViewErrorB],[0,90])
view(3)
end
numSteps = 300;%模拟时间步数
lambda = 0.1;%行人每个时间步以0.1的概率随机出现
rho = 0.2;%车辆出现的概率
pedPositions = [10 15 20];%行人位置
carPositions = []; % 车辆位置
pedVelocities = zeros(size(pedPositions));%行人速度[myModifyForViewErrorA,myModifyForViewErrorB]=view(gca);
carVelocities = randi([minCarSpeed,maxCarSpeed],size(carPositions)); % 车辆速度
rowRoad = 5;%道路所在行数
rowPedCrossing = 5;%人行横道所在行数
colPedCrossing = round(crosswalk_width/2/crosswalk_length);%人行横道所在列数
if colPedCrossing < 1
colPedCrossing = 1;
end
for t = 1:numSteps
% 行人随机出现
if rand < lambda && sum(cellStates(rowPedCrossing, colPedCrossing:colPedCrossing+4))==0
% 行人出现在人行横道处
cellStates(rowPedCrossing, colPedCrossing) = true;
% 随机行人初始速度
pedVelocities(end+1) = randi([minPedSpeed,maxPedSpeed],1);
% 行人初始位置为人行横道的一侧
pedPositions(end+1) = colPedCrossing-2;
end
% 车辆随机出现
if rand < rho && ~any(cellStates(rowRoad,1:colPedCrossing-3)) && ~any(cellStates(rowRoad, colPedCrossing+3:end))
% 车辆出现在路上
carPositions(end+1) = 1;
% 随机车辆初始速度
carVelocities(end+1) = randi([minCarSpeed,maxCarSpeed],1);
%将对应元胞设为有车
carCell = find(cellStates(rowRoad,:));
cellStates (rowRoad,carCell) = true;
end
% 更新行人状态和位置edit SerialLink.plot
[myModifyForViewErrorA,myModifyForViewErrorB]=view(gca);
if isequal([myModifyForViewErrorA,myModifyForViewErrorB],[0,90])
view(3)
end
% 定义交叉口网格的行数和列数
numRows = 10;
numCols = 30;
% 定义元胞长度
cellLength = 2;
% 定义交叉口矩阵,初始状态均为0(即空)
cellStates = zeros(numRows,numCols);
for i = 1:length(pedPositions)
% 行人速度
pedSpeed = pedVelocities(i);
% 行人位置
pedPos = pedPositions(i);
% 行人元胞位置
pedCell = ceil(pedPos/cellLength);
% 行人与车辆是否冲突
conflictThreshold = 2;%冲突域值
conflict = sum(cellStates(rowPedCrossing,max(1,floor(pedCell)):min(numCols,ceil(pedCell+conflictThreshold)))) > 0;
% 行人是否避让
pedAvoidProb = 0.5;%行人避让概率
if conflict && rand > pedAvoidProb
pedVelocities(i) = 0;
cellStates(rowPedCrossing, pedCell) = 0;
continue;
end
% 更新行人位置
if pedPos + pedSpeed > crosswalk_width+cellLength
% 行人到达路的另一侧
cellStates(rowPedCrossing, pedCell) = 0;
pedPositions(i) = crosswalk_width + cellLength;
else
% 更新行人位置
pedPositions(i) = pedPos+pedSpeed;
pedPosInCell = ceil((pedPositions(i) + cellLength)/cellLength);
cellStates(rowPedCrossing,pedCell) = 0;
cellStates(rowPedCrossing,pedPosInCell) = 1;
end
end
end
% 更新车辆状态和位置
for i = 1:length(carPositions)
% 车辆速度
carSpeed = carVelocities(i);
% 车辆位置
carPos = carPositions(i);
% 车辆元胞位置
carCell = ceil(carPos/cellLength);
% 车辆可能的下一步位置
nextPos = carPos + carSpeed;
nextCell = ceil(nextPos/cellLength);
end
% 车辆是否避让
% 判断车辆是否避让
if rand < 0.7 % 车辆避让
% 车辆减速并改变方向,避让行人
% 具体实现需要根据具体情况进行调整
else % 车辆不避让
% 车辆继续前进
end
% 判断行人和车辆是否发生冲突
if abs(double(pedestrian_pos(1)) - double(vehicle_pos(1))) < 0.5 && abs(double(pedestrian_pos(2)) - double(vehicle_pos(2))) < 1
% 行人和车辆距离小于阈值,发生冲突
% 发出警报等处理措施
end
% 计算TTC和TDTC
for i = 1:length(collision_pairs)
pair = collision_pairs(i,:);
time_to_collision = Inf;
distance_to_collision = Inf;
for j = 1:num_steps
if pairs_status(i,j) == 1
if time_to_collision == Inf
time_to_collision = j*delta_t;
distance_to_collision = abs(pair(2) - pair(1));
else
distance = abs(pair(2) - pair(1));
if distance < distance_to_collision
time_to_collision = j*delta_t;
distance_to_collision = distance;
end
end
end
end
end
TTC(i) = time_to_collision;
TDTC(i) = distance_to_collision / (delta_v + 1e-6);
% 计算平均延误时间
delay_time = zeros(num_steps,1);
for i = 1:num_steps
delay_time(i) = sum(wait_time(wait_time(:,i) > 0,i)) / sum(wait_time(:,i) > 0);
end
avg_delay_time = mean(delay_time);
%定义pedstrain_pos函数
[rowPedCrossing,colPedCrossing] = pedestrian_pos(pedestrian_pos,road_length,road_width);
function [rowPedCrossing, colPedCrossing] = pedestrian_pos(pedestrianPosition, road_length, road_width)
colPedCrossing = ceil(double(road_width)/2);% 计算行人所在元胞的列索引
rowPedCrossing = ceil(double(pedestrianPosition)/double(road_length) * 10);% 计算行人所在元胞的行索引
end
function pos = vehicle_pos(carPos, carWidth, road_width)%定义vehicle_pos函数
pos = [carPos - carWidth/2, ceil(double(road_width)/2)];% 计算车辆的位置
end