Guest User

Attempted Context Steering in Python

a guest
Jun 30th, 2024
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.68 KB | None | 0 0
  1. import pygame
  2. import math
  3.  
  4. pygame.init()
  5. screen = pygame.display.set_mode((800, 600))
  6. clock = pygame.time.Clock()
  7.  
  8. agent_pos = (400, 300)
  9. directions = [(math.cos(math.radians(angle)), math.sin(math.radians(angle))) for angle in range(0, 360, 45)]
  10. line_length = 100
  11.  
  12.  
  13. class Agent:
  14.     def __init__(self, position, speed, threshold, num_directions):
  15.         self.position = position
  16.         self.speed = speed
  17.         self.threshold = threshold
  18.         self.directions = [(math.cos(math.radians(angle)), math.sin(math.radians(angle))) for angle in
  19.                            range(0, 360, 360 // num_directions)]
  20.         self.context_map = {direction: 0 for direction in self.directions}
  21.         self.active_direction = None
  22.  
  23.     def update_context_map(self, obstacles, targets):
  24.         target_map = {direction: 0 for direction in self.directions}
  25.         obstacle_map = {direction: 0 for direction in self.directions}
  26.  
  27.         for target in targets:
  28.             target_vector = [(target.position[0] - self.position[0]), (target.position[1] - self.position[1])]
  29.             magnitude = math.hypot(target_vector[0], target_vector[1])
  30.             if magnitude > 0:
  31.                 normalized_target_vector = [target_vector[0] / magnitude, target_vector[1] / magnitude]
  32.                 for direction in self.directions:
  33.                     dot_product = direction[0] * normalized_target_vector[0] + direction[1] * normalized_target_vector[
  34.                         1]
  35.                     target_map[direction] += dot_product / magnitude
  36.  
  37.         for obstacle in obstacles:
  38.             obstacle_vector = [(self.position[0] - obstacle.position[0]), (self.position[1] - obstacle.position[1])]
  39.             distance = math.hypot(obstacle_vector[0], obstacle_vector[1])
  40.             if distance < self.threshold:
  41.                 normalized_obstacle_vector = [obstacle_vector[0] / distance, obstacle_vector[1] / distance]
  42.                 for direction in self.directions:
  43.                     dot_product = direction[0] * normalized_obstacle_vector[0] + direction[1] * \
  44.                                   normalized_obstacle_vector[1]
  45.                     obstacle_map[direction] -= dot_product / distance
  46.  
  47.         max_target_value = max(target_map.values()) if target_map else 1
  48.         max_obstacle_value = max(obstacle_map.values()) if obstacle_map else 1
  49.  
  50.         for direction in self.directions:
  51.             target_map[direction] = target_map[direction] / max_target_value if max_target_value > 0 else 0
  52.             obstacle_map[direction] = obstacle_map[direction] / max_obstacle_value if max_obstacle_value > 0 else 0
  53.             self.context_map[direction] = target_map[direction] - obstacle_map[direction]
  54.  
  55.     def move(self):
  56.         self.active_direction = max(self.directions, key=lambda d: self.context_map[d])
  57.         if self.context_map[self.active_direction] > 0:
  58.             self.position = (self.position[0] + self.active_direction[0] * self.speed,
  59.                              self.position[1] + self.active_direction[1] * self.speed)
  60.  
  61.     def draw(self, screen):
  62.         pygame.draw.circle(screen, (255, 0, 0), self.position, 10)
  63.         for direction in self.directions:
  64.             weight = self.context_map[direction]
  65.             end_pos = (self.position[0] + direction[0] * line_length * weight,
  66.                        self.position[1] + direction[1] * line_length * weight)
  67.             color = (255, 0, 0) if direction == self.active_direction else (0, 255, 0)
  68.             pygame.draw.line(screen, color, self.position, end_pos)
  69.  
  70.  
  71. class Obstacle:
  72.     def __init__(self, position):
  73.         self.position = position
  74.  
  75.     def draw(self, screen):
  76.         pygame.draw.circle(screen, (0, 0, 255), self.position, 10)
  77.  
  78.  
  79. class Target:
  80.     def __init__(self, position):
  81.         self.position = position
  82.  
  83.     def draw(self, screen):
  84.         pygame.draw.circle(screen, (0, 255, 0), self.position, 10)
  85.  
  86.  
  87. obstacles = []
  88. targets = []
  89.  
  90. agent = Agent((400, 300), 2, 150, 8)
  91.  
  92. running = True
  93. while running:
  94.     for event in pygame.event.get():
  95.         if event.type == pygame.QUIT:
  96.             running = False
  97.         elif event.type == pygame.MOUSEBUTTONDOWN:
  98.             mouse_pos = pygame.mouse.get_pos()
  99.             if event.button == 1:
  100.                 obstacles.append(Obstacle(mouse_pos))
  101.             elif event.button == 3:
  102.                 targets.append(Target(mouse_pos))
  103.  
  104.     screen.fill((0, 0, 0))
  105.  
  106.     agent.update_context_map(obstacles, targets)
  107.     agent.move()
  108.     agent.draw(screen)
  109.  
  110.     for obstacle in obstacles:
  111.         obstacle.draw(screen)
  112.     for target in targets:
  113.         target.draw(screen)
  114.  
  115.     pygame.display.flip()
  116.     clock.tick(60)
  117.  
  118. pygame.quit()
  119.  
Advertisement
Add Comment
Please, Sign In to add comment