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) """ # If you want to disable animation, please comment out line below. anim = ps3_visualize.RobotVisualization(num_robots, width, height, False, delay=0.5) avrg_num_of_steps = [ ] # average number of steps for trial will be stored in this list. for index in range(num_trials): room = EmptyRoom( width, height, dirt_amount) # by default we will simulate EmptyRoom type. amt_of_tiles = room.get_num_tiles( ) # storing amount of tiles in the room. cleaned_tiles = room.get_num_cleaned_tiles( ) # storing amount of cleaned tiles in the room. # list that stores all the robots in the simulation - number of robots is described by num_robots variable. my_robots = [] for i in range(1, num_robots + 1): my_robots.append(robot_type(room, speed, capacity)) num_of_steps = 0 # initializing counter of steps. while True: # when the goal is reached (min_coverage) we can break and finish current trial. if cleaned_tiles / amt_of_tiles >= min_coverage: break else: for robot in my_robots: robot.update_position_and_clean( ) # taking cleaning action (step). cleaned_tiles = room.get_num_cleaned_tiles( ) # updating number of cleaned tiles in the room. # updating animation, please comment out line below if animation was disabled. anim.update(room, my_robots) num_of_steps += 1 # incrementing number of steps that out robot/robots took. avrg_num_of_steps.append(num_of_steps) # closing animation, please comment this line out if animation was disabled at the beginning. anim.done() return sum(avrg_num_of_steps) / len(avrg_num_of_steps)
def animation(room, robots, min_coverage): anim = ps3_visualize.RobotVisualization( len(robots), room.width, room.height, False, 0.05 ) time_steps = 0 while room.get_num_cleaned_tiles() / room.get_num_tiles() < min_coverage: for robot in robots: robot.update_position_and_clean() anim.update(room, robots) time_steps += 1 anim.done() return time_steps
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) """ room = EmptyRoom(width, height, dirt_amount) robots = {} for i in range(1, num_robots + 1): robots["robot", str(i)] = robot_type(room, speed, capacity) is_furnished = None if type(room) == FurnishedRoom: is_furnished = True elif type(room) == EmptyRoom: is_furnished = False trials = [] for trial in range(0, num_trials): anim = ps3_visualize.RobotVisualization(num_robots, width, height, is_furnished) coverage = 0 steps_per_trial = 0 while coverage < min_coverage: steps_per_trial += 1 for robot in robots.keys(): robots[robot].update_position_and_clean() anim.update(room, robots.values()) coverage = room.get_num_cleaned_tiles() / room.get_num_tiles() anim.done() trials.append(steps_per_trial) room.dirty_room(dirt_amount) sum_of_all_steps = 0 for trial in trials: sum_of_all_steps += trial mean = round(sum_of_all_steps / len(trials)) return mean
def run_simulation(num_robots, speed, capacity, width, height, dirt_amount, min_coverage, num_trials, robot_type, anim=False): """ 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) """ time_steps = 0 for i in range(num_trials): coverage = 0 room = EmptyRoom(width, height, dirt_amount) robots = [robot_type(room, speed, capacity) for i in range(num_robots)] if anim: anim = ps3_visualize.RobotVisualization(num_robots, width, height, []) while coverage < min_coverage: for robot in robots: robot.update_position_and_clean() if anim: anim.update(room, robots) time_steps += 1 coverage = float(room.get_num_cleaned_tiles()) / \ room.get_num_tiles() return time_steps / num_trials
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) """ trialSteps = [] for _ in range(num_trials): room = EmptyRoom(width, height, dirt_amount) # isFurnished = type(room) != "<class '__main__.EmptyRoom'>" # print(type(room)) # print(isFurnished) anim = ps3_visualize.RobotVisualization(num_robots, width, height, False, 0.2) robots = [] for _ in range(num_robots): robots.append(robot_type(room, speed, capacity)) steps = 0 while True: roomTile = room.get_num_tiles() cleanTile = room.get_num_cleaned_tiles() if cleanTile/roomTile >= min_coverage: break else: for rob in robots: rob.update_position_and_clean() anim.update(room, robots) steps += 1 trialSteps.append(steps) anim.done() avgSteps = numpy.mean(trialSteps) return avgSteps
def test_robot_movement(robot_type, room_type): # check if room is furnished room is_furnished = str(room_type).find('FurnishedRoom') > 0 room = room_type(5, 5, 4) if is_furnished: room.add_furniture_to_room() robots = [robot_type(room, 1, 1)] coverage = 0 time_steps = 0 min_coverage = 1.0 if is_furnished: anim = ps3_visualize.RobotVisualization(1, 5, 5, room.furniture_tiles) else: anim = ps3_visualize.RobotVisualization(1, 5, 5, []) while coverage < min_coverage: time_steps += 1 for robot in robots: robot.update_position_and_clean() anim.update(room, robots) coverage = float(room.get_num_cleaned_tiles())/room.get_num_tiles() anim.done()
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) """ #raise NotImplementedError time_steps = 0 for n in range(num_trials): anim = ps3_visualize.RobotVisualization(num_robots, width, height, False, 0.05) room = EmptyRoom(width, height, dirt_amount) robots = [] for r in range(num_robots): robots.append(robot_type(room, speed, capacity)) fraction_cleaned = room.get_num_cleaned_tiles() / room.get_num_tiles() while fraction_cleaned < min_coverage: time_steps += 1 for r in range(num_robots): robots[r].update_position_and_clean() anim.update(room, robots) fraction_cleaned = room.get_num_cleaned_tiles( ) / room.get_num_tiles() anim.done() return time_steps / num_trials
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) """ avrg_num_of_steps = [] for index in range(num_trials): room = FurnishedRoom(width, height, dirt_amount) room.add_furniture_to_room() amt_of_tiles = room.get_num_tiles() cleaned_tiles = room.get_num_cleaned_tiles() anim = ps3_visualize.RobotVisualization(num_robots, width, height, True, delay=0.3) my_robots = [] for i in range(1, num_robots + 1): my_robots.append(robot_type(room, speed, capacity)) num_of_steps = 0 while True: if cleaned_tiles / amt_of_tiles >= min_coverage: break else: for robot in my_robots: robot.update_position_and_clean() cleaned_tiles = room.get_num_cleaned_tiles() anim.update(room, my_robots) num_of_steps += 1 avrg_num_of_steps.append(num_of_steps) anim.done() return sum(avrg_num_of_steps) / len(avrg_num_of_steps)
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) """ #array to contain time step count for each simulation run sims = [] for i in range(num_trials): anim = ps3_visualize.RobotVisualization(num_robots, width, height, False, 0.02) #reset time steps, room, and robots for each simulation time_steps = 0 room = EmptyRoom(width, height, dirt_amount) robots = [] for i in range(num_robots): robots.append(robot_type(room, speed, capacity)) #run cleaning method for each robot until we reach min coverage while room.get_num_cleaned_tiles() / room.get_num_tiles( ) < min_coverage: time_steps += 1 for robot in robots: robot.update_position_and_clean() anim.update(room, robots) #add the time steps to array sims.append(time_steps) anim.done() return numpy.mean(sims)
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) """ robot_workforce = [] room = EmptyRoom(width, height, dirt_amount) time_step = 0 average = 0 for trail in range(num_trials): anim = ps3_visualize.RobotVisualization(num_robots, width, height, False, 0.1) #Starts the animation for robot in range(num_robots): robot_workforce += [robot_type(room, speed, capacity)] while room.get_num_cleaned_tiles() / room.get_num_tiles( ) < min_coverage: for robot in robot_workforce: robot.update_position_and_clean() time_step += 1 anim.update(room, robot_workforce) anim.done() average += time_step time_step = 0 robot_workforce = [] room = EmptyRoom(width, height, dirt_amount) return average / num_trials