def test_robot_mov(room, robots, min_coverage): viz = pv.RobotVisualization(len(robots), room.width, room.height, delay=0.01) while (room.getNumCleanedTiles() / float(room.getNumTiles())) < min_coverage: for r in robots: r.updatePositionAndClean() viz.update(room, robots) viz.done()
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. StandardRobot or RandomWalkRobot) """ anim = ps7_visualize.RobotVisualization(num_robots, width, height) trial_ls = [] for t in range(num_trials): room = RectangularRoom(width, height) time = 0.0 robot_ls = {} for r in range(num_robots): robot_ls[r] = robot_type(room, speed) while float(room.getNumCleanedTiles()) / float( room.getNumTiles()) < min_coverage: anim.update(room, robot_ls.values()) for bot in robot_ls.values(): bot.updatePositionAndClean() time += 1.0 trial_ls.append(time) anim.done() return sum(trial_ls) / num_trials
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. StandardRobot or RandomWalkRobot) """ assert num_robots>0 and speed>0 and width>0 and height>0 and num_trials>0 \ and min_coverage<=1.0 and min_coverage>=0, "Wrong value of argument" counter = [0 for i in range(num_trials)] for num in range(num_trials): anim = ps7_visualize.RobotVisualization( num_robots, width, height, ) #VISUALIZE 1 room = RectangularRoom(width, height) #instantiation of RectangularRoom robots = [robot_type(room, speed) for i in range(num_robots)] while room.getNumCleanedTiles() / float( room.getNumTiles()) < min_coverage: for r in range(num_robots): anim.update(room, robots) #VISUALIZE 2 robots[r].updatePositionAndClean() counter[num] += 1 if room.getNumCleanedTiles() / float( room.getNumTiles()) == min_coverage: break anim.done() #VISUALIZE 3 total_moves = 0 for moves in counter: total_moves += moves mean_number = total_moves / float(num_trials) return mean_number
def runSimulation(num_robots, speed, width, height, min_coverage, num_trials, robot_type, animate=False, delay=0.2): """ 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. StandardRobot or RandomWalkRobot) """ stats = [] for current_run in range(num_trials): if animate: anim = ps7_visualize.RobotVisualization(num_robots, width, height, delay=delay) the_room = RectangularRoom(width, height) clean_needed = math.ceil(min_coverage * the_room.getNumTiles()) the_robots = [ robot_type(speed=speed, room=the_room) for _ in range(num_robots) ] count = 0.0 while the_room.getNumCleanedTiles() < clean_needed: for robot in the_robots: robot.updatePositionAndClean() if animate: anim.update(the_room, the_robots) count += 1 if animate: anim.done() stats.append(count) animate = False #print stats return sum(stats) / num_trials
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. StandardRobot or RandomWalkRobot) """ anim = ps7_visualize.RobotVisualization(num_robots, width, height) time_steps = [] for trials in range(num_trials): time_step = 0 # Initialize room room = RectangularRoom(width, height) #initialize robot given room and speed robots = [robot_type(room, speed) for robot in range(num_robots)] while room.getNumCleanedTiles() / float( room.getNumTiles()) < min_coverage: anim.update(room, robots) for robot in robots: robot.updatePositionAndClean() time_step += 1 if room.getNumCleanedTiles() / float(room.getNumTiles( )) == min_coverage: # in case of min_coverage = 100% break time_steps.append(time_step) anim.done() return sum(time_steps) / float(len(time_steps))
def singleSimulation(num_robots, speed, width, height, min_coverage, robot_type, visualize=False, sleep=0.2): room = RectangularRoom(width, height) robots = [robot_type(room, speed) for x in xrange(num_robots)] if True == visualize: anim = ps7_visualize.RobotVisualization(num_robots, width, height, sleep) steps = 0 while room.getNumCleanedTiles() < min_coverage * room.getNumTiles(): steps += 1 for r in robots: r.updatePositionAndClean() if True == visualize: anim.update(room, robots) if True == visualize: anim.done() return steps
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. StandardRobot or RandomWalkRobot) """ anim = True steps = 0 for _ in range(num_trials): robots = [] room = RectangularRoom(width, height) for _ in range(num_robots): robots.append(robot_type(room, speed)) total_tiles = float(room.getNumTiles()) if anim: anim = ps7_visualize.RobotVisualization(num_robots, width, height) while room.getNumCleanedTiles() / total_tiles < min_coverage: steps += 1 for r in robots: if anim: anim.update(room, robots) r.updatePositionAndClean() if anim: anim.done() return steps / float(num_trials)