# jaz303/coursework

cs373 finished

1 parent 57cb1a7 commit 326eaefbb0a50d398a20a014338faa26d50f13c2 committed Apr 10, 2012
183 udacity/cs373/final/planning.py
18 udacity/cs373/final/q14.rb
 @@ -0,0 +1,18 @@ +def go(state, remain, bins) + if remain == 0 + state[:count] += 1 + state[:matching] += 1 if bins.all? { |b| !b.empty? } + else + 4.times do |i| + bins[i].push true + go(state, remain - 1, bins) + bins[i].pop + end + end +end + +tally = {:count => 0, :matching => 0} + +go(tally, 12, [[], [], [], []]) + +p tally
15 udacity/cs373/unit-2/coconuts.py
 @@ -0,0 +1,15 @@ +def f(n): + return 4 * (n - 1) / 5.0 + +def f6(n): + for i in range(0, 6): + n = f(n) + return n + +def is_int(n): + return abs(n - int(n)) < 0.0010001 + +for i in range(0, 100000): + if is_int(f6(i)): + print i + break
6 udacity/cs373/unit-2/gaussian.py
 @@ -0,0 +1,6 @@ +from math import * + +def f(mu, sigma2, x): + return 1/sqrt(2.0*pi*sigma2) * exp(-0.5 * (x - mu)**2 / sigma2) + +print f(10.0, 4.0, 10.0)
29 udacity/cs373/unit-2/kalman-1d.py
 @@ -0,0 +1,29 @@ +def update(mean1, var1, mean2, var2): + new_mean = (var2*mean1 + var1*mean2) / (var1 + var2) + new_var = 1.0 / (1 / var1 + 1 / var2) + return [new_mean, new_var] + +def predict(mean1, var1, mean2, var2): + new_mean = mean1 + mean2 + new_var = var1 + var2 + return [new_mean, new_var] + +measurements = [5., 6., 7., 9., 10.] +motion = [1., 1., 2., 1., 1.] +measurement_sig = 4. +motion_sig = 2. +mu = 0 +sig = 0.0000000001 + +for i in range(0, len(measurements)): + [mu, sig] = update(mu, sig, measurements[i], measurement_sig) + print "update: ", [mu, sig] + [mu, sig] = predict(mu, sig, motion[i], motion_sig) + print "predict: ", [mu, sig] + +#Please print out ONLY the final values of the mean +#and the variance in a list [mu, sig]. + +# Insert code here + +print [mu, sig]
1 udacity/cs373/unit-2/kalman-matrices.py
 @@ -0,0 +1 @@ +a = matrix([])
6 udacity/cs373/unit-2/update-kalman.py
 @@ -0,0 +1,6 @@ +def update(mean1, var1, mean2, var2): + new_mean = (var2*mean1 + var1*mean2) / (var1 + var2) + new_var = 1.0 / (1 / var1 + 1 / var2) + return [new_mean, new_var] + +print update(1.0, 1.0, 5.0, 4.0)
233 udacity/cs373/unit-5/cyclic-smoother.py
154 udacity/cs373/unit-5/p-controller.py
 @@ -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 + + for i in range(100): + myrobot = myrobot.move(param * -myrobot.y, speed) + print myrobot + + # + # Add Code Here + # + +run(0.1) # call function with parameter tau of 0.1 and print results
67 udacity/cs373/unit-5/path-smoothing.py
 @@ -0,0 +1,67 @@ +# ----------- +# 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. +# ----------- + + +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, tolerance = 0.000001): + + # 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] + + + #### ENTER CODE BELOW THIS LINE ### + change = tolerance + while change >= tolerance: + change = 0.0 + for i in range(1, len(path)-1): + for j in range(2): + aux = newpath[i][j] + newpath[i][j] += weight_data * (path[i][j] - newpath[i][j]) + newpath[i][j] += weight_smooth * (newpath[i+1][j] + newpath[i-1][j] - (2 * newpath[i][j])) + 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, 0, 0.9) + +# 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]) +']' + + + + +
161 udacity/cs373/unit-5/pd-controller.py
 @@ -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) + speed = 1.0 # motion distance is equal to speed (we assume time = 1) + N = 1000 + myrobot.set_steering_drift(10.0 / 180.0 * pi) + + crosstrack_error = myrobot.y + for i in range(N): + diff_crosstrack_error = myrobot.y - crosstrack_error + crosstrack_error = myrobot.y + steering = - (param1 * crosstrack_error) - (param2 * diff_crosstrack_error) + myrobot = myrobot.move(steering, speed) + print [myrobot, steering] + + # + # Enter code here + # + +# Call your function with parameters of 0.2 and 3.0 and print results +run(0.2, 3.0) + + +
170 udacity/cs373/unit-5/pid-controller.py
 @@ -0,0 +1,170 @@ +# ----------- +# 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) + 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! + + prev_crosstrack_error = myrobot.y + total_crosstrack_error = 0 + + for i in range(1000): + crosstrack_error = myrobot.y + d_crosstrack_error = crosstrack_error - prev_crosstrack_error + total_crosstrack_error += crosstrack_error + steering = -param1 * crosstrack_error - param2 * d_crosstrack_error - param3 * total_crosstrack_error + myrobot = myrobot.move(steering, speed) + prev_crosstrack_error = crosstrack_error + print myrobot, steering + + +# Call your function with parameters of (0.2, 3.0, and 0.004) +run(0.2, 3.0, 0.004) + + + +
196 udacity/cs373/unit-5/twiddle.py
 @@ -0,0 +1,196 @@ +# ---------------- +# 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.2): #Make this tolerance bigger if you are timing out! +############## ADD CODE BELOW #################### + + params = [0.0, 0.0, 0.0] + deltas = [1.0, 1.0, 1.0] + + best_err = run(params) + while sum(deltas) > tol: + for i in range(len(params)): + params[i] += deltas[i] + err = run(params) + if err < best_err: + best_err = err + deltas[i] *= 1.1 + else: + params[i] -= 2 * deltas[i] + err = run(params) + if err < best_err: + best_err = err + deltas[i] *= 1.1 + else: + params[i] += deltas[i] + deltas[i] *= 0.9 + + + return run(params) + +print twiddle()