def plan_trip(self): ''' Plan new tip: i. Determine trip length ii. Determine new direction based on current direction (correlated RW) ''' # Direction if self.current_direction == 0: # first trip direction = random.choice(self.directions.directions) self.current_direction = Direction(direction, self.direction_range) else: # correlated random walk radians = np.random.vonmises(0, kappa=4) degrees = math.degrees(radians) jumps = math.floor(degrees / Pedestrian.DIRECTION_DEGREES) self.current_direction = self.directions.correlated( self.current_direction, jumps) # Update eucledian distance -> trip info eucledian_distance = math.sqrt((self.start[0] - self.pos[0])**2 + (self.start[1] - self.pos[1])**2) self.trip_info[-1][1] += eucledian_distance # Trip length length_of_trip = int(self.trip_lengths.generate_random(1) [0]) #generate_random ignores xmax self.remaining_steps = self.current_direction.calculate_steps( length_of_trip) self.on_trip = True # Saved to compare trip length (steps) and traveled euclidian distance self.start = self.pos traveled_distance = 0 self.trip_info.append([length_of_trip, traveled_distance])
def plan_trip(self): ''' Plan new tip: i. Determine trip length ii. Determine new direction based on current direction (correlated RW) ''' # Direction if self.current_direction == 0: # first trip direction = random.choice(self.directions.directions) self.current_direction = Direction(direction, self.direction_range) else: # correlated random walk radians = np.random.vonmises(0, kappa=4) degrees = math.degrees(radians) jumps = math.floor(degrees / Pedestrian.DIRECTION_DEGREES) self.current_direction = self.directions.correlated( self.current_direction, jumps) # Trip length length_of_trip = int(self.trip_lengths.generate_random(1) [0]) #generate_random ignores xmax self.remaining_steps = self.current_direction.calculate_steps( length_of_trip) self.trip_lengths_covered.append(length_of_trip) self.steps_covered.append(self.remaining_steps)
def test_Cell(self) : """ test le fonctionnement du constructeur """ empty_cell = Cell(0) self.assertEqual( empty_cell.walls, Direction(0) ) full_cell = Cell(15) self.assertEqual( full_cell.walls, Direction(15) ) cell1 = Cell(NORTH+SOUTH) self.assertEqual( cell1.walls, Direction(5) )
def test_add_wall(self) : """ teste la méthode add_wall""" empty_cell = Cell(0) empty_cell.add_wall(Direction.N) self.assertEqual( empty_cell.walls, Direction.N ) empty_cell.add_wall(Direction.N) self.assertEqual( empty_cell.walls, Direction.N ) empty_cell.add_wall(Direction.S) self.assertEqual( empty_cell.walls, Direction(5) ) empty_cell.add_wall(Direction.E) self.assertEqual( empty_cell.walls, Direction(7) ) empty_cell.add_wall(Direction.W) self.assertEqual( empty_cell.walls, Direction(15) )
def rotate_left(self): """ effectue une rotation des murs de la cellule d'un quart de tour vers la gauche. L'objet est modifiée en place et la méthode renvoie la référence de la cellule """ walls = Direction(0) if self.wall_at(Direction.E): walls = walls | Direction.N if self.wall_at(Direction.N): walls = walls | Direction.W if self.wall_at(Direction.W): walls = walls | Direction.S if self.wall_at(Direction.S): walls = walls | Direction.E self.walls = walls return self
def __init__(self, walls=0): """ constructeur cell = Cell(int|Direction) a = Cell() : construit une case sans murs a = Cell(NORTH + EAST) construit une case avec des murs au nord et à l'est a = Cell(2) construit une case avec un mur à l'EST """ assert (type(walls) is int or type(walls) is Direction) assert (0 <= walls <= 15) self.walls = Direction(walls)
class Pedestrian(Agent): DIRECTION_DEGREES = 15 #NR_OF_DIRECTIONS = 24 def __init__(self, unique_id, model, pos, exp, seed=None): self.unique_id = unique_id self.model = model self.pos = pos self.traversable = False self.area_traversed = np.zeros((model.width, model.height)) self.traversable = True self.trip_lengths = powerlaw.Power_Law( xmin=7, parameters=[exp]) #1.5-2.0 #xmin 17? self.direction_range = 3 self.directions = Directions(self.direction_range) self.trip_lengths_covered = [] self.steps_covered = [] self.area_traversed[self.pos[0], self.pos[1]] = 1 self.on_trip = False self.current_direction = 0 self.remaining_steps = 0 def plan_trip(self): ''' Plan new tip: i. Determine trip length ii. Determine new direction based on current direction (correlated RW) ''' # Direction if self.current_direction == 0: # first trip direction = random.choice(self.directions.directions) self.current_direction = Direction(direction, self.direction_range) else: # correlated random walk radians = np.random.vonmises(0, kappa=4) degrees = math.degrees(radians) jumps = math.floor(degrees / Pedestrian.DIRECTION_DEGREES) self.current_direction = self.directions.correlated( self.current_direction, jumps) # Trip length length_of_trip = int(self.trip_lengths.generate_random(1) [0]) #generate_random ignores xmax self.remaining_steps = self.current_direction.calculate_steps( length_of_trip) self.trip_lengths_covered.append(length_of_trip) self.steps_covered.append(self.remaining_steps) def contact(self): ''' Find all other agents within range (radius = 5) and update contacts. ''' neighbors_in_contact = self.model.grid.get_neighbors(self.pos, moore=True, radius=5) if len(neighbors_in_contact) > 0: for neighbor in neighbors_in_contact: if neighbor.unique_id != self.unique_id: self.model.contact_update( (self.unique_id, neighbor.unique_id)) def check_if_traversable(self, coordinate): ''' Check if location is traversable. ''' traversable = True if (0 <= coordinate[0] < self.model.width) and (0 <= coordinate[1] < self.model.height): contents = self.model.grid.get_cell_list_contents(coordinate) for content in contents: if not content.traversable: traversable = False else: traversable = False return traversable def move(self): ''' Move pedestrian one step ''' # Plan new trip, if current trip length reached if self.remaining_steps == 0: self.plan_trip() # Determine move based on current direction shift = self.current_direction.move() move = (self.pos[0] + shift[0], self.pos[1] + shift[1]) # Check if move is possible (can't traverse walls) if self.check_if_traversable(move): new_position = move self.model.grid.move_agent(self, new_position) self.remaining_steps -= 1 else: ''' If not traversable, determine the two alternative moves (i. bounce of vertical wall, ii. bounce of horizontal wall). Determine which bouncing move works, then move and update direction accordingly ''' # Bouncing moves alternative_shifts = [[-shift[0], shift[1]], [shift[0], -shift[1]], [-shift[0], -shift[1]]] # If bouncing move possible --> move and update direction # TODO: Possible bias for objects??? Always checks vertical wall shift first for bounce_shift in alternative_shifts: move = (self.pos[0] + bounce_shift[0], self.pos[1] + bounce_shift[1]) if self.check_if_traversable(move): # Bounce: move + new direction new_position = move self.model.grid.move_agent(self, new_position) self.remaining_steps -= 1 self.current_direction.change(bounce_shift) break def step(self): self.move()
def directions(self, location=None): """Returns a list of directions that the Location could have""" return Direction.all()