课程 22:Practice Exam
1. 练习:Question 1: Probability
2. 练习:Question 2:Probability
解答:设下雨事件为A,不下雨事件为B
P(A|A) = 0.6
P(A|B)=0.25
贝叶斯定理:
P(A)*P(B|A)=P(B)*P(A|B)
周五 P(A1)=0.75
周六 P(A2)= P(A1)*0.6+(1- P(A1))*0.25=0.5125
周日 P(A3)=P(A2)*0.6+(1-P(A2))*0.25=0.4294
P=1-P(A2)*P(A3)=1-0.22=0.78
P’=P(A2)*(1-P(A3))+ (1-P(A2))*P(A3)+( 1-P(A2))( 1-P(A3))
P’=0.2924+0.2093+0.2782=0.7799
两种方式算出来周末至少有一天不下雨的概率是0.78,但是提交时提示答案不正确。。
3. 练习:Question 3:Localization
4. 练习:Question 4:Localization
解答:
p=[0.2, 0.2, 0.2, 0.2, 0.2]
color=[‘G’,’G’,’R’,’G’,’R’]
measurement=[‘R’,’R’]
motion=[1]
sense:
p=[0.2*0.1, 0.2*0.1, 0.2*0.9, 0.2*0.1,0.2*0.9]=[0.02, 0.02, 0.18, 0.02, 0.18]
normalized:
p=[0.0476, 0.0476, 0.4286, 0.0476,0.4286]
move to right:
p=[0, 0.0476, 0. 0476, 0. 4286,0.4286+0.0476]= [0, 0.0476, 0. 0476, 0. 4286, 0.4762]
if sense again:
color=[‘G’,’G’,’R’,’G’,’R’]
p=[0, 0.0048, 0.9*0. 0476, 0.1*0. 4286,0.9*0.4762]=[0, 0.0048,0.0428,0.0429,0.4286]
normalize:
p=[0, 0.0092, 0.0825, 0.0826, 0.8257]
P(‘R’) = 0.0825+0.8257=0.9082
以这个答案或者0.91,0.908提交,都提示错误。。。
5. 练习:Question 5:Gaussians
6. 练习:Question 6:Gaussians
7. 练习:Question 7:Particle Filters
8. 练习:Question 8:Particle Filters
9. 练习:Question 9:A*
解答:选第三个(the costof motion is 2 or more)
因为,如果按照第一条那样的话,还会有个[1][4]=3 不能解释好。
如果按照第二条那样解释的话,对角线方向的很多值都需要修改。
而当第三条,motion cost 是2 或更多的时候,就能解释[1][4]=3了。而且能解释对角线方向的移动,对角线方向每次变化的值都<=2。
10. 练习:Question 10:Dynamic Programming
11. 练习:Question 11:PID Control
12. 练习:Question 12:Warehouse Robot
机器人仓库搬箱子问题:对于一个仓库warehouse,被划分成了若干个相等大小的方格,有若干个区域有货物,有货物的区域都被[1,99]之间的数字唯一标记了。搬运某个货物时,机器人从dropzone区域开始,通过水平或者垂直或者对角线方式进行移动寻找货物,其中水平和垂直方向的移动消耗1的代价,对角线方向移动消耗1.5的代价,进入目标货物所在的格子,捡起货物(不计时间),然后送回dropzone区域,放下货物(不计时间),表明完成一次货物搬运。货物搬运的任务列表是todo list。机器人必须按照todo list的顺序来搬运仓库中的货物,所有货物搬运完毕后,返回机器人消耗的总代价。代码主要添加到 plan()中。
我用A*算法写的代码,自定义了个启发函数,运行通过了程序后面附带的几个测试。
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Sun May 27 15:36:42 2018
@author: Spring
"""
# -------------------
# Background Information
#
# In this problem, you will build aplanner that helps a robot
# find the shortest way in a warehousefilled with boxes
# that he has to pick up and deliver to adrop zone.
#
# For example:
#
# warehouse = [[ 1, 2, 3],
# [ 0, 0, 0],
# [ 0, 0, 0]]
# dropzone = [2,0]
# todo = [2, 1]
#
# The robot starts at the dropzone.
# The dropzone can be in any free cornerof the warehouse map.
# todo is a list of boxes to be picked upand delivered to the dropzone.
#
# Robot can move diagonally, but the costof a diagonal move is 1.5.
# The cost of moving one stephorizontally or vertically is 1.
# So if the dropzone is at [2, 0], thecost to deliver box number 2
# would be 5.
# To pick up a box, the robot has to moveinto the same cell as the box.
# When the robot picks up a box, thatcell becomes passable (marked 0)
# The robot can pick up only one box at atime and once picked up
# it has to return the box to thedropzone by moving onto the dropzone cell.
# Once the robot has stepped on thedropzone, the box is taken away,
# and it is free to continue with itstodo list.
# Tasks must be executed in the orderthat they are given in the todo list.
# You may assume that in all warehousemaps, all boxes are
# reachable from beginning (the robot isnot boxed in).
# -------------------
# User Instructions
#
# Design a planner (any kind you like, solong as it works!)
# in a function named plan() that takesas input three parameters:
# warehouse, dropzone, and todo. Seeparameter info below.
#
# Your function should RETURN the final,accumulated cost to do
# all tasks in the todo list in the givenorder, which should
# match with our answer. You may includeprint statements to show
# the optimum path, but that will have noeffect on grading.
#
# Your solution must work for a varietyof warehouse layouts and
# any length of todo list.
#
# Add your code at line 76.
#
# --------------------
# Parameter Info
#
# warehouse - a grid of values, where 0means that the cell is passable,
# and a number 1 <= n <= 99 meansthat box n is located at that cell.
# dropzone - determines the robot's startlocation and the place to return boxes
# todo - list of tasks, containing boxnumbers that have to be picked up
#
# --------------------
# Testing
#
# You may use our test function below,solution_check(),
# to test your code for a variety ofinput parameters.
warehouse = [[ 1, 2, 3],
[ 0, 0, 0],
[ 0, 0, 0]]
dropzone = [2,0]
todo = [2, 1]
# ------------------------------------------
# plan - Returns cost to take all boxesin the todo list to dropzone
#
#----------------------------------------
# modify code below
#----------------------------------------
def plan(warehouse, dropzone, todo):
cost = 0
for k in range(len(todo)):
task_cost = float('Inf')
goal = []
# get the location of the goal location.
for x in range(len(warehouse)):
for y in range(len(warehouse[0])):
if warehouse[x][y] == todo[k]:
goal = [x, y]
break
if goal == []:
#return ValueError('Fail, can notfind the goal ', todo[k], ' in the warehouse!')
print('Error, can not find the goal', todo[k], ' in the warehouse!', warehouse)
return float('Inf')
closelist = []
for x in range(len(warehouse)):
rowlist = []
for y in range(len(warehouse[0])):
if warehouse[x][y] == 0:
rowlist.append(0)
else:
rowlist.append(1)
closelist.append(rowlist)
closelist[goal[0]][goal[1]] = 0
openlist = []
g = 0
x = dropzone[0]
y = dropzone[1]
h = getHeuristicValue(x, y, goal)
f = g + h
openlist.append([f, g, x, y])
closelist[x][y] = 1
isFound = False
while not isFound:
if len(openlist) == 0: # won'thappen, because we has found the goal location before.
#return ValueError('Fail, cannot find the goal ', todo[k], ' in the warehouse! openlist is blank.')
print('Error, can not find thegoal ', todo[k], ' in the warehouse! openlist is blank.')
return float('Inf')
openlist.sort()
openlist.reverse()
nextNode = openlist.pop()
x = nextNode[2]
y = nextNode[3]
g = nextNode[1]
f = nextNode[0]
if x == goal[0] and y == goal[1]:
isFound = True
task_cost = 2.0 * f
break;
for i in range(-1, 2):
for j in range(-1, 2):
if i == 0 and j == 0:
continue
x2 = x + i
y2 = y + j
if x2 >= 0 and x2 <len(warehouse) and y2 >= 0 and y2 < len(warehouse[0]) \
and closelist[x2][y2]== 0:
g2 = 0
if i == 0 or j ==0:
g2 = g + 1
else:
g2 = g + 1.5
h2 =getHeuristicValue(x2, y2, goal)
f2 = g2 + h2
openlist.append([f2, g2, x2, y2])
closelist[x2][y2] =1
cost += task_cost
# update the warehouse
if isFound:
warehouse[goal[0]][goal[1]] = 0
print("COST is ", cost)
return cost
# diagonalMove = 1.5, directMoveCost = 1.
def getHeuristicValue(x, y, goal):
deltaX = goal[0] - x
deltaY = goal[1] - y
diagonalMove = 0
directMove = 0
if abs(deltaX) < abs(deltaY):
diagonalMove = abs(deltaX)
directMove = abs(deltaY) - diagonalMove
else:
diagonalMove = abs(deltaY)
directMove = abs(deltaX) - diagonalMove
heuristicValue = diagonalMove * 1.5 + directMove * 1.0
return heuristicValue
################# TESTING##################
#------------------------------------------
# solution check - Checks your planfunction using
# data from list called test[]. Uncommentthe call
# to solution_check to test your code.
#
def solution_check(test, epsilon =0.00001):
answer_list = []
import time
start = time.clock()
correct_answers = 0
for i in range(len(test[0])):
user_cost = plan(test[0][i], test[1][i], test[2][i])
true_cost = test[3][i]
if abs(user_cost - true_cost) < epsilon:
print("\nTest case", i+1,"passed!")
answer_list.append(1)
correct_answers += 1
#print"#############################################"
else:
print("\nTest case ",i+1, "unsuccessful. Your answer ", user_cost,\
"was not within ",epsilon, "of ", true_cost)
answer_list.append(0)
runtime = time.clock() - start
if runtime > 1:
print("Your code is too slow, try to optimize it! Running time was:", runtime)
return False
if correct_answers == len(answer_list):
print("\nYou passed all test cases!")
return True
else:
print("\nYou passed",correct_answers, "of", len(answer_list), "test cases. Try to getthem all!")
return False
#Testing environment
# Test Case 1
warehouse1 = [[ 1, 2, 3],
[ 0, 0, 0],
[ 0, 0, 0]]
dropzone1 = [2,0]
todo1 = [2, 1]
true_cost1 = 9
# Test Case 2
warehouse2 = [[ 1, 2, 3, 4],
[ 0, 0, 0, 0],
[ 5, 6, 7, 0],
[ 'x', 0, 0, 8]]
dropzone2 = [3,0]
todo2 = [2, 5, 1]
true_cost2 = 21
# Test Case 3
warehouse3 = [[ 1, 2, 3, 4, 5, 6, 7],
[ 0, 0, 0, 0, 0, 0, 0],
[ 8, 9, 10, 11, 0, 0, 0],
[ 'x', 0, 0, 0,0, 0, 12]]
dropzone3 = [3,0]
todo3 = [5, 10]
true_cost3 = 18
# Test Case 4
warehouse4 = [[ 1, 17, 5, 18, 9, 19, 13],
[ 2, 0, 6, 0, 10, 0, 14],
[ 3, 0, 7, 0, 11, 0, 15],
[ 4, 0, 8, 0, 12, 0, 16],
[ 0, 0, 0, 0, 0, 0, 'x']]
dropzone4 = [4,6]
todo4 = [13, 11, 6, 17]
true_cost4 = 41
testing_suite = [[warehouse1, warehouse2,warehouse3, warehouse4],
[dropzone1, dropzone2,dropzone3, dropzone4],
[todo1, todo2, todo3, todo4],
[true_cost1, true_cost2,true_cost3, true_cost4]]
solution_check(testing_suite) #UNCOMMENTTHIS LINE TO TEST YOUR CODE