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) """ sum = 0 #init counter for e in range(0, num_trials): #for each trial ticks = 0 #print(e) room = RectangularRoom(width, height) #create a room target = room.getNumTiles() * min_coverage #set the target #print ("target=",target) bots = [] #create a container for the number of robots #trial_average=0 #create some robots for r in range( 0, num_robots): #create the correct number and type of robots. if robot_type == StandardRobot: bots.append(StandardRobot(room, speed)) elif robot_type == RandomWalkRobot: bots.append(RandomWalkRobot(room, speed)) #print("bots created=",len(bots)) #print("bots[]=",bots) #print("running trial number",e) ################################################################## anim = ps6_visualize.RobotVisualization(num_robots, width, height, 0) # ################################################################## while room.getNumCleanedTiles( ) < target: #until the room is clean enough ticks += 1 #count the number of ticks for m in range(0, len(bots)): #move each bot bots[r].updatePositionAndClean() ######################## anim.update(room, bots) # ######################## sum += ticks ############# anim.done() # ############# return sum / 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. Robot or RandomWalkRobot) """ totalTime = 0 for i in range(num_trials): robots = [] room = RectangularRoom(width, height) for n in range(num_robots): robots.append(robot_type(room, speed)) currentTime = 0 anim = ps6_visualize.RobotVisualization(len(robots), width, height) while room.getNumCleanedTiles() / room.getNumTiles() < min_coverage: currentTime += 1 for robot in robots: print('Trial No: ' + str(i + 1)) print( str(robot) + " is at position " + str(robot.getRobotPosition())) robot.updatePositionAndClean() anim.update(room, robots) print( str(robot) + " is at position " + str(robot.getRobotPosition())) print('Number of cleaned tiles: ' + str(room.getNumCleanedTiles()) + ' out of ' + str(room.getNumTiles())) print('Cleaned Tiles: ' + str(robot.getRoom().cleanedTiles)) totalTime += currentTime meanTime = totalTime / num_trials anim.done() return meanTime
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) """ room = RectangularRoom(width, height) robotlist = [] for i in range(num_robots): robotlist.append(robot_type(room, speed)) # print robotlist mean = 0 for i in range(num_trials): print '------------------------------------------------------------------' print 'Moving On' print '------------------------------------------------------------------' anim = ps6_visualize.RobotVisualization(num_robots, width, height) room.initializekey('') while float( room.getNumCleanedTiles()) / room.getNumTiles() < min_coverage: for robot in robotlist: robot.updatePositionAndClean() print 'room tiles: ' + str(robot.room.getNumTiles()) print 'current ratio: ' + str( float(robot.room.getNumCleanedTiles()) / robot.room.getNumTiles()) print 'cleanedtiles: ' + str(robot.room.getNumCleanedTiles()) print 'nummoves: ' + str(robot.getnummoves()) print 'direction: ' + str(robot.direction) anim.update(room, robotlist) for robot in robotlist: mean += float(robot.getnummoves()) / num_robots / num_trials robot.setnummoves(0) print 'mean: ' + str(mean) print '------------------------------------------------------------------' print 'Next Robot' print '------------------------------------------------------------------' anim.done() print 'Robot(s) took on average ' + str(mean) + \ ' clock ticks to clean ' + str(min_coverage) + ' of the ' + str(width) +'x' + str(height) + ' room.' return int(mean)
def testRobotMovement(robot_type, room_type, delay=0.4): """ Runs a simulation of a single robot of type robot_type in a 5x5 room. """ room = room_type(5, 5) robot = robot_type(room, 1) duck = Duck(room, 1) anim = ps6_visualize.RobotVisualization(1, 5, 5, delay) while room.getNumCleanedTiles() < room.getNumTiles(): robot.updatePositionAndClean() duck.updatePosition() anim.update(room, [robot], [duck]) anim.done()
def runSimulationVisual(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) """ totalStep = 0 room = RectangularRoom(width, height) tilesNeedClean = int(room.getNumTiles() * min_coverage) for i in range(num_trials): room.ResetRoom() timeStep = 0 tilesCleaned = 0 robotList = [] for i in range(num_robots): robotList.append(robot_type(room, speed)) anim = ps6_visualize.RobotVisualization(num_robots, width, height) while tilesCleaned < tilesNeedClean: for robot in robotList: robot.updatePositionAndClean() tilesCleaned = room.getNumCleanedTiles() # print tilesCleaned timeStep += 1 anim.update(room, robotList) anim.done() totalStep += timeStep return float(totalStep) / 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. Robot or RandomWalkRobot) """ visualize = False time_steps_trials = [] for trial in xrange(num_trials): if visualize: anim = ps6_visualize.RobotVisualization(num_robots, width, height) trial_room = RectangularRoom(width, height) bots = [robot_type(trial_room, speed) for i in xrange(num_robots)] if visualize: anim.update(trial_room, bots) time_steps = 0.0 while trial_room.cleanRatio() < min_coverage: for bot in bots: bot.updatePositionAndClean() time_steps += 1 if visualize: anim.update(trial_room, bots) if visualize: anim.done() time_steps_trials.append(time_steps) mean_time_steps_trials = sum(time_steps_trials) / len(time_steps_trials) if visualize: print "mean clock time of " + str(mean_time_steps_trials) + " steps" return mean_time_steps_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. Robot or RandomWalkRobot) """ def initsRobots(number, room, roboSpeed, roboType): robos = [] for i in range(number): initRobo = roboType(room, roboSpeed) robos.append(initRobo) return robos def allRobotsUpdateAndClean(roboArray): for robo in roboArray: robo.updatePositionAndClean() #raise NotImplementedError totalCoverageEachTrial = 0 for i in range(num_trials): room = RectangularRoom(width, height) robos = initsRobots(num_robots, room, speed, robot_type) totalCoverage = 0 anim = ps6_visualize.RobotVisualization(num_robots, width, height) while totalCoverage < min_coverage: allRobotsUpdateAndClean() anim.update(room, robos) totalCoverage = (room.getNumCleanedTiles() / room.getNumTiles()) * 100 totalCoverageEachTrial += totalCoverage anim.done()
def runSimulation(num_robots, speed, width, height, min_coverage, num_trials, robot_type): anim = ps6_visualize.RobotVisualization(num_robots, width, height) time_steps = 0 for x in range(num_trials): time_step = 0 room = RectangularRoom(width, height) robots = [] min_tiles = int(room.getNumTiles() * min_coverage) for y in range(num_robots): robot = robot_type(room, speed) robots.append(robot) while room.getNumCleanedTiles() < min_tiles: anim.update(room, robots) for w in robots: w.updatePositionAndClean() time_step += 1 time_steps += time_step anim.done() return time_steps / 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. Robot or RandomWalkRobot) """ visualize = False total_time_steps = 0.0 for trial in range(num_trials): if visualize: anim = ps6_visualize.RobotVisualization(num_robots, width, height) room = RectangularRoom(width, height) robotCollection = [] for i in range(num_robots): robotCollection.append(robot_type(room, speed)) if visualize: anim.update(room, robotCollection) while (room.getNumCleanedTiles() / float(room.getNumTiles())) < min_coverage: for robot in robotCollection: robot.updatePositionAndClean() total_time_steps += 1 if visualize: anim.update(room, robotCollection) if visualize: anim.done() print(trial) return total_time_steps / num_trials
def run_trial(num_robots, width, height, min_coverage, robot_type, speed, use_tk): """a single trial of simulation return number of clock ticks to clean > min_coverage """ # instantiate a room room = RectangularRoom(width, height) # print(room) # instantiate robot list robots = [] for i in range(num_robots): robot = robot_type(room, speed) # print("created a robot:") # print(robot) robots.append(robot) # count the clock ticks count = 0 if use_tk: # init the visualization anim = ps6_visualize.RobotVisualization(num_robots, width, height, delay=.01) # terminate when room is clean while not room_cleaned(room, min_coverage): # print("clock tick>>>") count += 1 # clean the room for robot in robots: # print(robot) robot.updatePositionAndClean() if use_tk: # update the tk window after delay anim.update(room, robots) if use_tk: # stop tk anim.done() return count
def checkPosition(rooms): for pos, cleaned in rooms.roomTiles.iteritems(): print("Position: x=", str(pos.getX()), ", y=", str(pos.getY()), ", isCleaned = ", str(cleaned)) def getCleanedTileLocation(rooms): for pos, cleaned in rooms.roomTiles.iteritems(): if cleaned: print("Position: x=", str(pos.getX()), ", y=", str(pos.getY()), ", isCleaned = ", str(cleaned)) ##checkPosition(room) ##robo = Robot(room, speed) ##robo = StandardRobot(room, speed) ##pos = robo.getRobotPosition() ##direction = robo.getRobotDirection() ##print("Now Robo Position: x=",pos.getX(),', y=',pos.getY(),', direction= ',str(direction)) ##print(room.isPositionInRoom(pos)) ##robo.updatePositionAndClean() ##getCleanedTileLocation(room) #runSimulation(10, 1.0, 15, 20, 0.8, 30, StandardRobot) import ps6_visualize ps6_visualize.RobotVisualization(10, 15, 20, 0.2)