From 7ea87efa14a05441546ac961e61925a8a137b6bb Mon Sep 17 00:00:00 2001 From: shima004 Date: Wed, 4 Dec 2024 17:14:35 +0900 Subject: [PATCH 1/3] fix: Update connector methods to return a dictionary of threads and events --- adf_core_python/core/agent/agent.py | 13 ++++++++++++- adf_core_python/core/agent/platoon/platoon.py | 10 ++++++++++ .../core/agent/platoon/platoon_ambulance.py | 4 ++++ adf_core_python/core/agent/platoon/platoon_fire.py | 4 ++++ .../core/agent/platoon/platoon_police.py | 4 ++++ .../core/component/module/abstract_module.py | 10 ++++++++++ .../core/component/tactics/tactics_agent.py | 9 +++++++++ adf_core_python/core/launcher/agent_launcher.py | 2 ++ adf_core_python/core/launcher/connect/connector.py | 2 +- .../launcher/connect/connector_ambulance_center.py | 9 +++++---- .../launcher/connect/connector_ambulance_team.py | 9 +++++---- .../core/launcher/connect/connector_fire_brigade.py | 9 +++++---- .../core/launcher/connect/connector_fire_station.py | 9 +++++---- .../core/launcher/connect/connector_police_force.py | 9 +++++---- .../launcher/connect/connector_police_office.py | 9 +++++---- .../module/algorithm/k_means_clustering.py | 7 ++++++- 16 files changed, 92 insertions(+), 27 deletions(-) diff --git a/adf_core_python/core/agent/agent.py b/adf_core_python/core/agent/agent.py index a5bbee66..5ff66de8 100644 --- a/adf_core_python/core/agent/agent.py +++ b/adf_core_python/core/agent/agent.py @@ -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 @@ -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 @@ -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 @@ -293,9 +297,16 @@ def handler_sense(self, msg: Any) -> None: ].intValue, ) ) - + start_marge_time = _time.time() self.world_model.merge(change_set) + end_marge_time = _time.time() + self.logger.debug( + f"Time to merge: {end_marge_time - start_marge_time:.2f} seconds" + ) self.update_step_info(time, change_set, heard_commands) + self.logger.info( + f"Time to update_step_info: {_time.time() - end_marge_time:.2f} seconds" + ) def send_acknowledge(self, request_id: int) -> None: ak_ack = AKAcknowledge() diff --git a/adf_core_python/core/agent/platoon/platoon.py b/adf_core_python/core/agent/platoon/platoon.py index 01babaaa..a58d672a 100644 --- a/adf_core_python/core/agent/platoon/platoon.py +++ b/adf_core_python/core/agent/platoon/platoon.py @@ -1,3 +1,6 @@ +import time +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 @@ -19,6 +22,7 @@ def __init__( data_storage_name: str, module_config: ModuleConfig, develop_data: DevelopData, + finish_post_connect_event: Event, ) -> None: super().__init__( is_precompute, @@ -28,6 +32,7 @@ def __init__( data_storage_name, module_config, develop_data, + finish_post_connect_event, ) self._tactics_agent = tactics_agent self._team_name = team_name @@ -83,6 +88,10 @@ def post_connect(self) -> None: case Mode.PRECOMPUTED: pass case Mode.NON_PRECOMPUTE: + start_time = time.time() + self._logger.info( + f"Prepare start {self._agent_info.get_entity_id().get_value()}" + ) self._tactics_agent.prepare( self._agent_info, self._world_info, @@ -91,6 +100,7 @@ def post_connect(self) -> None: self.precompute_data, self._develop_data, ) + self._logger.info(f"Prepare time: {time.time() - start_time:.3f} sec") def think(self) -> None: action: Action = self._tactics_agent.think( diff --git a/adf_core_python/core/agent/platoon/platoon_ambulance.py b/adf_core_python/core/agent/platoon/platoon_ambulance.py index ca8e1694..8a4d9762 100644 --- a/adf_core_python/core/agent/platoon/platoon_ambulance.py +++ b/adf_core_python/core/agent/platoon/platoon_ambulance.py @@ -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 @@ -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, @@ -25,6 +28,7 @@ def __init__( data_storage_name, module_config, develop_data, + finish_post_connect_event, ) def precompute(self) -> None: diff --git a/adf_core_python/core/agent/platoon/platoon_fire.py b/adf_core_python/core/agent/platoon/platoon_fire.py index 19357953..01d75bac 100644 --- a/adf_core_python/core/agent/platoon/platoon_fire.py +++ b/adf_core_python/core/agent/platoon/platoon_fire.py @@ -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 @@ -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, @@ -25,6 +28,7 @@ def __init__( data_storage_name, module_config, develop_data, + finish_post_connect_event, ) def precompute(self) -> None: diff --git a/adf_core_python/core/agent/platoon/platoon_police.py b/adf_core_python/core/agent/platoon/platoon_police.py index 129c6635..569159e9 100644 --- a/adf_core_python/core/agent/platoon/platoon_police.py +++ b/adf_core_python/core/agent/platoon/platoon_police.py @@ -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 @@ -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, @@ -25,6 +28,7 @@ def __init__( data_storage_name, module_config, develop_data, + finish_post_connect_event, ) def precompute(self) -> None: diff --git a/adf_core_python/core/component/module/abstract_module.py b/adf_core_python/core/component/module/abstract_module.py index 76605194..8115692b 100644 --- a/adf_core_python/core/component/module/abstract_module.py +++ b/adf_core_python/core/component/module/abstract_module.py @@ -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_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 @@ -32,6 +35,9 @@ def __init__( self._count_prepare: int = 0 self._count_update_info: int = 0 self._count_update_info_current_time: int = 0 + self._logger = get_logger( + f"{self.__class__.__module__}.{self.__class__.__qualname__}", + ) self._sub_modules: list[AbstractModule] = [] @@ -56,7 +62,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.info( + f"module {sub_module.__class__.__name__} prepare time: {time.time() - start_time:.3f}", + ) return self def update_info(self, message_manager: MessageManager) -> AbstractModule: diff --git a/adf_core_python/core/component/tactics/tactics_agent.py b/adf_core_python/core/component/tactics/tactics_agent.py index b5134418..d2d27e65 100644 --- a/adf_core_python/core/component/tactics/tactics_agent.py +++ b/adf_core_python/core/component/tactics/tactics_agent.py @@ -1,5 +1,6 @@ from __future__ import annotations +import time from abc import ABC, abstractmethod from typing import TYPE_CHECKING, Any, Optional @@ -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.info( + 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.info( + f"action {action.__class__.__name__} prepare time: {time.time() - start_time:.3f}", + ) # for executor in self._command_executor: # executor.prepare() diff --git a/adf_core_python/core/launcher/agent_launcher.py b/adf_core_python/core/launcher/agent_launcher.py index 9b275f65..1a05f970 100644 --- a/adf_core_python/core/launcher/agent_launcher.py +++ b/adf_core_python/core/launcher/agent_launcher.py @@ -1,5 +1,6 @@ import importlib import threading +import time from adf_core_python.core.component.abstract_loader import AbstractLoader from adf_core_python.core.config.config import Config @@ -68,6 +69,7 @@ def launch(self) -> None: for thread in threads: thread.daemon = True thread.start() + time.sleep(0.5) self.thread_list.extend(threads) for thread in self.thread_list: diff --git a/adf_core_python/core/launcher/connect/connector.py b/adf_core_python/core/launcher/connect/connector.py index 850023af..e517a843 100644 --- a/adf_core_python/core/launcher/connect/connector.py +++ b/adf_core_python/core/launcher/connect/connector.py @@ -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: diff --git a/adf_core_python/core/launcher/connect/connector_ambulance_center.py b/adf_core_python/core/launcher/connect/connector_ambulance_center.py index 98e02d8a..36e22f6e 100644 --- a/adf_core_python/core/launcher/connect/connector_ambulance_center.py +++ b/adf_core_python/core/launcher/connect/connector_ambulance_center.py @@ -24,12 +24,12 @@ def connect( component_launcher: ComponentLauncher, config: Config, loader: AbstractLoader, - ) -> list[threading.Thread]: + ) -> dict[threading.Thread, threading.Event]: count: int = config.get_value(ConfigKey.KEY_AMBULANCE_CENTRE_COUNT, 0) if count == 0: - return [] + return {} - threads: list[threading.Thread] = [] + threads: dict[threading.Thread, threading.Event] = {} for _ in range(count): if loader.get_tactics_ambulance_center() is None: @@ -54,6 +54,7 @@ def connect( ) request_id: int = component_launcher.generate_request_id() + finish_post_connect_event = threading.Event() thread = threading.Thread( target=component_launcher.connect, args=( @@ -70,7 +71,7 @@ def connect( ), name=f"AmbulanceCenterAgent-{request_id}", ) - threads.append(thread) + threads[thread] = finish_post_connect_event self.logger.info("Connected ambulance center (count: %d)" % count) return threads diff --git a/adf_core_python/core/launcher/connect/connector_ambulance_team.py b/adf_core_python/core/launcher/connect/connector_ambulance_team.py index 350d6bd8..50c41676 100644 --- a/adf_core_python/core/launcher/connect/connector_ambulance_team.py +++ b/adf_core_python/core/launcher/connect/connector_ambulance_team.py @@ -24,12 +24,12 @@ def connect( component_launcher: ComponentLauncher, config: Config, loader: AbstractLoader, - ) -> list[threading.Thread]: + ) -> dict[threading.Thread, threading.Event]: count: int = config.get_value(ConfigKey.KEY_AMBULANCE_TEAM_COUNT, 0) if count == 0: - return [] + return {} - threads: list[threading.Thread] = [] + threads: dict[threading.Thread, threading.Event] = {} for _ in range(count): if loader.get_tactics_ambulance_team() is None: @@ -54,6 +54,7 @@ def connect( ) request_id: int = component_launcher.generate_request_id() + finish_post_connect_event = threading.Event() thread = threading.Thread( target=component_launcher.connect, args=( @@ -70,7 +71,7 @@ def connect( ), name=f"AmbulanceTeam-{request_id}", ) - threads.append(thread) + threads[thread] = finish_post_connect_event self.logger.info("Connected ambulance team (count: %d)" % count) return threads diff --git a/adf_core_python/core/launcher/connect/connector_fire_brigade.py b/adf_core_python/core/launcher/connect/connector_fire_brigade.py index 9fc24aa7..a416f66f 100644 --- a/adf_core_python/core/launcher/connect/connector_fire_brigade.py +++ b/adf_core_python/core/launcher/connect/connector_fire_brigade.py @@ -24,12 +24,12 @@ def connect( component_launcher: ComponentLauncher, config: Config, loader: AbstractLoader, - ) -> list[threading.Thread]: + ) -> dict[threading.Thread, threading.Event]: count: int = config.get_value(ConfigKey.KEY_FIRE_BRIGADE_COUNT, 0) if count == 0: - return [] + return {} - threads: list[threading.Thread] = [] + threads: dict[threading.Thread, threading.Event] = {} for _ in range(count): if loader.get_tactics_fire_brigade() is None: @@ -52,6 +52,7 @@ def connect( ) request_id: int = component_launcher.generate_request_id() + finish_post_connect_event = threading.Event() thread = threading.Thread( target=component_launcher.connect, args=( @@ -68,7 +69,7 @@ def connect( ), name=f"FireBrigadeAgent-{request_id}", ) - threads.append(thread) + threads[thread] = finish_post_connect_event self.logger.info("Connected fire brigade (count: %d)" % count) return threads diff --git a/adf_core_python/core/launcher/connect/connector_fire_station.py b/adf_core_python/core/launcher/connect/connector_fire_station.py index f9f4abc0..413eecad 100644 --- a/adf_core_python/core/launcher/connect/connector_fire_station.py +++ b/adf_core_python/core/launcher/connect/connector_fire_station.py @@ -24,12 +24,12 @@ def connect( component_launcher: ComponentLauncher, config: Config, loader: AbstractLoader, - ) -> list[threading.Thread]: + ) -> dict[threading.Thread, threading.Event]: count: int = config.get_value(ConfigKey.KEY_FIRE_STATION_COUNT, 0) if count == 0: - return [] + return {} - threads: list[threading.Thread] = [] + threads: dict[threading.Thread, threading.Event] = {} for _ in range(count): if loader.get_tactics_fire_station() is None: @@ -52,6 +52,7 @@ def connect( ) request_id: int = component_launcher.generate_request_id() + finish_post_connect_event = threading.Event() thread = threading.Thread( target=component_launcher.connect, args=( @@ -68,7 +69,7 @@ def connect( ), name=f"FireStationAgent-{request_id}", ) - threads.append(thread) + threads[thread] = finish_post_connect_event self.logger.info("Connected fire station (count: %d)" % count) return threads diff --git a/adf_core_python/core/launcher/connect/connector_police_force.py b/adf_core_python/core/launcher/connect/connector_police_force.py index 2b3a905d..1a11a861 100644 --- a/adf_core_python/core/launcher/connect/connector_police_force.py +++ b/adf_core_python/core/launcher/connect/connector_police_force.py @@ -24,12 +24,12 @@ def connect( component_launcher: ComponentLauncher, config: Config, loader: AbstractLoader, - ) -> list[threading.Thread]: + ) -> dict[threading.Thread, threading.Event]: count: int = config.get_value(ConfigKey.KEY_POLICE_FORCE_COUNT, 0) if count == 0: - return [] + return {} - threads: list[threading.Thread] = [] + threads: dict[threading.Thread, threading.Event] = {} for _ in range(count): if loader.get_tactics_police_force() is None: @@ -52,6 +52,7 @@ def connect( ) request_id: int = component_launcher.generate_request_id() + finish_post_connect_event = threading.Event() thread = threading.Thread( target=component_launcher.connect, args=( @@ -68,7 +69,7 @@ def connect( ), name=f"PoliceForceAgent-{request_id}", ) - threads.append(thread) + threads[thread] = finish_post_connect_event self.logger.info("Connected police force (count: %d)" % count) return threads diff --git a/adf_core_python/core/launcher/connect/connector_police_office.py b/adf_core_python/core/launcher/connect/connector_police_office.py index 01601fc8..c039f898 100644 --- a/adf_core_python/core/launcher/connect/connector_police_office.py +++ b/adf_core_python/core/launcher/connect/connector_police_office.py @@ -24,12 +24,12 @@ def connect( component_launcher: ComponentLauncher, config: Config, loader: AbstractLoader, - ) -> list[threading.Thread]: + ) -> dict[threading.Thread, threading.Event]: count: int = config.get_value(ConfigKey.KEY_POLICE_OFFICE_COUNT, 0) if count == 0: - return [] + return {} - threads: list[threading.Thread] = [] + threads: dict[threading.Thread, threading.Event] = {} for _ in range(count): if loader.get_tactics_police_office() is None: @@ -54,6 +54,7 @@ def connect( ) request_id: int = component_launcher.generate_request_id() + finish_post_connect_event = threading.Event() thread = threading.Thread( target=component_launcher.connect, args=( @@ -70,7 +71,7 @@ def connect( ), name=f"PoliceOfficeAgent-{request_id}", ) - threads.append(thread) + threads[thread] = finish_post_connect_event self.logger.info("Connected police office (count: %d)" % count) return threads diff --git a/adf_core_python/implement/module/algorithm/k_means_clustering.py b/adf_core_python/implement/module/algorithm/k_means_clustering.py index b40f7c4d..804ae366 100644 --- a/adf_core_python/implement/module/algorithm/k_means_clustering.py +++ b/adf_core_python/implement/module/algorithm/k_means_clustering.py @@ -1,3 +1,5 @@ +import time + import numpy as np from rcrs_core.connection.URN import Entity as EntityURN from rcrs_core.entities.ambulanceCenter import AmbulanceCentre @@ -112,7 +114,7 @@ def prepare(self) -> Clustering: def create_cluster( self, cluster_number: int, entities: list[Entity] ) -> list[list[Entity]]: - kmeans = KMeans(n_clusters=cluster_number, random_state=0) + kmeans = KMeans(n_clusters=cluster_number, random_state=0, init="k-means++") entity_positions: np.ndarray = np.array([]) for entity in entities: location1_x, location1_y = entity.get_location() @@ -120,7 +122,10 @@ def create_cluster( continue entity_positions = np.append(entity_positions, [location1_x, location1_y]) + self._logger.info(f"Entity positions: {len(entity_positions) // 2}") + start_time = time.time() kmeans.fit(entity_positions.reshape(-1, 2)) + self._logger.info(f"KMeans clustering time: {time.time() - start_time:.3f} sec") clusters: list[list[Entity]] = [[] for _ in range(cluster_number)] for entity, label in zip(entities, kmeans.labels_): From 0e7f894870b64202e9d52c2232c0af98574fac78 Mon Sep 17 00:00:00 2001 From: shima004 Date: Thu, 5 Dec 2024 21:51:21 +0900 Subject: [PATCH 2/3] fix: Update logging levels to debug and add finish_post_connect_event parameter in connectors --- adf_core_python/core/agent/agent.py | 12 +++----- adf_core_python/core/agent/office/office.py | 5 ++++ .../core/agent/office/office_ambulance.py | 4 +++ .../core/agent/office/office_fire.py | 4 +++ .../core/agent/office/office_police.py | 4 +++ adf_core_python/core/agent/platoon/platoon.py | 7 ++--- .../core/component/module/abstract_module.py | 9 +++--- .../core/component/tactics/tactics_agent.py | 6 ++-- .../core/launcher/agent_launcher.py | 28 ++++++++++++++----- adf_core_python/core/launcher/config_key.py | 1 + .../launcher/connect/component_launcher.py | 9 +----- .../connect/connector_ambulance_center.py | 1 + .../connect/connector_ambulance_team.py | 1 + .../connect/connector_fire_brigade.py | 1 + .../connect/connector_fire_station.py | 1 + .../connect/connector_police_force.py | 1 + .../connect/connector_police_office.py | 1 + .../module/algorithm/k_means_clustering.py | 3 -- adf_core_python/launcher.py | 2 +- 19 files changed, 61 insertions(+), 39 deletions(-) diff --git a/adf_core_python/core/agent/agent.py b/adf_core_python/core/agent/agent.py index 5ff66de8..ec0933c3 100644 --- a/adf_core_python/core/agent/agent.py +++ b/adf_core_python/core/agent/agent.py @@ -152,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] @@ -297,15 +297,11 @@ def handler_sense(self, msg: Any) -> None: ].intValue, ) ) - start_marge_time = _time.time() self.world_model.merge(change_set) - end_marge_time = _time.time() - self.logger.debug( - f"Time to merge: {end_marge_time - start_marge_time:.2f} seconds" - ) + start_update_info_time = _time.time() self.update_step_info(time, change_set, heard_commands) - self.logger.info( - f"Time to update_step_info: {_time.time() - end_marge_time:.2f} seconds" + self.logger.debug( + f"{time} step calculation time: {_time.time() - start_update_info_time}" ) def send_acknowledge(self, request_id: int) -> None: diff --git a/adf_core_python/core/agent/office/office.py b/adf_core_python/core/agent/office/office.py index 4c43c4b2..0aed6bcf 100644 --- a/adf_core_python/core/agent/office/office.py +++ b/adf_core_python/core/agent/office/office.py @@ -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 @@ -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, @@ -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 @@ -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( diff --git a/adf_core_python/core/agent/office/office_ambulance.py b/adf_core_python/core/agent/office/office_ambulance.py index 4076f67a..6bec5308 100644 --- a/adf_core_python/core/agent/office/office_ambulance.py +++ b/adf_core_python/core/agent/office/office_ambulance.py @@ -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 @@ -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, @@ -25,6 +28,7 @@ def __init__( data_storage_name, module_config, develop_data, + finish_post_connect_event, ) def precompute(self) -> None: diff --git a/adf_core_python/core/agent/office/office_fire.py b/adf_core_python/core/agent/office/office_fire.py index bcd19460..dcab2bdf 100644 --- a/adf_core_python/core/agent/office/office_fire.py +++ b/adf_core_python/core/agent/office/office_fire.py @@ -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 @@ -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, @@ -25,6 +28,7 @@ def __init__( data_storage_name, module_config, develop_data, + finish_post_connect_event, ) def precompute(self) -> None: diff --git a/adf_core_python/core/agent/office/office_police.py b/adf_core_python/core/agent/office/office_police.py index ccfcbca9..b4c9fc4d 100644 --- a/adf_core_python/core/agent/office/office_police.py +++ b/adf_core_python/core/agent/office/office_police.py @@ -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 @@ -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, @@ -25,6 +28,7 @@ def __init__( data_storage_name, module_config, develop_data, + finish_post_connect_event, ) def precompute(self) -> None: diff --git a/adf_core_python/core/agent/platoon/platoon.py b/adf_core_python/core/agent/platoon/platoon.py index a58d672a..41e1f32d 100644 --- a/adf_core_python/core/agent/platoon/platoon.py +++ b/adf_core_python/core/agent/platoon/platoon.py @@ -88,10 +88,6 @@ def post_connect(self) -> None: case Mode.PRECOMPUTED: pass case Mode.NON_PRECOMPUTE: - start_time = time.time() - self._logger.info( - f"Prepare start {self._agent_info.get_entity_id().get_value()}" - ) self._tactics_agent.prepare( self._agent_info, self._world_info, @@ -100,7 +96,8 @@ def post_connect(self) -> None: self.precompute_data, self._develop_data, ) - self._logger.info(f"Prepare time: {time.time() - start_time:.3f} sec") + + self.finish_post_connect_event.set() def think(self) -> None: action: Action = self._tactics_agent.think( diff --git a/adf_core_python/core/component/module/abstract_module.py b/adf_core_python/core/component/module/abstract_module.py index 8115692b..dcba3aa7 100644 --- a/adf_core_python/core/component/module/abstract_module.py +++ b/adf_core_python/core/component/module/abstract_module.py @@ -4,7 +4,7 @@ from abc import ABC, abstractmethod from typing import TYPE_CHECKING -from adf_core_python.core.logger.logger import get_logger +from adf_core_python.core.logger.logger import get_agent_logger, get_logger if TYPE_CHECKING: from adf_core_python.core.agent.communication.message_manager import MessageManager @@ -35,8 +35,9 @@ def __init__( self._count_prepare: int = 0 self._count_update_info: int = 0 self._count_update_info_current_time: int = 0 - self._logger = get_logger( + self._logger = get_agent_logger( f"{self.__class__.__module__}.{self.__class__.__qualname__}", + self._agent_info, ) self._sub_modules: list[AbstractModule] = [] @@ -64,8 +65,8 @@ def prepare(self) -> AbstractModule: for sub_module in self._sub_modules: start_time = time.time() sub_module.prepare() - self._logger.info( - f"module {sub_module.__class__.__name__} prepare time: {time.time() - start_time:.3f}", + self._logger.debug( + f"{self.__class__.__name__}'s sub_module {sub_module.__class__.__name__} prepare time: {time.time() - start_time:.3f}", ) return self diff --git a/adf_core_python/core/component/tactics/tactics_agent.py b/adf_core_python/core/component/tactics/tactics_agent.py index d2d27e65..b0925175 100644 --- a/adf_core_python/core/component/tactics/tactics_agent.py +++ b/adf_core_python/core/component/tactics/tactics_agent.py @@ -133,14 +133,14 @@ def module_prepare(self) -> None: for module in self._modules: start_time = time.time() module.prepare() - self._logger.info( + 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.info( - f"action {action.__class__.__name__} prepare time: {time.time() - start_time:.3f}", + self._logger.debug( + f"module {action.__class__.__name__} prepare time: {time.time() - start_time:.3f}", ) # for executor in self._command_executor: # executor.prepare() diff --git a/adf_core_python/core/launcher/agent_launcher.py b/adf_core_python/core/launcher/agent_launcher.py index 1a05f970..aebcd241 100644 --- a/adf_core_python/core/launcher/agent_launcher.py +++ b/adf_core_python/core/launcher/agent_launcher.py @@ -33,7 +33,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( @@ -64,13 +64,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() - time.sleep(0.5) - self.thread_list.extend(threads) + self.agent_thread_list.extend(threads) - for thread in self.thread_list: + def connect(): + 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() diff --git a/adf_core_python/core/launcher/config_key.py b/adf_core_python/core/launcher/config_key.py index 69d7d354..d32afb2e 100644 --- a/adf_core_python/core/launcher/config_key.py +++ b/adf_core_python/core/launcher/config_key.py @@ -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 diff --git a/adf_core_python/core/launcher/connect/component_launcher.py b/adf_core_python/core/launcher/connect/component_launcher.py index d4f6ffd5..8e45009f 100644 --- a/adf_core_python/core/launcher/connect/component_launcher.py +++ b/adf_core_python/core/launcher/connect/component_launcher.py @@ -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 diff --git a/adf_core_python/core/launcher/connect/connector_ambulance_center.py b/adf_core_python/core/launcher/connect/connector_ambulance_center.py index 36e22f6e..1dca5afc 100644 --- a/adf_core_python/core/launcher/connect/connector_ambulance_center.py +++ b/adf_core_python/core/launcher/connect/connector_ambulance_center.py @@ -66,6 +66,7 @@ def connect( "test", module_config, develop_data, + finish_post_connect_event, ), request_id, ), diff --git a/adf_core_python/core/launcher/connect/connector_ambulance_team.py b/adf_core_python/core/launcher/connect/connector_ambulance_team.py index 50c41676..879f313d 100644 --- a/adf_core_python/core/launcher/connect/connector_ambulance_team.py +++ b/adf_core_python/core/launcher/connect/connector_ambulance_team.py @@ -66,6 +66,7 @@ def connect( "test", module_config, develop_data, + finish_post_connect_event, ), request_id, ), diff --git a/adf_core_python/core/launcher/connect/connector_fire_brigade.py b/adf_core_python/core/launcher/connect/connector_fire_brigade.py index a416f66f..ac7ecb37 100644 --- a/adf_core_python/core/launcher/connect/connector_fire_brigade.py +++ b/adf_core_python/core/launcher/connect/connector_fire_brigade.py @@ -64,6 +64,7 @@ def connect( "test", module_config, develop_data, + finish_post_connect_event, ), request_id, ), diff --git a/adf_core_python/core/launcher/connect/connector_fire_station.py b/adf_core_python/core/launcher/connect/connector_fire_station.py index 413eecad..de8dbc6b 100644 --- a/adf_core_python/core/launcher/connect/connector_fire_station.py +++ b/adf_core_python/core/launcher/connect/connector_fire_station.py @@ -64,6 +64,7 @@ def connect( "test", module_config, develop_data, + finish_post_connect_event, ), request_id, ), diff --git a/adf_core_python/core/launcher/connect/connector_police_force.py b/adf_core_python/core/launcher/connect/connector_police_force.py index 1a11a861..d2f45b12 100644 --- a/adf_core_python/core/launcher/connect/connector_police_force.py +++ b/adf_core_python/core/launcher/connect/connector_police_force.py @@ -64,6 +64,7 @@ def connect( "test", module_config, develop_data, + finish_post_connect_event, ), request_id, ), diff --git a/adf_core_python/core/launcher/connect/connector_police_office.py b/adf_core_python/core/launcher/connect/connector_police_office.py index c039f898..1c7a7f77 100644 --- a/adf_core_python/core/launcher/connect/connector_police_office.py +++ b/adf_core_python/core/launcher/connect/connector_police_office.py @@ -66,6 +66,7 @@ def connect( "test", module_config, develop_data, + finish_post_connect_event, ), request_id, ), diff --git a/adf_core_python/implement/module/algorithm/k_means_clustering.py b/adf_core_python/implement/module/algorithm/k_means_clustering.py index 804ae366..c18133dc 100644 --- a/adf_core_python/implement/module/algorithm/k_means_clustering.py +++ b/adf_core_python/implement/module/algorithm/k_means_clustering.py @@ -122,10 +122,7 @@ def create_cluster( continue entity_positions = np.append(entity_positions, [location1_x, location1_y]) - self._logger.info(f"Entity positions: {len(entity_positions) // 2}") - start_time = time.time() kmeans.fit(entity_positions.reshape(-1, 2)) - self._logger.info(f"KMeans clustering time: {time.time() - start_time:.3f} sec") clusters: list[list[Entity]] = [[] for _ in range(cluster_number)] for entity, label in zip(entities, kmeans.labels_): diff --git a/adf_core_python/launcher.py b/adf_core_python/launcher.py index e523a1f3..310ff9d7 100644 --- a/adf_core_python/launcher.py +++ b/adf_core_python/launcher.py @@ -98,7 +98,7 @@ def __init__( if value is not None: self.launcher_config.set_value(key, value) - self.logger.info(f"Config: {self.launcher_config}") + self.logger.debug(f"launcher_config: {self.launcher_config}") def launch(self) -> None: agent_launcher: AgentLauncher = AgentLauncher( From dabb446ce532571cfdccdb82ed080172245afd36 Mon Sep 17 00:00:00 2001 From: shima004 Date: Thu, 5 Dec 2024 21:58:36 +0900 Subject: [PATCH 3/3] fix: Remove unused time imports and add type hint for connect function fix: Remove init parameter from KMeans instantiation in create_cluster method fix: Remove unused import of time in k_means_clustering.py fix: Remove unused time imports and add type hint for connect function --- adf_core_python/core/agent/platoon/platoon.py | 1 - adf_core_python/core/component/module/abstract_module.py | 2 +- adf_core_python/core/launcher/agent_launcher.py | 3 +-- .../implement/module/algorithm/k_means_clustering.py | 4 +--- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/adf_core_python/core/agent/platoon/platoon.py b/adf_core_python/core/agent/platoon/platoon.py index 41e1f32d..36c46a6e 100644 --- a/adf_core_python/core/agent/platoon/platoon.py +++ b/adf_core_python/core/agent/platoon/platoon.py @@ -1,4 +1,3 @@ -import time from threading import Event from adf_core_python.core.agent.action.action import Action diff --git a/adf_core_python/core/component/module/abstract_module.py b/adf_core_python/core/component/module/abstract_module.py index dcba3aa7..4a3f3178 100644 --- a/adf_core_python/core/component/module/abstract_module.py +++ b/adf_core_python/core/component/module/abstract_module.py @@ -4,7 +4,7 @@ from abc import ABC, abstractmethod from typing import TYPE_CHECKING -from adf_core_python.core.logger.logger import get_agent_logger, get_logger +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 diff --git a/adf_core_python/core/launcher/agent_launcher.py b/adf_core_python/core/launcher/agent_launcher.py index aebcd241..f9efa15d 100644 --- a/adf_core_python/core/launcher/agent_launcher.py +++ b/adf_core_python/core/launcher/agent_launcher.py @@ -1,6 +1,5 @@ import importlib import threading -import time from adf_core_python.core.component.abstract_loader import AbstractLoader from adf_core_python.core.config.config import Config @@ -69,7 +68,7 @@ def launch(self) -> None: threads = connector.connect(component_launcher, self.config, self.loader) self.agent_thread_list.extend(threads) - def connect(): + def connect() -> None: for thread, event in threads.items(): thread.daemon = True thread.start() diff --git a/adf_core_python/implement/module/algorithm/k_means_clustering.py b/adf_core_python/implement/module/algorithm/k_means_clustering.py index c18133dc..b40f7c4d 100644 --- a/adf_core_python/implement/module/algorithm/k_means_clustering.py +++ b/adf_core_python/implement/module/algorithm/k_means_clustering.py @@ -1,5 +1,3 @@ -import time - import numpy as np from rcrs_core.connection.URN import Entity as EntityURN from rcrs_core.entities.ambulanceCenter import AmbulanceCentre @@ -114,7 +112,7 @@ def prepare(self) -> Clustering: def create_cluster( self, cluster_number: int, entities: list[Entity] ) -> list[list[Entity]]: - kmeans = KMeans(n_clusters=cluster_number, random_state=0, init="k-means++") + kmeans = KMeans(n_clusters=cluster_number, random_state=0) entity_positions: np.ndarray = np.array([]) for entity in entities: location1_x, location1_y = entity.get_location()