0

my professor has given me this code and the only part im suppose to do is the TODO area. i am rather new to python still and have never touched on this type of project so i am rather confused. all this project is for is to get the plotted graph image.

import math, random, pylab
class Location(object):
    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)
    def move(self, xc, yc):
        return Location(self.x+float(xc), self.y+float(yc))
    def getCoords(self):
        return self.x, self.y
    def getDist(self, other):
        ox, oy = other.getCoords()
        xDist = self.x - ox
        yDist = self.y - oy
        return math.sqrt(xDist**2 + yDist**2)
class CompassPt(object):
    possibles = ('N', 'S', 'E', 'W')
    def __init__(self, pt):
        if pt in self.possibles: self.pt = pt
        else: raise ValueError('in CompassPt.__init__')
    def move(self, dist):
        if self.pt == 'N': return (0, dist)
        elif self.pt == 'S': return (0, -dist)
        elif self.pt == 'E': return (dist, 0)
        elif self.pt == 'W': return (-dist, 0)
        else: raise ValueError('in CompassPt.move')
class Field(object):
    def __init__(self, drunk, loc):
        self.drunk = drunk
        self.loc = loc
    def move(self, cp, dist):
        oldLoc = self.loc
        xc, yc = cp.move(dist)
        self.loc = oldLoc.move(xc, yc)
    def getLoc(self):
        return self.loc
    def getDrunk(self):
        return self.drunk
class Drunk(object):
    def __init__(self, name):
        self.name = name
    def move(self, field, time = 1):
        if field.getDrunk() != self:
            raise ValueError('Drunk.move called with drunk not in field')
        for i in range(time):
            pt = CompassPt(random.choice(CompassPt.possibles))
            field.move(pt, 1)

here is the part that i need to do and have tried to do. so far my graph does not move lol. i get a dot in the middle of the graph, unless its randomly walking in one location if that's possible lol.

# TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
def performTrial(time, f):
    start = f.getLoc()
    # TODO
    distances = [0.0] 
    for t in range(1, time + 1):
        # TODO
        f.getDrunk().move(f) 
        newLoc = f.getLoc() 
        distance = newLoc.getDist(start) 
        distances.append(distance) 
    xcoords, ycoords = distances[::2], distances[1::2]
    return xcoords,ycoords
# END OF TODOEND OF TODO END OF TODO END OF TODO END OF TODO

the rest here is also provided by the professor

drunk = Drunk('Homer Simpson')
for i in range(1):
    f = Field(drunk, Location(0, 0))
    coords = performTrial(500, f)
    print(coords)
    pylab.plot(coords[0],coords[1],marker='^',linestyle=':',color='b')
pylab.title('Homer\'s Random Walk')
pylab.xlabel('Time')
pylab.ylabel('Distance from Origin')
pylab.show()

update: fixed the coordinates but now i get an error:

in _xy_from_xy(self, x, y)
    235         y = np.atleast_1d(y)
    236         if x.shape[0] != y.shape[0]:
--> 237             raise ValueError("x and y must have same first dimension")
    238         if x.ndim > 2 or y.ndim > 2:
    239             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension 
user3804711
  • 133
  • 3
  • 13
  • `distances[0]` and `distances[1]` are the first two distances. They are not coordinates or lists of coordinates. – user2357112 Aug 01 '14 at 09:26
  • yea ur right didn't notice that mistake. i changed it and now im getting an error – user3804711 Aug 01 '14 at 09:32
  • I realize that the todo part gathers all the coordinates but the later codes only print the first coordinates, so I plan to plot all the points in the todo area when I am able to get my hands on a computer. Just wondering if its a good idea – user3804711 Aug 01 '14 at 19:31

1 Answers1

0
def move(self, xc, yc):
    return Location(self.x+float(xc), self.y+float(yc))

this does not move anything, it just returns new coordinates. Also casting xc and yc to a float should not be needed.

If this function is intended to move the Location, you have to add this:

def move(self, xc, yc):
    self.x += xc
    self.y += yc
    return Location(self.x, self.y)
MaxNoe
  • 14,470
  • 3
  • 41
  • 46