-
Florian Schröder authored
All instances of 'overcooked_simulator' have been replaced with 'cooperative_cuisine'. This includes changes in file paths, imports, and script names. The necessary adjustments to reflect this rename have also been made in the documentation, setup files, test scripts, and CI/CD configuration.
Florian Schröder authoredAll instances of 'overcooked_simulator' have been replaced with 'cooperative_cuisine'. This includes changes in file paths, imports, and script names. The necessary adjustments to reflect this rename have also been made in the documentation, setup files, test scripts, and CI/CD configuration.
scores.py 4.04 KiB
"""
Scores are managed via hooks. You can add them in the `environment_config` under `extra_setup_functions`.
The here defined `ScoreViaHooks` is a `HookCallbackClass`. It allows you to define how the score is effected by
specific hook events.
You can:
- score an occurrence of an event with a **static** value (`static_score`)
- map the score based on the name of the hook (`score_map`)
- score based on a specific value in the kwargs passed with the hook (`score_on_specific_kwarg` and `score_map`)
You can filter the events via `kwarg_filter`.
```yaml
extra_setup_functions:
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
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
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
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
```
# Code Documentation
"""
from typing import Any
from cooperative_cuisine.environment import Environment
from cooperative_cuisine.hooks import HookCallbackClass
class ScoreViaHooks(HookCallbackClass):
"""
Defines a class ScoreViaHooks that extends the HookCallbackClass.
Attributes:
name (str): The name of the ScoreViaHooks instance.
env (Environment): The environment in which the ScoreViaHooks instance is being used.
static_score (float): The static score to be added if no other conditions are met.
score_map (dict[str, float]): Mapping of hook references to scores.
score_on_specific_kwarg (str): The specific keyword argument to score on.
kwarg_filter (dict[str, Any]): Filtering condition for keyword arguments.
"""
def __init__(
self,
name: str,
env: Environment,
static_score: float = 0,
score_map: dict[str, float] = None,
score_on_specific_kwarg: str = None,
kwarg_filter: dict[str, Any] = None,
**kwargs,
):
super().__init__(name, env, **kwargs)
self.score_map = score_map
self.static_score = static_score
self.kwarg_filter = kwarg_filter
self.score_on_specific_kwarg = score_on_specific_kwarg
def __call__(self, hook_ref: str, env: Environment, **kwargs):
if self.score_on_specific_kwarg:
if kwargs[self.score_on_specific_kwarg] in self.score_map:
self.env.increment_score(
self.score_map[kwargs[self.score_on_specific_kwarg]],
info=f"{hook_ref} - {kwargs[self.score_on_specific_kwarg]}",
)
else:
self.env.increment_score(self.static_score, info=hook_ref)
elif self.score_map and hook_ref in self.score_map:
if self.kwarg_filter:
if kwargs.items() <= self.kwarg_filter.items():
self.env.increment_score(
self.score_map[hook_ref],
info=f"{hook_ref} - {self.kwarg_filter}",
)
else:
self.env.increment_score(self.score_map[hook_ref], info=hook_ref)
else:
self.env.increment_score(self.static_score, info=hook_ref)