From 6a671b9e9cb00eba58515a73833949b4d28729fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Schr=C3=B6der?= <fschroeder@techfak.uni-bielefeld.de> Date: Sun, 10 Mar 2024 00:59:16 +0100 Subject: [PATCH] Refactor recorder and hooks setup in config files This commit refactors the setup of recorders and hooks in several configuration files. It simplifies the YAML structure, renames "extra_setup_functions" to "hook_callbacks", and replaces "log_path" with "record_path". This makes the configurations cleaner and more intuitive. The update also removes the "func" and "kwargs" in the extra setup functions. Now only hooks are registered. This change should not affect functionality, because until now only hooks were registered as extra setup functions. --- cooperative_cuisine/__init__.py | 2 +- .../configs/environment_config.yaml | 172 +++++++--------- .../configs/study/level1/level1_config.yaml | 192 ++++++++---------- .../configs/study/level2/level2_config.yaml | 192 ++++++++---------- .../configs/tutorial_env_config.yaml | 2 +- cooperative_cuisine/environment.py | 21 +- cooperative_cuisine/hooks.py | 4 +- cooperative_cuisine/info_msg.py | 26 +-- .../pygame_2d_vis/video_replay.py | 8 +- cooperative_cuisine/recording.py | 71 +++---- .../environment_config_rl.yaml | 118 +++++------ cooperative_cuisine/scores.py | 56 +++-- 12 files changed, 379 insertions(+), 485 deletions(-) diff --git a/cooperative_cuisine/__init__.py b/cooperative_cuisine/__init__.py index a0577c01..4ec76825 100644 --- a/cooperative_cuisine/__init__.py +++ b/cooperative_cuisine/__init__.py @@ -323,7 +323,7 @@ player_config: effect_manager: # fire effect ... -extra_setup_functions: # scores, recording, msgs, etc. +hook_callbacks: # scores, recording, msgs, etc. ... ``` diff --git a/cooperative_cuisine/configs/environment_config.yaml b/cooperative_cuisine/configs/environment_config.yaml index eec7ad50..8b27d42e 100644 --- a/cooperative_cuisine/configs/environment_config.yaml +++ b/cooperative_cuisine/configs/environment_config.yaml @@ -98,114 +98,96 @@ effect_manager: fire_burns_ingredients_and_meals: true -extra_setup_functions: +hook_callbacks: # # --------------- Scoring --------------- orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ completed_order ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 20 - score_on_specific_kwarg: meal_name - score_map: - Burger: 15 - OnionSoup: 10 - Salad: 5 - TomatoSoup: 10 + hooks: [ completed_order ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 20 + score_on_specific_kwarg: meal_name + score_map: + Burger: 15 + OnionSoup: 10 + Salad: 5 + TomatoSoup: 10 not_ordered_meals: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ serve_not_ordered_meal ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 2 + hooks: [ serve_not_ordered_meal ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 2 trashcan_usages: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ trashcan_usage ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -5 + hooks: [ trashcan_usage ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -5 expired_orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ order_expired ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -10 - # # --------------- Recording --------------- + hooks: [ order_expired ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -10 + # --------------- Recording --------------- # json_states: - # func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - # kwargs: - # hooks: [ json_state ] - # callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - # callback_class_kwargs: - # log_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl + # hooks: [ json_state ] + # callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + # callback_class_kwargs: + # record_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl actions: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ pre_perform_action ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + hooks: [ pre_perform_action ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl random_env_events: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ order_duration_sample, plate_out_of_kitchen_time ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + hooks: [ order_duration_sample, plate_out_of_kitchen_time ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true env_configs: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ env_initialized, item_info_config ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + hooks: [ env_initialized, item_info_config ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true # Game event recording game_events: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: - - post_counter_pick_up - - post_counter_drop_off - - post_dispenser_pick_up - - cutting_board_100 - - player_start_interaction - - player_end_interact - - post_serving - - no_serving - - dirty_plate_arrives - - trashcan_usage - - plate_cleaned - - added_plate_to_sink - - drop_on_sink_addon - - pick_up_from_sink_addon - - serve_not_ordered_meal - - serve_without_plate - - completed_order - - new_orders - - order_expired - - action_on_not_reachable_counter - - new_fire - - fire_spreading - - drop_off_on_cooking_equipment - - players_collide - - post_plate_dispenser_pick_up - - post_plate_dispenser_drop_off - - on_item_transition - - progress_started - - progress_finished - - content_ready + hooks: + - post_counter_pick_up + - post_counter_drop_off + - post_dispenser_pick_up + - cutting_board_100 + - player_start_interaction + - player_end_interact + - post_serving + - no_serving + - dirty_plate_arrives + - trashcan_usage + - plate_cleaned + - added_plate_to_sink + - drop_on_sink_addon + - pick_up_from_sink_addon + - serve_not_ordered_meal + - serve_without_plate + - completed_order + - new_orders + - order_expired + - action_on_not_reachable_counter + - new_fire + - fire_spreading + - drop_off_on_cooking_equipment + - players_collide + - post_plate_dispenser_pick_up + - post_plate_dispenser_drop_off + - on_item_transition + - progress_started + - progress_finished + - content_ready - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true # info_msg: diff --git a/cooperative_cuisine/configs/study/level1/level1_config.yaml b/cooperative_cuisine/configs/study/level1/level1_config.yaml index 4c7188a0..bbe47e5b 100644 --- a/cooperative_cuisine/configs/study/level1/level1_config.yaml +++ b/cooperative_cuisine/configs/study/level1/level1_config.yaml @@ -98,127 +98,105 @@ effect_manager: fire_burns_ingredients_and_meals: true -extra_setup_functions: +hook_callbacks: # # --------------- Scoring --------------- orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ completed_order ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 20 - score_on_specific_kwarg: meal_name - score_map: - Burger: 15 - OnionSoup: 10 - Salad: 5 - TomatoSoup: 10 + hooks: [ completed_order ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 20 + score_on_specific_kwarg: meal_name + score_map: + Burger: 15 + OnionSoup: 10 + Salad: 5 + TomatoSoup: 10 not_ordered_meals: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ serve_not_ordered_meal ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 2 + hooks: [ serve_not_ordered_meal ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 2 trashcan_usages: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ trashcan_usage ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -5 + hooks: [ trashcan_usage ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -5 expired_orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ order_expired ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -10 + hooks: [ order_expired ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -10 # # --------------- Recording --------------- # json_states: - # func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - # kwargs: - # hooks: [ json_state ] - # callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - # callback_class_kwargs: - # log_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl + # hooks: [ json_state ] + # callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + # callback_class_kwargs: + # record_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl # Game event recording game_events: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: - - post_counter_pick_up - - post_counter_drop_off - - post_dispenser_pick_up - - cutting_board_100 - - player_start_interaction - - player_end_interact - - post_serving - - no_serving - - dirty_plate_arrives - - trashcan_usage - - plate_cleaned - - added_plate_to_sink - - drop_on_sink_addon - - pick_up_from_sink_addon - - serve_not_ordered_meal - - serve_without_plate - - completed_order - - new_orders - - order_expired - - action_on_not_reachable_counter - - new_fire - - fire_spreading - - drop_off_on_cooking_equipment - - players_collide - - post_plate_dispenser_pick_up - - post_plate_dispenser_drop_off - - on_item_transition - - progress_started - - progress_finished - - content_ready + hooks: + - post_counter_pick_up + - post_counter_drop_off + - post_dispenser_pick_up + - cutting_board_100 + - player_start_interaction + - player_end_interact + - post_serving + - no_serving + - dirty_plate_arrives + - trashcan_usage + - plate_cleaned + - added_plate_to_sink + - drop_on_sink_addon + - pick_up_from_sink_addon + - serve_not_ordered_meal + - serve_without_plate + - completed_order + - new_orders + - order_expired + - action_on_not_reachable_counter + - new_fire + - fire_spreading + - drop_off_on_cooking_equipment + - players_collide + - post_plate_dispenser_pick_up + - post_plate_dispenser_drop_off + - on_item_transition + - progress_started + - progress_finished + - content_ready - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true actions: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ pre_perform_action ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + hooks: [ pre_perform_action ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl random_env_events: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ order_duration_sample, plate_out_of_kitchen_time ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + hooks: [ order_duration_sample, plate_out_of_kitchen_time ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true env_configs: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ env_initialized, item_info_config ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + hooks: [ env_initialized, item_info_config ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true # info_msg: -# func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' -# kwargs: -# hooks: [ cutting_board_100 ] -# log_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' -# log_class_kwargs: -# msg: Glückwunsch du hast was geschnitten! +# hooks: [ cutting_board_100 ] +# record_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' +# record_class_kwargs: +# msg: Glückwunsch du hast was geschnitten! # fire_msg: -# func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' -# kwargs: -# hooks: [ new_fire ] -# log_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' -# log_class_kwargs: -# msg: Feuer, Feuer, Feuer -# level: Warning +# hooks: [ new_fire ] +# record_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' +# record_class_kwargs: +# msg: Feuer, Feuer, Feuer +# level: Warning diff --git a/cooperative_cuisine/configs/study/level2/level2_config.yaml b/cooperative_cuisine/configs/study/level2/level2_config.yaml index e47528aa..273bbcdf 100644 --- a/cooperative_cuisine/configs/study/level2/level2_config.yaml +++ b/cooperative_cuisine/configs/study/level2/level2_config.yaml @@ -97,127 +97,105 @@ effect_manager: fire_burns_ingredients_and_meals: true -extra_setup_functions: +hook_callbacks: # # --------------- Scoring --------------- orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ completed_order ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 20 - score_on_specific_kwarg: meal_name - score_map: - Burger: 15 - OnionSoup: 10 - Salad: 5 - TomatoSoup: 10 + hooks: [ completed_order ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 20 + score_on_specific_kwarg: meal_name + score_map: + Burger: 15 + OnionSoup: 10 + Salad: 5 + TomatoSoup: 10 not_ordered_meals: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ serve_not_ordered_meal ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 2 + hooks: [ serve_not_ordered_meal ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 2 trashcan_usages: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ trashcan_usage ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -5 + hooks: [ trashcan_usage ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -5 expired_orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ order_expired ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -10 + hooks: [ order_expired ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -10 # # --------------- Recording --------------- # json_states: - # func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - # kwargs: - # hooks: [ json_state ] - # callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - # callback_class_kwargs: - # log_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl + # hooks: [ json_state ] + # callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + # callback_class_kwargs: + # record_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl actions: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ pre_perform_action ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + hooks: [ pre_perform_action ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl random_env_events: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ order_duration_sample, plate_out_of_kitchen_time ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + hooks: [ order_duration_sample, plate_out_of_kitchen_time ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true env_configs: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ env_initialized, item_info_config ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + hooks: [ env_initialized, item_info_config ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true # Game event recording game_events: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: - - post_counter_pick_up - - post_counter_drop_off - - post_dispenser_pick_up - - cutting_board_100 - - player_start_interaction - - player_end_interact - - post_serving - - no_serving - - dirty_plate_arrives - - trashcan_usage - - plate_cleaned - - added_plate_to_sink - - drop_on_sink_addon - - pick_up_from_sink_addon - - serve_not_ordered_meal - - serve_without_plate - - completed_order - - new_orders - - order_expired - - action_on_not_reachable_counter - - new_fire - - fire_spreading - - drop_off_on_cooking_equipment - - players_collide - - post_plate_dispenser_pick_up - - post_plate_dispenser_drop_off - - on_item_transition - - progress_started - - progress_finished - - content_ready + hooks: + - post_counter_pick_up + - post_counter_drop_off + - post_dispenser_pick_up + - cutting_board_100 + - player_start_interaction + - player_end_interact + - post_serving + - no_serving + - dirty_plate_arrives + - trashcan_usage + - plate_cleaned + - added_plate_to_sink + - drop_on_sink_addon + - pick_up_from_sink_addon + - serve_not_ordered_meal + - serve_without_plate + - completed_order + - new_orders + - order_expired + - action_on_not_reachable_counter + - new_fire + - fire_spreading + - drop_off_on_cooking_equipment + - players_collide + - post_plate_dispenser_pick_up + - post_plate_dispenser_drop_off + - on_item_transition + - progress_started + - progress_finished + - content_ready - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true # info_msg: -# func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' -# kwargs: -# hooks: [ cutting_board_100 ] -# log_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' -# log_class_kwargs: -# msg: Glückwunsch du hast was geschnitten! +# hooks: [ cutting_board_100 ] +# record_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' +# record_class_kwargs: +# msg: Glückwunsch du hast was geschnitten! # fire_msg: -# func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' -# kwargs: -# hooks: [ new_fire ] -# log_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' -# log_class_kwargs: -# msg: Feuer, Feuer, Feuer -# level: Warning +# hooks: [ new_fire ] +# record_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' +# record_class_kwargs: +# msg: Feuer, Feuer, Feuer +# level: Warning diff --git a/cooperative_cuisine/configs/tutorial_env_config.yaml b/cooperative_cuisine/configs/tutorial_env_config.yaml index 86a1d034..15a9fc6d 100644 --- a/cooperative_cuisine/configs/tutorial_env_config.yaml +++ b/cooperative_cuisine/configs/tutorial_env_config.yaml @@ -94,4 +94,4 @@ effect_manager: fire_burns_ingredients_and_meals: true -extra_setup_functions: \ No newline at end of file +hook_callbacks: \ No newline at end of file diff --git a/cooperative_cuisine/environment.py b/cooperative_cuisine/environment.py index e898be18..25eff9e6 100644 --- a/cooperative_cuisine/environment.py +++ b/cooperative_cuisine/environment.py @@ -51,6 +51,7 @@ from cooperative_cuisine.hooks import ( ACTION_INTERACT_START, ITEM_INFO_CONFIG, POST_STEP, + hooks_via_callback_class, ) from cooperative_cuisine.items import ( ItemInfo, @@ -90,8 +91,8 @@ class EnvironmentConfig(TypedDict): """Configuration about the player characters.""" layout_chars: dict[str, str] """Definition of which characters in the layout file correspond to which kitchen counter.""" - extra_setup_functions: dict[str, dict] - """Configuration of extra setup functions, for example hook behavior.""" + hook_callbacks: dict[str, dict] + """Configuration of callbacks via HookCallbackClass.""" effect_manager: dict """Config of different effects in the environment, which control for example fire behavior.""" @@ -184,7 +185,7 @@ class Environment: "player_config" ].view_range - self.extra_setup_functions() + self.hook_callbacks() self.layout_config: str = layout_config """The layout config for the environment""" @@ -570,16 +571,14 @@ class Environment: """ self.hook.register_callback(hook_ref, callback) - def extra_setup_functions(self): + def hook_callbacks(self): """Executes extra setup functions specified in the environment configuration.""" - if self.environment_config["extra_setup_functions"]: - for function_name, function_def in self.environment_config[ - "extra_setup_functions" + if self.environment_config["hook_callbacks"]: + for callback_name, setup_kwargs in self.environment_config[ + "hook_callbacks" ].items(): - log.info(f"Setup function {function_name}") - function_def["func"]( - name=function_name, env=self, **function_def["kwargs"] - ) + log.info(f"Setup hook callback {callback_name}") + hooks_via_callback_class(name=callback_name, env=self, **setup_kwargs) def increment_score(self, score: int | float, info: str = ""): """Add a value to the current score and log it.""" diff --git a/cooperative_cuisine/hooks.py b/cooperative_cuisine/hooks.py index 8c9cb70b..f16b10af 100644 --- a/cooperative_cuisine/hooks.py +++ b/cooperative_cuisine/hooks.py @@ -2,7 +2,7 @@ You can add callbacks at specific points in the environment. This "hook" mechanism is defined here. -You can add hooks via the `environment_config` under `extra_setup_functions` and the here defined +You can add hooks via the `environment_config` under `hook_callbacks` and the here defined `hooks_via_callback_class` function. Each hook get different kwargs. But `env` with the environment object and `hook_ref` with the name of the hook are @@ -229,7 +229,7 @@ def hooks_via_callback_class( callback_class: Type[HookCallbackClass], callback_class_kwargs: dict[str, Any], ): - """Function to reference in the `environment_config.yml` to add functionality via hooks and a configured callback class. + """Setup hook callback class. Args: name: A string representing the name of the callback class instance. diff --git a/cooperative_cuisine/info_msg.py b/cooperative_cuisine/info_msg.py index 3d6d9525..7151ac03 100644 --- a/cooperative_cuisine/info_msg.py +++ b/cooperative_cuisine/info_msg.py @@ -1,22 +1,18 @@ """Based on hooks, text-based info msgs can be displayed. ```yaml - extra_setup_functions: + hook_callbacks: info_msg: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ cutting_board_100 ] - callback_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' - callback_class_kwargs: - msg: Glückwunsch du hast was geschnitten! - fire_msg: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ new_fire ] - callback_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' - callback_class_kwargs: - msg: Feuer, Feuer, Feuer - level: Warning + hooks: [ cutting_board_100 ] + callback_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' + callback_class_kwargs: + msg: Glückwunsch du hast was geschnitten! + fire_msg: + hooks: [ new_fire ] + callback_class: !!python/name:cooperative_cuisine.info_msg.InfoMsgManager '' + callback_class_kwargs: + msg: Feuer, Feuer, Feuer + level: Warning ``` """ diff --git a/cooperative_cuisine/pygame_2d_vis/video_replay.py b/cooperative_cuisine/pygame_2d_vis/video_replay.py index acb08cba..46521e12 100644 --- a/cooperative_cuisine/pygame_2d_vis/video_replay.py +++ b/cooperative_cuisine/pygame_2d_vis/video_replay.py @@ -72,14 +72,14 @@ def simulate( You can record the relevant files via hooks in the environment_config: ```yaml - extra_setup_functions + hook_callbacks env_configs: func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' kwargs: hooks: [ env_initialized, item_info_config ] callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl add_hook_ref: true actions: func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' @@ -87,7 +87,7 @@ def simulate( hooks: [ pre_perform_action ] callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl ``` You can call simulation function via the command line. For example by replacing the ENVIRONMENT_ID (Linux system) or the complete path: @@ -219,7 +219,7 @@ def from_json_states( hooks: [ json_state ] callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl + record_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl ``` You can call this function via the command line: diff --git a/cooperative_cuisine/recording.py b/cooperative_cuisine/recording.py index b124a7f2..a71cc0b3 100644 --- a/cooperative_cuisine/recording.py +++ b/cooperative_cuisine/recording.py @@ -7,42 +7,35 @@ https://pypi.org/project/platformdirs/) -> `user_log_dir`. - `ENV_NAME` to the name of the environment. ```yaml -extra_setup_functions: +hook_callbacks: json_states: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ json_state ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl + hooks: [ json_state ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl actions: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ pre_perform_action ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + hooks: [ pre_perform_action ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl random_env_events: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ order_duration_sample, plate_out_of_kitchen_time ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + hooks: [ order_duration_sample, plate_out_of_kitchen_time ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true env_configs: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ env_initialized, item_info_config ] - callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' - callback_class_kwargs: - log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl - add_hook_ref: true + hooks: [ env_initialized, item_info_config ] + callback_class: !!python/name:cooperative_cuisine.recording.FileRecorder '' + callback_class_kwargs: + record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl + add_hook_ref: true ``` """ import json import logging import os +import sys import traceback from pathlib import Path from string import Template @@ -68,7 +61,7 @@ class FileRecorder(HookCallbackClass): self, name: str, env: Environment, - log_path: str = "USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl", + record_path: str = "USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl", add_hook_ref: bool = False, **kwargs, ): @@ -77,19 +70,19 @@ class FileRecorder(HookCallbackClass): Args: name (str): The name of the recorder. This name is used to replace the placeholder "LOG_RECORD_NAME" in the default log file path. env (Environment): The environment in which the recorder is being used. - log_path (str, optional): The path to the log file. Defaults to "USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl". + record_path (str, optional): The path to the log file. Defaults to "USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl". add_hook_ref (bool, optional): Indicates whether to add a hook reference to the recorded data. Defaults to False. **kwargs: Additional keyword arguments. """ super().__init__(name, env, **kwargs) self.add_hook_ref: bool = add_hook_ref """Indicates whether to add a hook reference to the recorded data. Default value is False.""" - log_path = log_path.replace("LOG_RECORD_NAME", name) - log_path = Path(expand_path(log_path, env_name=env.env_name)) - self.log_path: Path = log_path + record_path = record_path.replace("LOG_RECORD_NAME", name) + record_path = Path(expand_path(record_path, env_name=env.env_name)) + self.record_path: Path = record_path """The path to the log file. Default value is "USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl".""" - log.info(f"Recorder record for {name} in file://{log_path}") - os.makedirs(log_path.parent, exist_ok=True) + log.info(f"Recorder record for {name} in file://{record_path}") + os.makedirs(record_path.parent, exist_ok=True) def __call__(self, hook_ref: str, env: Environment, **kwargs): for key, item in kwargs.items(): @@ -134,8 +127,8 @@ class FileRecorder(HookCallbackClass): ) + "\n" ) - with open(self.log_path, "a") as log_file: - log_file.write(record) + with open(self.record_path, "a") as record_file: + record_file.write(record) except TypeError as e: traceback.print_exception(e) log.info( @@ -151,7 +144,7 @@ def print_recorded_events_human_readable(jsonl_path: Path): """ - def stringify_item(item_): + def stringify_item(item_) -> str | None: if isinstance(item_, float): return str(item_) if isinstance(item_, str): @@ -226,7 +219,5 @@ def print_recorded_events_human_readable(jsonl_path: Path): if __name__ == "__main__": - json_lines_path: Path = Path( - "/home/fabian/.local/state/cooperative_cuisine/log/fcb095915c454446b9ee2905ff534610/game_events.jsonl" - ) + json_lines_path: Path = Path(sys.argv[0]) print_recorded_events_human_readable(json_lines_path) diff --git a/cooperative_cuisine/reinforcement_learning/environment_config_rl.yaml b/cooperative_cuisine/reinforcement_learning/environment_config_rl.yaml index 2eb50382..1167ad76 100644 --- a/cooperative_cuisine/reinforcement_learning/environment_config_rl.yaml +++ b/cooperative_cuisine/reinforcement_learning/environment_config_rl.yaml @@ -98,86 +98,64 @@ effect_manager: { } # fire_burns_ingredients_and_meals: true -extra_setup_functions: +hook_callbacks: # # --------------- Scoring --------------- orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ completed_order ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 0.95 + hooks: [ completed_order ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 0.95 serve_not_ordered_meals: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ serve_not_ordered_meal ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 0.95 + hooks: [ serve_not_ordered_meal ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 0.95 trashcan_usages: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ trashcan_usage ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -0.2 + hooks: [ trashcan_usage ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -0.2 item_cut: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ cutting_board_100 ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 0.1 + hooks: [ cutting_board_100 ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 0.1 stepped: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ post_step ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -0.01 + hooks: [ post_step ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -0.01 combine: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ drop_off_on_cooking_equipment ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 0.01 + hooks: [ drop_off_on_cooking_equipment ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 0.01 start_interact: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ player_start_interaction ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 0.01 - # json_states: - # func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - # kwargs: - # hooks: [ json_state ] - # log_class: !!python/name:cooperative_cuisine.recording.LogRecorder '' - # log_class_kwargs: - # log_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl + hooks: [ player_start_interaction ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 0.01 +# json_states: +# hooks: [ json_state ] +# record_class: !!python/name:cooperative_cuisine.recording.LogRecorder '' +# record_class_kwargs: +# record_path: USER_LOG_DIR/ENV_NAME/json_states.jsonl # actions: -# func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' -# kwargs: -# hooks: [ pre_perform_action ] -# log_class: !!python/name:cooperative_cuisine.recording.LogRecorder '' -# log_class_kwargs: -# log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl +# hooks: [ pre_perform_action ] +# record_class: !!python/name:cooperative_cuisine.recording.LogRecorder '' +# record_class_kwargs: +# record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl # random_env_events: -# func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' -# kwargs: -# hooks: [ order_duration_sample, plate_out_of_kitchen_time ] -# log_class: !!python/name:cooperative_cuisine.recording.LogRecorder '' -# log_class_kwargs: -# log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl -# add_hook_ref: true +# hooks: [ order_duration_sample, plate_out_of_kitchen_time ] +# record_class: !!python/name:cooperative_cuisine.recording.LogRecorder '' +# record_class_kwargs: +# record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl +# add_hook_ref: true # env_configs: -# func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' -# kwargs: -# hooks: [ env_initialized, item_info_config ] -# log_class: !!python/name:cooperative_cuisine.recording.LogRecorder '' -# log_class_kwargs: -# log_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl -# add_hook_ref: true +# hooks: [ env_initialized, item_info_config ] +# record_class: !!python/name:cooperative_cuisine.recording.LogRecorder '' +# record_class_kwargs: +# record_path: USER_LOG_DIR/ENV_NAME/LOG_RECORD_NAME.jsonl +# add_hook_ref: true diff --git a/cooperative_cuisine/scores.py b/cooperative_cuisine/scores.py index 5ae2c6ce..c0cc1b21 100644 --- a/cooperative_cuisine/scores.py +++ b/cooperative_cuisine/scores.py @@ -1,5 +1,5 @@ """ -Scores are managed via hooks. You can add them in the `environment_config` under `extra_setup_functions`. +Scores are managed via hooks. You can add them in the `environment_config` under `hook_callbacks`. The here defined `ScoreViaHooks` is a `HookCallbackClass`. It allows you to define how the score is effected by specific hook events. @@ -12,41 +12,33 @@ You can: You can filter the events via `kwarg_filter`. ```yaml -extra_setup_functions: +hook_callbacks: orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ completed_order ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 20 - score_on_specific_kwarg: meal_name - score_map: - Burger: 15 - OnionSoup: 10 - Salad: 5 - TomatoSoup: 10 + hooks: [ completed_order ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 20 + score_on_specific_kwarg: meal_name + score_map: + Burger: 15 + OnionSoup: 10 + Salad: 5 + TomatoSoup: 10 not_ordered_meals: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ serve_not_ordered_meal ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: 2 + hooks: [ serve_not_ordered_meal ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: 2 trashcan_usages: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ trashcan_usage ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -5 + hooks: [ trashcan_usage ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -5 expired_orders: - func: !!python/name:cooperative_cuisine.hooks.hooks_via_callback_class '' - kwargs: - hooks: [ order_expired ] - callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' - callback_class_kwargs: - static_score: -10 + hooks: [ order_expired ] + callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks '' + callback_class_kwargs: + static_score: -10 ``` -- GitLab