Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: alexband/Udacity-373
base: 6c2436de9d
...
head fork: alexband/Udacity-373
compare: 6d1c1818f1
  • 6 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 23, 2012
huanghuang vimrc 35dd79d
Commits on Mar 25, 2012
huanghuang 5-5 3a67dac
huanghuang 5 8e741ef
huanghuang twiddle ce2ec7a
huanghuang 2 b083511
huanghuang 5-3 6d1c181
42 .vimrc
View
@@ -0,0 +1,42 @@
+set encoding=utf8
+set expandtab
+set textwidth=0
+set tabstop=4
+set softtabstop=4
+set shiftwidth=4
+set backspace=indent,eol,start
+set incsearch
+set ruler
+set wildmenu
+set commentstring=\ #\ %s
+set foldlevel=0
+set clipboard+=unnamed
+set tags=/home/huanghuang/shire/tags
+syntax on
+set list listchars=tab:▷⋅,trail:⋅,nbsp:⋅
+set statusline=%F%m%r%h%w\ [TYPE=%Y\ %{&ff}]\
+\ [%l/%L\ (%p%%)
+filetype plugin indent on
+au FileType py set autoindent
+au FileType py set smartindent
+au FileType py set textwidth=79 " PEP-8 Friendly
+" NERD_tree config
+let NERDTreeChDirMode=2
+let NERDTreeIgnore=['\.vim$', '\~$', '\.pyc$', '\.swp$']
+let NERDTreeSortOrder=['^__\.py$', '\/$', '*', '\.swp$', '\~$']
+let NERDTreeShowBookmarks=1
+map <F3> :NERDTreeToggle<CR>
+" Viewport Controls
+" ie moving between split panes
+map <silent>,h <C-w>h
+map <silent>,j <C-w>j
+map <silent>,k <C-w>k
+map <silent>,l <C-w>l
+
+let g:pydiction_location = '/home/huanghuang/.vim/pydiction-1.2/complete-dict'
+"autocmd FileType python compiler pylint
+"let g:pylint_onwrite = 0
+au BufRead,BufNewFile *.c,*.cpp,*.py 2match Underlined /.\%81v/
+if has("gui_running")
+ highlight SpellBad term=underline gui=undercurl guisp=Orange
+endif
161 5-11.py
View
@@ -0,0 +1,161 @@
+# -----------
+# User Instructions
+#
+# Implement a PD controller by running 100 iterations
+# of robot motion. The steering angle should be set
+# by the parameter tau so that:
+#
+# steering = -tau_p * CTE - tau_d * diff_CTE
+# where differential crosstrack error (diff_CTE)
+# is given by CTE(t) - CTE(t-1)
+#
+# Your code should print output that looks like
+# the output shown in the video.
+#
+# Only modify code at the bottom!
+# ------------
+
+from math import *
+import random
+
+
+# ------------------------------------------------
+#
+# this is the robot class
+#
+
+class robot:
+
+ # --------
+ # init:
+ # creates robot and initializes location/orientation to 0, 0, 0
+ #
+
+ def __init__(self, length = 20.0):
+ self.x = 0.0
+ self.y = 0.0
+ self.orientation = 0.0
+ self.length = length
+ self.steering_noise = 0.0
+ self.distance_noise = 0.0
+ self.steering_drift = 0.0
+
+ # --------
+ # set:
+ # sets a robot coordinate
+ #
+
+ def set(self, new_x, new_y, new_orientation):
+
+ self.x = float(new_x)
+ self.y = float(new_y)
+ self.orientation = float(new_orientation) % (2.0 * pi)
+
+
+ # --------
+ # set_noise:
+ # sets the noise parameters
+ #
+
+ def set_noise(self, new_s_noise, new_d_noise):
+ # makes it possible to change the noise parameters
+ # this is often useful in particle filters
+ self.steering_noise = float(new_s_noise)
+ self.distance_noise = float(new_d_noise)
+
+ # --------
+ # set_steering_drift:
+ # sets the systematical steering drift parameter
+ #
+
+ def set_steering_drift(self, drift):
+ self.steering_drift = drift
+
+ # --------
+ # move:
+ # steering = front wheel steering angle, limited by max_steering_angle
+ # distance = total distance driven, most be non-negative
+
+ def move(self, steering, distance,
+ tolerance = 0.001, max_steering_angle = pi / 4.0):
+
+ if steering > max_steering_angle:
+ steering = max_steering_angle
+ if steering < -max_steering_angle:
+ steering = -max_steering_angle
+ if distance < 0.0:
+ distance = 0.0
+
+
+ # make a new copy
+ res = robot()
+ res.length = self.length
+ res.steering_noise = self.steering_noise
+ res.distance_noise = self.distance_noise
+ res.steering_drift = self.steering_drift
+
+ # apply noise
+ steering2 = random.gauss(steering, self.steering_noise)
+ distance2 = random.gauss(distance, self.distance_noise)
+
+ # apply steering drift
+ steering2 += self.steering_drift
+
+ # Execute motion
+ turn = tan(steering2) * distance2 / res.length
+
+ if abs(turn) < tolerance:
+
+ # approximate by straight line motion
+
+ res.x = self.x + (distance2 * cos(self.orientation))
+ res.y = self.y + (distance2 * sin(self.orientation))
+ res.orientation = (self.orientation + turn) % (2.0 * pi)
+
+ else:
+
+ # approximate bicycle model for motion
+
+ radius = distance2 / turn
+ cx = self.x - (sin(self.orientation) * radius)
+ cy = self.y + (cos(self.orientation) * radius)
+ res.orientation = (self.orientation + turn) % (2.0 * pi)
+ res.x = cx + (sin(res.orientation) * radius)
+ res.y = cy - (cos(res.orientation) * radius)
+
+ return res
+
+ def __repr__(self):
+ return '[x=%.5f y=%.5f orient=%.5f]' % (self.x, self.y, self.orientation)
+
+############## ADD / MODIFY CODE BELOW ####################
+
+# ------------------------------------------------------------------------
+#
+# run - does a single control run.
+
+
+def run(param1, param2):
+ myrobot = robot()
+ myrobot.set(0.0, 1.0, 0.0)
+ myrobot.set_steering_drift(10.0 / 180.8 * pi)
+ speed = 1.0 # motion distance is equal to speed (we assume time = 1)
+ N = 100
+ cte = myrobot.y
+ for i in range(N):
+ cte_now = myrobot.y
+ steer = -param1 * cte_now - param2 * (cte_now - cte)
+ myrobot = myrobot.move(steer, speed)
+ cte = cte_now
+ print myrobot, steer
+
+ #
+ # Enter code here
+ #
+
+# Call your function with parameters of 0.2 and 3.0 and print results
+run(0.2, 4.0)
+
+
+
+
171 5-14.py
View
@@ -0,0 +1,171 @@
+# -----------
+# User Instructions
+#
+# Implement a P controller by running 100 iterations
+# of robot motion. The steering angle should be set
+# by the parameter tau so that:
+#
+# steering = -tau_p * CTE - tau_d * diff_CTE - tau_i * int_CTE
+#
+# where the integrated crosstrack error (int_CTE) is
+# the sum of all the previous crosstrack errors.
+# This term works to cancel out steering drift.
+#
+# Your code should print a list that looks just like
+# the list shown in the video.
+#
+# Only modify code at the bottom!
+# ------------
+
+from math import *
+import random
+
+
+# ------------------------------------------------
+#
+# this is the robot class
+#
+
+class robot:
+
+ # --------
+ # init:
+ # creates robot and initializes location/orientation to 0, 0, 0
+ #
+
+ def __init__(self, length = 20.0):
+ self.x = 0.0
+ self.y = 0.0
+ self.orientation = 0.0
+ self.length = length
+ self.steering_noise = 0.0
+ self.distance_noise = 0.0
+ self.steering_drift = 0.0
+
+ # --------
+ # set:
+ # sets a robot coordinate
+ #
+
+ def set(self, new_x, new_y, new_orientation):
+
+ self.x = float(new_x)
+ self.y = float(new_y)
+ self.orientation = float(new_orientation) % (2.0 * pi)
+
+
+ # --------
+ # set_noise:
+ # sets the noise parameters
+ #
+
+ def set_noise(self, new_s_noise, new_d_noise):
+ # makes it possible to change the noise parameters
+ # this is often useful in particle filters
+ self.steering_noise = float(new_s_noise)
+ self.distance_noise = float(new_d_noise)
+
+ # --------
+ # set_steering_drift:
+ # sets the systematical steering drift parameter
+ #
+
+ def set_steering_drift(self, drift):
+ self.steering_drift = drift
+
+ # --------
+ # move:
+ # steering = front wheel steering angle, limited by max_steering_angle
+ # distance = total distance driven, most be non-negative
+
+ def move(self, steering, distance,
+ tolerance = 0.001, max_steering_angle = pi / 4.0):
+
+ if steering > max_steering_angle:
+ steering = max_steering_angle
+ if steering < -max_steering_angle:
+ steering = -max_steering_angle
+ if distance < 0.0:
+ distance = 0.0
+
+
+ # make a new copy
+ res = robot()
+ res.length = self.length
+ res.steering_noise = self.steering_noise
+ res.distance_noise = self.distance_noise
+ res.steering_drift = self.steering_drift
+
+ # apply noise
+ steering2 = random.gauss(steering, self.steering_noise)
+ distance2 = random.gauss(distance, self.distance_noise)
+
+ # apply steering drift
+ steering2 += self.steering_drift
+
+ # Execute motion
+ turn = tan(steering2) * distance2 / res.length
+
+ if abs(turn) < tolerance:
+
+ # approximate by straight line motion
+
+ res.x = self.x + (distance2 * cos(self.orientation))
+ res.y = self.y + (distance2 * sin(self.orientation))
+ res.orientation = (self.orientation + turn) % (2.0 * pi)
+
+ else:
+
+ # approximate bicycle model for motion
+
+ radius = distance2 / turn
+ cx = self.x - (sin(self.orientation) * radius)
+ cy = self.y + (cos(self.orientation) * radius)
+ res.orientation = (self.orientation + turn) % (2.0 * pi)
+ res.x = cx + (sin(res.orientation) * radius)
+ res.y = cy - (cos(res.orientation) * radius)
+
+ return res
+
+
+
+
+ def __repr__(self):
+ return '[x=%.5f y=%.5f orient=%.5f]' % (self.x, self.y, self.orientation)
+
+
+
+
+############## ADD / MODIFY CODE BELOW ####################
+
+# ------------------------------------------------------------------------
+#
+# run - does a single control run.
+
+
+def run(param1, param2, param3):
+ myrobot = robot()
+ myrobot.set(0.0, 1.0, 0.0)
+ speed = 1.0 # motion distance is equal to speed (we assume time = 1)
+ N = 200
+ myrobot.set_steering_drift(10.0 / 180.0 * pi) # 10 degree bias, this will be added in by the move function, you do not need to add it below!
+ cte = myrobot.y
+ I = 0
+ for i in range(N):
+ cte_now = myrobot.y
+ p = cte
+ d = cte_now - cte
+ I += cte
+ steer = -param1 * p - param2 * d - param3 * I
+ myrobot = myrobot.move(steer, speed)
+ cte = cte_now
+ print myrobot, steer
+
+
+# Call your function with parameters of (0.2, 3.0, and 0.004)
+run(0.2, 3.0, 0.004)
+
+
+
+
+
81 5-5.py
View
@@ -0,0 +1,81 @@
+# -----------
+# User Instructions
+#
+# Define a function smooth that takes a path as its input
+# (with optional parameters for weight_data, weight_smooth)
+# and returns a smooth path.
+#
+# 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 previous video:
+#
+# If your function isn't submitting it is possible that the
+# runtime is too long. Try sacrificing accuracy for speed.
+# -----------
+#gradient descent to make
+#(Xi-Yi)**2 --> min
+#(Yi-Yi+1)**2 --> min
+#
+# Xi means the element in old path path[i][j]
+# Yi means the element in new path newpath[i][j] Yi-1 Yi+1 means newpath[i-1][j]
+# newpath[i+1][j]
+# j means for x cordinate and y cordiante we use the same method
+# tolerance means until the change would not make a difference big than that
+
+
+from math import *
+
+# 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]]
+
+# ------------------------------------------------
+# smooth coordinates
+#
+
+def smooth(path, weight_data = 0.5, weight_smooth = 0.1):
+
+ # Make a deep copy of path into newpath
+ #newpath = [[0 for col in range(len(path[0]))] for row in range(len(path))]
+ #for i in range(len(path)):
+ # for j in range(len(path[0])):
+ # newpath[i][j] = path[i][j]
+ from copy import deepcopy
+ newpath = deepcopy(path)
+
+
+ #### ENTER CODE BELOW THIS LINE ###
+ tolerance = 0.00001
+ change = tolerance
+ while (change >= tolerance):
+ change = 0.0
+ #a new round runs gradiant descent
+ for i in range(1,len(path)-1):
+ for j in range(len(path[0])):
+ aux = newpath[i][j]
+ newpath[i][j] = newpath[i][j] + weight_data * ( path[i][j] - newpath[i][j])
+ newpath[i][j] = newpath[i][j] + weight_smooth * (newpath[i-1][j] + newpath[i+1][j] - (2.0 * newpath[i][j]))
+ #how's the change compare with tolerance?
+ change += abs(aux - newpath[i][j])
+ return newpath # Leave this line for the grader!
+
+# feel free to leave this and the following lines if you want to print.
+newpath = smooth(path)
+
+# thank you - EnTerr - for posting this on our discussion forum
+for i in range(len(path)):
+ print '['+ ', '.join('%.3f'%x for x in path[i]) +'] -> ['+ ', '.join('%.3f'%x for x in newpath[i]) +']'
+
+
+
+
+
+
154 5-9.py
View
@@ -0,0 +1,154 @@
+# -----------
+# User Instructions
+#
+# Implement a P controller by running 100 iterations
+# of robot motion. The steering angle should be set
+# by the parameter tau so that:
+#
+# steering = -tau * crosstrack_error
+#
+# Note that tau is called "param" in the function
+# run to be completed below.
+#
+# Your code should print output that looks like
+# the output shown in the video. That is, at each step:
+# print myrobot, steering
+#
+# Only modify code at the bottom!
+# ------------
+
+from math import *
+import random
+
+
+# ------------------------------------------------
+#
+# this is the robot class
+#
+
+class robot:
+
+ # --------
+ # init:
+ # creates robot and initializes location/orientation to 0, 0, 0
+ #
+
+ def __init__(self, length = 20.0):
+ self.x = 0.0
+ self.y = 0.0
+ self.orientation = 0.0
+ self.length = length
+ self.steering_noise = 0.0
+ self.distance_noise = 0.0
+ self.steering_drift = 0.0
+
+ # --------
+ # set:
+ # sets a robot coordinate
+ #
+
+ def set(self, new_x, new_y, new_orientation):
+
+ self.x = float(new_x)
+ self.y = float(new_y)
+ self.orientation = float(new_orientation) % (2.0 * pi)
+
+
+ # --------
+ # set_noise:
+ # sets the noise parameters
+ #
+
+ def set_noise(self, new_s_noise, new_d_noise):
+ # makes it possible to change the noise parameters
+ # this is often useful in particle filters
+ self.steering_noise = float(new_s_noise)
+ self.distance_noise = float(new_d_noise)
+
+ # --------
+ # set_steering_drift:
+ # sets the systematical steering drift parameter
+ #
+
+ def set_steering_drift(self, drift):
+ self.steering_drift = drift
+
+ # --------
+ # move:
+ # steering = front wheel steering angle, limited by max_steering_angle
+ # distance = total distance driven, most be non-negative
+
+ def move(self, steering, distance,
+ tolerance = 0.001, max_steering_angle = pi / 4.0):
+
+ if steering > max_steering_angle:
+ steering = max_steering_angle
+ if steering < -max_steering_angle:
+ steering = -max_steering_angle
+ if distance < 0.0:
+ distance = 0.0
+
+
+ # make a new copy
+ res = robot()
+ res.length = self.length
+ res.steering_noise = self.steering_noise
+ res.distance_noise = self.distance_noise
+ res.steering_drift = self.steering_drift
+
+ # apply noise
+ steering2 = random.gauss(steering, self.steering_noise)
+ distance2 = random.gauss(distance, self.distance_noise)
+
+ # apply steering drift
+ steering2 += self.steering_drift
+
+ # Execute motion
+ turn = tan(steering2) * distance2 / res.length
+
+ if abs(turn) < tolerance:
+
+ # approximate by straight line motion
+
+ res.x = self.x + (distance2 * cos(self.orientation))
+ res.y = self.y + (distance2 * sin(self.orientation))
+ res.orientation = (self.orientation + turn) % (2.0 * pi)
+
+ else:
+
+ # approximate bicycle model for motion
+
+ radius = distance2 / turn
+ cx = self.x - (sin(self.orientation) * radius)
+ cy = self.y + (cos(self.orientation) * radius)
+ res.orientation = (self.orientation + turn) % (2.0 * pi)
+ res.x = cx + (sin(res.orientation) * radius)
+ res.y = cy - (cos(res.orientation) * radius)
+
+ return res
+
+ def __repr__(self):
+ return '[x=%.5f y=%.5f orient=%.5f]' % (self.x, self.y, self.orientation)
+
+############## ADD / MODIFY CODE BELOW ####################
+
+# ------------------------------------------------------------------------
+#
+# run - does a single control run
+
+
+def run(param):
+ myrobot = robot()
+ myrobot.set(0.0, 1.0, 0.0)
+ speed = 1.0 # motion distance is equal to speed (we assume time = 1)
+ N = 100
+ #
+ # Add Code Here
+ #
+ for i in range(N):
+ steering = -param * myrobot.y
+ myrobot = myrobot.move(steering, speed)
+ print myrobot, steering
+
+run(0.1) # call function with parameter tau of 0.1 and print results
+
207 circle.py
View
@@ -0,0 +1,207 @@
+# -------------
+# User Instructions
+#
+# Now you will be incorporating fixed points into
+# your smoother.
+#
+# You will need to use the equations from gradient
+# descent AND the new equations presented in the
+# previous lecture to implement smoothing with
+# fixed points.
+#
+# Your function should return the newpath that it
+# calculates.
+#
+# Feel free to use the provided solution_check function
+# to test your code. You can find it at the bottom.
+#
+# --------------
+# Testing Instructions
+#
+# To test your code, call the solution_check function with
+# two arguments. The first argument should be the result of your
+# smooth function. The second should be the corresponding answer.
+# For example, calling
+#
+# solution_check(smooth(testpath1), answer1)
+#
+# should return True if your answer is correct and False if
+# it is not.
+
+from math import *
+
+# Do not modify path inside your function.
+path=[[0, 0], #fix
+ [1, 0],
+ [2, 0],
+ [3, 0],
+ [4, 0],
+ [5, 0],
+ [6, 0], #fix
+ [6, 1],
+ [6, 2],
+ [6, 3], #fix
+ [5, 3],
+ [4, 3],
+ [3, 3],
+ [2, 3],
+ [1, 3],
+ [0, 3], #fix
+ [0, 2],
+ [0, 1]]
+
+# Do not modify fix inside your function
+fix = [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0]
+
+######################## ENTER CODE BELOW HERE #########################
+
+def smooth(path, fix, weight_data = 0.0, weight_smooth = 0.1, tolerance = 0.00001):
+ #
+ # Enter code here.
+ # The weight for each of the two new equations should be 0.5 * weight_smooth
+ #
+ newpath = [[0 for col in range(len(path[0]))] for row in range(len(path))]
+ for i in range(len(path)):
+ for j in range(len(path[0])):
+ newpath[i][j] = path[i][j]
+
+ change = tolerance
+ while (change >= tolerance):
+ change = 0.0
+ for i in range(len(path)):
+ if not fix[i]:
+ for j in range(len(path[0])):
+ aux = newpath[i][j]
+ newpath[i][j] = newpath[i][j] + weight_data * (path[i][j] - newpath[i][j])
+ last = newpath[(i-1) % len(newpath)][j]
+ nextone = newpath[(i+1) % len(newpath)][j]
+ newpath[i][j] = newpath[i][j] + weight_smooth * (last + nextone - (2.0 * newpath[i][j]))
+ #new equation
+ last2 = newpath[(i-2) % len(newpath)][j]
+ next2 = newpath[(i+2) % len(newpath)][j]
+ weight = 0.5 * weight_smooth
+ steplast = 2*last-last2-newpath[i][j]
+ newpath[i][j] = newpath[i][j]+weight*steplast
+ stepnext = 2*nextone-next2-newpath[i][j]
+ newpath[i][j] = newpath[i][j]+weight*stepnext
+ change += abs(aux - newpath[i][j])
+ return newpath
+
+
+
+#thank you - EnTerr - for posting this on our discussion forum
+
+##newpath = smooth(path)
+##for i in range(len(path)):
+## print '['+ ', '.join('%.3f'%x for x in path[i]) +'] -> ['+ ', '.join('%.3f'%x for x in newpath[i]) +']'
+
+# --------------------------------------------------
+# check if two numbers are 'close enough,'used in
+# solution_check function.
+#
+def close_enough(user_answer, true_answer, epsilon = 0.03):
+ if abs(user_answer - true_answer) > epsilon:
+ return False
+ return True
+
+# --------------------------------------------------
+# check your solution against our reference solution for
+# a variety of test cases (given below)
+#
+def solution_check(newpath, answer):
+ if type(newpath) != type(answer):
+ print "Error. You do not return a list."
+ return False
+ if len(newpath) != len(answer):
+ print 'Error. Your newpath is not the correct length.'
+ return False
+ if len(newpath[0]) != len(answer[0]):
+ print 'Error. Your entries do not contain an (x, y) coordinate pair.'
+ return False
+ for i in range(len(newpath)):
+ for j in range(len(newpath[0])):
+ if not close_enough(newpath[i][j], answer[i][j]):
+ print 'Error, at least one of your entries is not correct.'
+ return False
+ print "Test case correct!"
+ return True
+
+# --------------
+# Testing Instructions
+#
+# To test your code, call the solution_check function with
+# two arguments. The first argument should be the result of your
+# smooth function. The second should be the corresponding answer.
+# For example, calling
+#
+# solution_check(smooth(testpath1), answer1)
+#
+# should return True if your answer is correct and False if
+# it is not.
+
+testpath1=[[0, 0], #fix
+ [1, 0],
+ [2, 0],
+ [3, 0],
+ [4, 0],
+ [5, 0],
+ [6, 0], #fix
+ [6, 1],
+ [6, 2],
+ [6, 3], #fix
+ [5, 3],
+ [4, 3],
+ [3, 3],
+ [2, 3],
+ [1, 3],
+ [0, 3], #fix
+ [0, 2],
+ [0, 1]]
+testfix1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0]
+answer1 = [[0, 0],
+ [0.7938620981547201, -0.8311168821106101],
+ [1.8579052986461084, -1.3834788165869276],
+ [3.053905318597796, -1.5745863173084],
+ [4.23141390533387, -1.3784271816058231],
+ [5.250184859723701, -0.8264215958231558],
+ [6, 0],
+ [6.415150091996651, 0.9836951698796843],
+ [6.41942442687092, 2.019512290770163],
+ [6, 3],
+ [5.206131365604606, 3.831104483245191],
+ [4.142082497497067, 4.383455704596517],
+ [2.9460804122779813, 4.5745592975708105],
+ [1.768574219397359, 4.378404668718541],
+ [0.7498089205417316, 3.826409771585794],
+ [0, 3],
+ [-0.4151464728194156, 2.016311854977891],
+ [-0.4194207879552198, 0.9804948340550833]]
+
+testpath2 = [[0, 0], # fix
+ [2, 0],
+ [4, 0], # fix
+ [4, 2],
+ [4, 4], # fix
+ [2, 4],
+ [0, 4], # fix
+ [0, 2]]
+testfix2 = [1, 0, 1, 0, 1, 0, 1, 0]
+answer2 = [[0, 0],
+ [2.0116767115496095, -0.7015439080661671],
+ [4, 0],
+ [4.701543905420104, 2.0116768147460418],
+ [4, 4],
+ [1.9883231877640861, 4.701543807525115],
+ [0, 4],
+ [-0.7015438099112995, 1.9883232808252207]]
+
+solution_check(smooth(testpath1, testfix1), answer1)
+solution_check(smooth(testpath2, testfix2), answer2)
+
+
+
+for i in range(len(testpath1)):
+ print '['+ ', '.join('%.3f'%x for x in answer1[i]) +'] -> ['+ ', '.join('%.3f'%x for x in smooth(testpath1, testfix1)[i]) +']'
+
+for i in range(len(testpath2)):
+ print '['+ ', '.join('%.3f'%x for x in answer2[i]) +'] -> ['+ ', '.join('%.3f'%x for x in smooth(testpath2, testfix2)[i]) +']'
84 hw-4.py
View
@@ -0,0 +1,84 @@
+# --------------
+# USER INSTRUCTIONS
+#
+# Write a function called stochastic_value that
+# takes no input and RETURNS two grids. The
+# first grid, value, should contain the computed
+# value of each cell as shown in the video. The
+# second grid, policy, should contain the optimum
+# policy for each cell.
+#
+# Stay tuned for a homework help video! This should
+# be available by Thursday and will be visible
+# in the course content tab.
+#
+# Good luck! Keep learning!
+#
+# --------------
+# GRADING NOTES
+#
+# We will be calling your stochastic_value function
+# with several different grids and different values
+# of success_prob, collision_cost, and cost_step.
+# In order to be marked correct, your function must
+# RETURN (it does not have to print) two grids,
+# value and policy.
+#
+# When grading your value grid, we will compare the
+# value of each cell with the true value according
+# to this model. If your answer for each cell
+# is sufficiently close to the correct answer
+# (within 0.001), you will be marked as correct.
+#
+# NOTE: Please do not modify the values of grid,
+# success_prob, collision_cost, or cost_step inside
+# your function. Doing so could result in your
+# submission being inappropriately marked as incorrect.
+
+# -------------
+# GLOBAL VARIABLES
+#
+# You may modify these variables for testing
+# purposes, but you should only modify them here.
+# Do NOT modify them inside your stochastic_value
+# function.
+
+grid = [[0, 0, 0, 0],
+ [0, 0, 0, 0],
+ [0, 0, 0, 0],
+ [0, 1, 1, 0]]
+
+goal = [0, len(grid[0])-1] # Goal is in top right corner
+
+
+delta = [[-1, 0 ], # go up
+ [ 0, -1], # go left
+ [ 1, 0 ], # go down
+ [ 0, 1 ]] # go right
+
+delta_name = ['^', '<', 'v', '>'] # Use these when creating your policy grid.
+
+success_prob = 0.5
+failure_prob = (1.0 - success_prob)/2.0 # Probability(stepping left) = prob(stepping right) = failure_prob
+collision_cost = 100
+cost_step = 1
+
+
+############## INSERT/MODIFY YOUR CODE BELOW ##################
+#
+# You may modify the code below if you want, but remember that
+# your function must...
+#
+# 1) ...be called stochastic_value().
+# 2) ...NOT take any arguments.
+# 3) ...return two grids: FIRST value and THEN policy.
+
+def stochastic_value():
+ value = [[1000 for row in range(len(grid[0]))] for col in range(len(grid))]
+ policy = [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
+
+ return value, policy
+
+
+
+
238 hw5-2.py
View
@@ -0,0 +1,238 @@
+# -------------
+# User Instructions
+#
+# Here you will be implementing a cyclic smoothing
+# algorithm. This algorithm should not fix the end
+# points (as you did in the unit quizzes). You
+# should use the gradient descent equations that
+# you used previously.
+#
+# Your function should return the newpath that it
+# calculates..
+#
+# Feel free to use the provided solution_check function
+# to test your code. You can find it at the bottom.
+#
+# --------------
+# Testing Instructions
+#
+# To test your code, call the solution_check function with
+# two arguments. The first argument should be the result of your
+# smooth function. The second should be the corresponding answer.
+# For example, calling
+#
+# solution_check(smooth(testpath1), answer1)
+#
+# should return True if your answer is correct and False if
+# it is not.
+
+from math import *
+
+# Do not modify path inside your function.
+path=[[0, 0],
+ [1, 0],
+ [2, 0],
+ [3, 0],
+ [4, 0],
+ [5, 0],
+ [6, 0],
+ [6, 1],
+ [6, 2],
+ [6, 3],
+ [5, 3],
+ [4, 3],
+ [3, 3],
+ [2, 3],
+ [1, 3],
+ [0, 3],
+ [0, 2],
+ [0, 1]]
+
+############# ONLY ENTER CODE BELOW THIS LINE ##########
+
+# ------------------------------------------------
+# smooth coordinates
+# If your code is timing out, make the tolerance parameter
+# larger to decrease run time.
+#
+
+def smooth(path, weight_data = 0.1, weight_smooth = 0.1, tolerance = 0.00001):
+
+ #
+ # Enter code here
+ #
+ # Make a deep copy of path into newpath
+ newpath = [[0 for col in range(len(path[0]))] for row in range(len(path))]
+ for i in range(len(path)):
+ for j in range(len(path[0])):
+ newpath[i][j] = path[i][j]
+
+ change = tolerance
+ while (change >= tolerance):
+ change = 0.0
+ for i in range(len(path)):
+ for j in range(len(path[0])):
+ aux = newpath[i][j]
+ newpath[i][j] = newpath[i][j] + weight_data * (path[i][j] - newpath[i][j])
+ last = newpath[(i-1) % len(newpath)][j]
+ nextone = newpath[(i+1) % len(newpath)][j]
+ newpath[i][j] = newpath[i][j] + weight_smooth * (last + nextone - (2.0 * newpath[i][j]))
+ change += abs(aux - newpath[i][j])
+ return newpath
+
+# thank you - EnTerr - for posting this on our discussion forum
+
+#newpath = smooth(path)
+#for i in range(len(path)):
+# print '['+ ', '.join('%.3f'%x for x in path[i]) +'] -> ['+ ', '.join('%.3f'%x for x in newpath[i]) +']'
+
+
+##### TESTING ######
+
+# --------------------------------------------------
+# check if two numbers are 'close enough,'used in
+# solution_check function.
+#
+def close_enough(user_answer, true_answer, epsilon = 0.001):
+ if abs(user_answer - true_answer) > epsilon:
+ return False
+ return True
+
+# --------------------------------------------------
+# check your solution against our reference solution for
+# a variety of test cases (given below)
+#
+def solution_check(newpath, answer):
+ if type(newpath) != type(answer):
+ print "Error. You do not return a list."
+ return False
+ if len(newpath) != len(answer):
+ print 'Error. Your newpath is not the correct length.'
+ return False
+ if len(newpath[0]) != len(answer[0]):
+ print 'Error. Your entries do not contain an (x, y) coordinate pair.'
+ return False
+ for i in range(len(newpath)):
+ for j in range(len(newpath[0])):
+ if not close_enough(newpath[i][j], answer[i][j]):
+ print 'Error, at least one of your entries is not correct.'
+ return False
+ print "Test case correct!"
+ return True
+
+# --------------
+# Testing Instructions
+#
+# To test your code, call the solution_check function with
+# two arguments. The first argument should be the result of your
+# smooth function. The second should be the corresponding answer.
+# For example, calling
+#
+# solution_check(smooth(testpath1), answer1)
+#
+# should return True if your answer is correct and False if
+# it is not.
+
+testpath1 = [[0, 0],
+ [1, 0],
+ [2, 0],
+ [3, 0],
+ [4, 0],
+ [5, 0],
+ [6, 0],
+ [6, 1],
+ [6, 2],
+ [6, 3],
+ [5, 3],
+ [4, 3],
+ [3, 3],
+ [2, 3],
+ [1, 3],
+ [0, 3],
+ [0, 2],
+ [0, 1]]
+
+answer1 = [[0.5449300156668018, 0.47485226780102946],
+ [1.2230705677535505, 0.2046277687200752],
+ [2.079668890615267, 0.09810778721159963],
+ [3.0000020176660755, 0.07007646364781912],
+ [3.9203348821839112, 0.09810853832382399],
+ [4.7769324511170455, 0.20462917195702085],
+ [5.455071854686622, 0.4748541381544533],
+ [5.697264197153936, 1.1249625336275617],
+ [5.697263485026567, 1.8750401628534337],
+ [5.455069810373743, 2.5251482916876378],
+ [4.776929339068159, 2.795372759575895],
+ [3.92033110541304, 2.9018927284871063],
+ [2.999998066091118, 2.929924058932193],
+ [2.0796652780381826, 2.90189200881968],
+ [1.2230677654766597, 2.7953714133566603],
+ [0.544928391271399, 2.5251464933327794],
+ [0.3027360471605494, 1.875038145804603],
+ [0.302736726373967, 1.1249605602741133]]
+
+## [0.000, 0.000] -> [0.545, 0.475]
+## [1.000, 0.000] -> [1.223, 0.205]
+## [2.000, 0.000] -> [2.080, 0.098]
+## [3.000, 0.000] -> [3.000, 0.070]
+## [4.000, 0.000] -> [3.920, 0.098]
+## [5.000, 0.000] -> [4.777, 0.205]
+## [6.000, 0.000] -> [5.455, 0.475]
+## [6.000, 1.000] -> [5.697, 1.125]
+## [6.000, 2.000] -> [5.697, 1.875]
+## [6.000, 3.000] -> [5.455, 2.525]
+## [5.000, 3.000] -> [4.777, 2.795]
+## [4.000, 3.000] -> [3.920, 2.902]
+## [3.000, 3.000] -> [3.000, 2.930]
+## [2.000, 3.000] -> [2.080, 2.902]
+## [1.000, 3.000] -> [1.223, 2.795]
+## [0.000, 3.000] -> [0.545, 2.525]
+## [0.000, 2.000] -> [0.303, 1.875]
+## [0.000, 1.000] -> [0.303, 1.125]
+
+
+testpath2 = [[1, 0], # Move in the shape of a plus sign
+ [2, 0],
+ [2, 1],
+ [3, 1],
+ [3, 2],
+ [2, 2],
+ [2, 3],
+ [1, 3],
+ [1, 2],
+ [0, 2],
+ [0, 1],
+ [1, 1]]
+
+answer2 = [[1.239080543767428, 0.5047204351187283],
+ [1.7609243903912781, 0.5047216452560908],
+ [2.0915039821562416, 0.9085017167753027],
+ [2.495281862032503, 1.2390825203587184],
+ [2.4952805300504783, 1.7609262468826048],
+ [2.0915003641706296, 2.0915058211575475],
+ [1.7609195135622062, 2.4952837841027695],
+ [1.2390757942466555, 2.4952826072236918],
+ [0.9084962737918979, 2.091502621431358],
+ [0.5047183914625598, 1.7609219230352355],
+ [0.504719649257698, 1.2390782835562297],
+ [0.9084996902674257, 0.9084987462432871]]
+
+## [1.000, 0.000] -> [1.239, 0.505]
+## [2.000, 0.000] -> [1.761, 0.505]
+## [2.000, 1.000] -> [2.092, 0.909]
+## [3.000, 1.000] -> [2.495, 1.239]
+## [3.000, 2.000] -> [2.495, 1.761]
+## [2.000, 2.000] -> [2.092, 2.092]
+## [2.000, 3.000] -> [1.761, 2.495]
+## [1.000, 3.000] -> [1.239, 2.495]
+## [1.000, 2.000] -> [0.908, 2.092]
+## [0.000, 2.000] -> [0.505, 1.761]
+## [0.000, 1.000] -> [0.505, 1.239]
+## [1.000, 1.000] -> [0.908, 0.908]
+
+solution_check(smooth(testpath1), answer1)
+solution_check(smooth(testpath2), answer2)
+
+
+
+
205 twiddle.py
View
@@ -0,0 +1,205 @@
+# ----------------
+# User Instructions
+#
+# Implement twiddle as shown in the previous two videos.
+# Your accumulated error should be very small!
+#
+# Your twiddle function should RETURN the accumulated
+# error. Try adjusting the parameters p and dp to make
+# this error as small as possible.
+#
+# Try to get your error below 1.0e-10 with as few iterations
+# as possible (too many iterations will cause a timeout).
+# No cheating!
+# ------------
+
+from math import *
+import random
+
+
+# ------------------------------------------------
+#
+# this is the robot class
+#
+
+class robot:
+
+ # --------
+ # init:
+ # creates robot and initializes location/orientation to 0, 0, 0
+ #
+
+ def __init__(self, length = 20.0):
+ self.x = 0.0
+ self.y = 0.0
+ self.orientation = 0.0
+ self.length = length
+ self.steering_noise = 0.0
+ self.distance_noise = 0.0
+ self.steering_drift = 0.0
+
+ # --------
+ # set:
+ # sets a robot coordinate
+ #
+
+ def set(self, new_x, new_y, new_orientation):
+
+ self.x = float(new_x)
+ self.y = float(new_y)
+ self.orientation = float(new_orientation) % (2.0 * pi)
+
+
+ # --------
+ # set_noise:
+ # sets the noise parameters
+ #
+
+ def set_noise(self, new_s_noise, new_d_noise):
+ # makes it possible to change the noise parameters
+ # this is often useful in particle filters
+ self.steering_noise = float(new_s_noise)
+ self.distance_noise = float(new_d_noise)
+
+ # --------
+ # set_steering_drift:
+ # sets the systematical steering drift parameter
+ #
+
+ def set_steering_drift(self, drift):
+ self.steering_drift = drift
+
+ # --------
+ # move:
+ # steering = front wheel steering angle, limited by max_steering_angle
+ # distance = total distance driven, most be non-negative
+
+ def move(self, steering, distance,
+ tolerance = 0.001, max_steering_angle = pi / 4.0):
+
+ if steering > max_steering_angle:
+ steering = max_steering_angle
+ if steering < -max_steering_angle:
+ steering = -max_steering_angle
+ if distance < 0.0:
+ distance = 0.0
+
+
+ # make a new copy
+ res = robot()
+ res.length = self.length
+ res.steering_noise = self.steering_noise
+ res.distance_noise = self.distance_noise
+ res.steering_drift = self.steering_drift
+
+ # apply noise
+ steering2 = random.gauss(steering, self.steering_noise)
+ distance2 = random.gauss(distance, self.distance_noise)
+
+ # apply steering drift
+ steering2 += self.steering_drift
+
+ # Execute motion
+ turn = tan(steering2) * distance2 / res.length
+
+ if abs(turn) < tolerance:
+
+ # approximate by straight line motion
+
+ res.x = self.x + (distance2 * cos(self.orientation))
+ res.y = self.y + (distance2 * sin(self.orientation))
+ res.orientation = (self.orientation + turn) % (2.0 * pi)
+
+ else:
+
+ # approximate bicycle model for motion
+
+ radius = distance2 / turn
+ cx = self.x - (sin(self.orientation) * radius)
+ cy = self.y + (cos(self.orientation) * radius)
+ res.orientation = (self.orientation + turn) % (2.0 * pi)
+ res.x = cx + (sin(res.orientation) * radius)
+ res.y = cy - (cos(res.orientation) * radius)
+
+ return res
+
+
+
+
+ def __repr__(self):
+ return '[x=%.5f y=%.5f orient=%.5f]' % (self.x, self.y, self.orientation)
+
+
+# ------------------------------------------------------------------------
+#
+# run - does a single control run.
+
+
+def run(params, printflag = False):
+ myrobot = robot()
+ myrobot.set(0.0, 1.0, 0.0)
+ speed = 1.0
+ err = 0.0
+ int_crosstrack_error = 0.0
+ N = 100
+ # myrobot.set_noise(0.1, 0.0)
+ myrobot.set_steering_drift(10.0 / 180.0 * pi) # 10 degree steering error
+
+
+ crosstrack_error = myrobot.y
+
+
+ for i in range(N * 2):
+
+ diff_crosstrack_error = myrobot.y - crosstrack_error
+ crosstrack_error = myrobot.y
+ int_crosstrack_error += crosstrack_error
+
+ steer = - params[0] * crosstrack_error \
+ - params[1] * diff_crosstrack_error \
+ - int_crosstrack_error * params[2]
+ myrobot = myrobot.move(steer, speed)
+ if i >= N:
+ err += (crosstrack_error ** 2)
+ if printflag:
+ print myrobot, steer
+ return err / float(N)
+
+
+def twiddle(tol = 0.0000000001): #Make this tolerance bigger if you are timing out!
+############## ADD CODE BELOW ####################
+
+
+
+ # -------------
+ # Add code here
+ # -------------
+
+ params = [0,0,0]
+ dp = [1,1,1]
+ best_error = run(params)
+ while sum(dp) > tol:
+ for i in range(len(params)):
+ params[i] += dp[i]
+ error = run(params)
+ if error < best_error:
+ best_error = error
+ dp[i] *= 1.1
+ else:
+ params[i] -= 2*dp[i]
+ error = run(params)
+ if error < best_error:
+ best_error = error
+ dp[i] *= 1.1
+ else:
+ params[i] += dp[i]
+ dp[i] *= 0.9
+ print params
+ print dp
+ return run(params)
+
+
+twiddle()
+
+
+

No commit comments for this range

Something went wrong with that request. Please try again.