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
11 changes: 9 additions & 2 deletions adf_core_python/core/agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
import time as _time
from abc import abstractmethod
from threading import Event
from typing import Any, Callable, NoReturn

from bitarray import bitarray
Expand Down Expand Up @@ -91,6 +93,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
) -> None:
self.name = name
self.connect_request_id = None
Expand All @@ -102,6 +105,7 @@ def __init__(
self.logger = get_logger(
f"{self.__class__.__module__}.{self.__class__.__qualname__}"
)
self.finish_post_connect_event = finish_post_connect_event

self.team_name = team_name
self.is_debug = is_debug
Expand Down Expand Up @@ -148,7 +152,7 @@ def post_connect(self) -> None:
self._agent_info,
)

self.logger.info(f"config: {self.config}")
self.logger.debug(f"agent_config: {self.config}")

def update_step_info(
self, time: int, change_set: ChangeSet, hear: list[Command]
Expand Down Expand Up @@ -293,9 +297,12 @@ def handler_sense(self, msg: Any) -> None:
].intValue,
)
)

self.world_model.merge(change_set)
start_update_info_time = _time.time()
self.update_step_info(time, change_set, heard_commands)
self.logger.debug(
f"{time} step calculation time: {_time.time() - start_update_info_time}"
)

def send_acknowledge(self, request_id: int) -> None:
ak_ack = AKAcknowledge()
Expand Down
5 changes: 5 additions & 0 deletions adf_core_python/core/agent/office/office.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from threading import Event

from adf_core_python.core.agent.agent import Agent
from adf_core_python.core.agent.config.module_config import ModuleConfig
from adf_core_python.core.agent.develop.develop_data import DevelopData
Expand All @@ -18,6 +20,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
) -> None:
super().__init__(
is_precompute,
Expand All @@ -27,6 +30,7 @@ def __init__(
data_storage_name,
module_config,
develop_data,
finish_post_connect_event,
)
self._tactics_center = tactics_center
self._team_name = team_name
Expand Down Expand Up @@ -90,6 +94,7 @@ def post_connect(self) -> None:
self.precompute_data,
self._develop_data,
)
self.finish_post_connect_event.set()

def think(self) -> None:
self._tactics_center.think(
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/office/office_ambulance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from threading import Event

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -16,6 +18,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
) -> None:
super().__init__(
tactics_center,
Expand All @@ -25,6 +28,7 @@ def __init__(
data_storage_name,
module_config,
develop_data,
finish_post_connect_event,
)

def precompute(self) -> None:
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/office/office_fire.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from threading import Event

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -16,6 +18,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
) -> None:
super().__init__(
tactics_center,
Expand All @@ -25,6 +28,7 @@ def __init__(
data_storage_name,
module_config,
develop_data,
finish_post_connect_event,
)

def precompute(self) -> None:
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/office/office_police.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from threading import Event

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -16,6 +18,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
) -> None:
super().__init__(
tactics_center,
Expand All @@ -25,6 +28,7 @@ def __init__(
data_storage_name,
module_config,
develop_data,
finish_post_connect_event,
)

def precompute(self) -> None:
Expand Down
6 changes: 6 additions & 0 deletions adf_core_python/core/agent/platoon/platoon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from threading import Event

from adf_core_python.core.agent.action.action import Action
from adf_core_python.core.agent.agent import Agent
from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -19,6 +21,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
) -> None:
super().__init__(
is_precompute,
Expand All @@ -28,6 +31,7 @@ def __init__(
data_storage_name,
module_config,
develop_data,
finish_post_connect_event,
)
self._tactics_agent = tactics_agent
self._team_name = team_name
Expand Down Expand Up @@ -92,6 +96,8 @@ def post_connect(self) -> None:
self._develop_data,
)

self.finish_post_connect_event.set()

def think(self) -> None:
action: Action = self._tactics_agent.think(
self._agent_info,
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/platoon/platoon_ambulance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from threading import Event

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -16,6 +18,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
):
super().__init__(
tactics_agent,
Expand All @@ -25,6 +28,7 @@ def __init__(
data_storage_name,
module_config,
develop_data,
finish_post_connect_event,
)

def precompute(self) -> None:
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/platoon/platoon_fire.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from threading import Event

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -16,6 +18,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
):
super().__init__(
tactics_agent,
Expand All @@ -25,6 +28,7 @@ def __init__(
data_storage_name,
module_config,
develop_data,
finish_post_connect_event,
)

def precompute(self) -> None:
Expand Down
4 changes: 4 additions & 0 deletions adf_core_python/core/agent/platoon/platoon_police.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from threading import Event

from rcrs_core.connection.URN import Entity as EntityURN

from adf_core_python.core.agent.config.module_config import ModuleConfig
Expand All @@ -16,6 +18,7 @@ def __init__(
data_storage_name: str,
module_config: ModuleConfig,
develop_data: DevelopData,
finish_post_connect_event: Event,
):
super().__init__(
tactics_agent,
Expand All @@ -25,6 +28,7 @@ def __init__(
data_storage_name,
module_config,
develop_data,
finish_post_connect_event,
)

def precompute(self) -> None:
Expand Down
11 changes: 11 additions & 0 deletions adf_core_python/core/component/module/abstract_module.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

import time
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING

from adf_core_python.core.logger.logger import get_agent_logger

if TYPE_CHECKING:
from adf_core_python.core.agent.communication.message_manager import MessageManager
from adf_core_python.core.agent.develop.develop_data import DevelopData
Expand Down Expand Up @@ -32,6 +35,10 @@ def __init__(
self._count_prepare: int = 0
self._count_update_info: int = 0
self._count_update_info_current_time: int = 0
self._logger = get_agent_logger(
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
self._agent_info,
)

self._sub_modules: list[AbstractModule] = []

Expand All @@ -56,7 +63,11 @@ def resume(self, precompute_data: PrecomputeData) -> AbstractModule:
def prepare(self) -> AbstractModule:
self._count_prepare += 1
for sub_module in self._sub_modules:
start_time = time.time()
sub_module.prepare()
self._logger.debug(
f"{self.__class__.__name__}'s sub_module {sub_module.__class__.__name__} prepare time: {time.time() - start_time:.3f}",
)
return self

def update_info(self, message_manager: MessageManager) -> AbstractModule:
Expand Down
9 changes: 9 additions & 0 deletions adf_core_python/core/component/tactics/tactics_agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import time
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Optional

Expand Down Expand Up @@ -130,9 +131,17 @@ def module_resume(self, precompute_data: PrecomputeData) -> None:

def module_prepare(self) -> None:
for module in self._modules:
start_time = time.time()
module.prepare()
self._logger.debug(
f"module {module.__class__.__name__} prepare time: {time.time() - start_time:.3f}",
)
for action in self._actions:
start_time = time.time()
action.prepare()
self._logger.debug(
f"module {action.__class__.__name__} prepare time: {time.time() - start_time:.3f}",
)
# for executor in self._command_executor:
# executor.prepare()

Expand Down
27 changes: 21 additions & 6 deletions adf_core_python/core/launcher/agent_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, config: Config):
self.config = config
self.logger = get_logger(__name__)
self.connectors: list[Connector] = []
self.thread_list: list[threading.Thread] = []
self.agent_thread_list: list[threading.Thread] = []

def init_connector(self) -> None:
loader_name, loader_class_name = self.config.get_value(
Expand Down Expand Up @@ -63,12 +63,27 @@ def launch(self) -> None:
host, port, self.logger
)

connector_thread_list: list[threading.Thread] = []
for connector in self.connectors:
threads = connector.connect(component_launcher, self.config, self.loader)
for thread in threads:
thread.daemon = True
thread.start()
self.thread_list.extend(threads)
self.agent_thread_list.extend(threads)

for thread in self.thread_list:
def connect() -> None:
for thread, event in threads.items():
thread.daemon = True
thread.start()
is_not_timeout = event.wait(5)
if not is_not_timeout:
break

connector_thread = threading.Thread(target=connect)
connector_thread_list.append(connector_thread)
connector_thread.start()

for thread in connector_thread_list:
thread.join()

self.logger.info("All agents have been launched")

for thread in self.agent_thread_list:
thread.join()
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 @@ -22,3 +22,4 @@ class ConfigKey:
KEY_AMBULANCE_CENTRE_COUNT: Final[str] = "adf.team.office.ambulance.count"
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
9 changes: 1 addition & 8 deletions adf_core_python/core/launcher/connect/component_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@ def make_connection(self) -> Connection:
return Connection(self.host, self.port)

def connect(self, agent: Agent, _request_id: int) -> None:
# self.logger.bind(agent_id=agent.get_id())

self.logger.info(
f"{agent.__class__.__name__} connecting to {self.host}:{self.port} request_id: {_request_id}"
f"{agent.__class__.__name__} trying to connect to {self.host}:{self.port} request_id: {_request_id}"
)
connection = self.make_connection()
try:
connection.connect()
# ソケットが使用しているPORT番号を取得
if connection.socket is not None:
self.logger.info(
f"Connected to {self.host}:{self.port} on port {connection.socket.getsockname()[1]}"
)
except socket.timeout:
self.logger.warning(f"Connection to {self.host}:{self.port} timed out")
return
Expand Down
2 changes: 1 addition & 1 deletion adf_core_python/core/launcher/connect/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def connect(
component_launcher: ComponentLauncher,
config: Config,
loader: AbstractLoader,
) -> list[threading.Thread]:
) -> dict[threading.Thread, threading.Event]:
raise NotImplementedError

def get_connected_agent_count(self) -> int:
Expand Down
Loading
Loading