ProgrammingAssignmen2
13 views
27 slides
Jun 25, 2024
Slide 1 of 27
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
About This Presentation
Explore the fascinating world of robotics and simulation with our in-depth presentation on Simulating Robot Movement for Cleaning a Room in Python. Perfect for students and programming enthusiasts, this presentation walks you through the process of developing a simulation that mimics a robot's c...
Explore the fascinating world of robotics and simulation with our in-depth presentation on Simulating Robot Movement for Cleaning a Room in Python. Perfect for students and programming enthusiasts, this presentation walks you through the process of developing a simulation that mimics a robot's cleaning path within a room using Python.
Whether you're a beginner learning the basics of simulation or an advanced programmer looking to enhance your skills, this presentation provides valuable insights and practical knowledge to help you succeed. Follow along with the provided code examples to simulate your own robot cleaner and understand the intricacies of robotic movement.
Visit ProgrammingAssignmentHelper.com for more tutorials, assignment help, and resources to support your programming education.
Size: 279.35 KB
Language: en
Added: Jun 25, 2024
Slides: 27 pages
Slide Content
Implementing Object-Oriented Programming to Simulate Roomba Movements Robot Movement Simulation Project For any Assignment related queries, Call us at : - +1(315) 557-6473 You can mail us at : - [email protected] or Reach us at : - https://www.programmingassignmenthelper.com/
Introduction Overview of the assignment: Simulating robot movement for cleaning a room. Learning objectives: Understand object-oriented programming. Implement classes and inheritance. Work with abstract classes and methods. Use Python’s random module. Project Overview You will simulate the movement of Roomba -like robots in a room. Robots will clean the room using different strategies. The project includes creating several classes: RectangularRoom , Robot, EmptyRoom , FurnishedRoom , StandardRobot , and FaultyRobot .
In this problem set, you will design a simulation and implement a program that uses classes to simulate robot movement. We recommend testing your code incrementally to see if your code is not working as expected . As always, please do not change any given function signatures. Read the Style Guide Make sure you consult the Style Guide as we will be taking point deductions for violations (e.g. non-descriptive variable names and uncommented code). Using Python’s Random Module You will be using Python's random module, Make sure you import random at the top of your file. Some useful function calls include:
random.randint (a, b) for integer inputs a and b, returns a random integer N such that a <= N <= b random.random () returns a float N such that 0.0 <= N < 1.0 random.seed (0) starts the pseudorandom number generator Python uses at the same spot so that the sequence of random numbers it produces from different runs of your code will be the same. You may find using this is useful while debugging. Simulation Overview iRobot is a company (started by MIT alumni and faculty) that sells the Roomba vacuuming robot (watch one of the product videos to see these robots in action). Roomba robots move around the floor, cleaning the area they pass over. You will code a simulation to compare how much time a group of Roomba -like robots will take to clean the floor of a room using two different strategies. The following simplified model of a single robot moving in a square 5x5 room should give you some intuition about the system we are simulating.
A description and sample illustrations are below. The robot starts out at some random position in the room. Its direction is specified by the angle of motion measured in degrees clockwise from “north.” Its position is specified from the lower left corner of the room, which is considered the origin (0.0, 0.0). The illustrations below show the robot's position (indicated by a black dot) as well as its direction (indicated by the direction of the red arrowhead).
D) Simulation Components: Here are the components of the simulation model. Room: Rooms are rectangles, divided into square tiles. At the start of the simulation, each tile is covered in some amount of dirt, which is the same across tiles. You will first implement the abstract class RectangularRoom in Problem 1, and then you will implement the subclasses EmptyRoom and FurnishedRoom in Problem 2. Robot: Multiple robots can exist in the room. iRobot has invested in technology that allows the robots to exist in the same position as another robot without causing a collision. You will implement the abstract class Robot in Problem 1. You will then implement the subclasses StandardRobot and FaultyRobot in Problems 3 and 4. More details about the properties of these components will be described later in the problem set. E) Helper Code
We have provided two additional files: ps3_visualize.py and ps3_verify_movement27.py . These Python files contain helper code for testing your code and for visualizing your robot simulation. Do not modify them. If one of these files throws an error, it is because of an error in your code implementation. To test your code, run ps3_tests_f16.py. Problem 1: Implementing the RectangularRoom and Robot classes Read ps3.py carefully before starting, so that you understand the provided code and its capabilities. Remember to carefully read the docstrings for each function to understand what it should do and what it needs to return. The first task is to implement two abstract classes, RectangularRoom and Robot . An abstract class will never be instantiated, and is instead used as a template for other classes that inherit from it. Abstract classes define methods that are shared by their subclasses. These methods can be implemented in the abstract classes
, but they can also be left unimplemented and instead implemented in their subclasses. In the skeleton code provided, the abstract classes contain some methods which should only be implemented in the subclasses. If the comment for the method says “do not change,” please do not change it. You can test your code as you go along by running the provided tests in ps3_test_f16.py. In ps3.py , we've provided skeletons for these classes, which you will fill in for Problem 1. We've also provided for you a complete implementation of the class Position . Class Descriptions: RectangularRoom - Represents the space to be cleaned and keeps track of which tiles have been cleaned. Robot - Stores the position, direction, and cleaning capacity of a robot. Position - Represents a location in x- and y -coordinates. x and y are floats satisfying ≤ x < w and ≤ y < h
RectangularRoom Implementation Details: Representation: You will need to keep track of which parts of the floor have been cleaned by the robot(s). When a robot's location is anywhere inside a particular tile, we will consider the dirt on that entire tile to be reduced by some amount determined by the robot. We consider the tile to be “clean” when the amount of dirt on the tile is 0. We will refer to the tiles using ordered pairs of integers : (0, 0), (0, 1), …, (0, h - 1), (1, 0), (1, 1), …, ( w -1, h -1). Tiles can never have a negative amount of dirt. Starting Conditions: Initially, the entire floor is uniformly dirty. Each tile should start with an integer amount of dirt, specified by dirt_amount . Robot Implementation Details: Representation Each robot has a position inside the room. We'll represent the position using an instance of the Position class. Remember the Position coordinates are floats. A robot has a direction of motion . We'll represent the direction using a float direction satisfying ≤ direction < 360, which gives an angle in degrees from north .
A robot has a cleaning capacity , capacity , which describes how much dirt is cleaned on each tile at each time. Starting Conditions Each robot should start at a random position in the room (hint: the Robot ’s roo m a ttr i bu t e ha s a m e t ho d y o u c a n u s e ) Movement Strategy A robot moves according to its movement strategy, which you will implement in update_position_and_clean Note that room tiles are represented using ordered pairs of integers (0, 0), (0, 1), …, (0, h - 1), (1, 0), (1, 1), …, ( w -1, h -1). But a robot’s Position is specified as floats (x, y). Be careful converting between the two! If you find any places above where the specification of the simulation dynamics seems ambiguous, it is up to you to make a reasonable decision about how your program/model will behave, and document that decision in your code.
Problem 2: Implementing EmptyRoom and FurnishedRoom In the previous problem, you implemented the RectangularRoom class. Now we want to consider additional kinds of rooms: rooms with furniture ( FurnishedRoom ) (thanks IKEA!) and rooms without furniture ( EmptyRoom ). These rooms are implemented in their own classes and have many of the same methods as RectangularRoom . Therefore, we'd like to use inheritance to reduce the amount of duplicated code by implementing FurnishedRoom and EmptyRoom as subclasses of RectangularRoom according to the image below:
Think about how the methods you need to implement differ for the two classes. and how you can use methods already implemented in the parent class RectangularRoom . Note: failure to take advantage of inheritance will result in a deduction. Additionally, be careful in determining whether a position is valid. Recall that in the case of FurnishedRoom , a robot cannot be in a position (in a tile) that has furniture. Finally, in the FurnishedRoom class, we have implemented the add_furniture_to_room method to add a rectangular furniture piece to the room for you. You do not need to call this method; the provided test code will call it for you. Do not change this method.
Implementing RectangularRoom and Robot Abstract Classes First, let's define the RectangularRoom class. RectangularRoom Class This class keeps track of which parts of the room have been cleaned. import random import math class RectangularRoom : def __init__(self, width, height, dirt_amount ): """ Initializes a rectangular room of given width and height with all tiles starting with dirt_amount units of dirt. """ self.width = width self.height = height self.dirt_amount = dirt_amount self.tiles = {} for x in range(width): for y in range(height): self.tiles [(x, y)] = dirt_amount
def clean_tile_at_position (self, pos, capacity): """ Mark the tile under the position POS as cleaned by removing dirt by the amount specified by capacity. The tile should not have negative dirt. """ tile = ( math.floor ( pos.get_x ()), math.floor ( pos.get_y ())) self.tiles [tile] = max( self.tiles [tile] - capacity, 0) def is_tile_cleaned (self, m, n): """ Return True if the tile (m, n) has been cleaned. """ return self.tiles [(m, n)] == 0 def get_num_cleaned_tiles (self): """ Return the total number of clean tiles. """ return sum(1 for dirt in self.tiles.values () if dirt == 0) def is_position_in_room (self, pos): """
Return True if pos is inside the room. """ 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). """ return self.tiles [(m, n)] def get_num_tiles (self): """ Return the total number of tiles in the room. """ return self.width * self.height def get_random_position (self): """ Return a random position inside the room. """
x = random.uniform (0, self.width ) y = random.uniform (0, self.height ) return Position(x, y) Position Class This class represents a location in the room. class Position: 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, with this object as the current position, and with the specified angle and speed. """ old_x , old_y = self.get_x (), self.get_y () angle = float(angle) 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 ) Robot Class This class represents the robot with its position and cleaning capacity. class Robot: def __init__(self, room, speed, capacity): """
Initializes a Robot with the given speed and capacity in a specified room. """ self.room = room self.speed = speed self.capacity = capacity self.position = room.get_random_position () self.direction = random.uniform (0, 360) def get_robot_position (self): return self.position def get_robot_direction (self): return self.direction def set_robot_position (self, position): self.position = position def set_robot_direction (self, direction): self.direction = direction
def update_position_and_clean (self): """ Simulate the passage of a single time-step. Move the robot to a new position and mark the tile it is on as having been cleaned by capacity units of dirt. """ raise NotImplementedError # This should be implemented by subclasses 2. Implementing EmptyRoom and FurnishedRoom These classes inherit from RectangularRoom . EmptyRoom Class The EmptyRoom class does not add any additional behavior, so it can just inherit from RectangularRoom . class EmptyRoom ( RectangularRoom ): def __init__(self, width, height, dirt_amount ): super().__init__(width, height, dirt_amount )
FurnishedRoom Class The FurnishedRoom class adds furniture to the room and ensures robots cannot move into those areas. class FurnishedRoom ( RectangularRoom ): def __init__(self, width, height, dirt_amount ): super().__init__(width, height, dirt_amount ) self.furniture_tiles = set() def add_furniture_to_room (self, top_left , bottom_right ): """ Adds furniture to the room from top_left to bottom_right , inclusive. """ for x in range( math.floor ( top_left.get_x ()), math.ceil ( bottom_right.get_x ())): for y in range( math.floor ( top_left.get_y ()), math.ceil ( bottom_right.get_y ())): self.furniture_tiles.add ((x, y)) def is_tile_furnished (self, m, n): """ Return True if the tile (m, n) is furnished. """
return (m, n) in self.furniture_tiles def is_position_furnished (self, pos): """ Return True if the position is furnished. """ tile = ( math.floor ( pos.get_x ()), math.floor ( pos.get_y ())) return tile in self.furniture_tiles def is_position_in_room (self, pos): """ Return True if pos is in the room and not furnished. """ if super(). is_position_in_room (pos): return not self.is_position_furnished (pos) return False With these classes implemented, you can now proceed to implement subclasses of Robot to define specific movement strategies and clean behaviors. 3. Implementing Subclasses of Robot Let's implement two subclasses of Robot: StandardRobot and FaultyRobot .
StandardRobot Class The StandardRobot moves in a straight line until it hits a wall, at which point it chooses a new random direction. class StandardRobot (Robot): def update_position_and_clean (self): """ Move the robot to a new position and mark the tile it is on as having been cleaned by capacity units of dirt. """ new_position = self.position.get_new_position ( self.direction , self.speed ) if self.room.is_position_in_room ( new_position ): self.set_robot_position ( new_position ) self.room.clean_tile_at_position ( new_position , self.capacity ) else: self.set_robot_direction ( random.uniform (0, 360))
FaultyRobot Class The FaultyRobot has a chance of malfunctioning and staying in place for a timestep . class FaultyRobot (Robot): def update_position_and_clean (self): """ Move the robot to a new position and mark the tile it is on as having been cleaned by capacity units of dirt. Sometimes the robot malfunctions and does not move. """ if random.random () < 0.15: # Malfunction: stay in the same place return new_position = self.position.get_new_position ( self.direction , self.speed ) if self.room.is_position_in_room ( new_position ): self.set_robot_position ( new_position ) self.room.clean_tile_at_position ( new_position , self.capacity ) else: self.set_robot_direction ( random.uniform (0, 360))
class FaultyRobot (Robot): def update_position_and_clean (self): """ Move the robot to a new position and mark the tile it is on as having been cleaned by capacity units of dirt. Sometimes the robot malfunctions and does not move. """ if random.random () < 0.15: # Malfunction: stay in the same place return new_position = self.position.get_new_position ( self.direction , self.speed ) if self.room.is_position_in_room ( new_position ): self.set_robot_position ( new_position ) self.room.clean_tile_at_position ( new_position , self.capacity ) else: self.set_robot_direction ( random.uniform (0, 360))
4. Running the Simulation Now that the classes are implemented, you can run simulations to compare different strategies for cleaning the room. Simulation Code The simulation code is provided in the ps3.py file, and you can run the ps3_tests_f16.py file to test your implementations. Make sure to test your code incrementally and check for correctness at each step. By following this guide, you will have a working simulation of robot movement and cleaning in different room configurations.
Conclusion & Next Steps Summary: Recap the key concepts learned: OOP principles, inheritance, abstract classes, and simulation. Highlight the practical application of these concepts in simulating a real-world scenario. Next Steps: Encourage students to test and refine their code. Suggest exploring additional robot behaviors and room configurations. Recommend further reading on advanced OOP and simulation techniques. Call to Action: Visit our website for more programming projects and resources. Contact us for personalized help with your assignments.
Thank You & Contact Information Thank You: Thank you for your attention and participation. We hope you found this project challenging and educational. Contact Information: Website: www.programmingassignmenthelper.com Email: [email protected] Call/ WhatsApp : +1(812)783-0640 Support: Reach out for additional support and guidance on this project or any other programming assignments.