viernes, 4 de mayo de 2018

HORA 6 OPEN COURSE

# The function localize takes the following arguments:

#

# colors:

# 2D list, each entry either 'R' (for red cell) or 'G' (for green cell)

#

# measurements:

# list of measurements taken by the robot, each entry either 'R' or 'G'

#

# motions:

# list of actions taken by the robot, each entry of the form [dy,dx],

# where dx refers to the change in the x-direction (positive meaning

# movement to the right) and dy refers to the change in the y-direction

# (positive meaning movement downward)

# NOTE: the *first* coordinate is change in y; the *second* coordinate is

# change in x

#

# sensor_right:

# float between 0 and 1, giving the probability that any given

# measurement is correct; the probability that the measurement is

# incorrect is 1-sensor_right

#

# p_move:

# float between 0 and 1, giving the probability that any given movement

# command takes place; the probability that the movement command fails

# (and the robot remains still) is 1-p_move; the robot will NOT overshoot

# its destination in this exercise

#

# The function should RETURN (not just show or print) a 2D list (of the same

# dimensions as colors) that gives the probabilities that the robot occupies

# each cell in the world.

#

# Compute the probabilities by assuming the robot initially has a uniform

# probability of being in any cell.

#

# Also assume that at each step, the robot:

# 1) first makes a movement,

# 2) then takes a measurement.

#

# Motion:

# [0,0] - stay

# [0,1] - right

# [0,-1] - left

# [1,0] - down

# [-1,0] - up


def localize(colors,measurements,motions,sensor_right,p_move):

# initializes p to a uniform distribution over a grid of the same dimensions as colors

pinit = 1.0 / float(len(colors)) / float(len(colors[0]))

p = [[pinit for row in range(len(colors[0]))] for col in range(len(colors))]

# >>> Insert your code here <<<

for k in range (0, (len(measurements))):

s=0

q=[]

for i in range (0, (len(p))):

q.append([])

for j in range (0, (len(p[i]))):

q[i].append(p[i][j])

for i in range (0, (len(p))):

for j in range (0, (len(p[i]))):

q[i][j]=(p_move*p[(i-(motions[k][0]))%len(p)][(j-(motions[k][1]))%len(p[i])]+(1-p_move)*p[i][j])

for i in range (0, (len(p))):

for j in range (0, (len(p[i]))):

hit = (measurements[k]==colors[i][j])

p[i][j]=q[i][j]*(hit*sensor_right+(1-hit)*(1-sensor_right))

for i in range (0, (len(p))):

for j in range (0, (len(p[i]))):

s=s+p[i][j]

for i in range (0, (len(p))):

for j in range (0, (len(p[i]))):

p[i][j]=p[i][j]/s

return p


def show(p):

rows = ['[' + ','.join(map(lambda x: '{0:.5f}'.format(x),r)) + ']' for r in p]

print '[' + ',\n '.join(rows) + ']'

#############################################################

# For the following test case, your output should be

# [[0.01105, 0.02464, 0.06799, 0.04472, 0.02465],

# [0.00715, 0.01017, 0.08696, 0.07988, 0.00935],

# [0.00739, 0.00894, 0.11272, 0.35350, 0.04065],

# [0.00910, 0.00715, 0.01434, 0.04313, 0.03642]]

# (within a tolerance of +/- 0.001 for each entry)


colors = [['R','G','G','R','R'],

['R','R','G','R','R'],

['R','R','G','G','R'],

['R','R','R','R','R']]

measurements = ['G','G','G','G','G']

motions = [[0,0],[0,1],[1,0],[1,0],[0,1]]

p = localize(colors,measurements,motions,sensor_right = 0.7, p_move = 0.8)

show(p) # displays your answer



HORA 5 OPEN COURSE

Lo siguiente que hice durante el curso fue un ejercicio de programación bastante largo y complicado, al menos para mí.
Me llevó aproximadamente una hora y cincuenta minutos realizarlo, por lo que he decidido contarlo como dos horas del open course
Como no puedo poner la mitad del código en un post y la otra mitad en el otro, pongo aquí los apuntes que tomé al ver el vídeo del enunciado y en el post correspondiente a la hora 6 subiré el código

HORA 4 OPEN COURSE


HORA 3 OPEN COURSE

Aqui pongo también el código de otro pequeño programa que hice como actividad durante la hora 2

CÓDIGO FUNCIÓN MOVE


p=[0, 1, 0, 0, 0]

world=['green', 'red', 'red', 'green', 'green']

measurements = ['red', 'green']

pHit = 0.6

pMiss = 0.2

pExact = 0.8

pOvershoot = 0.1

pUndershoot = 0.1


def sense(p, Z):

q=[]

for i in range(len(p)):

hit = (Z == world[i])

q.append(p[i] * (hit * pHit + (1-hit) * pMiss))

s = sum(q)

for i in range(len(q)):

q[i] = q[i] / s

return q


def move(p, U):

q = []

for i in range(len(p)):

q.append((p[(i-U)%len(p)])*pExact+(p[(i-U-1)%len(p)])*pOvershoot+(p[(i-U+1)%len(p)])*pUndershoot)

return q






HORA 2 OPEN COURSE

Intenté adjuntar también un documento con el código de un pequeño programa que hice como ejercicio del curso durante la hora 1, pero no se pudo adjuntas, así que copio aqui el codigo
Las tabulaciones no se copian correctamente, desconozco por qué

CÓDIGO FUNCIÓN SENSE


p=[0.2, 0.2, 0.2, 0.2, 0.2]

world=['green', 'red', 'red', 'green', 'green']

measurements = ['red', 'green']

pHit = 0.6

pMiss = 0.2


def sense(p, Z):

q=[]

for i in range(len(p)):

hit = (Z == world[i])

q.append(p[i] * (hit * pHit + (1-hit) * pMiss))

s = sum(q)

for i in range(len(q)):

q[i] = q[i] / s

return q

for i in range (len(measurements)):

p=sense(p, measurements[i])

print p


HORA 1 OPEN COURSE

​​​​