2D Random Walk Write a program to simulate two random walkers in 2D finite space
ID: 3757160 • Letter: 2
Question
2D Random Walk
Write a program to simulate two random walkers in 2D finite space
o Define a rectangular space of height M and width N. Place each walker in this space at a randomly chosen location (x,y, x<=M and y<=N). Step size is 1 for both walkers. The direction of each step is random and independent. The walkers can only visit a given space only once.
o Run the simulation until one of the following occurs:
1) Both walkers get stuck and can't move to an open space, or
2) when either walker crosses path with the other walker. In other words, the next step of a walker happens to be occupied by the other walker. Count the number of steps and the reason for stopping the simulation.
o Run the simulation many times and plot the number of steps when walkers stop moving.
This is the question my professor gave me. I dont know how I can add details other than the question itself.
Explanation / Answer
Since there is no programming language mentioned, I will use python .
Just run this file and see the output.
Provided: You have python installed in your system.
PS: You need patience because it will take time for both walkers to collide or cross each others path.
import random
class Point:
def __init__(self, x: int, y: int):
self.__x = x
self.__y = y
def getPointX(self):
return self.__x
def getPointY(self):
return self.__y
def checkIntersection(self, p):
return ((self.__x == p.getPointX()) and (self.__y == p.getPointY()))
class Walker:
def __init__(self, startPoint: Point, boardDimension: Point):
self.__points = []
self.__points.append(startPoint)
self.__maxDimX = boardDimension.getPointX()
self.__maxDimY = boardDimension.getPointY()
def move(self, direction: int):
if (direction == 0):
print('Moving Up')
if(self.getPosition().getPointY() + 1 < self.__maxDimY):
nextPoint = Point(self.getPosition().getPointX(),
self.getPosition().getPointY() + 1)
self.__points.append(nextPoint)
if (direction == 1):
print('Moving Right')
if(self.getPosition().getPointX() + 1 < self.__maxDimX):
nextPoint = Point(self.getPosition().getPointX(
) + 1, self.getPosition().getPointY())
self.__points.append(nextPoint)
if (direction == 2):
print('Moving Down')
if(self.getPosition().getPointY() - 1 > 0):
nextPoint = Point(self.getPosition().getPointX(),
self.getPosition().getPointY() - 1)
self.__points.append(nextPoint)
if (direction == 3):
print('Moving Left')
if(self.getPosition().getPointX() - 1 > 0):
nextPoint = Point(
self.getPosition().getPointX() - 1, self.getPosition().getPointY())
self.__points.append(nextPoint)
def checkCollision(self, point: Point):
return self.getPosition().checkIntersection(point)
def checkCrossSection(self, points: Point = []):
for point in points:
if(self.getPosition().checkIntersection(point)):
return True
return False
def getPosition(self):
return self.__points[len(self.__points) - 1]
def getPath(self):
return self.__points
if __name__ == '__main__':
board_width = random.randint(100, 200)
board_height = random.randint(100, 200)
w1 = Walker(Point(random.randint(0, board_width), random.randint(
0, board_height)), Point(board_width, board_height))
w2 = Walker(Point(random.randint(0, board_width), random.randint(
0, board_height)), Point(board_width, board_height))
runSim = True
print('RunningSimulation... ')
steps = 0
while runSim:
direction = random.randint(0, 3)
w1.move(direction)
print('Walker 1 position:(', w1.getPosition().getPointX(),
',', w1.getPosition().getPointY(), ')')
if (w1.checkCollision(w2.getPosition())):
runSim = False
print('Walker 1 and Walker 2 collide')
break
if (w1.checkCrossSection(w2.getPath())):
runSim = False
print('Walker 1 crosses Walker 2 path')
break
direction = random.randint(0, 3)
w2.move(direction)
print('Walker 2 position:(', w2.getPosition().getPointX(),
',', w2.getPosition().getPointY(), ')')
if (w2.checkCollision(w1.getPosition())):
runSim = False
print('Walker 1 and Walker 2 collide')
break
if (w2.checkCrossSection(w1.getPath())):
runSim = False
print('Walker 2 crosses Walker 1 path')
break
steps += 1
print('Simulation completed.. at step:', steps)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.