Skip to content

Instantly share code, notes, and snippets.

@max-giro
Created November 26, 2012 05:27
Show Gist options
  • Save max-giro/4146722 to your computer and use it in GitHub Desktop.
Save max-giro/4146722 to your computer and use it in GitHub Desktop.
# === Problem 1
class RectangularRoom(object):
"""
A RectangularRoom represents a rectangular region containing clean or dirty
tiles.
A room has a width and a height and contains (width * height) tiles. At any
particular time, each of these tiles is either clean or dirty.
"""
def __init__(self, width, height):
"""
Initializes a rectangular room with the specified width and height.
Initially, no tiles in the room have been cleaned.
width: an integer > 0
height: an integer > 0
"""
#raise NotImplementedError
self.width = width
self.height = height
self.cleaned_tiles = {}
for x in range(width):
for y in range(height):
self.cleaned_tiles[(x, y)] = False
def cleanTileAtPosition(self, pos):
"""
Mark the tile under the position POS as cleaned.
Assumes that POS represents a valid position inside this room.
pos: a Position
"""
#raise NotImplementedError
self.cleaned_tiles[(math.floor(pos.x), math.floor(pos.y))] = True
def isTileCleaned(self, m, n):
"""
Return True if the tile (m, n) has been cleaned.
Assumes that (m, n) represents a valid tile inside the room.
m: an integer
n: an integer
returns: True if (m, n) is cleaned, False otherwise
"""
#raise NotImplementedError
return self.cleaned_tiles[(math.floor(m), math.floor(n))]
def getNumTiles(self):
"""
Return the total number of tiles in the room.
returns: an integer
"""
#raise NotImplementedError
return self.width * self.height
def getNumCleanedTiles(self):
"""
Return the total number of clean tiles in the room.
returns: an integer
"""
#raise NotImplementedError
tot_cleaned_tiles = 0
for v in self.cleaned_tiles.values():
if (v == True):
tot_cleaned_tiles += 1
return tot_cleaned_tiles
def getRandomPosition(self):
"""
Return a random position inside the room.
returns: a Position object.
"""
#raise NotImplementedError
return Position(random.uniform(0, self.width), random.uniform(0, self.height))
def isPositionInRoom(self, pos):
"""
Return True if pos is inside the room.
pos: a Position object.
returns: True if pos is in the room, False otherwise.
"""
#raise NotImplementedError
class Robot(object):
"""
Represents a robot cleaning a particular room.
At all times the robot has a particular position and direction in the room.
The robot also has a fixed speed.
Subclasses of Robot should provide movement strategies by implementing
updatePositionAndClean(), which simulates a single time-step.
"""
def __init__(self, room, speed):
"""
Initializes a Robot with the given speed in the specified room. The
robot initially has a random direction and a random position in the
room. The robot cleans the tile it is on.
room: a RectangularRoom object.
speed: a float (speed > 0)
"""
#raise NotImplementedError
self.room = room
self.speed = speed
self.direction = random.randrange(360)
#self.direction = self.setRobotDirection(random.randint(0, 359))
#self.direction = random.randint(0, 359)
#self.position = self.setRobotPosition(self.room.getRandomPosition())
#self.room.cleanTileAtPosition(self.getRobotPosition())
self.pos = self.room.getRandomPosition()
self.room.cleanTileAtPosition(self.pos)
def getRobotPosition(self):
"""
Return the position of the robot.
returns: a Position object giving the robot's position.
"""
#raise NotImplementedError
return self.pos
def getRobotDirection(self):
"""
Return the direction of the robot.
returns: an integer d giving the direction of the robot as an angle in
degrees, 0 <= d < 360.
"""
#raise NotImplementedError
return self.direction
def setRobotPosition(self, position):
"""
Set the position of the robot to POSITION.
position: a Position object.
"""
#raise NotImplementedError
self.pos = position
def setRobotDirection(self, direction):
"""
Set the direction of the robot to DIRECTION.
direction: integer representing an angle in degrees
"""
#raise NotImplementedError
self.direction = direction
def updatePositionAndClean(self):
"""
Simulate the raise passage of a single time-step.
Move the robot to a new position and mark the tile it is on as having
been cleaned.
"""
raise NotImplementedError # don't change this!
# === Problem 3
def runSimulation(num_robots, speed, width, height, min_coverage, num_trials,
robot_type):
"""
Runs NUM_TRIALS trials of the simulation and returns the mean number of
time-steps needed to clean the fraction MIN_COVERAGE of the room.
The simulation is run with NUM_ROBOTS robots of type ROBOT_TYPE, each with
speed SPEED, in a room of dimensions WIDTH x HEIGHT.
num_robots: an int (num_robots > 0)
speed: a float (speed > 0)
width: an int (width > 0)
height: an int (height > 0)
min_coverage: a float (0 <= min_coverage <= 1.0)
num_trials: an int (num_trials > 0)
robot_type: class of robot to be instantiated (e.g. StandardRobot or
RandomWalkRobot)
"""
#raise NotImplementedError
robots = []
room = RectangularRoom(width, height)
coverage = min_coverage * room.getNumTiles()
times = 0.0
for r in range(num_robots):
robots.append(robot_type(room, speed))
for n in range(num_trials):
while coverage > room.getNumCleanedTiles():
for n in range(num_trials):
for r in range(num_robots):
robots[r].updatePositionAndClean()
times = times + 1
for x in range(width):
for y in range(height):
room.cleaned_tiles[(x, y)] = False
return times / num_trials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment