Spaces:
Runtime error
Runtime error
Update factory/world_factory.py
Browse files- factory/world_factory.py +34 -2
factory/world_factory.py
CHANGED
|
@@ -42,6 +42,7 @@ from __future__ import annotations
|
|
| 42 |
import copy
|
| 43 |
import inspect
|
| 44 |
import logging
|
|
|
|
| 45 |
from collections.abc import Mapping, MutableMapping, Sequence
|
| 46 |
from dataclasses import dataclass, field
|
| 47 |
from importlib import import_module
|
|
@@ -374,6 +375,35 @@ class WorldFactory:
|
|
| 374 |
owner = owner_agent_id or "world"
|
| 375 |
index = 0 if behavior_index is None else int(behavior_index)
|
| 376 |
return f"{owner}:behavior:{index}:{spec.name}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
def create_world(self, spec: WorldSpec | Mapping[str, Any]) -> World:
|
| 379 |
"""Create a runtime world from a validated or mappable world spec.
|
|
@@ -459,10 +489,12 @@ class WorldFactory:
|
|
| 459 |
)
|
| 460 |
policy = self.create_policy_for_agent(spec)
|
| 461 |
|
|
|
|
|
|
|
| 462 |
params = {
|
| 463 |
"id": spec.id,
|
| 464 |
"type": spec.type,
|
| 465 |
-
"position":
|
| 466 |
"state": copy.deepcopy(spec.state),
|
| 467 |
"memory": copy.deepcopy(spec.memory),
|
| 468 |
"goals": copy.deepcopy(spec.goals),
|
|
@@ -482,7 +514,7 @@ class WorldFactory:
|
|
| 482 |
|
| 483 |
self._safe_setattr(agent, "id", spec.id)
|
| 484 |
self._safe_setattr(agent, "type", spec.type)
|
| 485 |
-
self._safe_setattr(agent, "position",
|
| 486 |
self._safe_setattr(agent, "state", copy.deepcopy(spec.state))
|
| 487 |
self._safe_setattr(agent, "memory", copy.deepcopy(spec.memory))
|
| 488 |
self._safe_setattr(agent, "goals", copy.deepcopy(spec.goals))
|
|
|
|
| 42 |
import copy
|
| 43 |
import inspect
|
| 44 |
import logging
|
| 45 |
+
import numpy as np
|
| 46 |
from collections.abc import Mapping, MutableMapping, Sequence
|
| 47 |
from dataclasses import dataclass, field
|
| 48 |
from importlib import import_module
|
|
|
|
| 375 |
owner = owner_agent_id or "world"
|
| 376 |
index = 0 if behavior_index is None else int(behavior_index)
|
| 377 |
return f"{owner}:behavior:{index}:{spec.name}"
|
| 378 |
+
|
| 379 |
+
@staticmethod
|
| 380 |
+
def _agent_runtime_position(position: Any) -> np.ndarray:
|
| 381 |
+
"""Return a NumPy position vector compatible with core.agent.Agent.
|
| 382 |
+
|
| 383 |
+
The current Agent implementation expects position to behave like a NumPy
|
| 384 |
+
array. In particular, Agent.observe() and Agent.snapshot() use array-style
|
| 385 |
+
methods such as copy() and tolist().
|
| 386 |
+
"""
|
| 387 |
+
|
| 388 |
+
if position is None:
|
| 389 |
+
return np.zeros(2, dtype=np.float64)
|
| 390 |
+
|
| 391 |
+
try:
|
| 392 |
+
array = np.asarray(position, dtype=np.float64).reshape(-1)
|
| 393 |
+
except (TypeError, ValueError) as exc:
|
| 394 |
+
raise RuntimeObjectBuildError(
|
| 395 |
+
f"Agent position must be numeric, got {position!r}"
|
| 396 |
+
) from exc
|
| 397 |
+
|
| 398 |
+
if array.size == 0:
|
| 399 |
+
return np.zeros(2, dtype=np.float64)
|
| 400 |
+
|
| 401 |
+
if not np.all(np.isfinite(array)):
|
| 402 |
+
raise RuntimeObjectBuildError(
|
| 403 |
+
f"Agent position must contain only finite values, got {position!r}"
|
| 404 |
+
)
|
| 405 |
+
|
| 406 |
+
return array
|
| 407 |
|
| 408 |
def create_world(self, spec: WorldSpec | Mapping[str, Any]) -> World:
|
| 409 |
"""Create a runtime world from a validated or mappable world spec.
|
|
|
|
| 489 |
)
|
| 490 |
policy = self.create_policy_for_agent(spec)
|
| 491 |
|
| 492 |
+
runtime_position = self._agent_runtime_position(spec.position)
|
| 493 |
+
|
| 494 |
params = {
|
| 495 |
"id": spec.id,
|
| 496 |
"type": spec.type,
|
| 497 |
+
"position": runtime_position,
|
| 498 |
"state": copy.deepcopy(spec.state),
|
| 499 |
"memory": copy.deepcopy(spec.memory),
|
| 500 |
"goals": copy.deepcopy(spec.goals),
|
|
|
|
| 514 |
|
| 515 |
self._safe_setattr(agent, "id", spec.id)
|
| 516 |
self._safe_setattr(agent, "type", spec.type)
|
| 517 |
+
self._safe_setattr(agent, "position", runtime_position.copy())
|
| 518 |
self._safe_setattr(agent, "state", copy.deepcopy(spec.state))
|
| 519 |
self._safe_setattr(agent, "memory", copy.deepcopy(spec.memory))
|
| 520 |
self._safe_setattr(agent, "goals", copy.deepcopy(spec.goals))
|