public jaz303 /coursework

Subversion checkout URL

You can clone with HTTPS or Subversion.

cs373 finished

commit 326eaefbb0a50d398a20a014338faa26d50f13c2 1 parent 57cb1a7
authored
183  udacity/cs373/final/planning.py
 ... ... `@@ -0,0 +1,183 @@` 1 `+# -------------------` 2 `+# Background Information` 3 `+#` 4 `+# In this problem, you will build a planner that makes a robot` 5 `+# car's lane decisions. On a highway, the left lane (in the US)` 6 `+# generally has a higher traffic speed than the right line. ` 7 `+#` 8 `+# In this problem, a 2 lane highway of length 5 could be` 9 `+# represented as:` 10 `+#` 11 `+# road = [[80, 80, 80, 80, 80],` 12 `+# [60, 60, 60, 60, 60]]` 13 `+#` 14 `+# In this case, the left lane has an average speed of 80 km/h and` 15 `+# the right lane has a speed of 60 km/h. We can use a 0 to indicate` 16 `+# an obstacle in the road.` 17 `+#` 18 `+# To get to a location as quickly as possible, we usually` 19 `+# want to be in the left lane. But there is a cost associated` 20 `+# with changing lanes. This means that for short trips, it is` 21 `+# sometimes optimal to stay in the right lane.` 22 `+#` 23 `+# -------------------` 24 `+# User Instructions` 25 `+#` 26 `+# Design a planner (any kind you like, so long as it works).` 27 `+# This planner should be a function named plan() that takes` 28 `+# as input four parameters: road, lane_change_cost, init, and` 29 `+# goal. See parameter info below.` 30 `+#` 31 `+# Your function should RETURN the final cost to reach the` 32 `+# goal from the start point (which should match with our answer).` 33 `+# You may include print statements to show the optimum policy,` 34 `+# though this is not necessary for grading.` 35 `+#` 36 `+# Your solution must work for a variety of roads and lane` 37 `+# change costs.` 38 `+#` 39 `+# Add your code at line 92.` 40 `+# ` 41 `+# --------------------` 42 `+# Parameter Info` 43 `+#` 44 `+# road - A grid of values. Each value represents the speed associated` 45 `+# with that cell. A value of 0 means the cell in non-navigable.` 46 `+# The cost for traveling in a cell must be (1.0 / speed).` 47 `+#` 48 `+# lane_change_cost - The cost associated with changing lanes.` 49 `+#` 50 `+# init - The starting point for your car. This will always be somewhere` 51 `+# in the right (bottom) lane to simulate a highway on-ramp.` 52 `+#` 53 `+# goal - The destination. This will always be in the right lane to` 54 `+# simulate a highway exit-ramp.` 55 `+#` 56 `+# --------------------` 57 `+# Testing` 58 `+#` 59 `+# You may use our test function below, solution_check` 60 `+# to test your code for a variety of input parameters. ` 61 `+#` 62 `+# You may also use the build_road function to build` 63 `+# your own roads to test your function with.` 64 `+` 65 `+import random` 66 `+` 67 `+# ------------------------------------------` 68 `+# build_road - Makes a road according to your specified length and` 69 `+# lane_speeds. lane_speeds is a list of speeds for the lanes (listed` 70 `+# from left lane to right). You can also include random obstacles.` 71 `+#` 72 `+def build_road(length, lane_speeds, print_flag = False, obstacles = False, obstacle_prob = 0.05):` 73 `+ num_lanes = len(lane_speeds)` 74 `+ road = [[lane_speeds[i] for dist in range(length)] for i in range(len(lane_speeds))]` 75 `+ if obstacles:` 76 `+ for x in range(len(road)):` 77 `+ for y in range(len(road[0])):` 78 `+ if random.random() < obstacle_prob:` 79 `+ road[x][y] = 0` 80 `+ if print_flag:` 81 `+ for lane in road:` 82 `+ print '[' + ', '.join('%5.3f' % speed for speed in lane) + ']'` 83 `+ return road` 84 `+` 85 `+def plan_r(road, costs, lcc, cost, x, y):` 86 `+ if costs[x][y] == -1 or cost < costs[x][y]:` 87 `+ costs[x][y] = cost` 88 `+ ` 89 `+ for d in [[0,1],[-1,1],[1,1]]:` 90 `+ next_x = x + d[0]` 91 `+ next_y = y + d[1]` 92 `+ ` 93 `+ if next_x >= 0 and next_x < len(road) and next_y >= 0 and next_y < len(road[0]):` 94 `+ if road[next_x][next_y] > 0:` 95 `+ move_cost = 1.0 / road[x][y]` 96 `+ if next_x != x:` 97 `+ move_cost += lcc` 98 `+ plan_r(road, costs, lcc, cost + move_cost, next_x, next_y)` 99 `+` 100 `+# ------------------------------------------` 101 `+# plan - Returns cost to get from init to goal on road given a` 102 `+# lane_change_cost.` 103 `+#` 104 `+def plan(road, lane_change_cost, init, goal): # Don't change the name of this function!` 105 `+ costs = [[-1.0 for i in range(len(road[0]))] for j in range(len(road))]` 106 `+ plan_r(road, costs, lane_change_cost, 0.0, init[0], init[1])` 107 `+ print costs` 108 `+ return costs[goal[0]][goal[1]]` 109 `+` 110 `+################# TESTING ##################` 111 `+ ` 112 `+# ------------------------------------------` 113 `+# solution check - Checks your path function using` 114 `+# data from list called test[]. Uncomment the call` 115 `+# to solution_check at the bottom to test your code.` 116 `+#` 117 `+def solution_check(test, epsilon = 0.00001):` 118 `+ answer_list = []` 119 `+ for i in range(len(test[0])):` 120 `+ user_cost = plan(test[0][i], test[1][i], test[2][i], test[3][i])` 121 `+ true_cost = test[4][i]` 122 `+ if abs(user_cost - true_cost) < epsilon:` 123 `+ answer_list.append(1)` 124 `+ else:` 125 `+ answer_list.append(0)` 126 `+ correct_answers = 0` 127 `+ print` 128 `+ for i in range(len(answer_list)):` 129 `+ if answer_list[i] == 1:` 130 `+ print 'Test case', i+1, 'passed!'` 131 `+ correct_answers += 1` 132 `+ else:` 133 `+ print 'Test case', i+1, 'failed.'` 134 `+ if correct_answers == len(answer_list):` 135 `+ print "\nYou passed all test cases!"` 136 `+ return True` 137 `+ else:` 138 `+ print "\nYou passed", correct_answers, "of", len(answer_list), "test cases. Try to get them all!"` 139 `+ return False` 140 `+ ` 141 `+# Test Case 1 (FAST left lane)` 142 `+test_road1 = build_road(8, [100, 10, 1])` 143 `+lane_change_cost1 = 1.0 / 1000.0` 144 `+test_init1 = [len(test_road1) - 1, 0]` 145 `+test_goal1 = [len(test_road1) - 1, len(test_road1[0]) - 1]` 146 `+true_cost1 = 1.244` 147 `+` 148 `+# Test Case 2 (more realistic road)` 149 `+test_road2 = build_road(14, [80, 60, 40, 20])` 150 `+lane_change_cost2 = 1.0 / 100.0` 151 `+test_init2 = [len(test_road2) - 1, 0]` 152 `+test_goal2 = [len(test_road2) - 1, len(test_road2[0]) - 1]` 153 `+true_cost2 = 0.293333333333` 154 `+` 155 `+# Test Case 3 (Obstacles included)` 156 `+test_road3 = [[50, 50, 50, 50, 50, 40, 0, 40, 50, 50, 50, 50, 50, 50, 50], # left lane: 50 km/h` 157 `+ [40, 40, 40, 40, 40, 30, 20, 30, 40, 40, 40, 40, 40, 40, 40],` 158 `+ [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]] # right lane: 30 km/h` 159 `+lane_change_cost3 = 1.0 / 500.0` 160 `+test_init3 = [len(test_road3) - 1, 0]` 161 `+test_goal3 = [len(test_road3) - 1, len(test_road3[0]) - 1]` 162 `+true_cost3 = 0.355333333333` 163 `+` 164 `+# Test Case 4 (Slalom)` 165 `+test_road4 = [[50, 50, 50, 50, 50, 40, 0, 40, 50, 50, 0, 50, 50, 50, 50], # left lane: 50 km/h` 166 `+ [40, 40, 40, 40, 0, 30, 20, 30, 0, 40, 40, 40, 40, 40, 40],` 167 `+ [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]] # right lane: 30 km/h` 168 `+lane_change_cost4 = 1.0 / 65.0` 169 `+test_init4 = [len(test_road4) - 1, 0]` 170 `+test_goal4 = [len(test_road4) - 1, len(test_road4[0]) - 1]` 171 `+true_cost4 = 0.450641025641` 172 `+` 173 `+` 174 `+testing_suite = [[test_road1, test_road2, test_road3, test_road4],` 175 `+ [lane_change_cost1, lane_change_cost2, lane_change_cost3, lane_change_cost4],` 176 `+ [test_init1, test_init2, test_init3, test_init4],` 177 `+ [test_goal1, test_goal2, test_goal3, test_goal4],` 178 `+ [true_cost1, true_cost2, true_cost3, true_cost4]]` 179 `+` 180 `+solution_check(testing_suite) #UNCOMMENT THIS LINE TO TEST YOUR CODE` 181 `+` 182 `+` 183 `+`
18  udacity/cs373/final/q14.rb
 ... ... `@@ -0,0 +1,18 @@` 1 `+def go(state, remain, bins)` 2 `+ if remain == 0` 3 `+ state[:count] += 1` 4 `+ state[:matching] += 1 if bins.all? { |b| !b.empty? }` 5 `+ else` 6 `+ 4.times do |i|` 7 `+ bins[i].push true` 8 `+ go(state, remain - 1, bins)` 9 `+ bins[i].pop` 10 `+ end` 11 `+ end` 12 `+end` 13 `+` 14 `+tally = {:count => 0, :matching => 0}` 15 `+` 16 `+go(tally, 12, [[], [], [], []])` 17 `+` 18 `+p tally`
15  udacity/cs373/unit-2/coconuts.py
 ... ... `@@ -0,0 +1,15 @@` 1 `+def f(n):` 2 `+ return 4 * (n - 1) / 5.0` 3 `+ ` 4 `+def f6(n):` 5 `+ for i in range(0, 6):` 6 `+ n = f(n)` 7 `+ return n` 8 `+ ` 9 `+def is_int(n):` 10 `+ return abs(n - int(n)) < 0.0010001` 11 `+` 12 `+for i in range(0, 100000):` 13 `+ if is_int(f6(i)):` 14 `+ print i` 15 `+ break`
6  udacity/cs373/unit-2/gaussian.py
 ... ... `@@ -0,0 +1,6 @@` 1 `+from math import *` 2 `+` 3 `+def f(mu, sigma2, x):` 4 `+ return 1/sqrt(2.0*pi*sigma2) * exp(-0.5 * (x - mu)**2 / sigma2)` 5 `+ ` 6 `+print f(10.0, 4.0, 10.0)`
29  udacity/cs373/unit-2/kalman-1d.py
 ... ... `@@ -0,0 +1,29 @@` 1 `+def update(mean1, var1, mean2, var2):` 2 `+ new_mean = (var2*mean1 + var1*mean2) / (var1 + var2)` 3 `+ new_var = 1.0 / (1 / var1 + 1 / var2)` 4 `+ return [new_mean, new_var]` 5 `+ ` 6 `+def predict(mean1, var1, mean2, var2):` 7 `+ new_mean = mean1 + mean2` 8 `+ new_var = var1 + var2` 9 `+ return [new_mean, new_var]` 10 `+ ` 11 `+measurements = [5., 6., 7., 9., 10.]` 12 `+motion = [1., 1., 2., 1., 1.]` 13 `+measurement_sig = 4.` 14 `+motion_sig = 2.` 15 `+mu = 0` 16 `+sig = 0.0000000001` 17 `+` 18 `+for i in range(0, len(measurements)):` 19 `+ [mu, sig] = update(mu, sig, measurements[i], measurement_sig)` 20 `+ print "update: ", [mu, sig]` 21 `+ [mu, sig] = predict(mu, sig, motion[i], motion_sig)` 22 `+ print "predict: ", [mu, sig]` 23 `+` 24 `+#Please print out ONLY the final values of the mean` 25 `+#and the variance in a list [mu, sig]. ` 26 `+` 27 `+# Insert code here` 28 `+` 29 `+print [mu, sig]`
1  udacity/cs373/unit-2/kalman-matrices.py
 ... ... `@@ -0,0 +1 @@` 1 `+a = matrix([])`
6  udacity/cs373/unit-2/update-kalman.py
 ... ... `@@ -0,0 +1,6 @@` 1 `+def update(mean1, var1, mean2, var2):` 2 `+ new_mean = (var2*mean1 + var1*mean2) / (var1 + var2)` 3 `+ new_var = 1.0 / (1 / var1 + 1 / var2)` 4 `+ return [new_mean, new_var]` 5 `+` 6 `+print update(1.0, 1.0, 5.0, 4.0)`
233  udacity/cs373/unit-5/cyclic-smoother.py
 ... ... `@@ -0,0 +1,233 @@` 1 `+# -------------` 2 `+# User Instructions` 3 `+#` 4 `+# Here you will be implementing a cyclic smoothing` 5 `+# algorithm. This algorithm should not fix the end` 6 `+# points (as you did in the unit quizzes). You ` 7 `+# should use the gradient descent equations that` 8 `+# you used previously.` 9 `+#` 10 `+# Your function should return the newpath that it` 11 `+# calculates..` 12 `+#` 13 `+# Feel free to use the provided solution_check function` 14 `+# to test your code. You can find it at the bottom.` 15 `+#` 16 `+# --------------` 17 `+# Testing Instructions` 18 `+# ` 19 `+# To test your code, call the solution_check function with` 20 `+# two arguments. The first argument should be the result of your` 21 `+# smooth function. The second should be the corresponding answer.` 22 `+# For example, calling` 23 `+#` 24 `+# solution_check(smooth(testpath1), answer1)` 25 `+#` 26 `+# should return True if your answer is correct and False if` 27 `+# it is not.` 28 `+` 29 `+from math import *` 30 `+` 31 `+# Do not modify path inside your function.` 32 `+path=[[0, 0], ` 33 `+ [1, 0],` 34 `+ [2, 0],` 35 `+ [3, 0],` 36 `+ [4, 0],` 37 `+ [5, 0],` 38 `+ [6, 0],` 39 `+ [6, 1],` 40 `+ [6, 2],` 41 `+ [6, 3],` 42 `+ [5, 3],` 43 `+ [4, 3],` 44 `+ [3, 3],` 45 `+ [2, 3],` 46 `+ [1, 3],` 47 `+ [0, 3],` 48 `+ [0, 2],` 49 `+ [0, 1]]` 50 `+` 51 `+############# ONLY ENTER CODE BELOW THIS LINE ##########` 52 `+` 53 `+# ------------------------------------------------` 54 `+# smooth coordinates` 55 `+# If your code is timing out, make the tolerance parameter` 56 `+# larger to decrease run time.` 57 `+#` 58 `+` 59 `+def smooth(path, weight_data = 0.1, weight_smooth = 0.1, tolerance = 0.00001):` 60 `+ ` 61 `+ # Make a deep copy of path into newpath` 62 `+ newpath = [[0 for col in range(len(path[0]))] for row in range(len(path))]` 63 `+ for i in range(len(path)):` 64 `+ for j in range(len(path[0])):` 65 `+ newpath[i][j] = path[i][j]` 66 `+` 67 `+ #### ENTER CODE BELOW THIS LINE ###` 68 `+ change = tolerance` 69 `+ while change >= tolerance:` 70 `+ change = 0.0` 71 `+ for i in range(len(path)):` 72 `+ for j in range(2):` 73 `+ aux = newpath[i][j]` 74 `+ newpath[i][j] += weight_data * (path[i][j] - newpath[i][j])` 75 `+ newpath[i][j] += weight_smooth * (newpath[(i+1)%len(path)][j] + newpath[(i-1)%len(path)][j] - (2 * newpath[i][j]))` 76 `+ change += abs(aux - newpath[i][j])` 77 `+` 78 `+ return newpath # Leave this line for the grader!` 79 `+` 80 `+# thank you - EnTerr - for posting this on our discussion forum` 81 `+` 82 `+#newpath = smooth(path)` 83 `+#for i in range(len(path)):` 84 `+# print '['+ ', '.join('%.3f'%x for x in path[i]) +'] -> ['+ ', '.join('%.3f'%x for x in newpath[i]) +']'` 85 `+` 86 `+` 87 `+##### TESTING ######` 88 `+` 89 `+# --------------------------------------------------` 90 `+# check if two numbers are 'close enough,'used in` 91 `+# solution_check function.` 92 `+#` 93 `+def close_enough(user_answer, true_answer, epsilon = 0.001):` 94 `+ if abs(user_answer - true_answer) > epsilon:` 95 `+ return False` 96 `+ return True` 97 `+` 98 `+# --------------------------------------------------` 99 `+# check your solution against our reference solution for` 100 `+# a variety of test cases (given below)` 101 `+#` 102 `+def solution_check(newpath, answer):` 103 `+ if type(newpath) != type(answer):` 104 `+ print "Error. You do not return a list."` 105 `+ return False` 106 `+ if len(newpath) != len(answer):` 107 `+ print 'Error. Your newpath is not the correct length.'` 108 `+ return False` 109 `+ if len(newpath[0]) != len(answer[0]):` 110 `+ print 'Error. Your entries do not contain an (x, y) coordinate pair.'` 111 `+ return False` 112 `+ for i in range(len(newpath)): ` 113 `+ for j in range(len(newpath[0])):` 114 `+ if not close_enough(newpath[i][j], answer[i][j]):` 115 `+ print 'Error, at least one of your entries is not correct.'` 116 `+ return False` 117 `+ print "Test case correct!"` 118 `+ return True` 119 `+` 120 `+# --------------` 121 `+# Testing Instructions` 122 `+# ` 123 `+# To test your code, call the solution_check function with` 124 `+# two arguments. The first argument should be the result of your` 125 `+# smooth function. The second should be the corresponding answer.` 126 `+# For example, calling` 127 `+#` 128 `+# solution_check(smooth(testpath1), answer1)` 129 `+#` 130 `+# should return True if your answer is correct and False if` 131 `+# it is not.` 132 `+ ` 133 `+testpath1 = [[0, 0],` 134 `+ [1, 0],` 135 `+ [2, 0],` 136 `+ [3, 0],` 137 `+ [4, 0],` 138 `+ [5, 0],` 139 `+ [6, 0],` 140 `+ [6, 1],` 141 `+ [6, 2],` 142 `+ [6, 3],` 143 `+ [5, 3],` 144 `+ [4, 3],` 145 `+ [3, 3],` 146 `+ [2, 3],` 147 `+ [1, 3],` 148 `+ [0, 3],` 149 `+ [0, 2],` 150 `+ [0, 1]]` 151 `+` 152 `+answer1 = [[0.5449300156668018, 0.47485226780102946],` 153 `+ [1.2230705677535505, 0.2046277687200752],` 154 `+ [2.079668890615267, 0.09810778721159963],` 155 `+ [3.0000020176660755, 0.07007646364781912],` 156 `+ [3.9203348821839112, 0.09810853832382399],` 157 `+ [4.7769324511170455, 0.20462917195702085],` 158 `+ [5.455071854686622, 0.4748541381544533],` 159 `+ [5.697264197153936, 1.1249625336275617],` 160 `+ [5.697263485026567, 1.8750401628534337],` 161 `+ [5.455069810373743, 2.5251482916876378],` 162 `+ [4.776929339068159, 2.795372759575895],` 163 `+ [3.92033110541304, 2.9018927284871063],` 164 `+ [2.999998066091118, 2.929924058932193],` 165 `+ [2.0796652780381826, 2.90189200881968],` 166 `+ [1.2230677654766597, 2.7953714133566603],` 167 `+ [0.544928391271399, 2.5251464933327794],` 168 `+ [0.3027360471605494, 1.875038145804603],` 169 `+ [0.302736726373967, 1.1249605602741133]]` 170 `+` 171 `+## [0.000, 0.000] -> [0.545, 0.475]` 172 `+## [1.000, 0.000] -> [1.223, 0.205]` 173 `+## [2.000, 0.000] -> [2.080, 0.098]` 174 `+## [3.000, 0.000] -> [3.000, 0.070]` 175 `+## [4.000, 0.000] -> [3.920, 0.098]` 176 `+## [5.000, 0.000] -> [4.777, 0.205]` 177 `+## [6.000, 0.000] -> [5.455, 0.475]` 178 `+## [6.000, 1.000] -> [5.697, 1.125]` 179 `+## [6.000, 2.000] -> [5.697, 1.875]` 180 `+## [6.000, 3.000] -> [5.455, 2.525]` 181 `+## [5.000, 3.000] -> [4.777, 2.795]` 182 `+## [4.000, 3.000] -> [3.920, 2.902]` 183 `+## [3.000, 3.000] -> [3.000, 2.930]` 184 `+## [2.000, 3.000] -> [2.080, 2.902]` 185 `+## [1.000, 3.000] -> [1.223, 2.795]` 186 `+## [0.000, 3.000] -> [0.545, 2.525]` 187 `+## [0.000, 2.000] -> [0.303, 1.875]` 188 `+## [0.000, 1.000] -> [0.303, 1.125]` 189 `+` 190 `+` 191 `+testpath2 = [[1, 0], # Move in the shape of a plus sign` 192 `+ [2, 0],` 193 `+ [2, 1],` 194 `+ [3, 1],` 195 `+ [3, 2],` 196 `+ [2, 2],` 197 `+ [2, 3],` 198 `+ [1, 3],` 199 `+ [1, 2],` 200 `+ [0, 2], ` 201 `+ [0, 1],` 202 `+ [1, 1]]` 203 `+` 204 `+answer2 = [[1.239080543767428, 0.5047204351187283],` 205 `+ [1.7609243903912781, 0.5047216452560908],` 206 `+ [2.0915039821562416, 0.9085017167753027],` 207 `+ [2.495281862032503, 1.2390825203587184],` 208 `+ [2.4952805300504783, 1.7609262468826048],` 209 `+ [2.0915003641706296, 2.0915058211575475],` 210 `+ [1.7609195135622062, 2.4952837841027695],` 211 `+ [1.2390757942466555, 2.4952826072236918],` 212 `+ [0.9084962737918979, 2.091502621431358],` 213 `+ [0.5047183914625598, 1.7609219230352355],` 214 `+ [0.504719649257698, 1.2390782835562297],` 215 `+ [0.9084996902674257, 0.9084987462432871]]` 216 `+` 217 `+## [1.000, 0.000] -> [1.239, 0.505]` 218 `+## [2.000, 0.000] -> [1.761, 0.505]` 219 `+## [2.000, 1.000] -> [2.092, 0.909]` 220 `+## [3.000, 1.000] -> [2.495, 1.239]` 221 `+## [3.000, 2.000] -> [2.495, 1.761]` 222 `+## [2.000, 2.000] -> [2.092, 2.092]` 223 `+## [2.000, 3.000] -> [1.761, 2.495]` 224 `+## [1.000, 3.000] -> [1.239, 2.495]` 225 `+## [1.000, 2.000] -> [0.908, 2.092]` 226 `+## [0.000, 2.000] -> [0.505, 1.761]` 227 `+## [0.000, 1.000] -> [0.505, 1.239]` 228 `+## [1.000, 1.000] -> [0.908, 0.908]` 229 `+` 230 `+solution_check(smooth(testpath2), answer2)` 231 `+` 232 `+` 233 `+`
154  udacity/cs373/unit-5/p-controller.py
 ... ... `@@ -0,0 +1,154 @@` 1 `+# -----------` 2 `+# User Instructions` 3 `+#` 4 `+# Implement a P controller by running 100 iterations` 5 `+# of robot motion. The steering angle should be set` 6 `+# by the parameter tau so that:` 7 `+#` 8 `+# steering = -tau * crosstrack_error` 9 `+#` 10 `+# Note that tau is called "param" in the function` 11 `+# run to be completed below.` 12 `+#` 13 `+# Your code should print output that looks like` 14 `+# the output shown in the video. That is, at each step:` 15 `+# print myrobot, steering` 16 `+#` 17 `+# Only modify code at the bottom!` 18 `+# ------------` 19 `+ ` 20 `+from math import *` 21 `+import random` 22 `+` 23 `+` 24 `+# ------------------------------------------------` 25 `+# ` 26 `+# this is the robot class` 27 `+#` 28 `+` 29 `+class robot:` 30 `+` 31 `+ # --------` 32 `+ # init: ` 33 `+ # creates robot and initializes location/orientation to 0, 0, 0` 34 `+ #` 35 `+` 36 `+ def __init__(self, length = 20.0):` 37 `+ self.x = 0.0` 38 `+ self.y = 0.0` 39 `+ self.orientation = 0.0` 40 `+ self.length = length` 41 `+ self.steering_noise = 0.0` 42 `+ self.distance_noise = 0.0` 43 `+ self.steering_drift = 0.0` 44 `+` 45 `+ # --------` 46 `+ # set: ` 47 `+ # sets a robot coordinate` 48 `+ #` 49 `+` 50 `+ def set(self, new_x, new_y, new_orientation):` 51 `+` 52 `+ self.x = float(new_x)` 53 `+ self.y = float(new_y)` 54 `+ self.orientation = float(new_orientation) % (2.0 * pi)` 55 `+` 56 `+` 57 `+ # --------` 58 `+ # set_noise: ` 59 `+ # sets the noise parameters` 60 `+ #` 61 `+` 62 `+ def set_noise(self, new_s_noise, new_d_noise):` 63 `+ # makes it possible to change the noise parameters` 64 `+ # this is often useful in particle filters` 65 `+ self.steering_noise = float(new_s_noise)` 66 `+ self.distance_noise = float(new_d_noise)` 67 `+` 68 `+ # --------` 69 `+ # set_steering_drift: ` 70 `+ # sets the systematical steering drift parameter` 71 `+ #` 72 `+` 73 `+ def set_steering_drift(self, drift):` 74 `+ self.steering_drift = drift` 75 `+ ` 76 `+ # --------` 77 `+ # move: ` 78 `+ # steering = front wheel steering angle, limited by max_steering_angle` 79 `+ # distance = total distance driven, most be non-negative` 80 `+` 81 `+ def move(self, steering, distance, ` 82 `+ tolerance = 0.001, max_steering_angle = pi / 4.0):` 83 `+` 84 `+ if steering > max_steering_angle:` 85 `+ steering = max_steering_angle` 86 `+ if steering < -max_steering_angle:` 87 `+ steering = -max_steering_angle` 88 `+ if distance < 0.0:` 89 `+ distance = 0.0` 90 `+` 91 `+` 92 `+ # make a new copy` 93 `+ res = robot()` 94 `+ res.length = self.length` 95 `+ res.steering_noise = self.steering_noise` 96 `+ res.distance_noise = self.distance_noise` 97 `+ res.steering_drift = self.steering_drift` 98 `+` 99 `+ # apply noise` 100 `+ steering2 = random.gauss(steering, self.steering_noise)` 101 `+ distance2 = random.gauss(distance, self.distance_noise)` 102 `+` 103 `+ # apply steering drift` 104 `+ steering2 += self.steering_drift` 105 `+` 106 `+ # Execute motion` 107 `+ turn = tan(steering2) * distance2 / res.length` 108 `+` 109 `+ if abs(turn) < tolerance:` 110 `+` 111 `+ # approximate by straight line motion` 112 `+` 113 `+ res.x = self.x + (distance2 * cos(self.orientation))` 114 `+ res.y = self.y + (distance2 * sin(self.orientation))` 115 `+ res.orientation = (self.orientation + turn) % (2.0 * pi)` 116 `+` 117 `+ else:` 118 `+` 119 `+ # approximate bicycle model for motion` 120 `+` 121 `+ radius = distance2 / turn` 122 `+ cx = self.x - (sin(self.orientation) * radius)` 123 `+ cy = self.y + (cos(self.orientation) * radius)` 124 `+ res.orientation = (self.orientation + turn) % (2.0 * pi)` 125 `+ res.x = cx + (sin(res.orientation) * radius)` 126 `+ res.y = cy - (cos(res.orientation) * radius)` 127 `+` 128 `+ return res` 129 `+` 130 `+ def __repr__(self):` 131 `+ return '[x=%.5f y=%.5f orient=%.5f]' % (self.x, self.y, self.orientation)` 132 `+` 133 `+############## ADD / MODIFY CODE BELOW ####################` 134 `+ ` 135 `+# ------------------------------------------------------------------------` 136 `+#` 137 `+# run - does a single control run` 138 `+` 139 `+` 140 `+def run(param):` 141 `+ myrobot = robot()` 142 `+ myrobot.set(0.0, 1.0, 0.0)` 143 `+ speed = 1.0 # motion distance is equal to speed (we assume time = 1)` 144 `+ N = 100` 145 `+ ` 146 `+ for i in range(100):` 147 `+ myrobot = myrobot.move(param * -myrobot.y, speed)` 148 `+ print myrobot` 149 `+ ` 150 `+ #` 151 `+ # Add Code Here` 152 `+ # ` 153 `+` 154 `+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 @@` 1 `+# -----------` 2 `+# User Instructions` 3 `+#` 4 `+# Define a function smooth that takes a path as its input` 5 `+# (with optional parameters for weight_data, weight_smooth)` 6 `+# and returns a smooth path.` 7 `+#` 8 `+# Smoothing should be implemented by iteratively updating` 9 `+# each entry in newpath until some desired level of accuracy` 10 `+# is reached. The update should be done according to the` 11 `+# gradient descent equations given in the previous video:` 12 `+#` 13 `+# If your function isn't submitting it is possible that the` 14 `+# runtime is too long. Try sacrificing accuracy for speed.` 15 `+# -----------` 16 `+` 17 `+` 18 `+from math import *` 19 `+` 20 `+# Don't modify path inside your function.` 21 `+path = [[0, 0],` 22 `+ [0, 1],` 23 `+ [0, 2],` 24 `+ [1, 2],` 25 `+ [2, 2],` 26 `+ [3, 2],` 27 `+ [4, 2],` 28 `+ [4, 3],` 29 `+ [4, 4]]` 30 `+` 31 `+# ------------------------------------------------` 32 `+# smooth coordinates` 33 `+#` 34 `+` 35 `+def smooth(path, weight_data = 0.5, weight_smooth = 0.1, tolerance = 0.000001):` 36 `+` 37 `+ # Make a deep copy of path into newpath` 38 `+ newpath = [[0 for col in range(len(path[0]))] for row in range(len(path))]` 39 `+ for i in range(len(path)):` 40 `+ for j in range(len(path[0])):` 41 `+ newpath[i][j] = path[i][j]` 42 `+` 43 `+` 44 `+ #### ENTER CODE BELOW THIS LINE ###` 45 `+ change = tolerance` 46 `+ while change >= tolerance:` 47 `+ change = 0.0` 48 `+ for i in range(1, len(path)-1):` 49 `+ for j in range(2):` 50 `+ aux = newpath[i][j]` 51 `+ newpath[i][j] += weight_data * (path[i][j] - newpath[i][j])` 52 `+ newpath[i][j] += weight_smooth * (newpath[i+1][j] + newpath[i-1][j] - (2 * newpath[i][j]))` 53 `+ change += abs(aux - newpath[i][j])` 54 `+ ` 55 `+ return newpath # Leave this line for the grader!` 56 `+` 57 `+# feel free to leave this and the following lines if you want to print.` 58 `+newpath = smooth(path, 0, 0.9)` 59 `+` 60 `+# thank you - EnTerr - for posting this on our discussion forum` 61 `+for i in range(len(path)):` 62 `+ print '['+ ', '.join('%.3f'%x for x in path[i]) +'] -> ['+ ', '.join('%.3f'%x for x in newpath[i]) +']'` 63 `+` 64 `+` 65 `+` 66 `+` 67 `+`
161  udacity/cs373/unit-5/pd-controller.py
 ... ... `@@ -0,0 +1,161 @@` 1 `+# -----------` 2 `+# User Instructions` 3 `+#` 4 `+# Implement a PD controller by running 100 iterations` 5 `+# of robot motion. The steering angle should be set` 6 `+# by the parameter tau so that:` 7 `+#` 8 `+# steering = -tau_p * CTE - tau_d * diff_CTE` 9 `+# where differential crosstrack error (diff_CTE)` 10 `+# is given by CTE(t) - CTE(t-1)` 11 `+#` 12 `+# Your code should print output that looks like` 13 `+# the output shown in the video.` 14 `+#` 15 `+# Only modify code at the bottom!` 16 `+# ------------` 17 `+ ` 18 `+from math import *` 19 `+import random` 20 `+` 21 `+` 22 `+# ------------------------------------------------` 23 `+# ` 24 `+# this is the robot class` 25 `+#` 26 `+` 27 `+class robot:` 28 `+` 29 `+ # --------` 30 `+ # init: ` 31 `+ # creates robot and initializes location/orientation to 0, 0, 0` 32 `+ #` 33 `+` 34 `+ def __init__(self, length = 20.0):` 35 `+ self.x = 0.0` 36 `+ self.y = 0.0` 37 `+ self.orientation = 0.0` 38 `+ self.length = length` 39 `+ self.steering_noise = 0.0` 40 `+ self.distance_noise = 0.0` 41 `+ self.steering_drift = 0.0` 42 `+` 43 `+ # --------` 44 `+ # set: ` 45 `+ # sets a robot coordinate` 46 `+ #` 47 `+` 48 `+ def set(self, new_x, new_y, new_orientation):` 49 `+` 50 `+ self.x = float(new_x)` 51 `+ self.y = float(new_y)` 52 `+ self.orientation = float(new_orientation) % (2.0 * pi)` 53 `+` 54 `+` 55 `+ # --------` 56 `+ # set_noise: ` 57 `+ # sets the noise parameters` 58 `+ #` 59 `+` 60 `+ def set_noise(self, new_s_noise, new_d_noise):` 61 `+ # makes it possible to change the noise parameters` 62 `+ # this is often useful in particle filters` 63 `+ self.steering_noise = float(new_s_noise)` 64 `+ self.distance_noise = float(new_d_noise)` 65 `+` 66 `+ # --------` 67 `+ # set_steering_drift: ` 68 `+ # sets the systematical steering drift parameter` 69 `+ #` 70 `+` 71 `+ def set_steering_drift(self, drift):` 72 `+ self.steering_drift = drift` 73 `+ ` 74 `+ # --------` 75 `+ # move: ` 76 `+ # steering = front wheel steering angle, limited by max_steering_angle` 77 `+ # distance = total distance driven, most be non-negative` 78 `+` 79 `+ def move(self, steering, distance, ` 80 `+ tolerance = 0.001, max_steering_angle = pi / 4.0):` 81 `+` 82 `+ if steering > max_steering_angle:` 83 `+ steering = max_steering_angle` 84 `+ if steering < -max_steering_angle:` 85 `+ steering = -max_steering_angle` 86 `+ if distance < 0.0:` 87 `+ distance = 0.0` 88 `+` 89 `+` 90 `+ # make a new copy` 91 `+ res = robot()` 92 `+ res.length = self.length` 93 `+ res.steering_noise = self.steering_noise` 94 `+ res.distance_noise = self.distance_noise` 95 `+ res.steering_drift = self.steering_drift` 96 `+` 97 `+ # apply noise` 98 `+ steering2 = random.gauss(steering, self.steering_noise)` 99 `+ distance2 = random.gauss(distance, self.distance_noise)` 100 `+` 101 `+ # apply steering drift` 102 `+ steering2 += self.steering_drift` 103 `+` 104 `+ # Execute motion` 105 `+ turn = tan(steering2) * distance2 / res.length` 106 `+` 107 `+ if abs(turn) < tolerance:` 108 `+` 109 `+ # approximate by straight line motion` 110 `+` 111 `+ res.x = self.x + (distance2 * cos(self.orientation))` 112 `+ res.y = self.y + (distance2 * sin(self.orientation))` 113 `+ res.orientation = (self.orientation + turn) % (2.0 * pi)` 114 `+` 115 `+ else:` 116 `+` 117 `+ # approximate bicycle model for motion` 118 `+` 119 `+ radius = distance2 / turn` 120 `+ cx = self.x - (sin(self.orientation) * radius)` 121 `+ cy = self.y + (cos(self.orientation) * radius)` 122 `+ res.orientation = (self.orientation + turn) % (2.0 * pi)` 123 `+ res.x = cx + (sin(res.orientation) * radius)` 124 `+ res.y = cy - (cos(res.orientation) * radius)` 125 `+` 126 `+ return res` 127 `+` 128 `+ def __repr__(self):` 129 `+ return '[x=%.5f y=%.5f orient=%.5f]' % (self.x, self.y, self.orientation)` 130 `+` 131 `+############## ADD / MODIFY CODE BELOW ####################` 132 `+` 133 `+# ------------------------------------------------------------------------` 134 `+#` 135 `+# run - does a single control run.` 136 `+` 137 `+` 138 `+def run(param1, param2):` 139 `+ myrobot = robot()` 140 `+ myrobot.set(0.0, 1.0, 0.0)` 141 `+ speed = 1.0 # motion distance is equal to speed (we assume time = 1)` 142 `+ N = 1000` 143 `+ myrobot.set_steering_drift(10.0 / 180.0 * pi)` 144 `+ ` 145 `+ crosstrack_error = myrobot.y` 146 `+ for i in range(N):` 147 `+ diff_crosstrack_error = myrobot.y - crosstrack_error` 148 `+ crosstrack_error = myrobot.y` 149 `+ steering = - (param1 * crosstrack_error) - (param2 * diff_crosstrack_error)` 150 `+ myrobot = myrobot.move(steering, speed)` 151 `+ print [myrobot, steering]` 152 `+ ` 153 `+ #` 154 `+ # Enter code here` 155 `+ #` 156 `+` 157 `+# Call your function with parameters of 0.2 and 3.0 and print results` 158 `+run(0.2, 3.0)` 159 `+` 160 `+` 161 `+`
170  udacity/cs373/unit-5/pid-controller.py
 ... ... `@@ -0,0 +1,170 @@` 1 `+# -----------` 2 `+# User Instructions` 3 `+#` 4 `+# Implement a P controller by running 100 iterations` 5 `+# of robot motion. The steering angle should be set` 6 `+# by the parameter tau so that:` 7 `+#` 8 `+# steering = -tau_p * CTE - tau_d * diff_CTE - tau_i * int_CTE` 9 `+#` 10 `+# where the integrated crosstrack error (int_CTE) is` 11 `+# the sum of all the previous crosstrack errors.` 12 `+# This term works to cancel out steering drift.` 13 `+#` 14 `+# Your code should print a list that looks just like` 15 `+# the list shown in the video.` 16 `+#` 17 `+# Only modify code at the bottom!` 18 `+# ------------` 19 `+ ` 20 `+from math import *` 21 `+import random` 22 `+` 23 `+` 24 `+# ------------------------------------------------` 25 `+# ` 26 `+# this is the robot class` 27 `+#` 28 `+` 29 `+class robot:` 30 `+` 31 `+ # --------` 32 `+ # init: ` 33 `+ # creates robot and initializes location/orientation to 0, 0, 0` 34 `+ #` 35 `+` 36 `+ def __init__(self, length = 20.0):` 37 `+ self.x = 0.0` 38 `+ self.y = 0.0` 39 `+ self.orientation = 0.0` 40 `+ self.length = length` 41 `+ self.steering_noise = 0.0` 42 `+ self.distance_noise = 0.0` 43 `+ self.steering_drift = 0.0` 44 `+` 45 `+ # --------` 46 `+ # set: ` 47 `+ # sets a robot coordinate` 48 `+ #` 49 `+` 50 `+ def set(self, new_x, new_y, new_orientation):` 51 `+` 52 `+ self.x = float(new_x)` 53 `+ self.y = float(new_y)` 54 `+ self.orientation = float(new_orientation) % (2.0 * pi)` 55 `+` 56 `+` 57 `+ # --------` 58 `+ # set_noise: ` 59 `+ # sets the noise parameters` 60 `+ #` 61 `+` 62 `+ def set_noise(self, new_s_noise, new_d_noise):` 63 `+ # makes it possible to change the noise parameters` 64 `+ # this is often useful in particle filters` 65 `+ self.steering_noise = float(new_s_noise)` 66 `+ self.distance_noise = float(new_d_noise)` 67 `+` 68 `+ # --------` 69 `+ # set_steering_drift: ` 70 `+ # sets the systematical steering drift parameter` 71 `+ #` 72 `+` 73 `+ def set_steering_drift(self, drift):` 74 `+ self.steering_drift = drift` 75 `+ ` 76 `+ # --------` 77 `+ # move: ` 78 `+ # steering = front wheel steering angle, limited by max_steering_angle` 79 `+ # distance = total distance driven, most be non-negative` 80 `+` 81 `+ def move(self, steering, distance, ` 82 `+ tolerance = 0.001, max_steering_angle = pi / 4.0):` 83 `+` 84 `+ if steering > max_steering_angle:` 85 `+ steering = max_steering_angle` 86 `+ if steering < -max_steering_angle:` 87 `+ steering = -max_steering_angle` 88 `+ if distance < 0.0:` 89 `+ distance = 0.0` 90 `+` 91 `+` 92 `+ # make a new copy` 93 `+ res = robot()` 94 `+ res.length = self.length` 95 `+ res.steering_noise = self.steering_noise` 96 `+ res.distance_noise = self.distance_noise` 97 `+ res.steering_drift = self.steering_drift` 98 `+` 99 `+ # apply noise` 100 `+ steering2 = random.gauss(steering, self.steering_noise)` 101 `+ distance2 = random.gauss(distance, self.distance_noise)` 102 `+` 103 `+ # apply steering drift` 104 `+ steering2 += self.steering_drift` 105 `+` 106 `+ # Execute motion` 107 `+ turn = tan(steering2) * distance2 / res.length` 108 `+` 109 `+ if abs(turn) < tolerance:` 110 `+` 111 `+ # approximate by straight line motion` 112 `+` 113 `+ res.x = self.x + (distance2 * cos(self.orientation))` 114 `+ res.y = self.y + (distance2 * sin(self.orientation))` 115 `+ res.orientation = (self.orientation + turn) % (2.0 * pi)` 116 `+` 117 `+ else:` 118 `+` 119 `+ # approximate bicycle model for motion` 120 `+` 121 `+ radius = distance2 / turn` 122 `+ cx = self.x - (sin(self.orientation) * radius)` 123 `+ cy = self.y + (cos(self.orientation) * radius)` 124 `+ res.orientation = (self.orientation + turn) % (2.0 * pi)` 125 `+ res.x = cx + (sin(res.orientation) * radius)` 126 `+ res.y = cy - (cos(res.orientation) * radius)` 127 `+` 128 `+ return res` 129 `+` 130 `+` 131 `+` 132 `+` 133 `+ def __repr__(self):` 134 `+ return '[x=%.5f y=%.5f orient=%.5f]' % (self.x, self.y, self.orientation)` 135 `+` 136 `+` 137 `+` 138 `+` 139 `+############## ADD / MODIFY CODE BELOW ####################` 140 `+` 141 `+# ------------------------------------------------------------------------` 142 `+#` 143 `+# run - does a single control run.` 144 `+` 145 `+` 146 `+def run(param1, param2, param3):` 147 `+ myrobot = robot()` 148 `+ myrobot.set(0.0, 1.0, 0.0)` 149 `+ speed = 1.0 # motion distance is equal to speed (we assume time = 1)` 150 `+ 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!` 151 `+ ` 152 `+ prev_crosstrack_error = myrobot.y` 153 `+ total_crosstrack_error = 0` 154 `+ ` 155 `+ for i in range(1000):` 156 `+ crosstrack_error = myrobot.y` 157 `+ d_crosstrack_error = crosstrack_error - prev_crosstrack_error` 158 `+ total_crosstrack_error += crosstrack_error` 159 `+ steering = -param1 * crosstrack_error - param2 * d_crosstrack_error - param3 * total_crosstrack_error` 160 `+ myrobot = myrobot.move(steering, speed)` 161 `+ prev_crosstrack_error = crosstrack_error` 162 `+ print myrobot, steering` 163 `+` 164 `+` 165 `+# Call your function with parameters of (0.2, 3.0, and 0.004)` 166 `+run(0.2, 3.0, 0.004)` 167 `+` 168 `+` 169 `+` 170 `+`
196  udacity/cs373/unit-5/twiddle.py
 ... ... `@@ -0,0 +1,196 @@` 1 `+# ----------------` 2 `+# User Instructions` 3 `+#` 4 `+# Implement twiddle as shown in the previous two videos.` 5 `+# Your accumulated error should be very small!` 6 `+#` 7 `+# Your twiddle function should RETURN the accumulated` 8 `+# error. Try adjusting the parameters p and dp to make` 9 `+# this error as small as possible.` 10 `+#` 11 `+# Try to get your error below 1.0e-10 with as few iterations` 12 `+# as possible (too many iterations will cause a timeout).` 13 `+# No cheating!` 14 `+# ------------` 15 `+ ` 16 `+from math import *` 17 `+import random` 18 `+` 19 `+` 20 `+# ------------------------------------------------` 21 `+# ` 22 `+# this is the robot class` 23 `+#` 24 `+` 25 `+class robot:` 26 `+` 27 `+ # --------` 28 `+ # init: ` 29 `+ # creates robot and initializes location/orientation to 0, 0, 0` 30 `+ #` 31 `+` 32 `+ def __init__(self, length = 20.0):` 33 `+ self.x = 0.0` 34 `+ self.y = 0.0` 35 `+ self.orientation = 0.0` 36 `+ self.length = length` 37 `+ self.steering_noise = 0.0` 38 `+ self.distance_noise = 0.0` 39 `+ self.steering_drift = 0.0` 40 `+` 41 `+ # --------` 42 `+ # set: ` 43 `+ # sets a robot coordinate` 44 `+ #` 45 `+` 46 `+ def set(self, new_x, new_y, new_orientation):` 47 `+` 48 `+ self.x = float(new_x)` 49 `+ self.y = float(new_y)` 50 `+ self.orientation = float(new_orientation) % (2.0 * pi)` 51 `+` 52 `+` 53 `+ # --------` 54 `+ # set_noise: ` 55 `+ # sets the noise parameters` 56 `+ #` 57 `+` 58 `+ def set_noise(self, new_s_noise, new_d_noise):` 59 `+ # makes it possible to change the noise parameters` 60 `+ # this is often useful in particle filters` 61 `+ self.steering_noise = float(new_s_noise)` 62 `+ self.distance_noise = float(new_d_noise)` 63 `+` 64 `+ # --------` 65 `+ # set_steering_drift: ` 66 `+ # sets the systematical steering drift parameter` 67 `+ #`