import math
import random
class Position(object):
"""
A Position represents a location in a two-dimensional room, where
coordinates are given by floats (x, y).
"""
def __init__(self, x, y):
"""
Initializes a position with coordinates (x, y).
"""
self.x = x
self.y = y
def get_x(self):
return self.x
def get_y(self):
return self.y
def get_new_position(self, angle, speed):
"""
Computes and returns the new Position after a single clock-tick has
passed, with this object as the current position, and with the
specified angle and speed.
Does NOT test whether the returned position fits inside the room.
angle: float representing angle in degrees, 0 <= angle < 360
speed: positive float representing speed
Returns: a Position object representing the new position.
"""
old_x, old_y = self.get_x(), self.get_y()
delta_y = speed * math.cos(math.radians(angle))
delta_x = speed * math.sin(math.radians(angle))
new_x = old_x + delta_x
new_y = old_y + delta_y
return Position(new_x, new_y)
def __str__(self):
return "Position: " + str(math.floor(self.x)) + ", " + str(math.floor(self.y))
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. Each tile
has some fixed amount of dirt. The tile is considered clean only when the amount
of dirt on this tile is 0.
"""
def __init__(self, width, height, dirt_amount):
"""
Initializes a rectangular room with the specified width, height, and
dirt_amount on each tile.
width: an integer > 0
height: an integer > 0
dirt_amount: an integer >= 0
"""
self.width = width
self.height = height
self.tiles = {}
for m in range(width):
for n in range(height):
self.tiles[(m, n)] = dirt_amount
def clean_tile_at_position(self, pos, capacity):
"""
Mark the tile under the position pos as cleaned by capacity amount of dirt.
Assumes that pos represents a valid position inside this room.
pos: a Position object
capacity: the amount of dirt to be cleaned in a single time-step
can be negative which would mean adding dirt to the tile
Note: The amount of dirt on each tile should be NON-NEGATIVE.
If the capacity exceeds the amount of dirt on the tile, mark it as 0.
"""
tile = (math.floor(pos.get_x()), math.floor(pos.get_y()))
self.tiles[tile] = max(0, self.tiles[tile] - capacity)
def is_tile_cleaned(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 the tile (m, n) is cleaned, False otherwise
Note: The tile is considered clean only when the amount of dirt on this
tile is 0.
"""
return self.tiles[(m, n)] == 0
def get_num_cleaned_tiles(self):
"""
Returns: an integer; the total number of clean tiles in the room
"""
return sum(1 for dirt in self.tiles.values() if dirt == 0)
def is_position_in_room(self, pos):
"""
Determines if pos is inside the room.
pos: a Position object.
Returns: True if pos is in the room, False otherwise.
"""
return 0 <= pos.get_x() < self.width and 0 <= pos.get_y() < self.height
def get_dirt_amount(self, m, n):
"""
Return the amount of dirt on the tile (m, n)
Assumes that (m, n) represents a valid tile inside the room.
m: an integer
n: an integer
Returns: an integer
"""
return self.tiles[(m, n)]
def get_num_tiles(self):
"""
Returns: an integer; the total number of tiles in the room
"""
raise NotImplementedError
def is_position_valid(self, pos):
"""
pos: a Position object.
returns: True if pos is in the room and (in the case of FurnishedRoom)
if position is unfurnished, False otherwise.
"""
raise NotImplementedError
def get_random_position(self):
"""
Returns: a Position object; a random position inside the room
"""
raise NotImplementedError
class EmptyRoom(RectangularRoom):
"""
An EmptyRoom represents a RectangularRoom with no furniture.
"""
def get_num_tiles(self):
"""
Returns: an integer; the total number of tiles in the room
"""
return self.width * self.height
def is_position_valid(self, pos):
"""
pos: a Position object.
Returns: True if pos is in the room, False otherwise.
"""
return self.is_position_in_room(pos)
def get_random_position(self):
"""
Returns: a Position object; a valid random position (inside the room).
"""
return Position(random.random() * self.width, random.random() * self.height)
class FurnishedRoom(RectangularRoom):
"""
A FurnishedRoom represents a RectangularRoom with a rectangular piece of
furniture. The robot should not be able to land on these furniture tiles.
"""
def __init__(self, width, height, dirt_amount):
"""
Initializes a FurnishedRoom, a subclass of RectangularRoom. FurnishedRoom
also has a list of tiles which are furnished (furniture_tiles).
"""
RectangularRoom.__init__(self, width, height, dirt_amount)
self.furniture_tiles = []
def add_furniture_to_room(self):
"""
Add a rectangular piece of furniture to the room. Furnished tiles are stored
as (x, y) tuples in the list furniture_tiles
Furniture location and size is randomly selected. Width and height are selected
so that the piece of furniture fits within the room and does not occupy the
entire room. Position is selected by randomly selecting the location of the
bottom left corner of the piece of furniture so that the entire piece of
furniture lies in the room.
"""
furniture_width = random.randint(1, self.width - 1)
furniture_height = random.randint(1, self.height - 1)
f_bottom_left_x = random.randint(0, self.width - furniture_width)
f_bottom_left_y = random.randint(0, self.height - furniture_height)
for i in range(f_bottom_left_x, f_bottom_left_x + furniture_width):
for j in range(f_bottom_left_y, f_bottom_left_y + furniture_height):
self.furniture_tiles.append((i,j))
def is_tile_furnished(self, m, n):
"""
Return True if tile (m, n) is furnished.
"""
return (m, n) in self.furniture_tiles
def is_position_furnished(self, pos):
"""
pos: a Position object.
Returns True if pos is furnished and False otherwise
"""
return self.is_tile_furnished(math.floor(pos.get_x()), math.floor(pos.get_y()))
def is_position_valid(self, pos):
"""
pos: a Position object.
returns: True if pos is in the room and is unfurnished, False otherwise.
"""
return self.is_position_in_room(pos) and not self.is_position_furnished(pos)
def get_num_tiles(self):
"""
Returns: an integer; the total number of tiles in the room that can be accessed.
"""
return self.width * self.height - len(set(self.furniture_tiles))
def get_random_position(self):
"""
Returns: a Position object; a valid random position (inside the room and not in a furnished area).
"""
while True:
pos = Position(random.random() * self.width, random.random() * self.height)
if self.is_position_valid(pos):
return pos
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 and a fixed cleaning capacity.
Subclasses of Robot should provide movement strategies by implementing
update_position_and_clean, which simulates a single time-step.
"""
def __init__(self, room, speed, capacity):
"""
Initializes a Robot with the given speed and given cleaning capacity in the
specified room. The robot initially has a random direction and a random
position in the room.
room: a RectangularRoom object.
speed: a float (speed > 0)
capacity: a positive interger; the amount of dirt cleaned by the robot
in a single time-step
"""
self.room = room
self.speed = speed
self.capacity = capacity
self.position = room.get_random_position()
self.direction = random.random() * 360
def get_robot_position(self):
"""
Returns: a Position object giving the robot's position in the room.
"""
return self.position
def get_robot_direction(self):
"""
Returns: a float d giving the direction of the robot as an angle in
degrees, 0.0 <= d < 360.0.
"""
return self.direction
def set_robot_position(self, position):
"""
Set the position of the robot to position.
position: a Position object.
"""
self.position = position
def set_robot_direction(self, direction):
"""
Set the direction of the robot to direction.
direction: float representing an angle in degrees
"""
self.direction = direction
def update_position_and_clean(self):
"""
Simulate the raise passage of a single time-step.
Move the robot to a new random position (if the new position is invalid,
rotate once to a random new direction, and stay stationary) and mark the tile it is on as having
been cleaned by capacity amount.
"""
raise NotImplementedError
class StandardRobot(Robot):
"""
A StandardRobot is a Robot with the standard movement strategy.
At each time-step, a StandardRobot attempts to move in its current
direction; when it would hit a wall or furtniture, it *instead*
chooses a new direction randomly.
"""
def update_position_and_clean(self):
"""
Simulate the raise passage of a single time-step.
Move the robot to a new random position (if the new position is invalid,
rotate once to a random new direction, and stay stationary) and clean the dirt on the tile
by its given capacity.
"""
new_pos = self.position.get_new_position(self.direction, self.speed)
if self.room.is_position_valid(new_pos):
self.position = new_pos
self.room.clean_tile_at_position(self.position, self.capacity)
else:
self.direction = random.random() * 360
class FaultyRobot(Robot):
"""
A FaultyRobot is a robot that will not clean the tile it moves to and
pick a new, random direction for itself with probability p rather
than simply cleaning the tile it moves to.
"""
p = 0.15
@staticmethod
def set_faulty_probability(prob):
"""
Sets the probability of getting faulty equal to PROB.
prob: a float (0 <= prob <= 1)
"""
FaultyRobot.p = prob
def gets_faulty(self):
"""
Answers the question: Does this FaultyRobot get faulty at this timestep?
A FaultyRobot gets faulty with probability p.
returns: True if the FaultyRobot gets faulty, False otherwise.
"""
return random.random() < FaultyRobot.p
def update_position_and_clean(self):
"""
Simulate the passage of a single time-step.
Check if the robot gets faulty. If the robot gets faulty,
do not clean the current tile and change its direction randomly.
If the robot does not get faulty, the robot should behave like
StandardRobot at this time-step (checking if it can move to a new position,
move there if it can, pick a new direction and stay stationary if it can't)
"""
if self.gets_faulty():
self.direction = random.random() * 360
return
new_pos = self.position.get_new_position(self.direction, self.speed)
if self.room.is_position_valid(new_pos):
self.position = new_pos
self.room.clean_tile_at_position(self.position, self.capacity)
else:
self.direction = random.random() * 360
def run_simulation(num_robots, speed, capacity, width, height, dirt_amount, 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 the input speed and capacity in a room of dimensions width x height
with the dirt dirt_amount on each tile.
num_robots: an int (num_robots > 0)
speed: a float (speed > 0)
capacity: an int (capacity >0)
width: an int (width > 0)
height: an int (height > 0)
dirt_amount: an int
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
FaultyRobot)
"""
pass