forked from Shushman/16-831-Class-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.m
141 lines (116 loc) · 4.38 KB
/
Map.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
classdef Map
%MAP Summary of this class goes here
% Detailed explanation goes here
properties
siteDist % pairwise distance.
locations % 2xn where ith column is the (x,y) loc of ride i
im;
map;
gridSize
m0 % initial distance to all sites, Should be [0,1]
nSites % number of sites
end
methods
function self = Map(nSites, gridSize, m0, maptype)
self.nSites = nSites;
self.gridSize = gridSize;
self.m0 = m0;
assert (m0 <= 1 && m0 >= 0);
if strcmp(maptype,'uniform')
% Uniformly random
X = ceil(gridSize*rand(2, nSites));
else
nparts = nSites / 4;
% X, Y location of rides
XMAX = gridSize;
YMAX = gridSize;
GRIDS = XMAX*YMAX;
[X,Y] = meshgrid(1:XMAX,1:YMAX);
X = X(:);
Y = Y(:);
index = randperm(GRIDS,nparts);
X = X(index);
Y = Y(index);
Cntr = [X';Y'];
Std = 5*ones(1,nparts);
X = [];
n = round(nSites/nparts);
for i = 1:nparts
X = [X randn(2,n)*Std(i)+repmat(Cntr(:,i),1,n)];
end
end
siteDist = pdist2(X',X');
self.locations = X;
self.siteDist = siteDist / max(max(siteDist));
end
function draw_map(self)
figure(1);clf;
m = self.gridSize;
hold on;
scatter(self.locations(1,:), self.locations(2,:), 200, 'Filled');
% axis
plot([0.5 0.5], [0.5,m+0.5], 'k', 'LineWidth',2);
plot([m+0.5,0.5], [m+0.5,m+0.5],'k','LineWidth',2);
plot([0.5 m+0.5], [0.5,0.5], 'k', 'LineWidth',2);
plot([m+0.5,m+0.5], [0.5,m+0.5],'k','LineWidth',2);
axis([0,m+1,0,m+1])
axis equal tight
end
% draws expected reward from current site
function draw(self, site, nextsite, game, filename)
figure(2);clf;hold on;
n = size(self.locations, 2);
m = self.gridSize;
means = game.means;
lambdas = game.lambdas;
% siteDist
if site > 0
siteDist = self.siteDist(site,:);
else
siteDist = ones(1,n)*game.m0;
end
% expected rewards
rewards = compute_reward(game.weightDist, game.weightWait, game.weightRide,...
siteDist, lambdas, means);
% all rides
for i = 1:n
loc = self.locations(:,i);
x = loc(1);
y = loc(2);
s = scatter(x, y, 200);
s.LineWidth = 0.6;
c = [rewards(i), 1-rewards(i), 0];
s.MarkerFaceColor = c;
s.MarkerEdgeColor = c;
end
if site > 0
pos = self.locations(:,site);
else
pos = [m/2, m/2]';
end
nextpos = self.locations(:,nextsite);
% draw current site and arrow to next site
if site ~= nextsite
direction = nextpos - pos;
quiver(pos(1), pos(2), direction(1), direction(2), 'LineWidth', 3, 'color', 'r');
else
quiver(pos(1)-2, pos(2)-2, 2, 2, 'LineWidth', 3, 'color','b');
end
cmap = [linspace(0,1,64); linspace(1, 0, 64); zeros(1,64)]';
colormap(cmap);
colorbar
% axis
plot([0.5 0.5], [0.5,m+0.5], 'k', 'LineWidth',2);
plot([m+0.5,0.5], [m+0.5,m+0.5],'k','LineWidth',2);
plot([0.5 m+0.5], [0.5,0.5], 'k', 'LineWidth',2);
plot([m+0.5,m+0.5], [0.5,m+0.5],'k','LineWidth',2);
axis([0,m+1,0,m+1])
axis equal tight
% drawnow;
saveas(gcf,strcat('../fig/', filename, ...
sprintf('%.3d',game.round), '.png'));
% pause(0.001);
hold off;
end
end
end