-
Notifications
You must be signed in to change notification settings - Fork 2
/
animate.m
129 lines (101 loc) · 4.27 KB
/
animate.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
function animate(validation_data, reward, lambda, varargin)
% Optional arguments:
% - 'start' Iteration at which to start animation (default 1)
% - 'end' Iteration at which to stop animation
% - 'sheep' Number of sheep
% - 'pause' Duration to pause between steps (default 0.25)
% - 'angles' Angles of possible directions (default 0:pi/4:7*pi/4)
% - 'video' Filepath to video file for saving (default none)
% Parse optional input arguments
if ~isempty(varargin)
for arg = 1:length(varargin)
if strcmp(varargin{arg}, 'start'); start_iteration = varargin{arg + 1};
elseif strcmp(varargin{arg}, 'end'); end_iteration = varargin{arg + 1};
elseif strcmp(varargin{arg}, 'sheep'); numSheep = varargin{arg + 1};
elseif strcmp(varargin{arg}, 'pause'); pauseTime = varargin{arg + 1};
elseif strcmp(varargin{arg}, 'angles'); angles = varargin{arg + 1};
elseif strcmp(varargin{arg}, 'video'); vidFLAG = true; vidPath = varargin{arg + 1};
end
end
end
% Set defaults for optional input arguments
if ~exist('start_iteration', 'var'); start_iteration = 1; end
if ~exist('end_iteration', 'var'); end_iteration = size(validation_data, 1)/lambda; end
if ~exist('numSheep', 'var'); numSheep = (size(validation_data, 2) - 2)/2; end
if ~exist('pauseTime', 'var'); pauseTime = 0.25; end
if ~exist('angles', 'var'); angles = 0:pi/4:7*pi/4; end
if ~exist('vidFLAG', 'var'); vidFLAG = false; end
% Set placeholder for sheep indicators and graph lines
s = cell(numSheep, 1); l = cell(length(angles), 1);
% Initialize the sheep indicators
for i = 1:numSheep
s{i} = scatter(0, 0, 'or', 'MarkerFaceColor', 'r');
end
% Initialize dog indicator
d = scatter(1, -1, 'ok', 'MarkerFaceColor', 'k'); M = 0.25;
% Initialize lines for possible directions
for i = 1:length(angles)
l{i} = line([d.XData d.XData + M*cos(angles(i))], [d.YData d.YData + M*sin(angles(i))], ...
'Color', [0, 0, 0], 'LineWidth', 2);
end
% Initialize boundary lines
line([-1.5 1.5], [1, -1], 'Color', 'k', 'LineStyle', '--')
line([-1.5 1.5], [1, 1], 'Color', 'k', 'LineWidth', 2)
line([-1.5 1.5], [-1, -1], 'Color', 'k', 'LineWidth', 2)
line([-1.5 -1.5], [-1, 1], 'Color', 'k', 'LineWidth', 2)
line([1.5 1.5], [-1, 1], 'Color', 'k', 'LineWidth', 2)
% Set limits on axes
xlim([-1.5, 1.5]); ylim([-1.5, 1.5])
% Initialize video
if vidFLAG
vid = VideoWriter(vidPath);
vid.Quality = 100; vid.FrameRate = 10;
open(vid);
end
% For each iteration...
for i = start_iteration:end_iteration
% Set placeholders for iteration
startIdx = lambda*(i-1) + 1; endIdx = startIdx + (lambda - 1);
data = validation_data(startIdx:endIdx, :);
weights = reward(startIdx:endIdx, :);
if ~isempty(find(isnan(weights(:)), 1)); continue; end
% For each step in the iteration
for j = 1:lambda
% Set figure title
title("Iteration: " + i + ", Step: " + j)
% Initialize counter for parsing validation data
counter = 1;
% Update the x and y data for each sheep
for k = 1:numSheep
s{k}.XData = data(j, counter); counter = counter + 1;
end
for k = 1:numSheep
s{k}.YData = data(j, counter); counter = counter + 1;
end
% Update the x and y data for the dog
d.XData = data(j, counter); counter = counter + 1;
d.YData = data(j, counter);
% Update the movement lines
for k = 1:length(angles)
l{k}.XData = [d.XData d.XData + M*cos(angles(k))];
l{k}.YData = [d.YData d.YData + M*sin(angles(k))];
end
% Update the weights such that the minimum value is 0
col = weights(j, :) - min(weights(j, :));
col = col./max(col); % Normalize to range [0, 1]
% Update the colors of each line based on the weights
try
for k = 1:length(angles)
l{k}.Color = 1 - [col(k) col(k) col(k)];
end
catch; continue
end
% Record video frame
if vidFLAG; writeVideo(vid, getframe(gcf)); end
% Pause animation
pause(pauseTime)
end
end
% End video, if necessary
if vidFLAG; close(vid); end
end