コード例 #1
0
ファイル: ps2.py プロジェクト: ronger4242/MITx-6.00.2x
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)
    """
    total_steps = 0
    for t in range(num_trials):
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        room = RectangularRoom(width, height)
        robots = []
        for i in range(num_robots):
            robots.append(robot_type(room, speed))
        while room.getNumCleanedTiles() / room.getNumTiles() < min_coverage:
            anim.update(room, robots)
            for robot in robots:
                robot.updatePositionAndClean()
            total_steps += 1  #why not inside for loop. if thr r 2 robots, update these 2, and step is counted as 1.
            anim.done()
    return total_steps / num_trials
コード例 #2
0
ファイル: ps2.py プロジェクト: aandres99/mit_6002x
    def clean_room():
        # place room inside clean room in order to reset
        if animate:
            anim = ps2_visualize.RobotVisualization(num_robots, width, height)

        room = RectangularRoom(width, height)
        min_coverage_tiles = int(min_coverage * room.getNumTiles())

        # init robot list
        robots = []
        for i in range(num_robots):
            robots.append(i)

        for i in range(len(robots)):
            robots[i] = robot_type(room, speed)

        if len(robots) == 1:
            clock_ticks = 1
        else:
            clock_ticks = 0

        while room.getNumCleanedTiles() < min_coverage_tiles:
            for robot in robots:
                robot.updatePositionAndClean()
            clock_ticks += 1
            if animate:
                anim.update(room, robots)
        if animate:
            anim.done()
        return clock_ticks
コード例 #3
0
ファイル: ps2.py プロジェクト: ryan-neal/MIT_6002
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)
    """
    robot_counter = []
    for j in range(num_trials):
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        rect = RectangularRoom(width, height)
        robots = []
        for i in range(num_robots):
            robot = robot_type(rect, speed)
            robots.append(robot)
        counter = 0
        while rect.getNumCleanedTiles() / rect.getNumTiles() < min_coverage:
            anim.update(rect, robots)
            for i in robots:
                i.updatePositionAndClean()
            counter += 1
        robot_counter.append(counter)
        anim.done()
    return sum(robot_counter) / num_trials
コード例 #4
0
ファイル: ps2.py プロジェクト: yash-saini275/CleaningRobot
def runSimulationAnimation(num_robots,
                           speed,
                           width,
                           height,
                           min_coverage,
                           num_trials,
                           robot_type,
                           animate=False):
    total_steps = 0
    for __ in range(num_trials):
        if animate:
            anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        room = RectangularRoom(width, height)
        robots = []
        for r in range(num_robots):
            robots.append(robot_type(room, speed))
        while room.getNumCleanedTiles() / float(
                room.getNumTiles()) < min_coverage:
            if animate:
                anim.update(room, robots)
            for robot in robots:
                robot.updatePositionAndClean()
            total_steps += 1
        if animate:
            anim.update(room, robots)
            anim.done()
    return total_steps / float(num_trials)
コード例 #5
0
 def oneSimulation():  
     
     testedRoom = RectangularRoom(width, height)
     listOfRobots = []
     for i in range(num_robots):
         listOfRobots.append(robot_type(testedRoom, speed))
         
     numOfTimeSteps = 0
     
     
     #visualisation of simulation
     if visualizeSimulation:
         anim = ps2_visualize.RobotVisualization(num_robots, width, height, delay = 0.1)
         anim.update(testedRoom, listOfRobots)
     
     while True:
         cleanT = testedRoom.getNumCleanedTiles()
         totalT = testedRoom.getNumTiles()
         if cleanT / totalT >= min_coverage:
             #visualisation of the simulation
             if visualizeSimulation:
                 anim.done()
             
             
             return numOfTimeSteps
         for i in range(num_robots):                
             listOfRobots[i].updatePositionAndClean()
             
         #visualisation of the simulation
         if visualizeSimulation:
             anim.update(testedRoom, listOfRobots)
         numOfTimeSteps += 1
コード例 #6
0
ファイル: ps2.py プロジェクト: yingpublic/iRobot
def runSimulation(num_robots, speed, width, height, min_coverage, num_trials,
                  robot_type):

    WorkDoneList = numpy.zeros(shape=(num_trials))
    anim = ps2_visualize.RobotVisualization(num_robots, width, height)
    for i in range(num_trials):
        cycle = 0
        # for only one robot
        # robot = StandardRobot(RectangularRoom(width,height), speed)
        room = RectangularRoom(width, height)
        robots = [robot_type(room, speed) for r in range(num_robots)]
        while robots[0].room.getNumCleanedTiles() / room.getNumTiles(
        ) < min_coverage:
            anim.update(room, robots)
            for j in range(num_robots):
                robots[j].updatePositionAndClean()
                # Just for testing
                # print(j)
                # print(robots[j].room.getNumCleanedTiles())
            cycle += 1
        WorkDoneList[i] = cycle
        anim.done()
    # print(WorkDoneList)
    # print(numpy.mean(WorkDoneList))
    return numpy.mean(WorkDoneList)
コード例 #7
0
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)
    """
    time_taken_all = []
    for attempt_num in range(num_trials):
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        room = RectangularRoom(width, height)
        robots = [robot_type(room, speed) for robot in range(num_robots)]
        time_taken = 0
        while (room.getNumCleanedTiles() / room.getNumTiles()) < min_coverage:
            time_taken += 1
            for robot in robots:
                anim.update(room, robots)
                robot.updatePositionAndClean()
        time_taken_all.append(time_taken)
    anim.done()

    #print(time_taken_all)
    return (sum(time_taken_all) / len(time_taken_all))
コード例 #8
0
ファイル: 6.00.2x-pset2.py プロジェクト: thiagork/MIT-6.00.2x
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)
    anim = ps2_visualize.RobotVisualization(1, 5, 5, delay)
    while room.getNumCleanedTiles() / room.getNumTiles() < 1:
        robot.updatePositionAndClean()
        anim.update(room, [robot])
    anim.done()
コード例 #9
0
def testRobotMovement(robot_type, room_type, delay=0.4):
    """
    Runs a simulation of a single robot of type robot_type in a 5x5 room.
    """
    import ps2_visualize
    room = room_type(5, 5)
    robot = robot_type(room, 1)
    anim = ps2_visualize.RobotVisualization(1, 5, 5, delay)
    while ## the room is not cleaned ##
        ## move the robot ##
        anim.update(room, [robot])
    anim.done()
コード例 #10
0
ファイル: ps2.py プロジェクト: serarm/edx_mitx
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)
    """
    #raise NotImplementedError
    #1. define Room

    steps = [0] * num_trials
    viz=False
    for trial in range(num_trials):
        #Inserting for visualisation
        if viz:
            anim = ps2_visualize.RobotVisualization(num_robots, width, height)

        room = RectangularRoom(width, height)
        robots = []
        #Initialise robots
        for i in range(num_robots):
            robots.append(robot_type(room, speed))
        if not room.getNumCleanedTiles()/room.getNumCleanedTiles() < min_coverage:
            #Make first move for each robot
            for robot in robots:
                robot.updatePositionAndClean()
            steps[trial]+=1
            #Inserting before making first move

            while room.getNumCleanedTiles()/room.getNumTiles() < min_coverage:
                if viz:
                    anim.update(room, robots)
                for robot in robots:
                    robot.updatePositionAndClean()
                steps[trial] += 1
       #completion of trial
            if viz:
                anim.done()


    return sum(steps)/num_trials
コード例 #11
0
ファイル: ps2.py プロジェクト: resolutedreamer/XSeriesMITx
def runSimulation(num_robots, speed, width, height, min_coverage, num_trials,
                  robot_type):
    """
    Runs NUM_TRIALS trials of the simulation and returns the mean numxber 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, ni 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)
    """
    trial_durations = []
    sum = 0
    for trial in range(num_trials):
        # animation code
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        # prepare for a fresh simulation
        robot_list = []
        simulationRoom = RectangularRoom(width, height)
        total_tiles = float(width * height)
        cleaned_tiles = 0
        cleaned_fraction = 0.0
        time_trial_took = 0
        for makerobots in range(num_robots):
            robot_list.append(robot_type(simulationRoom,speed))
            cleaned_tiles += 1
        #for i in range(num_robots):
        #    print "Robot ", i+1
        # actually start the simulation
        while (cleaned_fraction < min_coverage):
            time_trial_took += 1
            anim.update(simulationRoom, robot_list)
            for eachrobot in robot_list:
                eachrobot.updatePositionAndClean()
            # check if it's time to end the simulation
            cleaned_fraction = float(simulationRoom.getNumCleanedTiles())/total_tiles
            if (cleaned_fraction >= min_coverage):
                trial_durations.append(time_trial_took)
            else:
                continue
        anim.done()
    for time_taken in trial_durations:
        sum += time_taken
    average = float(sum)/float(len(trial_durations))
    return average
コード例 #12
0
def runTrial(num_robots, speed, width, height, min_coverage, robot_type):
    anim = ps2_visualize.RobotVisualization(num_robots, width, height)
    room = RectangularRoom(width, height)
    robots = [robot_type(room, speed) for _ in range(num_robots)]
    steps = 0
    tileCount = float(room.getNumTiles())
    while room.getNumCleanedTiles() / tileCount < min_coverage:
        steps = steps + 1
        for robot in robots:
            anim.update(room, robots)
            robot.updatePositionAndClean()
    anim.done()
    return steps
コード例 #13
0
def testRobotMovement(robot_type, room_type, delay=0.4):
    """
    Runs a simulation of a single robot of type robot_type in a 5x5 room.
    """
    angular_speed = 30 / 180.0 * math.pi
    room = room_type(20, 20)
    robot = robot_type(room, 1.0, angular_speed)
    anim = ps2_visualize.RobotVisualization(1, 10, 10, delay)
    while room.getNumCleanedTiles() < room.getNumTiles():
        robot.updatePositionAndClean()
        anim.update(room, [robot])

    anim.done()
コード例 #14
0
ファイル: ps2.py プロジェクト: parmarmanojkumar/MITx_Python
def runSimulation(num_robots,
                  speed,
                  width,
                  height,
                  min_coverage,
                  num_trials,
                  robot_type,
                  animation=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
    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)
    animation: a boolean to indicate animation of robots is enabled
    """
    totalTimeStep = 0  # Variable to accumulate no. of time stamps
    for i in range(num_trials):
        # To enable animation of robot movements
        if animation:
            roboMoves = ps2_visualize.RobotVisualization(
                num_robots, width, height, 0.01)
        # Initialize room for a given trial
        targetRoom = RectangularRoom(width, height)
        # Create robots based on type of it
        testRobots = [robot_type(targetRoom, speed) for i in range(num_robots)]
        # Run the simulations till minimum coverage criteria is met
        while (min_coverage * targetRoom.getNumTiles() >
               targetRoom.getNumCleanedTiles()):
            # Change position of all robots
            for robot in testRobots:
                robot.updatePositionAndClean()
            # Increment total time step counter
            totalTimeStep += 1
            # To provide movements of robots in animation when enabled
            if animation:
                roboMoves.update(targetRoom, testRobots)
        # Close the animation for given trial
        if animation:
            roboMoves.done()
    # Return mean value of steps in given experiment
    return float(totalTimeStep / num_trials)
コード例 #15
0
def visualization():
    width = 10
    height = 20
    room = RectangularRoom(width,height)
    robots = [RandomWalkRobot(room, 1.0), RandomWalkRobot(room, 1.0), StandardRobot(room, 1.0), StandardRobot(room, 1.0)]
    num_robots = len(robots)
    
    anim = ps2_visualize.RobotVisualization(num_robots, width, height)
    #anim.update(room, robots)
    while room.getNumTiles() != room.getNumCleanedTiles():
        for robot in robots:
            robot.updatePositionAndClean() 
        anim.update(room, robots)                
    anim.done()
コード例 #16
0
def runSimulation(num_robots,
                  speed,
                  width,
                  height,
                  min_coverage,
                  num_trials,
                  robot_type,
                  animated=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
    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 int(num_robots) > 0, "機器人最少一隻"
    assert float(speed) > 0, "速度要大於零"
    assert int(width) > 0, "長寬要大於零"
    assert int(height) > 0, "長寬要大於零"
    assert 0 <= float(min_coverage) <= 1, "掃地覆蓋面積大於等於0, 小於等於1"
    assert num_trials > 0, "嘗試次數要大於零"
    if animated:
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
    total_steps = 0
    for trial in range(num_trials):
        room = RectangularRoom(width, height)
        Robots = []
        for num in range(num_robots):
            Robots.append(robot_type(room, speed))
        while room.getNumCleanedTiles() / float(
                room.getNumTiles()) < min_coverage:
            if animated:
                anim.update(room, Robots)
            for robot in Robots:
                robot.updatePositionAndClean()
            total_steps += 1
        if animated:
            anim.update(room, Robots)
            anim.done()
    return total_steps / int(num_trials)
コード例 #17
0
def RunRobots(robot_list, room, min_coverage, num_robots, width, height):

    anim = ps2_visualize.RobotVisualization(num_robots, width, height)

    count_steps = 0

    temp_robots = robot_list.copy()

    while room.getNumCleanedTiles() / room.getNumTiles() < min_coverage:
        for i in range(len(temp_robots)):
            anim.update(room, temp_robots)
            temp_robots[i].updatePositionAndClean()
        count_steps += 1
    anim.done()
    return count_steps
コード例 #18
0
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)
    """
    # Create total steps counter
    totalSteps = 0

    # Run all trials
    for i in range(num_trials):

        # Create num_amount of robots
        LoR = []
        recRoom = RectangularRoom(width, height)
        for i in range(num_robots):
            LoR.append(robot_type(recRoom, speed))

        isMin = False

        # Run each trial
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        while not isMin:
            totalSteps += 1
            for i in range(num_robots):
                # Have each robot clean
                anim.update(recRoom, LoR)
                LoR[i].updatePositionAndClean()

                # Check if min_coverage has been reached
                if (recRoom.getNumCleanedTiles() /
                        recRoom.getNumTiles()) >= min_coverage:
                    isMin = True
                    break
        anim.done()
    return totalSteps / num_trials
コード例 #19
0
ファイル: ps2.py プロジェクト: Kalwing/job-ready-project
def runSimulation(num_robots,
                  speed,
                  width,
                  height,
                  min_coverage,
                  num_trials,
                  robot_type,
                  visualize=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
    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)
    """
    total_time = 0
    for i in range(num_trials):
        # Initialize the space
        room = RectangularRoom(width=width, height=height)

        robots = []
        for i in range(num_robots):
            robots.append(robot_type(room, speed))
        if visualize:
            anim = ps2_visualize.RobotVisualization(num_robots, width, height)

        # Clean it
        while (room.getNumCleanedTiles() / room.getNumTiles()) < min_coverage:
            if visualize:
                anim.update(room, robots)

            total_time += 1
            for r in range(num_robots):
                robots[r].updatePositionAndClean()

        if visualize:
            anim.done()

    return total_time / num_trials
コード例 #20
0
ファイル: ps2.py プロジェクト: ADeeley/mit_work
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)
    """
    trial_results = []

    for trial in range(num_trials):
        room = RectangularRoom(width, height)

        # Create a list of robots
        robots = []
        for robot in range(num_robots):
            robots.append(robot_type(room, speed))

        percentage_cleaned = 0
        clock_tick = 0
        # remove---
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        #----------
        while True:
            percentage_cleaned = (room.getNumCleanedTiles() /
                                  room.getNumTiles())

            if percentage_cleaned >= min_coverage:
                break
            else:
                anim.update(room, robots)  # remove
                clock_tick += 1
                for robot in robots:
                    robot.updatePositionAndClean()
        trial_results.append(clock_tick)
        anim.done()  # remove
    mean_cleaning_time = round(sum(trial_results) / num_trials, 2)
    return mean_cleaning_time
コード例 #21
0
ファイル: ps2.py プロジェクト: mai1346/600.2x
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 = ps2_visualize.RobotVisualization(num_robots, width, height)
    clocklist = []  # a list used to store the clock for different trials
    for i in range(
            num_trials):  #perform num times trials and get the average clock
        room = RectangularRoom(width,
                               height)  #initialize the room need to clean
        targetCoverage = math.ceil(
            min_coverage *
            room.getNumTiles())  #target of percentage to be cleaned
        robotlist = [
            robot_type(room, speed) for _ in enumerate(range(num_robots))
        ]  #make a list to store the robots
        j = 0  #count for clock
        #        room.tilecleaned=[]#reset the room to dirty
        while room.getNumCleanedTiles() < targetCoverage:
            anim.update(room, robotlist)
            for robot in robotlist:
                robot.updatePositionAndClean(
                )  #robots clean the room at the same time for once
            j += 1  #clock increase by one
        clocklist.append(
            j)  #end cleaning up the room and store the clock to clcok list
#    print(clocklist)
    anim.done()
    mean = sum(clocklist) / num_trials  #calculate the mean
    return mean
コード例 #22
0
def testRobotMovement(robot_types, room, speed, delay):
    """
    Runs a simulation of a single robot of type robot_type in a RectangularRoom.
    
    robot_types: a dict, mapping a robot class to the number of that kind of robot to be tested
    """
    
    robots = []
    for robot_type in robot_types:
        for i in range(0, robot_types[robot_type]):
            robots.append(robot_type(room, speed))
    
    anim = ps2_visualize.RobotVisualization(len(robots), room.width, room.height, delay)
    while room.getNumCleanedTiles() < room.getNumTiles():
        for robot in robots:
            robot.updatePositionAndClean()
        anim.update(room, robots)
    anim.done()
コード例 #23
0
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 = ps2_visualize.RobotVisualization(num_robots, width, height, 0.1)
    result = 0
    if math.ceil(width * height * min_coverage) == 0:
        return 0
    for i in range(num_trials):
        room = RectangularRoom(width, height)
        area = room.getNumTiles()
        goal = math.ceil(area * min_coverage)
        robots = []
        for j in range(num_robots):
            robots.append(robot_type(room, speed))
        steps = 0
        x = 0
        if room.getNumCleanedTiles() >= goal:
            return 0
        while x == 0:
            for k in robots:
                k.updatePositionAndClean()
                if room.getNumCleanedTiles() >= goal:
                    result += steps
                    x = 1
                    break
            anim.update(room, robots)
            steps += 1
    anim.done()
    return (result / num_trials) + 1
コード例 #24
0
ファイル: ps2.py プロジェクト: victoriamreese/MIT-6.00.2x
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 = ps2_visualize.RobotVisualization(num_robots, width, height)
    results = []
    for trial in range(num_trials):
        num_steps = 0
        #since we invoke methods from the RectangularRoom method later in this func,
        #we need to instantiate the room with that class and call the class through the instance for the rest of the func
        room = RectangularRoom(width, height)
        #must instantiate the robots
        robots = [robot_type(room, speed) for j in range(num_robots)]
        #this while loop runs for the num of trials passed as param
        #one while to completion = 1 trial
        #adds a step to counter and cleans a square for each robot then checks if
        #enough squares are clean
        #continues until there are enough clean squares
        while (room.getNumCleanedTiles() / room.getNumTiles()) < min_coverage:
            num_steps += 1
            anim.update(room, robots)
            for k in robots:
                k.updatePositionAndClean()
            if (room.getNumCleanedTiles() /
                    room.getNumTiles()) >= min_coverage:
                results.append(num_steps)
                anim.done
    return sum(results) / len(results)
コード例 #25
0
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)
    """

    timeSteps = []

    for j in range(num_trials):
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        room = RectangularRoom(width, height)
        robotList = []
        for i in range(num_robots):
            robotList.append(robot_type(room, speed))
        for i in range(num_robots):
            room.cleanTileAtPosition(robotList[i].getRobotPosition())
        tempTime = 0
        while float(
                room.getNumCleanedTiles()) / room.getNumTiles() < min_coverage:
            for i in range(num_robots):
                anim.update(room, robotList)
                robotList[i].updatePositionAndClean()
                room.cleanTileAtPosition(robotList[i].getRobotPosition())
            tempTime += 1
        timeSteps.append(tempTime)
        anim.done()

    mean = float(sum(timeSteps)) / len(timeSteps)
    return mean
コード例 #26
0
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)
    """
    #raise NotImplementedError
    # do this in a while loop, but has to be in for loop of num_trials:
    # and instantiate a container to store the results
    results = []
    for i in range(num_trials):
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        num_steps = 0
        # Instantiate a new room
        room = RectangularRoom(width, height)
        # Instantiate the robots
        robots = [robot_type(room, speed) for j in range(num_robots)]
        while (room.getNumCleanedTiles() / room.getNumTiles()) < min_coverage:
            num_steps += 1
            anim.update(room, robots)
            for k in robots:
                k.updatePositionAndClean()
            if (room.getNumCleanedTiles() /
                    room.getNumTiles()) >= min_coverage:
                results.append(num_steps)
                anim.done()
            else:
                continue
    # return mean
    return sum(results) / len(results)
コード例 #27
0
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)
    """
    robots, trials = [], []
    for i in range(num_trials):
        anim = ps2_visualize.RobotVisualization(num_robots, width, height)
        room = RectangularRoom(width, height)
        count, status = 0, 0
        for n in range(1, num_robots + 1):
            robots.append(robot_type(room, speed))
        while True:
            anim.update(room, robots)
            for robot in robots:
                robot.updatePositionAndClean()
            status = room.getNumCleanedTiles() / room.getNumTiles()
            count += 1
            if status >= min_coverage:
                break
        trials.append(count)
        anim.done()
    average, summe, items = 0, 0, 0
    for x in trials:
        items += 1
        summe += x
    average = summe / items
    return average
コード例 #28
0
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)
    """
    #raise NotImplementedError
    trials = []

    #run num_trials for each test
    for trial in range(num_trials):
        anim = ps2_visualize.RobotVisualization(num_robots, width, height, .01)
        #create room
        room = RectangularRoom(width, height)

        robots = []
        for i in range(num_robots):
            robots.append(robot_type(room, speed))

        timestep = 0
        while room.getNumCleanedTiles() / room.getNumTiles() <= min_coverage:
            for robot in robots:
                anim.update(room, robots)
                robot.updatePositionAndClean()
            timestep += 1
        trials.append(timestep)

    anim.done()
    return sum(trials) / len(trials)
コード例 #29
0
ファイル: ps2.py プロジェクト: butuzov/CS-CourseWork-EDx
def aminate( num_robots, speed, width, height, min_coverage, robot_type):

    anim = ps2_visualize.RobotVisualization(num_robots, width, height)
    room = RectangularRoom( width, height );
    robots = [ robot_type(room, speed) for r in range(num_robots) ]

    curr_coverage = 0.0;
    num = 0
    while curr_coverage < min_coverage:
        [ robot.updatePositionAndClean() for robot in robots  ]

        anim.update(room, robots)

        curr_coverage = room.getNumCleanedTiles() / room.getNumTiles()
        num += 1;
        if num > 10000:
            # de inffinite loop protection.
            print("f**k");
            break;

    anim.done()
コード例 #30
0
ファイル: ps2.py プロジェクト: fridastya/data-science
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 = ps2_visualize.RobotVisualization(num_robots, width,
                                            height)  # Start animation

    sumtime = 0
    for y in range(num_trials):
        room = RectangularRoom(width, height)
        rc = []
        for x in range(num_robots):
            rc.append(robot_type(room, speed))
        time = 0
        pc = 0.01
        while pc < min_coverage:
            pcl = []
            for r in rc:
                r.updatePositionAndClean()
                pcl.append(r.room.getNumCleanedTiles() / r.room.getNumTiles())
            time += 1
            pc = max(pcl)
            anim.update(room, rc)  # Animate robot movement
        sumtime += time
        anim.done()  # End of animation
    return sumtime / num_trials