%% version 1.0
% simulation of going off and going up in subway
% writen by Song Wang Xia
clc,clear,close all
time = 0.2;
P = 0.5;% 当车剩下 比例P的人数时 上车!
off_field = [
2, 2, 2, 2, 2, 2, 2;
2, 1, 1, 1, 1, 1, 2;
2, 1, 0.5, 0.5, 0.5, 1, 2;
2, 2,-0.5,-0.5,-0.5, 2, 2;
2, 2, -1, -1, -1, 2, 2;
2, 2, -2, -2, -2, 2, 2
];
up_field = [
-2, -2, -2, -2, -2, -2, -2;
-2, -1, -1, -1, -1, -1, -2;
2, 2,-0.5,-0.5,-0.5, 2, 2;
2, 1, 0.5, 0.5, 0.5, 1, 2;
2, 1, 1, 1, 1, 1, 2;
2, 2, 2, 2, 2, 2, 2
];
human_x = [1 1 2 3 2]; % randi
human_y = [2 5 4 6 1];
human_x_up = [4 4 5 5];
human_y_up = [6 2 6 2];
[len,wid] = size(off_field);
cell = zeros(len,wid);
human = length(human_x);
cell(sub2ind( size(cell), human_x, human_y )) = 1; % 有人的地方变 1
cell(sub2ind( size(cell),human_x_up, human_y_up )) = 1; % 有人的地方变 1 %%难点
%% 画图
h1 = plot(human_x,human_y,'.','Color','blue','MarkerSize',100);
hold on
h2 = plot(human_x_up,human_y_up,'.','Color','red','MarkerSize',100);
axis image
axis([1 len 1 wid])
grid on
%% 主循环
while sum(sum(cell))
if isempty(human_x) == 0
%let people go die
ind = find(human_x==len);
cell(sub2ind( size(cell), human_x(ind), human_y(ind) )) = 0;
human_x(ind) = [];
human_y(ind) = [];
for ii = 1:length(human_x) %对每个下车的行人循环
i = human_x(ii);
j = human_y(ii);
extend_field = [inf*ones(len/2+1,wid+2);inf*ones(len/2+1,wid+2)];
Temp_off_field = off_field;
[refresh_i, refresh_j] = find(cell);
Temp_off_field(sub2ind( size(off_field), refresh_i, refresh_j )) = inf;% 有人的地方势变inf
Temp_off_field(i,j) = off_field(i,j); % 本地恢复
extend_field(2:end-1,2:end-1) = Temp_off_field; % 拓展势场
NeighborsField = extend_field(i:i+2,j:j+2); % 取出邻居势场
[val,~] = min(min(NeighborsField)); % 找最小的邻居的场值
[row,vol] = find(NeighborsField == val); % 返回坐标
RandOrders = randperm(length(row)); % 存在多尔最小随机选
NEW_i = row(RandOrders(1)) - 2 + i; % 目的地坐标x % 难点
NEW_j = vol(RandOrders(1)) - 2 + j; % 目的地坐标y
%% 更新
human_x(ii) = NEW_i;
human_y(ii) = NEW_j;
cell(i,j) = 0;
cell(NEW_i,NEW_j) = 1;
%% 刷新
set(h1, 'xdata',human_x,'ydata',human_y)
set(h2, 'xdata',human_x_up,'ydata',human_y_up)
drawnow
pause(time)
end
end
if isempty(human_x_up) == 0 && length(find(human_x<3)) <= 5 * P %let people go die %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%注意分界
ind = find(human_x_up==1);
cell(sub2ind( size(cell), human_x_up(ind), human_y_up(ind) )) = 0;
human_x_up(ind) = [];
human_y_up(ind) = [];
for jj = 1:length(human_x_up) %四个上车的人循环
i = human_x_up(jj);
j = human_y_up(jj);
extend_field = [inf*ones(len/2+1,wid+2);inf*ones(len/2+1,wid+2)];
Temp_off_field = up_field;
[refresh_i, refresh_j] = find(cell);
Temp_off_field(...
sub2ind( size(off_field), refresh_i, refresh_j )...
) = inf;% 有人的地方势变inf
Temp_off_field(i,j) = up_field(i,j); % 本地恢复
extend_field(2:end-1,2:end-1) = Temp_off_field; % 拓展势场
NeighborsField = extend_field(i:i+2,j:j+2); % 取出邻居势场
[val,~] = min(min(NeighborsField)); % 找最小的邻居的场值
[row,vol] = find(NeighborsField == val); % 返回坐标
RandOrders = randperm(length(row)); % 存在多尔最小随机选
NEW_i = row(RandOrders(1)) - 2 + i; % 目的地坐标x
NEW_j = vol(RandOrders(1)) - 2 + j; % 目的地坐标y
%% 更新
human_x_up(jj) = NEW_i;
human_y_up(jj) = NEW_j;
cell(i,j) = 0;
cell(NEW_i,NEW_j) = 1;
%% 刷新
set(h1, 'xdata',human_x,'ydata',human_y)
set(h2, 'xdata',human_x_up,'ydata',human_y_up)
drawnow
pause(time)
end
end
set(h1, 'xdata',human_x,'ydata',human_y)
set(h2, 'xdata',human_x_up,'ydata',human_y_up)
drawnow
pause(time) %pause
end
%% note
%sum(sum(cell(1:3,:))) this is very useful thing
% randi([min_int max_int],row,vol)
final_version.rar_move_元胞自动机
版权申诉
71 浏览量
2022-07-14
20:07:35
上传
评论
收藏 1KB RAR 举报
御道御小黑
- 粉丝: 61
- 资源: 1万+
最新资源
- v2.1.6-Unity3D插件 SUIMONO Water System 效果逼真交互水系统
- 基于STM32 Discovery(STM32f407vgt6)Discovery板的STM32裸机项目集合
- mmexport1717246170188.jpg
- 近代史调查问卷_统计报表_20240601205759.xlsx
- v2.1.2-Unity3D插件 SUIMONO Water System 效果逼真交互水系统
- 农村小别墅图纸编号D040-三层-08.30&14.60米-施工图.dwg
- 三层别墅图纸编号D039-三层-16.70&14.70米- 结构图.dwg
- 三层农村小别墅编号D038-三层-11.90&13.50米-施工图.dwg
- 采购组·主管业务原型集与说明(Xmind书写)
- 三层图纸编号D037-三层-10.40&15.10米-施工图.dwg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈