File size: 10,696 Bytes
de8e64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import logging
import datetime
import random
import math
from typing import Any, Dict, List, Optional, Set, Tuple
import uuid

from config import config

class VirtualObject:
    """Class representing an object in the virtual world."""
    
    def __init__(self, name: str, position: Tuple[float, float], properties: Dict[str, Any] = None):
        """Initialize a virtual object with name, position, and properties."""
        self.id = str(uuid.uuid4())
        self.name = name
        self.position = position  # (x, y)
        self.properties = properties or {}
        self.created_at = datetime.datetime.now().isoformat()
    
    def update_position(self, new_position: Tuple[float, float]) -> None:
        """Update the object's position."""
        self.position = new_position
    
    def get_property(self, property_name: str, default: Any = None) -> Any:
        """Get a property value."""
        return self.properties.get(property_name, default)
    
    def set_property(self, property_name: str, value: Any) -> None:
        """Set a property value."""
        self.properties[property_name] = value
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert object to dictionary."""
        return {
            "id": self.id,
            "name": self.name,
            "position": self.position,
            "properties": self.properties,
            "created_at": self.created_at
        }


class VirtualLocation:
    """Class representing a location in the virtual world."""
    
    def __init__(self, name: str, position: Tuple[float, float], radius: float = 1.0, 
                properties: Dict[str, Any] = None):
        """Initialize a virtual location with name, position, radius, and properties."""
        self.id = str(uuid.uuid4())
        self.name = name
        self.position = position  # (x, y)
        self.radius = radius
        self.properties = properties or {}
        self.created_at = datetime.datetime.now().isoformat()
    
    def contains(self, position: Tuple[float, float]) -> bool:
        """Check if a position is within this location."""
        x1, y1 = self.position
        x2, y2 = position
        distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
        return distance <= self.radius
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert location to dictionary."""
        return {
            "id": self.id,
            "name": self.name,
            "position": self.position,
            "radius": self.radius,
            "properties": self.properties,
            "created_at": self.created_at
        }


class VirtualAgent:
    """Class representing an agent's presence in the virtual world."""
    
    def __init__(self, agent_id: str, position: Tuple[float, float], 
                properties: Dict[str, Any] = None):
        """Initialize a virtual agent with agent_id, position, and properties."""
        self.agent_id = agent_id
        self.position = position  # (x, y)
        self.properties = properties or {}
        self.created_at = datetime.datetime.now().isoformat()
    
    def update_position(self, new_position: Tuple[float, float]) -> None:
        """Update the agent's position."""
        self.position = new_position
    
    def get_property(self, property_name: str, default: Any = None) -> Any:
        """Get a property value."""
        return self.properties.get(property_name, default)
    
    def set_property(self, property_name: str, value: Any) -> None:
        """Set a property value."""
        self.properties[property_name] = value
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert virtual agent to dictionary."""
        return {
            "agent_id": self.agent_id,
            "position": self.position,
            "properties": self.properties,
            "created_at": self.created_at
        }


class VirtualWorld:
    """Class representing a virtual world for agent interactions."""
    
    def __init__(self, name: str, size: Tuple[float, float] = (100.0, 100.0)):
        """Initialize a virtual world with name and size."""
        self.name = name
        self.size = size  # (width, height)
        self.objects = {}  # object_id -> VirtualObject
        self.locations = {}  # location_id -> VirtualLocation
        self.virtual_agents = {}  # agent_id -> VirtualAgent
        self.created_at = datetime.datetime.now().isoformat()
        self.logger = logging.getLogger(f"virtual_world.{name}")
        self.logger.info(f"Virtual world {name} initialized with size {size}")
    
    def add_object(self, obj: VirtualObject) -> str:
        """Add an object to the world."""
        self.objects[obj.id] = obj
        self.logger.info(f"Added object {obj.name} at position {obj.position}")
        return obj.id
    
    def remove_object(self, object_id: str) -> bool:
        """Remove an object from the world."""
        if object_id not in self.objects:
            return False
        
        obj = self.objects[object_id]
        del self.objects[object_id]
        
        self.logger.info(f"Removed object {obj.name}")
        return True
    
    def add_location(self, location: VirtualLocation) -> str:
        """Add a location to the world."""
        self.locations[location.id] = location
        self.logger.info(f"Added location {location.name} at position {location.position}")
        return location.id
    
    def remove_location(self, location_id: str) -> bool:
        """Remove a location from the world."""
        if location_id not in self.locations:
            return False
        
        location = self.locations[location_id]
        del self.locations[location_id]
        
        self.logger.info(f"Removed location {location.name}")
        return True
    
    def add_agent(self, agent_id: str, position: Tuple[float, float], 
                 properties: Dict[str, Any] = None) -> bool:
        """Add an agent to the world."""
        if agent_id in self.virtual_agents:
            self.logger.warning(f"Agent {agent_id} is already in the world")
            return False
        
        virtual_agent = VirtualAgent(agent_id, position, properties)
        self.virtual_agents[agent_id] = virtual_agent
        
        self.logger.info(f"Added agent {agent_id} at position {position}")
        return True
    
    def remove_agent(self, agent_id: str) -> bool:
        """Remove an agent from the world."""
        if agent_id not in self.virtual_agents:
            return False
        
        del self.virtual_agents[agent_id]
        
        self.logger.info(f"Removed agent {agent_id}")
        return True
    
    def move_agent(self, agent_id: str, new_position: Tuple[float, float]) -> bool:
        """Move an agent to a new position."""
        if agent_id not in self.virtual_agents:
            self.logger.warning(f"Agent {agent_id} is not in the world")
            return False
        
        # Ensure position is within world bounds
        x, y = new_position
        width, height = self.size
        x = max(0, min(width, x))
        y = max(0, min(height, y))
        
        virtual_agent = self.virtual_agents[agent_id]
        old_position = virtual_agent.position
        virtual_agent.update_position((x, y))
        
        self.logger.info(f"Moved agent {agent_id} from {old_position} to {(x, y)}")
        return True
    
    def get_objects_near(self, position: Tuple[float, float], radius: float) -> List[VirtualObject]:
        """Get objects near a position within a radius."""
        nearby_objects = []
        x1, y1 = position
        
        for obj in self.objects.values():
            x2, y2 = obj.position
            distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
            if distance <= radius:
                nearby_objects.append(obj)
        
        return nearby_objects
    
    def get_agents_near(self, position: Tuple[float, float], radius: float) -> List[str]:
        """Get agent IDs near a position within a radius."""
        nearby_agents = []
        x1, y1 = position
        
        for agent_id, virtual_agent in self.virtual_agents.items():
            x2, y2 = virtual_agent.position
            distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
            if distance <= radius:
                nearby_agents.append(agent_id)
        
        return nearby_agents
    
    def get_location_at(self, position: Tuple[float, float]) -> Optional[VirtualLocation]:
        """Get the location at a position."""
        for location in self.locations.values():
            if location.contains(position):
                return location
        return None
    
    def get_agent_location(self, agent_id: str) -> Optional[VirtualLocation]:
        """Get the location of an agent."""
        if agent_id not in self.virtual_agents:
            return None
        
        virtual_agent = self.virtual_agents[agent_id]
        return self.get_location_at(virtual_agent.position)
    
    def get_distance(self, position1: Tuple[float, float], position2: Tuple[float, float]) -> float:
        """Calculate the distance between two positions."""
        x1, y1 = position1
        x2, y2 = position2
        return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    
    def get_agent_distance(self, agent_id1: str, agent_id2: str) -> Optional[float]:
        """Calculate the distance between two agents."""
        if agent_id1 not in self.virtual_agents or agent_id2 not in self.virtual_agents:
            return None
        
        pos1 = self.virtual_agents[agent_id1].position
        pos2 = self.virtual_agents[agent_id2].position
        return self.get_distance(pos1, pos2)
    
    def can_agents_interact(self, agent_id1: str, agent_id2: str, 
                           max_distance: float = 5.0) -> bool:
        """Check if two agents can interact based on distance."""
        distance = self.get_agent_distance(agent_id1, agent_id2)
        if distance is None:
            return False
        return distance <= max_distance
    
    def get_random_position(self) -> Tuple[float, float]:
        """Get a random position within the world."""
        width, height = self.size
        x = random.uniform(0, width)
        y = random.uniform(0, height)
        return (x, y)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert virtual world to dictionary representation."""
        return {
            "name": self.name,
            "size": self.size,
            "object_count": len(self.objects),
            "location_count": len(self.locations),
            "agent_count": len(self.virtual_agents),
            "created_at": self.created_at
        }