Guest User

Untitled

a guest
May 11th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. # Problem Set 6: Simulating robots
  2. # Name:
  3. # Collaborators:
  4. # Time:
  5.  
  6. import math
  7. import random
  8.  
  9. import ps6_visualize
  10. import pylab
  11.  
  12. # === Provided classes
  13.  
  14. class Position(object):
  15. """
  16. A Position represents a location in a two-dimensional room.
  17. """
  18. def __init__(self, x, y):
  19. """
  20. Initializes a position with coordinates (x, y).
  21. """
  22. self.x = x
  23. self.y = y
  24. def getX(self):
  25. return self.x
  26. def getY(self):
  27. return self.y
  28. def getNewPosition(self, angle, speed):
  29. """
  30. Computes and returns the new Position after a single clock-tick has
  31. passed, with this object as the current position, and with the
  32. specified angle and speed.
  33.  
  34. Does NOT test whether the returned position fits inside the room.
  35.  
  36. angle: float representing angle in degrees, 0 <= angle < 360
  37. speed: positive float representing speed
  38.  
  39. Returns: a Position object representing the new position.
  40. """
  41. old_x, old_y = self.getX(), self.getY()
  42. # Compute the change in position
  43. delta_y = speed * math.cos(math.radians(angle))
  44. delta_x = speed * math.sin(math.radians(angle))
  45. # Add that to the existing position
  46. new_x = old_x + delta_x
  47. new_y = old_y + delta_y
  48. return Position(new_x, new_y)
  49.  
  50. def __eq__(self, pos):
  51. if self.getX() == pos.getX() and self.getY() == pos.getY():
  52. return True
  53. else:
  54. return False
  55.  
  56. # === Problems 1
  57.  
  58. class RectangularRoom(object):
  59. """
  60. A RectangularRoom represents a rectangular region containing clean or dirty
  61. tiles.
  62.  
  63. A room has a width and a height and contains (width * height) tiles. At any
  64. particular time, each of these tiles is either clean or dirty.
  65. """
  66. def __init__(self, width, height):
  67. """
  68. Initializes a rectangular room with the specified width and height.
  69.  
  70. Initially, no tiles in the room have been cleaned.
  71.  
  72. width: an integer > 0
  73. height: an integer > 0
  74. """
  75. self.width = int(width)
  76. self.height = int(height)
  77.  
  78. # Tiles are a list of positions
  79. self.tiles = []
  80. for i in range(self.width):
  81. for j in range(self.height):
  82. self.tiles.append(Position(i,j))
  83.  
  84. self.dirty_tiles = self.tiles[:]
  85.  
  86. def cleanTileAtPosition(self, pos):
  87. """
  88. Mark the tile under the position POS as cleaned.
  89.  
  90. Assumes that POS represents a valid position inside this room.
  91.  
  92. pos: a Position
  93. """
  94. if pos in self.dirty_tiles:
  95. self.dirty_tiles.remove(pos)
  96.  
  97. def getDirtyTiles(self):
  98. """
  99. Returns a list of dirty tiles
  100. """
  101. d_tiles_list = []
  102. for tiles in self.dirty_tiles:
  103. # yield tiles.getX(), tiles.getY()
  104. d_tiles_list.append(str(tiles.getX())+","+str(tiles.getY()))
  105. return d_tiles_list
  106.  
  107. def isTileCleaned(self, pos):
  108. """
  109. Return True if the tile (m, n) has been cleaned.
  110.  
  111. Assumes that (m, n) represents a valid tile inside the room.
  112.  
  113. m: an integer
  114. n: an integer
  115. returns: True if (m, n) is cleaned, False otherwise
  116. """
  117. #is_clean = Position(m, n)
  118. return pos not in self.dirty_tiles
  119.  
  120. def getNumTiles(self):
  121. """
  122. Return the total number of tiles in the room.
  123.  
  124. returns: an integer
  125. """
  126. return len(self.tiles)
  127.  
  128. def getNumCleanedTiles(self):
  129. """
  130. Return the total number of clean tiles in the room.
  131.  
  132. returns: an integer
  133. """
  134. return len(self.tiles) - len(self.dirty_tiles)
  135.  
  136. def getRandomPosition(self):
  137. """
  138. Return a random position inside the room.
  139.  
  140. returns: a Position object.
  141. """
  142. return self.tiles[random.randint(0,len(self.tiles))]
  143.  
  144. def isPositionInRoom(self, pos):
  145. """
  146. Return True if pos is inside the room.
  147.  
  148. pos: a Position object.
  149. returns: True if pos is in the room, False otherwise.
  150. """
  151. # Check for position out of rectangle bounds
  152. if pos.getX() > self.width or pos.getY() > self.height:
  153. return False
  154. if pos.getX() < 0 or pos.getY() < 0:
  155. return False
  156. return True
  157.  
  158.  
  159. class Robot(object):
  160. """
  161. Represents a robot cleaning a particular room.
  162.  
  163. At all times the robot has a particular position and direction in the room.
  164. The robot also has a fixed speed.
  165.  
  166. Subclasses of Robot should provide movement strategies by implementing
  167. updatePositionAndClean(), which simulates a single time-step.
  168. """
  169. def __init__(self, room, speed, name="Little Jimmy"):
  170. """
  171. Initializes a Robot with the given speed in the specified room. The
  172. robot initially has a random direction and a random position in the
  173. room. The robot cleans the tile it is on.
  174.  
  175. room: a RectangularRoom object.
  176. speed: a float (speed > 0)
  177. """
  178. self.name = name
  179. self.room = room
  180. self.speed = speed
  181. self.position = Position(0,0)
  182. self.direction = 0
  183.  
  184. def getRobotPosition(self):
  185. """
  186. Return the position of the robot.
  187.  
  188. returns: a Position object giving the robot's position.
  189. """
  190. return self.position
  191.  
  192. def getRobotDirection(self):
  193. """
  194. Return the direction of the robot.
  195.  
  196. returns: an integer d giving the direction of the robot as an angle in
  197. degrees, 0 <= d < 360.
  198. """
  199. return self.direction
  200.  
  201. def setRobotPosition(self, position):
  202. """
  203. Set the position of the robot to POSITION.
  204.  
  205. position: a Position object.
  206. """
  207. self.position = position
  208.  
  209. def setRobotDirection(self, direction):
  210. """
  211. Set the direction of the robot to DIRECTION.
  212.  
  213. direction: integer representing an angle in degrees
  214. """
  215. self.direction = direction
  216.  
  217. def updatePositionAndClean(self):
  218. """
  219. Simulate the raise passage of a single time-step.
  220.  
  221. Move the robot to a new position and mark the tile it is on as having
  222. been cleaned.
  223. """
  224. while True:
  225. self.setRobotDirection(random.randint(0,359))
  226. self.position = self.position.getNewPosition(self.getRobotDirection,1)
  227.  
  228. current_tile = Position(x=float(self.position.getX()), y=float(self.position.getY()))
  229.  
  230. print "Full steam ahead", self.position.getX(), self.position.getX(), "@",self.direction
  231.  
  232. if self.room.isPositionInRoom(self.position):
  233. print self.name,"takes a skip and a hop."
  234. if not self.room.isTileCleaned(current_tile):
  235. self.room.cleanTileAtPosition(current_tile)
  236. print self.name,"pirouettes, unleashing a fury of dust."
  237. break
  238. print "Bump."
Advertisement
Add Comment
Please, Sign In to add comment