Skip to content
Snippets Groups Projects
Commit 584c17c0 authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

Added modifier functions for scores. They depend on the remaining time of the order.

parent 19de1cb7
No related branches found
No related tags found
1 merge request!94Resolve "Score of order depends on remaining time"
Pipeline #49878 passed
......@@ -106,7 +106,9 @@ hook_callbacks:
hooks: [ completed_order ]
callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks ''
callback_class_kwargs:
static_score: 20
time_dependence_func: !!python/name:cooperative_cuisine.scores.constant_score ''
time_dependence_kwargs: { }
static_score: 100
score_on_specific_kwarg: meal_name
score_map:
Burger: 15
......
......@@ -174,7 +174,7 @@ FishAndChips:
BurgerWithChips:
type: Meal
needs: [ Burger, Chips ]
needs: [ Bun, ChoppedLettuce, ChoppedTomato, CookedPatty, Chips ]
equipment: ~
Pizza:
......
......@@ -150,6 +150,11 @@ Burger:
needs: [ Bun, ChoppedLettuce, ChoppedTomato, CookedPatty ]
equipment: ~
BurgerWithChips:
type: Meal
needs: [ Bun, ChoppedLettuce, ChoppedTomato, CookedPatty, Chips ]
equipment: ~
Salad:
type: Meal
needs: [ ChoppedLettuce, ChoppedTomato ]
......
......@@ -16,6 +16,7 @@ levels:
dirty_plates: 0
return_dirty: true
orders:
serving_not_ordered_meals: false
meals:
all: false
list:
......@@ -29,8 +30,22 @@ levels:
sample_on_dur_random_func:
func: uniform
kwargs:
a: 45
b: 35
a: 20
b: 30
hook_callbacks:
orders:
hooks: [ completed_order ]
callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks ''
callback_class_kwargs:
time_dependence_func: !!python/name:cooperative_cuisine.scores.stepped_score ''
time_dependence_kwargs:
score_ratios: [ 0.4, 0.8, 1 ]
steps: [ 0, 0.33, 0.66 ]
round_decimals: 0
static_score: 100
score_on_specific_kwarg: meal_name
score_map: [ ]
- config_path: CONFIGS_DIR/environment_config.yaml
layout_path: LAYOUTS_DIR/study_layouts/1-4-bottleneck.layout
......@@ -47,6 +62,7 @@ levels:
dirty_plates: 0
return_dirty: true
orders:
serving_not_ordered_meals: false
meals:
all: false
list:
......@@ -61,8 +77,8 @@ levels:
sample_on_dur_random_func:
func: uniform
kwargs:
a: 45
b: 35
a: 20
b: 30
- config_path: CONFIGS_DIR/environment_config.yaml
layout_path: LAYOUTS_DIR/study_layouts/1-5-circle.layout
......@@ -80,6 +96,7 @@ levels:
dirty_plates: 0
return_dirty: true
orders:
serving_not_ordered_meals: false
meals:
all: false
list:
......@@ -93,8 +110,8 @@ levels:
sample_on_dur_random_func:
func: uniform
kwargs:
a: 45
b: 35
a: 20
b: 30
- config_path: CONFIGS_DIR/environment_config.yaml
......@@ -104,14 +121,15 @@ levels:
seed: 12345
config_overwrite:
player_config:
speed_units_per_seconds: 300
speed_units_per_seconds: 5
game:
time_limit_seconds: 10
time_limit_seconds: 300
plates:
clean_plates: 0
dirty_plates: 0
return_dirty: true
orders:
serving_not_ordered_meals: false
meals:
all: false
list:
......@@ -127,8 +145,8 @@ levels:
sample_on_dur_random_func:
func: uniform
kwargs:
a: 40
b: 50
a: 20
b: 30
num_players: 1
num_bots: 0
......@@ -231,6 +231,9 @@ class OrderManager:
self.last_finished.append(order)
del self.open_orders[index]
self.served_meals.append((meal, env_time, player))
order.finished_info["remaining_time_ratio"] = (
order.start_time + order.max_duration - env_time
).total_seconds() / order.max_duration.total_seconds()
self.hook(
COMPLETED_ORDER,
order=order,
......
......@@ -5,7 +5,7 @@ Gui:
use_player_cook_sprites: True
show_interaction_range: False
show_counter_centers: False
press_button_to_continue: False
press_button_to_continue: True
GameWindow:
screen_margin_proportion: 0.15
......
......@@ -39,6 +39,41 @@ hook_callbacks:
callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks ''
callback_class_kwargs:
static_score: -10
The score can be modified based on the time remaining for the order to be completed.
hook_callbacks:
orders:
hooks: [ completed_order ]
callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks ''
callback_class_kwargs:
time_dependence_func: !!python/name:cooperative_cuisine.scores.linear_score ''
time_dependence_kwargs:
min_score_ratio: 0.3
round_decimals: 2
static_score: 100
score_on_specific_kwarg: meal_name
score_map: []
hooks: [ completed_order ]
callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks ''
callback_class_kwargs:
time_dependence_func: !!python/name:cooperative_cuisine.scores.stepped_score ''
time_dependence_kwargs:
steps: [0.3, 0.6, 1.0]
ratios: [0.3, 0.6, 1.0]
round_decimals: 2
static_score: 100
score_on_specific_kwarg: meal_name
score_map: []
hooks: [ completed_order ]
callback_class: !!python/name:cooperative_cuisine.scores.ScoreViaHooks ''
callback_class_kwargs:
time_dependence_func: !!python/name:cooperative_cuisine.scores.constant_score ''
time_dependence_kwargs: {}
static_score: 100
score_on_specific_kwarg: meal_name
score_map: []
```
......@@ -47,10 +82,44 @@ hook_callbacks:
from typing import Any
import numpy as np
from cooperative_cuisine.environment import Environment
from cooperative_cuisine.hooks import HookCallbackClass
def constant_score(max_score: float, time_percentage: float = 1.0):
return max_score
def linear_score(
max_score: float,
min_score_ratio: float = 0.0,
time_percentage: float = 1.0,
round_decimals: int = 0,
):
modified_score = np.round(
max(max_score * time_percentage, max_score * min_score_ratio),
decimals=round_decimals,
)
return modified_score
def stepped_score(
max_score: float,
steps: list[float],
score_ratios: list[float],
round_decimals: int = 0,
time_percentage: float = 1.0,
):
if len(steps) != len(score_ratios):
raise ValueError("steps and vals must have the same length")
for threshold, ratio in zip(reversed(steps), reversed(score_ratios)):
if time_percentage >= threshold:
return np.round(max_score * ratio, decimals=round_decimals)
class ScoreViaHooks(HookCallbackClass):
"""
Defines a class ScoreViaHooks that extends the HookCallbackClass.
......@@ -71,6 +140,8 @@ class ScoreViaHooks(HookCallbackClass):
static_score: float = 0,
score_map: dict[str, float] = None,
score_on_specific_kwarg: str = None,
time_dependence_func: callable = constant_score,
time_dependence_kwargs: dict[str, Any] = None,
kwarg_filter: dict[str, Any] = None,
**kwargs,
):
......@@ -94,24 +165,44 @@ class ScoreViaHooks(HookCallbackClass):
"""Filtering condition for keyword arguments."""
self.score_on_specific_kwarg: str = score_on_specific_kwarg
"""The specific keyword argument to score on."""
self.time_dependence_func: callable = time_dependence_func
"""The function to calculate the score based on time."""
self.time_dependence_kwargs: dict[str, Any] = (
time_dependence_kwargs if time_dependence_kwargs else {}
)
"""The keyword arguments to be passed to the time_dependence_func."""
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]}",
)
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)
score = self.static_score
info = hook_ref
elif self.score_map and hook_ref in self.score_map:
if self.kwarg_filter:
if self.kwarg_filter.items() <= kwargs.items():
self.env.increment_score(
self.score_map[hook_ref],
info=f"{hook_ref} - {self.kwarg_filter}",
)
score = self.score_map[hook_ref]
info = f"{hook_ref} - {self.kwarg_filter}"
else:
score = 0
info = "NO INFO?"
else:
self.env.increment_score(self.score_map[hook_ref], info=hook_ref)
score = self.score_map[hook_ref]
info = hook_ref
else:
self.env.increment_score(self.static_score, info=hook_ref)
score = self.static_score
info = hook_ref
if score:
if hook_ref == "completed_order":
ratio = kwargs["order"].finished_info["remaining_time_ratio"]
else:
ratio = 1.0
modified_score = self.time_dependence_func(
score, time_percentage=ratio, **self.time_dependence_kwargs
)
print("PREV SCORE", score, "MODIFIED SCORE", modified_score, "RATIO", ratio)
self.env.increment_score(modified_score, info=info)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment