-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL5Q5.py
46 lines (36 loc) · 1.6 KB
/
L5Q5.py
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
# -----------
# User Instructions
#
# Define a function smooth that takes a path as its input
# (with optional parameters for weight_data, weight_smooth,
# and tolerance) and returns a smooth path. The first and
# last points should remain unchanged.
#
# Smoothing should be implemented by iteratively updating
# each entry in newpath until some desired level of accuracy
# is reached. The update should be done according to the
# gradient descent equations given in the instructor's note
# below (the equations given in the video are not quite
# correct).
# -----------
from copy import deepcopy
# thank you to EnTerr for posting this on our discussion forum
def printpaths(path, newpath):
for old, new in zip(path, newpath):
print '[' + ', '.join('%.3f' % x for x in old) + '] -> [' + ', '.join('%.3f' % x for x in new) + ']'
# Don't modify path inside your function.
path = [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [4, 3], [4, 4]]
def smooth(path, weight_data=0.5, weight_smooth=0.1, tolerance=0.000001):
# Make a deep copy of path into newpath
newpath = deepcopy(path)
delta = tolerance
while delta >= tolerance:
delta = 0.0
for i in range(1, len(path) - 1):
for j in range(len(path[i])):
old = newpath[i][j]
newpath[i][j] += weight_data * (path[i][j] - newpath[i][j]) + weight_smooth * (
newpath[i - 1][j] + newpath[i + 1][j] - 2.0 * newpath[i][j])
delta = abs(old - newpath[i][j])
return newpath # Leave this line for the grader!
printpaths(path, smooth(path, 0, 0.1))