Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ cython_debug/

# ADF
agent.log*
precompute
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from adf_core_python.core.agent.communication.message_manager import MessageManager
from adf_core_python.core.agent.develop.develop_data import DevelopData
from adf_core_python.core.agent.info.agent_info import AgentInfo
from adf_core_python.core.agent.info.scenario_info import Mode, ScenarioInfo
from adf_core_python.core.agent.info.scenario_info import ScenarioInfo
from adf_core_python.core.agent.info.world_info import WorldInfo
from adf_core_python.core.agent.module.module_manager import ModuleManager
from adf_core_python.core.agent.precompute.precompute_data import PrecomputeData
Expand All @@ -31,15 +31,14 @@ def __init__(
super().__init__(
agent_info, world_info, scenario_info, module_manager, develop_data
)
match scenario_info.get_mode():
case Mode.NON_PRECOMPUTE:
self._path_planning: PathPlanning = cast(
PathPlanning,
module_manager.get_module(
"SampleRoadDetector.PathPlanning",
"adf_core_python.implement.module.algorithm.a_star_path_planning.AStarPathPlanning",
),
)

self._path_planning: PathPlanning = cast(
PathPlanning,
module_manager.get_module(
"SampleRoadDetector.PathPlanning",
"adf_core_python.implement.module.algorithm.a_star_path_planning.AStarPathPlanning",
),
)

self.register_sub_module(self._path_planning)
self._result = None
Expand Down
30 changes: 14 additions & 16 deletions adf_core_python/core/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,15 @@ def __init__(
self.is_precompute = is_precompute

if is_precompute:
# PrecomputeData.remove_date(data_storage_name)
self.mode = Mode.PRECOMPUTATION
self._mode = Mode.PRECOMPUTATION

try:
self._precompute_data = PrecomputeData(data_storage_name)
except Exception as _:
pass

self._module_config = module_config
self._develop_data = develop_data
self._precompute_data = PrecomputeData(data_storage_name)
self._message_manager: MessageManager = MessageManager()
self._communication_module: CommunicationModule = StandardCommunicationModule()

Expand All @@ -131,11 +134,10 @@ def post_connect(self) -> None:
if self.is_precompute:
self._mode = Mode.PRECOMPUTATION
else:
# if self._precompute_data.is_ready():
# self._mode = Mode.PRECOMPUTED
# else:
# self._mode = Mode.NON_PRECOMPUTE
self._mode = Mode.NON_PRECOMPUTE
if self._precompute_data.is_available():
self._mode = Mode.PRECOMPUTED
else:
self._mode = Mode.NON_PRECOMPUTE

self.config.set_value(ConfigKey.KEY_DEBUG_FLAG, self.is_debug)
self.config.set_value(
Expand Down Expand Up @@ -210,10 +212,6 @@ def update_step_info(
def think(self) -> None:
pass

@abstractmethod
def precompute(self) -> None:
pass

@abstractmethod
def get_requested_entities(self) -> list[EntityURN]:
pass
Expand Down Expand Up @@ -266,12 +264,12 @@ def handle_connect_ok(self, msg: Any) -> None:
self.send_acknowledge(msg.request_id)
self.post_connect()
self.logger.info(
f"Connected to kernel: {self.__class__.__qualname__} (request_id: {msg.request_id})",
f"Connected to kernel: {self.__class__.__qualname__} (request_id: {msg.request_id}, agent_id: {self.agent_id}, mode: {self._mode})",
request_id=msg.request_id,
)
if self.precompute_flag:
print("self.precompute_flag: ", self.precompute_flag)
self.precompute()
if self.is_precompute:
self.logger.info("Precompute finished")
exit(0)

self.finish_post_connect_event.set()

Expand Down
20 changes: 18 additions & 2 deletions adf_core_python/core/agent/office/office.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,25 @@ def post_connect(self) -> None:

match self._scenario_info.get_mode():
case Mode.PRECOMPUTATION:
pass
self._tactics_center.precompute(
self._agent_info,
self._world_info,
self._scenario_info,
self._module_manager,
self._precompute_data,
self._message_manager,
self._develop_data,
)
case Mode.PRECOMPUTED:
pass
self._tactics_center.resume(
self._agent_info,
self._world_info,
self._scenario_info,
self._module_manager,
self._precompute_data,
self._message_manager,
self._develop_data,
)
case Mode.NON_PRECOMPUTE:
self._tactics_center.prepare(
self._agent_info,
Expand Down
3 changes: 0 additions & 3 deletions adf_core_python/core/agent/office/office_fire.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@ def __init__(
finish_post_connect_event,
)

def precompute(self) -> None:
pass

def get_requested_entities(self) -> list[EntityURN]:
return [EntityURN.FIRE_STATION]
3 changes: 0 additions & 3 deletions adf_core_python/core/agent/office/office_police.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@ def __init__(
finish_post_connect_event,
)

def precompute(self) -> None:
pass

def get_requested_entities(self) -> list[EntityURN]:
return [EntityURN.POLICE_OFFICE]
20 changes: 18 additions & 2 deletions adf_core_python/core/agent/platoon/platoon.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,25 @@ def post_connect(self) -> None:

match self._scenario_info.get_mode():
case Mode.PRECOMPUTATION:
pass
self._tactics_agent.precompute(
self._agent_info,
self._world_info,
self._scenario_info,
self._module_manager,
self._precompute_data,
self._message_manager,
self._develop_data,
)
case Mode.PRECOMPUTED:
pass
self._tactics_agent.resume(
self._agent_info,
self._world_info,
self._scenario_info,
self._module_manager,
self._precompute_data,
self._message_manager,
self._develop_data,
)
case Mode.NON_PRECOMPUTE:
self._tactics_agent.prepare(
self._agent_info,
Expand Down
3 changes: 0 additions & 3 deletions adf_core_python/core/agent/platoon/platoon_ambulance.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@ def __init__(
finish_post_connect_event,
)

def precompute(self) -> None:
pass

def get_requested_entities(self) -> list[EntityURN]:
return [EntityURN.AMBULANCE_TEAM]
3 changes: 0 additions & 3 deletions adf_core_python/core/agent/platoon/platoon_fire.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@ def __init__(
finish_post_connect_event,
)

def precompute(self) -> None:
pass

def get_requested_entities(self) -> list[EntityURN]:
return [EntityURN.FIRE_BRIGADE]
3 changes: 0 additions & 3 deletions adf_core_python/core/agent/platoon/platoon_police.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@ def __init__(
finish_post_connect_event,
)

def precompute(self) -> None:
pass

def get_requested_entities(self) -> list[EntityURN]:
return [EntityURN.POLICE_FORCE]
77 changes: 74 additions & 3 deletions adf_core_python/core/agent/precompute/precompute_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,75 @@
# TODO: Implement the PrecomputeData class
import json
import os

ENCODE = "utf-8"


class PrecomputeData:
def __init__(self, file_path: str) -> None:
self._file_path = file_path
def __init__(self, dir_path: str) -> None:
"""
Initialize the PrecomputeData object.

Parameters
----------
dir_path : str
The directory path to save the precompute data.

Raises
------
Exception
"""
self._dir_path = dir_path

def read_json_data(self, module_name: str) -> dict:
"""
Read the precompute data from the file.

Returns
-------
dict
The precompute data.

Raises
------
Exception
"""

with open(f"{self._dir_path}/{module_name}.json", "r", encoding=ENCODE) as file:
return json.load(file)

def write_json_data(self, data: dict, module_name: str) -> None:
"""
Write the precompute data to the file.

Parameters
----------
data : dict
The data to write.

Raises
------
Exception
"""
if not os.path.exists(self._dir_path):
os.makedirs(self._dir_path)

with open(f"{self._dir_path}/{module_name}.json", "w", encoding=ENCODE) as file:
json.dump(data, file, indent=4)

def remove_precompute_data(self) -> None:
"""
Remove the precompute data file.
"""
if os.path.exists(self._dir_path):
os.remove(self._dir_path)

def is_available(self) -> bool:
"""
Check if the precompute data is available.

Returns
-------
bool
True if the precompute data is available, False otherwise.
"""
return os.path.exists(self._dir_path)
13 changes: 13 additions & 0 deletions adf_core_python/core/component/tactics/tactics_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ def resume(
) -> None:
raise NotImplementedError

@abstractmethod
def precompute(
self,
agent_info: AgentInfo,
world_info: WorldInfo,
scenario_info: ScenarioInfo,
module_manager: ModuleManager,
precompute_data: PrecomputeData,
message_manager: MessageManager,
develop_data: DevelopData,
) -> None:
raise NotImplementedError

@abstractmethod
def prepare(
self,
Expand Down
1 change: 1 addition & 0 deletions adf_core_python/core/launcher/config_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ class ConfigKey:
KEY_FIRE_STATION_COUNT: Final[str] = "adf.team.office.fire.count"
KEY_POLICE_OFFICE_COUNT: Final[str] = "adf.team.office.police.count"
# adf-core-python
KEY_PRECOMPUTE_DATA_DIR: Final[str] = "adf.agent.precompute.dir_name"
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ def connect(
),
)

request_id: int = component_launcher.generate_request_id()
precompute_data_dir: str = f"{config.get_value(ConfigKey.KEY_PRECOMPUTE_DATA_DIR, 'precompute')}/ambulance_center"

finish_post_connect_event = threading.Event()
request_id: int = component_launcher.generate_request_id()
thread = threading.Thread(
target=component_launcher.connect,
args=(
Expand All @@ -63,7 +65,7 @@ def connect(
"ambulance_center",
config.get_value(ConfigKey.KEY_PRECOMPUTE, False),
config.get_value(ConfigKey.KEY_DEBUG_FLAG, False),
"test",
precompute_data_dir,
module_config,
develop_data,
finish_post_connect_event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ def connect(
),
)

request_id: int = component_launcher.generate_request_id()
precompute_data_dir: str = f"{config.get_value(ConfigKey.KEY_PRECOMPUTE_DATA_DIR, 'precompute')}/ambulance_team"

finish_post_connect_event = threading.Event()
request_id: int = component_launcher.generate_request_id()
thread = threading.Thread(
target=component_launcher.connect,
args=(
Expand All @@ -63,7 +65,7 @@ def connect(
"ambulance_team",
config.get_value(ConfigKey.KEY_PRECOMPUTE, False),
config.get_value(ConfigKey.KEY_DEBUG_FLAG, False),
"test",
precompute_data_dir,
module_config,
develop_data,
finish_post_connect_event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def connect(
),
)

precompute_data_dir: str = f"{config.get_value(ConfigKey.KEY_PRECOMPUTE_DATA_DIR, 'precompute')}/fire_brigade"

request_id: int = component_launcher.generate_request_id()
finish_post_connect_event = threading.Event()
thread = threading.Thread(
Expand All @@ -61,7 +63,7 @@ def connect(
"fire_brigade",
config.get_value(ConfigKey.KEY_PRECOMPUTE, False),
config.get_value(ConfigKey.KEY_DEBUG_FLAG, False),
"test",
precompute_data_dir,
module_config,
develop_data,
finish_post_connect_event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def connect(
),
)

precompute_data_dir: str = f"{config.get_value(ConfigKey.KEY_PRECOMPUTE_DATA_DIR, 'precompute')}/fire_station"

request_id: int = component_launcher.generate_request_id()
finish_post_connect_event = threading.Event()
thread = threading.Thread(
Expand All @@ -61,7 +63,7 @@ def connect(
"fire_station",
config.get_value(ConfigKey.KEY_PRECOMPUTE, False),
config.get_value(ConfigKey.KEY_DEBUG_FLAG, False),
"test",
precompute_data_dir,
module_config,
develop_data,
finish_post_connect_event,
Expand Down
Loading
Loading