2023-09-21 10:13:01 +01:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
2023-10-09 18:35:30 +01:00
|
|
|
|
|
|
|
|
class AbstractReward:
|
2023-10-02 17:21:43 +01:00
|
|
|
def __init__(self):
|
|
|
|
|
...
|
2023-09-21 10:13:01 +01:00
|
|
|
|
2023-10-06 20:32:52 +01:00
|
|
|
@abstractmethod
|
2023-10-09 18:35:30 +01:00
|
|
|
def calculate(self, state: Dict) -> float:
|
2023-10-02 17:21:43 +01:00
|
|
|
return 0.3
|
2023-09-21 10:13:01 +01:00
|
|
|
|
2023-10-06 20:32:52 +01:00
|
|
|
|
2023-10-09 18:35:30 +01:00
|
|
|
class DummyReward(AbstractReward):
|
2023-10-06 20:32:52 +01:00
|
|
|
def calculate(self, state: Dict) -> float:
|
|
|
|
|
return -0.1
|
2023-09-21 10:13:01 +01:00
|
|
|
|
2023-10-09 18:35:30 +01:00
|
|
|
|
|
|
|
|
class RewardFunction:
|
|
|
|
|
__rew_class_identifiers: Dict[str, type[AbstractReward]] = {"DUMMY": DummyReward}
|
|
|
|
|
|
|
|
|
|
def __init__(self, reward_function: AbstractReward):
|
2023-10-02 17:21:43 +01:00
|
|
|
self.reward: AbstractReward = reward_function
|
2023-09-21 10:13:01 +01:00
|
|
|
|
2023-10-09 18:35:30 +01:00
|
|
|
def calculate(self, state: Dict) -> float:
|
2023-10-02 17:21:43 +01:00
|
|
|
return self.reward.calculate(state)
|
2023-10-06 20:32:52 +01:00
|
|
|
|
|
|
|
|
@classmethod
|
2023-10-09 18:35:30 +01:00
|
|
|
def from_config(cls, cfg: Dict) -> "RewardFunction":
|
|
|
|
|
for rew_component_cfg in cfg["reward_components"]:
|
|
|
|
|
rew_type = rew_component_cfg["type"]
|
2023-10-06 20:32:52 +01:00
|
|
|
rew_component = cls.__rew_class_identifiers[rew_type]()
|
|
|
|
|
new = cls(reward_function=rew_component)
|
|
|
|
|
return new
|