Homework - Localization Program: Why incorrect??

For the Localization program I've submitted the code below

When I run the code I get the following result

[[0.011059807427972012, 0.02464041578496803, 0.06799662806785915, 0.04472487045812158, 0.02465153121665372], [0.0071532041833209815, 0.010171326481705892, 0.08696596002664689, 0.07988429965998084, 0.009350668508437186], [0.007397366886111671, 0.008943730670452702, 0.11272964670259776, 0.3535072295521272, 0.040655492078276775], [0.009106505805646497, 0.0071532041833209815, 0.014349221618346574, 0.04313329135844895, 0.036425599329004736]]

I would like to know why is hat been marked 'incorrect'

Thx

(Here is the code copied from the Udacity-Window)

    colors = [['red', 'green', 'green', 'red' , 'red'],
          ['red', 'red', 'green', 'red', 'red'],
          ['red', 'red', 'green', 'green', 'red'],
          ['red', 'red', 'red', 'red', 'red']]

measurements = ['green', 'green', 'green' ,'green', 'green']

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

sensor_right = 0.7

p_move = 0.8

def show(p):
    for i in range(len(p)):
        print p[i]

#DO NOT USE IMPORT
#ENTER CODE BELOW HERE
#ANY CODE ABOVE WILL CAUSE
#HOMEWORK TO BE GRADED
#INCORRECT
print('Unit1: Assignment')

#Global variables
worldColor = [[ 'Green', 'Green', 'Green' ],
              [ 'Green', 'Red', 'Red' ],
              [ 'Green', 'Green', 'Green' ]]
worldColorSize = 3, 3

uniformedProbabilityDistribution = [[ .2, .2, .2, .2, .2],
                                    [ .2, .2, .2, .2, .2],
                                    [ .2, .2, .2, .2, .2],
                                    [ .2, .2, .2, .2, .2]]
botMovement = ['Idle', 'Left', 'Right', 'Up', 'Down']
botMotions = [ [ 0, 0], [ 0, -1], [ 0, 1], [ -1, 0], [ 1, 0] ]

sensorAccuracy = 1.0
botMvmtAccuracy = 0.5

def GetTransposedList( aList):
    tmp = []
    for i in range( len( aList ) ):
        tmp.append([ row[ i ] for row in aList ] )
    return tmp

def GetZeroList( aList):
    tmp = []
    for i in range( len( aList ) ):
        tmp.append([ 0 for i in aList[i] ] )
    return tmp

def Sum2DList( aList ):
    sum = 0
    for m in range( len(aList ) ):
        for n in range( len(aList[m]) ):
            sum += aList[m][n]
    return sum

def normedSense( aDistribution, measurementValue ):
    senseProb = GetZeroList( aDistribution )

    for m in range( len(worldColor) ):
        for n in range( len(worldColor[m]) ):
            hitFlag = ( measurementValue == worldColor[ m ][ n ] )
            senseProb[m][n] =  aDistribution[ m ][ n ] * ( ( hitFlag * sensorAccuracy ) + ( 1 - hitFlag )*(1.0 - sensorAccuracy) )

    localSum = Sum2DList( senseProb )    
    normedSenseProb = GetZeroList(aDistribution)
    for m in range( len(normedSenseProb) ):
        for n in range( len(normedSenseProb[m]) ):
            normedSenseProb[m][n] = senseProb[m][n] / localSum

    return normedSenseProb

def MoveWithMotion( aDistribution, aMotion ):
    distr = GetZeroList( aDistribution )

    for m in range( len(worldColor) ):
        for n in range( len(worldColor[m]) ):
            rowMove = ( m  - aMotion[ 0 ] ) % len( worldColor )
            columnMove = ( n  - aMotion[ 1 ] ) % len( worldColor[ m ] )
            distr[m][n] =  aDistribution[ rowMove ][ columnMove ] * botMvmtAccuracy + aDistribution[ m ][ n ] * ( 1.0 - botMvmtAccuracy )
    return distr

#Run code
worldColor = colors
sensorAccuracy = sensor_right
botMvmtAccuracy = p_move
botSense = measurements 
botMoves = motions
aDistr = uniformedProbabilityDistribution

for i in range( len(botSense ) ):
    aDistr = normedSense( MoveWithMotion( aDistr, botMoves[ i ] ), botSense[ i ] )

print(aDistr )
#Your probability array must be printed 
#with the following code.

#show(p)

asked 29 Feb '12, 19:48

DaGakusei's gravatar image

DaGakusei
322410
accept rate: 0%

edited 29 Feb '12, 20:08

Gundega's gravatar image

Gundega ♦♦
44.1k70170315

Please go back and edit your comment. Select your code, then select that 101 010 icon to display the code as code. Then we will be able to read it.

(29 Feb '12, 19:50) Anne Paulson Anne%20Paulson's gravatar image

6 Answers:

did you read the comment at the end of the file?


# Your probability array must be printed
# with the following code.

Why did you comment out the show(p) call and used print instead???

link

answered 29 Feb '12, 19:52

Anna-Chiara%20Bellini's gravatar image

Anna-Chiara ...
5.2k103175

edited 29 Feb '12, 19:53

3

This is the reason the code was graded as incorrect.

(29 Feb '12, 20:02) PeterUdacity ♦♦ PeterUdacity's gravatar image

When I ran that code on Udacity (when it worked) I was getting an error with 'p' ([...] not defined)

(29 Feb '12, 20:08) DaGakusei DaGakusei's gravatar image

In the original supplied code, a definition for p was included. You could use whatever name for your probability vector that you wanted as long as you printed it out with the show() provided.

(29 Feb '12, 20:14) PeterUdacity ♦♦ PeterUdacity's gravatar image
uniformedProbabilityDistribution = [[ .2, .2, .2, .2, .2],
                                    [ .2, .2, .2, .2, .2],
                                    [ .2, .2, .2, .2, .2],
                                    [ .2, .2, .2, .2, .2]]

Even if you hadn't deleted the comments, your code still was incorrect.

link

answered 29 Feb '12, 21:27

Anne%20Paulson's gravatar image

Anne Paulson
9.5k327698

show(p) is commented out.

link

answered 29 Feb '12, 19:51

Adam%20Sherwin's gravatar image

Adam Sherwin ♦♦
17.9k2176125

Really?
I had an error with that 'p not defined....' aaaaaargh!
I should have left that in

(29 Feb '12, 20:40) DaGakusei DaGakusei's gravatar image

You might notice that the show method is printing the 2D-array differently (printing each "row" separately) ... not sure how the correctness of the programming exercise is being checked, but if it is purely on the output then there could be the reason for rendering your input incorrect.

link

answered 29 Feb '12, 20:06

kodra's gravatar image

kodra
27225

I don't use the show method. I haven't even noticed it, as I code on my IDE.
I just read the instruction from the video and started writing some code.
I commented it out because I was getting the matrix and an "[...] p not defined" or something error from the Python-Window

(29 Feb '12, 20:11) DaGakusei DaGakusei's gravatar image

I'm not sure if it matters, but I wrote a function to create the initial uniform distribution, so that it would work if they ran your code against different 'colors' inputs. I assumed that they would test your code against a number of inputs, as otherwise you'd just be able to write a function that output the correct answer for these particular inputs. I tested mine by running it against all of the examples from the video.

Here's my code:

# initialize
def initialize(world):
worldSize = len(world)*len(world[0])
q = []
for i in range(len(world)):
    q.append([])
    for j in range(len(world[0])):
        q[i].append(1.0/worldSize)
return q

# Move in 2-D World
def move(p, U):
    q = []
    for ii in range(len(p)):
        q.append([])
        for jj in range(len(p[0])):
            s = p_move*p[(ii-U[0]) % len(p)][(jj-U[1]) % len(p[0])]
            s = s + (1-p_move)*p[ii][jj]
            q[ii].append(s)
    return q

# 2D Sense
def sense(p, Z):
    q=[]
    s=0
    for ii in range(len(p)):
        q.append([])
        for jj in range(len(p[0])):
            hit = (Z == colors[ii][jj])
            q[ii].append(p[ii][jj] * (hit*sensor_right + (1-hit)*(1-sensor_right)))
            s = s + q[ii][jj]
    for ii in range(len(p)):
        for jj in range(len(p[0])):
            q[ii][jj] = q[ii][jj] / s
    return q

p = initialize(colors)
for i in range(len(measurements)):
    p = move(p, motions[i])
    p = sense(p,measurements[i])

#Your probability array must be printed 
#with the following code.

show(p)
link

answered 29 Feb '12, 20:18

Michael%20Sterling's gravatar image

Michael Ster...
884

Thx,
I'll have to reformat to read it better

But as Anne Paulson pointed out I should have kept my function that generate a uniformed distribution. I just added the variable as I posted it. I've worked with the 3x3-Matrix and trusted it to work with an MxN-Matrix.

Anyway....

(29 Feb '12, 22:25) DaGakusei DaGakusei's gravatar image

I code on VS2010 using the PythonTool - So I basically pasted the code on the Udacity-Window.
When I try to run any code on the Udacity-Window the browser often states 'Something happens.....'

So I code mostly on my IDE and paste the answers. Is it why I got almost every submission wrong?

It is pathetic! >_<

link

answered 29 Feb '12, 20:04

DaGakusei's gravatar image

DaGakusei
322410

You can use IDE but after pasting the code in browser you must test your code because sometimes code is not pasted correctly

(04 Mar '12, 12:33) Abdul Rauf-4 Abdul%20Rauf-4's gravatar image
Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×5,188
×369
×219
×192

Asked: 29 Feb '12, 19:48

Seen: 429 times

Last updated: 04 Mar '12, 12:33