示例#1
0
文件: ps2.py 项目: javier-iesn/prj
      y=self.getRobotPosition().getY()
      r=RectangularRoom(5,5)
      p=Position(x, y)
      newp=p.getNewPosition(self.dir, self.speed)
      if (self.room.isPositionInRoom(newp)):
         self.room.cleanTileAtPosition(newp)
         self.pos=newp
      else:
         while (not self.room.isPositionInRoom(newp)):
            self.dir=random.randint(0,359)
            newp=p.getNewPosition(self.dir, self.speed)
              

# Uncomment this line to see your implementation of StandardRobot in action!
tiles=16
testRobotMovement(StandardRobot, RectangularRoom,0.1,tiles,tiles)


# === Problem 3
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)
    def updatePositionAndClean(self):
        """
        Simulate the raise passage of a single time-step.

        Move the robot to a new position and mark the tile it is on as having
        been cleaned.
        """
        new_position = self.room.getRandomPosition()
        self.setRobotPosition(new_position)
        X = int(new_position.getX())
        Y = int(new_position.getY())

        if not self.room.isTileCleaned(X, Y):
            self.room.cleanTileAtPosition(new_position)

testRobotMovement(StandardRobot, RectangularRoom)


# === Problem 3
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)
示例#3
0
        
        newPosition = currentPosition.getNewPosition(360*(random.random()),currentSpeed)
        if self.room.isPositionInRoom(newPosition) == True:
            self.setRobotPosition(newPosition)
            self.room.cleanTileAtPosition(newPosition)
            return
        else:
            while self.room.isPositionInRoom(newPosition) == False:
                self.setRobotDirection(360*(random.random()))
                newPosition = currentPosition.getNewPosition(self.getRobotDirection(),currentSpeed)
            self.setRobotPosition(newPosition)
            self.room.cleanTileAtPosition(newPosition)
            return

# Uncomment this line to see your implementation of StandardRobot in action!
testRobotMovement(RandomWalkRobot, RectangularRoom)


def showPlot1(title, x_label, y_label):
    """
    What information does the plot produced by this function tell you?
    """
    num_robot_range = range(1, 11)
    times1 = []
    times2 = []
    for num_robots in num_robot_range:
        print "Plotting", num_robots, "robots..."
        times1.append(runSimulation(num_robots, 1.0, 20, 20, 0.8, 20, StandardRobot))
        times2.append(runSimulation(num_robots, 1.0, 20, 20, 0.8, 20, RandomWalkRobot))
    pylab.plot(num_robot_range, times1)
    pylab.plot(num_robot_range, times2)
示例#4
0
文件: ps2.py 项目: nwinds/jugar
        mode = self.getMode(self.pos)
        choices = self.generatChoices(mode)
        try:
            if self.d not in choices: # if original direction cannot go further
                angle = choices.pop(random.randrange(len(choices)))  
            else:
                angle = self.d
            while self.setRobotPosition(self.pos.getNewPosition(angle, self.speed)) == False:   
                angle = choices.pop(random.randrange(len(choices)))  
            if self.d != angle:
                self.setRobotDirection(angle) #tilt to another direction
        except IndexError:
            return

# Uncomment this line to see your implementation of StandardRobot in action!
testRobotMovement(StandardRobot, RectangularRoom)


# === Problem 3
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)
示例#5
0
    num_robot_range = range(1, 11)
    times1 = []
    times2 = []
    for num_robots in num_robot_range:
        print "Plotting", num_robots, "robots..."
        times1.append(runSimulation(num_robots, 1.0, 20, 20, 0.8, 20, StandardRobot))
        times2.append(runSimulation(num_robots, 1.0, 20, 20, 0.8, 20, RandomWalkRobot))
    pylab.plot(num_robot_range, times1)
    pylab.plot(num_robot_range, times2)
    pylab.title(title)
    pylab.legend(('StandardRobot', 'RandomWalkRobot'))
    pylab.xlabel(x_label)
    pylab.ylabel(y_label)
    pylab.show()

testRobotMovement(RandomWalkRobot, RectangularRoom)
def showPlot2(title, x_label, y_label):
    """
    What information does the plot produced by this function tell you?
    """
    aspect_ratios = []
    times1 = []
    times2 = []
    for width in [10, 20, 25, 50]:
        height = 300/width
        print "Plotting cleaning time for a room of width:", width, "by height:", height
        aspect_ratios.append(float(width) / height)
        times1.append(runSimulation(2, 1.0, width, height, 0.8, 200, StandardRobot))
        times2.append(runSimulation(2, 1.0, width, height, 0.8, 200, RandomWalkRobot))
    pylab.plot(aspect_ratios, times1)
    pylab.plot(aspect_ratios, times2)