-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_policy_evaluation.html
327 lines (281 loc) · 7.38 KB
/
2_policy_evaluation.html
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<div id="WriteUp">
This is from Page 89 of https://web.stanford.edu/class/psych209/Readings/SuttonBartoIPRLBook2ndEd.pdf
I implement Policy Evaluation. Dynamic Programming.
DP assumes full model of environment. Computationally Expensive.
Policy Evaluation is used to determine the State-Value function for a given policy pi.
It can be solved in closed form. But we turn the Bellman Equation into an update rule, which eventually converges to the true state value function for a given policy, pi.
</div>
<div id="grid"></div>
<div id="coords"></div>
<div id="lastaction"></div>
</body>
<script>
var numRows = 10
var numCols = 10
function getData(){
var xPos = 1
var yPos = 1
var widthPx = 100
var heightPx = 100
var data = new Array()
for(var row = 0; row < numRows; row ++){
data.push(new Array())
for(var col = 0; col < numCols; col ++){
data[row].push({
xIdx: col,
yIdx: row,
x: xPos,
y: yPos,
width: widthPx,
height: heightPx
})
xPos += widthPx
}
xPos = 1
yPos += heightPx
}
return data
}
var gridData = getData()
function drawGrid(){
var grid = d3.select("#grid")
.append("svg")
.attr("width", (100 * numCols + 2) + "px")
.attr("height", (100 * numRows + 2) + "px")
var row = grid.selectAll(".row")
.data(gridData)
.enter()
.append("g")
.attr("class", "row")
var column = row.selectAll(".square")
.data(function(d) { return d })
.enter()
.append("rect")
.attr("class", "square")
.attr("x", function(d){ return d.x })
.attr("y", function(d){ return d.y })
.attr("width", function(d){ return d.width })
.attr("height", function(d){ return d.height })
.attr("yIdx", function(d){ return d.yIdx })
.attr("xIdx", function(d){ return d.xIdx })
.attr("valueAtState", function(d){ return Math.round(normalized[d.yIdx][d.xIdx] * 255) })
.style("fill", function(d){ return "rgb(" + (Math.round(normalized[d.yIdx][d.xIdx] * 255)) + ", " + (Math.round(normalized[d.yIdx][d.xIdx] * 255)) + ", " + (Math.round(normalized[d.yIdx][d.xIdx] * 255)) + ")"})
.style("stroke", "#222");
}
function drawBall(xPos, yPos) {
d3.select(".circle")
.remove()
.exit()
var ball = d3.select("#grid")
.select("svg")
.append("g")
.attr("class", "circle")
.append("circle")
.attr("cx", (50 + 100 * (xPos)) + "px")
.attr("cy", (50 + 100 * (yPos)) + "px")
.attr("r", "25px")
.style("fill", "red")
.style("stroke", "black")
.style("stroke-width", "3")
}
function printCoords(coords){
d3.select("#coords")
.select("p")
.remove()
.exit()
d3.select("#coords")
.append("p")
.text("(" + coords + ")")
}
function printLastAction(lastaction){
d3.select("#lastaction")
.select("p")
.remove()
.exit()
d3.select("#lastaction")
.append("p")
.text("Last Action: " + lastaction)
if(collision){
console.log("collision!")
d3.select("#lastaction")
.select("p")
.style("color", "red")
}
}
function getNextAction(){
actions = ["n", "s", "w", "e"]
return actions[Math.floor(Math.random() * actions.length)];
}
function takeAction(coords, action){
if(action == 'n'){
if (coords[1] == 0){
collision = true;
return coords
}
else
return [coords[0], coords[1] - 1]
} else if (action == 's'){
if(coords[1] == numRows - 1){
collision = true;
return coords
}
else
return [coords[0], coords[1] + 1]
} else if (action == 'w'){
if(coords[0] == 0){
collision = true
return coords
}
else
return [coords[0] - 1, coords[1]]
} else if (action == 'e'){
if(coords[0] == numCols - 1){
collision = true
return coords
}
else
return [coords[0] + 1, coords[1]]
}
}
function playGame(){
collision = false
action = getNextAction()
coords = takeAction(coords, action)
drawBall(coords[0], coords[1])
printCoords(coords)
printLastAction(action)
}
class ActionProbabilities {
constructor(){
this.actionProbs = {
"n": 0.25,
"s": 0.25,
"e": 0.25,
"w": 0.25
};
}
}
/* Policy Class */
class Policy {
constructor(height, width) {
this.rows = new Array (height);
for (var y = 0; y < height; y ++){
this.rows[y] = new Array(width)
for (var x = 0; x < width; x ++){
this.rows[y][x] = new ActionProbabilities();
}
}
}
getPolicyAtState(y, x){
return (this.rows[y][x].actionProbs);
}
}
class ValueFunction{
constructor(height, width){
this.height = height;
this.width = width;
this.rows = new Array(height);
for (var y = 0; y < height; y++){
this.rows[y] = new Array(width)
for(var x = 0; x < numCols; x++){
this.rows[y][x] = 0;
}
}
}
getValue(y, x){
return (this.rows[y][x])
}
setValue(y, x, value){
this.rows[y][x] = value;
}
print(){
for (var y = 0; y < this.height; y++){
var line = "";
for(var x = 0; x < this.width; x++){
//TODO: Pretty this print up at some point (for spaces)
line = line + (Math.round(this.rows[y][x] * 1000) / 1000).toString() + "|";
}
console.log(line);
}
}
getNormalized(){
var normalized = new Array();
var min = Infinity;
var max = -Infinity;
//Todo: Make more efficient.. if you care enough. Only gets run once at the end
for (var y = 0; y < this.height; y++){
normalized.push(new Array())
for(var x = 0; x < this.width; x++){
var abs = Math.abs(v.getValue(y, x))
normalized[y].push(abs);
if(abs < min)
min = abs
if(abs > max)
max = abs
}
}
for (var y = 0; y < this.height; y++){
for(var x = 0; x < this.width; x++){
normalized[y][x] = (normalized[y][x] - min) / (max - min)
}
}
return normalized;
}
}
class RewardFunction{
constructor(height, width){
this.rows = new Array(height);
for (var y = 0; y < height; y++){
this.rows[y] = new Array(width)
for(var x = 0; x < numCols; x++){
this.rows[y][x] = -1;
}
}
this.rows[0][width - 1] = 0; //set goal state to top right
}
getReward(y, x){
return (this.rows[y][x])
}
}
p = new Policy(numRows, numCols);
v = new ValueFunction(numRows, numCols);
r = new RewardFunction(numRows, numCols);
gamma = 0.9;
function policyEvaluation(height, width){
theta = 0.1;
do {
delta = 0;
for (var y = 0; y < height; y++){
for(var x = 0; x < width; x++){
currentV = v.getValue(y, x);
policyAtState = p.getPolicyAtState(y, x);
newValueAtState = Object.keys(policyAtState).map(function(key, index){
nextState = takeAction([y, x], key);
rewardAtNextState = r.getReward(nextState[0], nextState[1]);
discountedValueAtNextState = gamma * v.getValue(nextState[0], nextState[1]);
return (policyAtState[key] * (rewardAtNextState + discountedValueAtNextState));
}).reduce((a, b) => a + b, 0);
v.setValue(y, x, newValueAtState);
delta = Math.max(delta, Math.abs(currentV - newValueAtState));
numIter ++;
}
}
} while(delta >= theta)
}
var numIter = 0;
policyEvaluation(numRows, numCols);
console.log(numIter);
normalized = v.getNormalized()
var collision = false
var coords = [0, numRows - 1] //Starting co-ords
drawGrid()
drawBall(coords[0], coords[1])
printCoords(coords)
//d3.interval(playGame, 1000)
</script>
</html>