-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsim_approx_navigation.m
More file actions
177 lines (145 loc) · 4.06 KB
/
sim_approx_navigation.m
File metadata and controls
177 lines (145 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function h = sim_approx_navigation(plt_nr,plt_nc,plt_np)
do_plot = 1;
n = 10;
[maze, co, cl] = run_navigation(n);
if ~do_plot
return;
end
%--------------------------------------------------------------------------
if nargin<1
close all;
plt_nr = 1;
plt_nc = 2;
plt_np = [1 2];
fsiz = [0.3536 0.6907 0.3 0.2204];
figure; set(gcf,'units','normalized'); set(gcf,'position',fsiz);
end
%----------------------
fs = def('fs');
fn = def('fn');
fsy = def('fsy');
alf = def('alf');
fsA = def('fsA');
xsA = -.05 + def('xsA');
ysA = def('ysA');
abc = def('abc');
bw = .15;
mclr = 'k';
mlw = 1;
subplot(plt_nr,plt_nc,plt_np(1));
show_maze(maze, mlw, mclr);
% title('10x10 maze','fontsize',fsy,'fontname',fn,'fontweight','normal')
h(1) = gca;
%-------------------------------------
mm = max(max([co cl]-1));
mstep = 10;
mm = ceil(mm/mstep)*mstep;
dd = 0:mstep:mm;
h(2) = subplot(plt_nr,plt_nc,plt_np(2));
plot(co-1,cl-1,'k','linewidth',2);
xlabel('Optimal exhaustive search','fontsize',fsy,'fontname',fn);
ylabel('Linear RL approximation','fontsize',fsy,'fontname',fn);
set(gca,'fontsize',fs,'fontname',fn);
xlim(dd([1 end]));
ylim(dd([1 end]));
set(gca,'xtick',dd);
set(gca,'ytick',dd);
end
function [maze, co, cl, dd, to, tl] = run_navigation(n)
nsim = 'all'; snsim = 'all';
q = 1;
bigpipedir = def('bigpipedir');
pipedir = def('pipedir');
fdir = fullfile(bigpipedir,'run_navigation'); makedir(fdir);
fmaze = fullfile(fdir,sprintf('n%d_maze.mat',n));
fname0 = fullfile(fdir,sprintf('n%d_M0.mat',n));
fdir = fullfile(pipedir,'run_navigation'); makedir(fdir);
fname = fullfile(fdir,sprintf('n%d_%s.mat',n,snsim));
ftname= fullfile(fdir,sprintf('n%d_%s_temp.mat',n,snsim));
fm = load(fmaze);
maze = [fm.rr, fm.cc, fm.ptr_left, fm.ptr_up, fm.ptr_right, fm.ptr_down];
P = zeros(size(maze,1),size(maze,1));
for i=1:size(P,1)
d = maze(i,3:end);
di = abs(d(d<0));
P(i,di) = 1/length(di);
end
P0 = P;
q = q*ones(size(P0,1),1);
np = size(P0,1);
if ~exist(fname,'file')
if ~exist(fname0,'file')
M0 = (diag(exp(q))-P0)^-1;
save(fname0,'M0');
else
M0 = load(fname0); M0 = M0.M0;
end
if ~ischar(nsim)
for i=1:nsim
ss = randperm(np);
states(i,1:2) = ss(1:2);
end
else
states = nchoosek(1:np,2);
% states = [ones(np-1,1) (2:np)'];
nsim = size(states,1);
end
for i=1:nsim
ss = states(i,:);
s0 = ss(1);
st = ss(2);
P = P0;
P(st,:) = 0; P(st,st) = 1;
tic;
[policy, value, pi] = core_value_iteration(P,q);
to(i,1) = toc;
[c] = core_follow_path(P,policy,q,s0);
co(i,1) = length(c);
tic;
U = core_new_goal(P0,M0,st,q);
tl(i,1) = toc;
[c, path, intens, U1] = core_follow_path(P,U,q,s0);
cl(i,1) = length(c);
if mod(i,50)==0
fprintf('sim %04d\n',i);
save(ftname,'states','co','cl','to','tl');
end
end
save(fname,'states','co','cl','to','tl');
end
ff = load(fname);
co = ff.co;
cl = ff.cl;
to = ff.to;
tl = ff.tl;
dd = 1:2:(2*n);
end
function show_maze(maze, lw, colr)
rr = maze(:,1);
cc = maze(:,2);
% ptr_left = maze(:,3);
% ptr_up = maze(:,4);
ptr_right = maze(:,5);
ptr_down = maze(:,6);
row = max(rr);
col = max(cc);
line([.5,col+.5],[.5,.5],'linewidth',lw,'color',colr) % draw top border
line([.5,col+.5],[row+.5,row+.5],'linewidth',lw,'color',colr) % draw bottom border
line([.5,.5],[0.5,row+.5],'linewidth',lw,'color',colr) % draw left border
line([col+.5,col+.5],[.5,row+.5],'linewidth',lw,'color',colr) % draw right border
for ii=1:length(ptr_right)
if ptr_right(ii)>0 % right passage blocked
line([cc(ii)+.5,cc(ii)+.5],[rr(ii)-.5,rr(ii)+.5],'linewidth',lw,'color',colr);
hold on
end
if ptr_down(ii)>0 % down passage blocked
line([cc(ii)-.5,cc(ii)+.5],[rr(ii)+.5,rr(ii)+.5],'linewidth',lw,'color',colr);
hold on
end
end
axis equal
axis([.5,col+.5,.5,row+.5])
% axis off
set(gca,'YDir','reverse')
set(gca,'ytick',[],'xtick',[])
end