-
Notifications
You must be signed in to change notification settings - Fork 0
/
avoid_damage.py
56 lines (48 loc) · 1.69 KB
/
avoid_damage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from collections import deque
from typing import SupportsFloat, Any, Optional
import gymnasium as gym
from gymnasium.core import WrapperActType, WrapperObsType
class AvoidDamageWrapper(gym.Wrapper):
def __init__(
self, env, alive_reward=0.5, damage_reward=-0.1, death_reward=-1, **kwargs
):
self.env = env
self.base_reward = alive_reward
self.damage_reward = damage_reward
self.death_reward = death_reward
super().__init__(self.env)
self.health_deque = deque(maxlen=2)
def step(
self, action: WrapperActType
) -> tuple[WrapperObsType, SupportsFloat, bool, bool, dict[str, Any]]:
obs, reward, terminated, truncated, info = self.env.step(action)
info_obs = info["obs"]
is_dead = info_obs.is_dead
reward = reward + self.base_reward
if is_dead:
print(f"{info_obs.last_death_message=}")
self.health_deque.append(20)
terminated = True
reward = self.death_reward
else:
self.health_deque.append(info_obs.health)
if self.health_deque[0] < self.health_deque[1]:
reward = self.damage_reward
return (
obs,
reward,
terminated,
truncated,
info,
) # , done: deprecated
def reset(
self,
*,
seed: Optional[int] = None,
options: Optional[dict[str, Any]] = None,
) -> tuple[WrapperObsType, dict[str, Any]]:
obs, info = self.env.reset(seed=seed, options=options)
info_obs = info["obs"]
self.health_deque.clear()
self.health_deque.append(info_obs.health)
return obs, info