-
Notifications
You must be signed in to change notification settings - Fork 1
/
anmAgentActions.m
170 lines (136 loc) · 5.5 KB
/
anmAgentActions.m
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
function h = anmAgentActions(M,HA,pxlCellWidth,doEndResult,sFile)
% Plot setup
% ---------------------------------------------------------------------
% Get maze size
[nr,nc,~] = size(M);
% Get action log size
[~,nE] = size(HA);
% Numer of maze cells
N = nr*nc;
% Number of teleport locations
nTP = max(max(M(:,:,7)));
% Start/End position
[p0(1),p0(2)] = find(M(:,:,6)==1);
[p1(1),p1(2)] = find(M(:,:,6)==2);
% Create figure
[h,objShape,idxR,tbTitle] = pltDrawMaze(M,pxlCellWidth);
% Shape color
valShapeColor = [ 1.0, 1.0, 1.0; ... % [1] White
0.8, 0.8, 0.8; ... % [2] Drak-Grey
1.0, 0.5, 0.5; ... % [3] Red
0.8, 1.0, 0.8; ... % [4] Green
1.0, 1.0, 0.8; ... % [5] Yellow
1.0, 0.0, 1.0]; % [6] Magenta
% Modify figure
tbTitle.String = '';
% Only display end result
if doEndResult == 1
nStart = nE;
else
nStart = 1;
end
% TextBox & Cell color finishing
% ---------------------------------------------------------------------
% Start/End location
hold on, text( p0(2) - 0.5, nr + 0.5 - p0(1) , 'S', ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle', ...
'FontSize', 8, ...
'FontWeight', 'Bold' ); hold off
hold on, text( p1(2) - 0.5, nr + 0.5 - p1(1), 'F', ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle', ...
'FontSize', 8, ...
'FontWeight', 'Bold' ); hold off
% Cell color : Start/Finish
objShape(idxR(p0(1),p0(2))).FaceColor = valShapeColor(2,:);
objShape(idxR(p1(1),p1(2))).FaceColor = valShapeColor(2,:);
% Teleport Locations
idx = 0; exLocs = [];
for i = 1:nTP
% Teleport location
[tR(i,:),tC(i,:)] = find(M(:,:,7)==i);
% Excluded color change locations
idx = idx + 1; exLocs(idx,:) = [tR(i,1),tC(i,1)];
idx = idx + 1; exLocs(idx,:) = [tR(i,2),tC(i,2)];
% String
strTP = sprintf('%i',i);
% Add to figure
hold on, text( tC(i,1) - 0.5, nr + 0.5 - tR(i,1) , strTP, ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle', ...
'FontSize', 8, ...
'FontWeight', 'Bold' ); hold off
hold on, text( tC(i,2) - 0.5, nr + 0.5 - tR(i,2), strTP, ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle', ...
'FontSize', 8, ...
'FontWeight', 'Bold' ); hold off
% Cell color : Teleport Location
objShape(idxR(tR(i,1),tC(i,1))).FaceColor = valShapeColor(6,:);
objShape(idxR(tR(i,2),tC(i,2))).FaceColor = valShapeColor(6,:);
end
% Add start/fishish to excluded locations
if isempty(exLocs)
exLocs(1,:) = p0;
exLocs(2,:) = p1;
else
exLocs(end+1,:) = p0;
exLocs(end+1,:) = p1;
end
% Replay action log
% ---------------------------------------------------------------------
% Update all cell wrt to the corresponding Q-Matrix
for episode = nStart:nE
% Number of iterations
[ittMax,~] = size(HA(episode).logAgent);
idxS0 = [];
idxS1 = [];
for itt = 2:ittMax
tbTitle.String = ...
sprintf('[ episode : %05i / %05i | steps : %04i ]', ...
episode,nE,HA(episode).steps);
s0 = HA(episode).logAgent(itt-1,:);
s1 = HA(episode).logAgent(itt,:);
% Grid locations
idxS0(itt) = idxR(s0(1),s0(2));
idxS1(itt) = idxR(s1(1),s1(2));
% Find excluded index
idxL0 = find(and(s0(1)==exLocs(:,1),s0(2)==exLocs(:,2)));
idxL1 = find(and(s1(1)==exLocs(:,1),s1(2)==exLocs(:,2)));
% Update current state in figure
if isempty(idxL0)
objShape(idxS0(itt)).FaceColor = valShapeColor(4,:);
objShape(idxS0(itt)).EdgeColor = valShapeColor(4,:);
else
idxS0(itt) = 0;
end
% Update next state in figure
if isempty(idxL1)
objShape(idxS1(itt)).FaceColor = valShapeColor(5,:);
objShape(idxS1(itt)).EdgeColor = valShapeColor(5,:);
else
idxS1(itt) = 0;
end
end
pause(6/ittMax);
% Reset figure
for itt = 1:ittMax
% Reset current state in figure
if idxS0(itt) > 0 && episode < nE
objShape(idxS0(itt)).FaceColor = valShapeColor(1,:);
objShape(idxS0(itt)).EdgeColor = valShapeColor(1,:);
end
% Reset next state in figure
if idxS1(itt) > 0 && episode < nE
objShape(idxS1(itt)).FaceColor = valShapeColor(1,:);
objShape(idxS1(itt)).EdgeColor = valShapeColor(1,:);
end
end
end
% Save file
if exist('sFile','var')
strSave = [ '../Report/figures/' sFile '.png' ];
saveas(gcf,strSave)
end
end