-
Notifications
You must be signed in to change notification settings - Fork 0
/
w1l212_randomWalks-segment4.py
203 lines (141 loc) · 4.86 KB
/
w1l212_randomWalks-segment4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class Location(object):
def __init__(self, x, y):
"""x and y are floats"""
self.x = x
self.y = y
def move(self, deltaX, deltaY):
"""deltaX and deltaY are floats"""
return Location(self.x + deltaX, self.y + deltaY)
def getX(self):
return self.x
def getY(self):
return self.y
def distFrom(self, other):
ox = other.x
oy = other.y
xDist = self.x - ox
yDist = self.y - oy
return (xDist**2 + yDist**2)**0.5
def __str__(self):
return '<' + str(self.x) + ', ' + str(self.y) + '>'
class Field(object):
def __init__(self):
self.drunks = {}
def addDrunk(self, drunk, loc):
if drunk in self.drunks:
raise ValueError('Duplicate drunk')
else:
self.drunks[drunk] = loc
def moveDrunk(self, drunk):
if not drunk in self.drunks:
raise ValueError('Drunk not in field')
xDist, yDist = drunk.takeStep()
currentLocation = self.drunks[drunk]
#use move method of Location to get new location
self.drunks[drunk] = currentLocation.move(xDist, yDist)
def getLoc(self, drunk):
if not drunk in self.drunks:
raise ValueError('Drunk not in field')
return self.drunks[drunk]
import random
class Drunk(object):
def __init__(self, name):
self.name = name
def __str__(self):
return 'This drunk is named ' + self.name
class UsualDrunk(Drunk):
def takeStep(self):
stepChoices =\
[(0.0,1.0), (0.0,-1.0), (1.0, 0.0), (-1.0, 0.0)]
return random.choice(stepChoices)
def walk(f, d, numSteps):
start = f.getLoc(d)
for s in range(numSteps):
f.moveDrunk(d)
return(start.distFrom(f.getLoc(d)))
import pylab
#set line width
pylab.rcParams['lines.linewidth'] = 6
#set font size for titles
pylab.rcParams['axes.titlesize'] = 20
#set font size for labels on axes
pylab.rcParams['axes.labelsize'] = 20
#set size of numbers on x-axis
pylab.rcParams['xtick.major.size'] = 5
#set size of numbers on y-axis
pylab.rcParams['ytick.major.size'] = 5
#set size of markers
pylab.rcParams['lines.markersize'] = 10
#Start code added in Segment 4
def drunkTestP(numTrials = 50):
stepsTaken = [10, 100, 1000, 10000]
meanDistances = []
for numSteps in stepsTaken:
distances = simWalks(numSteps, numTrials)
meanDistances.append(sum(distances)/len(distances))
pylab.plot(stepsTaken, meanDistances)
pylab.title('Mean Distance from Origin')
pylab.xlabel('Steps Taken')
pylab.ylabel('Steps from Origin')
pylab.show()
def drunkTestP1(numTrials = 50):
stepsTaken = [10, 100, 1000, 10000]
meanDistances = []
squareRootOfSteps = []
for numSteps in stepsTaken:
distances = simWalks(numSteps, numTrials)
meanDistances.append(sum(distances)/len(distances))
squareRootOfSteps.append(numSteps**0.5)
pylab.plot(stepsTaken, meanDistances, 'b-',
label = 'Mean distance')
pylab.plot(stepsTaken, squareRootOfSteps, 'g-.',
label = 'Square root of steps')
pylab.title('Mean Distance from Origin')
pylab.xlabel('Steps Taken')
pylab.ylabel('Steps from Origin')
pylab.legend()
pylab.show()
#Look at different kinds of drunks
class UsualDrunk(Drunk):
def takeStep(self):
stepChoices =\
[(0.0,1.0), (0.0,-1.0), (1.0, 0.0), (-1.0, 0.0)]
return random.choice(stepChoices)
class ColdDrunk(Drunk):
def takeStep(self):
stepChoices =\
[(0.0,0.95), (0.0,-1.0), (1.0, 0.0), (-1.0, 0.0)]
return random.choice(stepChoices)
class EDrunk(Drunk):
def takeStep(self):
deltaX = random.random()
if random.random() < 0.5:
deltaX = -deltaX
deltaY = random.random()
if random.random() < 0.5:
deltaY = -deltaY
return (deltaX, deltaY)
# New version of simWalks
def simWalks(numSteps, numTrials, dClass):
homer = dClass('Homer')
origin = Location(0, 0)
distances = []
for t in range(numTrials):
f = Field()
f.addDrunk(homer, origin)
distances.append(walk(f, homer, numSteps))
return distances
def drunkTestP(numTrials = 50):
stepsTaken = [10, 100, 1000, 10000]
for dClass in (UsualDrunk, ColdDrunk, EDrunk):
meanDistances = []
for numSteps in stepsTaken:
distances = simWalks(numSteps, numTrials, dClass)
meanDistances.append(sum(distances)/len(distances))
pylab.plot(stepsTaken, meanDistances,
label = dClass.__name__)
pylab.title('Mean Distance from Origin')
pylab.xlabel('Steps Taken')
pylab.ylabel('Steps from Origin')
pylab.legend(loc = 'upper left')
pylab.show()