anonymous
  • anonymous
Hey friends, in problems set 6, I have written every thing and my simulation works pretty good. But I have a problem; when my robot goes out of the room it stays out and it doesn't go back in. In other word the "room.isPositionInRoom" always stays "False". What's the problem?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
e.mccormick
  • e.mccormick
Well, it could be anything... could be how it is moced, how it detects location, etc. Without seeing the code it is hard to tell. When pasting code, a code paste service such as pastebin or dpaste is a good idea. It keeps the code formatting, as opposed to just posting it here. If you do post here, put a ``` above and below to help keep the formatting.
anonymous
  • anonymous
''' # Problem Set 6: Simulating robots # Name: # Collaborators: # Time: import math import random import numpy import ps6_visualize import pylab # === Provided classes #CleanedTiles=[] class Position(object): """ A Position represents a location in a two-dimensional room. """ def __init__(self, x, y): """ Initializes a position with coordinates (x, y). """ self.x = x self.y = y def getX(self): return self.x def getY(self): return self.y def getNewPosition(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.getX(), self.getY() # Compute the change in position delta_y = speed * math.cos(math.radians(angle)) delta_x = speed * math.sin(math.radians(angle)) # Add that to the existing position new_x = old_x + delta_x new_y = old_y + delta_y return Position(new_x, new_y) # === Problems 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 """ self.width=width self.height=height self.CleanedTiles=[] ## raise NotImplementedError 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 """ if [int(pos.getX()),int(pos.getY())] not in self.CleanedTiles: self.CleanedTiles.append([int(pos.getX()),int(pos.getY())]) 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 """ return [m,n] in self.CleanedTiles ## raise NotImplementedError def getNumTiles(self): """ Return the total number of tiles in the room. returns: an integer """ return self.width*self.height ## raise NotImplementedError def getNumCleanedTiles(self): """ Return the total number of clean tiles in the room. returns: an integer """ return len(self.CleanedTiles) ## raise NotImplementedError def getRandomPosition(self): """ Return a random position inside the room. returns: a Position object. """ return Position(int(random.random()*self.width),int(random.random()*self.height)) ## raise NotImplementedError 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. """ return (0 <= pos.getX() < self.width) and (0 <= pos.getY() < self.height) ## 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) """ self.room=room self.speed=speed self.Position=room.getRandomPosition() ## raise NotImplementedError def getRobotPosition(self): """ Return the position of the robot. returns: a Position object giving the robot's position. """ return self.Position ## raise NotImplementedError 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. """ d=random.randrange(360) return d ## raise NotImplementedError def setRobotPosition(self, position): """ Set the position of the robot to POSITION. position: a Position object. """ Position.__init__(self,x,y) ## raise NotImplementedError def setRobotDirection(self, direction): """ Set the direction of the robot to DIRECTION. direction: integer representing an angle in degrees """ Robot.direction=direction ## raise NotImplementedError 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. """ self.setRobotDirection(self.getRobotDirection()) print 'local direction is' , Robot.direction Newpos=self.getRobotPosition().getNewPosition(Robot.direction,self.speed) return Newpos ## while True: #### print 'Newpos is' , Newpos.getX() , Newpos.getY() ## if self.room.isPositionInRoom(Newpos)==True: ## return Newpos ## break ## else: ## Newpos=self.getRobotPosition().getNewPosition(self.getRobotDirection(),self.speed) ## ## raise NotImplementedError # === Problem 2 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 hits a wall, it chooses a new direction randomly. """ def updatePositionAndClean(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. """ return Robot.updatePositionAndClean() ## raise NotImplementedError # === 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. Robot or RandomWalkRobot) """ n=1 trials=[] while n
e.mccormick
  • e.mccormick
Forst, tjat was ``` not '''. They are accents, not quotes... so I did my best to capture the formatting: ``` # Problem Set 6: Simulating robots # Name: # Collaborators: # Time: import math import random import numpy import ps6_visualize import pylab class Position(object): """ A Position represents a location in a two-dimensional room. """ def __init__(self, x, y): """ Initializes a position with coordinates (x, y). """ self.x = x self.y = y def getX(self): return self.x def getY(self): return self.y def getNewPosition(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.getX(), self.getY() # Compute the change in position delta_y = speed * math.cos(math.radians(angle)) delta_x = speed * math.sin(math.radians(angle)) # Add that to the existing position new_x = old_x + delta_x new_y = old_y + delta_y return Position(new_x, new_y) # === Problems 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 """ self.width=width self.height=height self.CleanedTiles=[] ## raise NotImplementedError 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 """ if [int(pos.getX()),int(pos.getY())] not in self.CleanedTiles: self.CleanedTiles.append([int(pos.getX()),int(pos.getY())]) 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 """ return [m,n] in self.CleanedTiles ## raise NotImplementedError def getNumTiles(self): """ Return the total number of tiles in the room. returns: an integer """ return self.width*self.height ## raise NotImplementedError def getNumCleanedTiles(self): """ Return the total number of clean tiles in the room. returns: an integer """ return len(self.CleanedTiles) ## raise NotImplementedError def getRandomPosition(self): """ Return a random position inside the room. returns: a Position object. """ return Position(int(random.random()*self.width),int(random.random()*self.height)) ## raise NotImplementedError 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. """ return (0 <= pos.getX() < self.width) and (0 <= pos.getY() < self.height) ## 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) """ self.room=room self.speed=speed self.Position=room.getRandomPosition() ## raise NotImplementedError def getRobotPosition(self): """ Return the position of the robot. returns: a Position object giving the robot's position. """ return self.Position ## raise NotImplementedError 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. """ d=random.randrange(360) return d ### Why is this random? ## raise NotImplementedError def setRobotPosition(self, position): """ Set the position of the robot to POSITION. position: a Position object. """ Position.__init__(self,x,y) ## raise NotImplementedError def setRobotDirection(self, direction): """ Set the direction of the robot to DIRECTION. direction: integer representing an angle in degrees """ Robot.direction=direction ## raise NotImplementedError 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. """ self.setRobotDirection(self.getRobotDirection()) print 'local direction is' , Robot.direction Newpos=self.getRobotPosition().getNewPosition(Robot.direction,self.speed) return Newpos ## while True: ## ## print 'Newpos is' , Newpos.getX() , Newpos.getY() ## if self.room.isPositionInRoom(Newpos)==True: ## return Newpos ## break ## else: ## Newpos=self.getRobotPosition().getNewPosition(self.getRobotDirection(),self.speed) ## ## raise NotImplementedError # === Problem 2 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 hits a wall, it chooses a new direction randomly. """ def updatePositionAndClean(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. """ return Robot.updatePositionAndClean() ## raise NotImplementedError # === 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. Robot or RandomWalkRobot) """ n=1 trials=[] while n

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

e.mccormick
  • e.mccormick
Second, I do not see room.isPositionInRoom in there anywhere. Is it even defined? Third, why is this random? ``` 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. """ d=random.randrange(360) return d ### Why is this random? ``` I would think you would want to get the direction, not a random direction.
anonymous
  • anonymous
Hi @e.mccormick sorry about that I had problem putting those in open study app. If you look at the 'RectangularRoom' class you can see the 'isPositionInRoom'. It's there, the last function. About the 'getRobotDirection' I must say I defined it to give me a new random direction when I want to use 'updatePositionAndClean' when the Robot is out of the room. I really appreciate your help, but I don't think any of these is the problem.
e.mccormick
  • e.mccormick
I must have missed it while looking... it took a bit to reformat. My point on the random direction is that you don't ever really get the direction it faces. If you try, you move it. Also, from a programming standpoint, a getter should NEVER set or change a value. Otherwise it is not a getter. I saw some prints in other parts to see what values were passed. What about here? ``` 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. """ return (0 <= pos.getX() < self.width) and (0 <= pos.getY() < self.height) ## raise NotImplementedError ``` Did you ever print out pos.getX(), self.width, pos.getY, and self.height to see if they actually were the values you thought and if they would be true/false?
e.mccormick
  • e.mccormick
If you think about it logically, there are very few reasons a true-false evaluation would fail. If what is passed to the evaluation is incorrect, it can give an incorrect result. If what is evaluated is incorrect, it can give an incorrect result. If the construction of the return is incorrect, it can give incorrect results. By testing items before and after they are passed you can see if what is intended to be passed is actually being passed. This checks the input to the function on both sides. Now, if the values you see being passed are passed correctly and they are what is intended, then it is all about the evaluation and return. On the other hand, if the values are passed correctly but are not what is expected, then the problem is before the evaluation is called. And if the values are not passed correctly, then something is happening there. In Python, I doubt this would happen much. In other languages, sometimes people end up evaluating the memory address of an object and not the object itself... oops. I have not actually tried to run your code. I don't have all the packages installed that you included. But that is where I would look first.
anonymous
  • anonymous
I put print statements there. And it seems it gets what I want.
1 Attachment
anonymous
  • anonymous
And yes about the 'get direction' your are right it shouldn't be like that in programming point of view. But if you see in the 'run simulation' function I used this 'get direction' and I assign it to direction.
anonymous
  • anonymous
For running the program you don't need much packages, just the 'random' package! I don't think the problem of my code is about evaluation, you see it detects perfectly if the robot is in the room or not. But the problem rises when robot goes out of the room, when I change it's direction the new position won't be in the room again and it will outer and outer of the room and the 'isPositionInRoom' will give False and it won't clean anymore and program won't break then.
anonymous
  • anonymous
@e.mccormick i solved the problem!! the problem was here: ``` while True: pos=pos.getNewPosition(direction,speed) while room.isPositionInRoom(pos)!=True: direction=robot.getRobotDirection() pos=pos.getNewPosition(direction,speed) ``` here i might move the robot further in the wrong direction, this way changing direction might not help him to come back to the room. so now i wrote it this way: ``` while True: pos=pos.getNewPosition(direction,speed) if room.isPositionInRoom(pos)!=True: while True: direction=robot.getRobotDirection() if room.isPositionInRoom(pos.getNewPosition(direction,speed))==True: pos=pos.getNewPosition(direction,speed) break ``` and its working!!!
anonymous
  • anonymous
now i have a new problem here. about speed. when i increase speed it doesn't give me smaller 'NumSteps' it gives me a bigger 'NumSteps'.
e.mccormick
  • e.mccormick
On how large a room? A fast robot might leave the room more.

Looking for something else?

Not the answer you are looking for? Search for more explanations.