-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_doom.py
272 lines (219 loc) · 9.5 KB
/
custom_doom.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import gymnasium
import os
import vizdoom as vzd
import cv2
from gymnasium.envs.registration import register
from vizdoom.gymnasium_wrapper import gymnasium_env_defns
import numpy as np
from copy import deepcopy
# Register the custom scenario
# scenario_file = os.path.join(os.path.dirname(__file__), "scenarios", "oblige_custom.cfg")
scenario_file = os.path.join(os.path.dirname(__file__), "scenarios", "freedom_custom.cfg")
register(
id="VizdoomCustom-v0",
entry_point="vizdoom.gymnasium_wrapper.gymnasium_env_defns:VizdoomScenarioEnv",
kwargs={"scenario_file": scenario_file},
)
from dataclasses import dataclass
def symlog(x):
return np.sign(x) * np.log(1 + np.abs(x))
@dataclass
class VizDoomRewardFeatures:
# https://vizdoom.farama.org/api/python/enums/#vizdoom.GameVariable
KILLCOUNT: int
ITEMCOUNT: int
SECRETCOUNT: int
FRAGCOUNT: int
DEATHCOUNT: int
HITCOUNT: int
HITS_TAKEN: int
DAMAGECOUNT: int
DAMAGE_TAKEN: int
HEALTH: int
ARMOR: int
DEAD: int
SELECTED_WEAPON_AMMO: int
SELECTED_WEAPON: int
POSITION_X: int
POSITION_Y: int
POSITION_Z: int
TRAVELED_BOX: "TraveledBox"
@classmethod
def make_from_game(cls, game, traveled_box):
# https://vizdoom.farama.org/api/python/enums/#vizdoom.GameVariable
return cls(
KILLCOUNT=game.get_game_variable(vzd.GameVariable.KILLCOUNT),
ITEMCOUNT=game.get_game_variable(vzd.GameVariable.ITEMCOUNT),
SECRETCOUNT=game.get_game_variable(vzd.GameVariable.SECRETCOUNT),
FRAGCOUNT=game.get_game_variable(vzd.GameVariable.FRAGCOUNT),
DEATHCOUNT=game.get_game_variable(vzd.GameVariable.DEATHCOUNT),
HITCOUNT=game.get_game_variable(vzd.GameVariable.HITCOUNT),
HITS_TAKEN=game.get_game_variable(vzd.GameVariable.HITS_TAKEN),
DAMAGECOUNT=game.get_game_variable(vzd.GameVariable.DAMAGECOUNT),
DAMAGE_TAKEN=game.get_game_variable(vzd.GameVariable.DAMAGE_TAKEN),
HEALTH=game.get_game_variable(vzd.GameVariable.HEALTH),
ARMOR=game.get_game_variable(vzd.GameVariable.ARMOR),
DEAD=game.get_game_variable(vzd.GameVariable.DEAD),
SELECTED_WEAPON_AMMO=game.get_game_variable(vzd.GameVariable.SELECTED_WEAPON_AMMO),
SELECTED_WEAPON=game.get_game_variable(vzd.GameVariable.SELECTED_WEAPON),
POSITION_X=game.get_game_variable(vzd.GameVariable.POSITION_X),
POSITION_Y=game.get_game_variable(vzd.GameVariable.POSITION_Y),
POSITION_Z=game.get_game_variable(vzd.GameVariable.POSITION_Z),
TRAVELED_BOX=deepcopy(traveled_box), # TODO: probably a better way to do this?
)
def get_deltas(self, other: "VizDoomRewardFeatures") -> "VizDoomRewardFeatures":
# loop through all our fields and subtract the other fields from them
field_names = self.__annotations__.keys()
return VizDoomRewardFeatures(
**{field: getattr(self, field) - getattr(other, field) for field in field_names}
)
def get_summary(self) -> str:
# new line for every field
summary = "-" * 20 + "\n"
summary += "\n".join([f"{field}: {getattr(self, field)}" for field in self.__annotations__.keys()])
return summary
@dataclass
class TraveledBox:
"""Tracks the volume of coverage in the map the player has traveled. Grows as the player moves around the map.
Used for calculating deltas to view the amount of new map the player has explored.
"""
min_x: int = None
max_x: int = None
min_y: int = None
max_y: int = None
min_z: int = None
max_z: int = None
@property
def is_initialized(self):
return self.min_x is not None
def update(self, x, y, z):
if not self.is_initialized:
self.min_x = x
self.max_x = x
self.min_y = y
self.max_y = y
self.min_z = z
self.max_z = z
return
self.min_x = min(self.min_x, x)
self.max_x = max(self.max_x, x)
self.min_y = min(self.min_y, y)
self.max_y = max(self.max_y, y)
self.min_z = min(self.min_z, z)
self.max_z = max(self.max_z, z)
def x_distance(self):
if not self.is_initialized:
return 0
return self.max_x - self.min_x
def y_distance(self):
if not self.is_initialized:
return 0
return self.max_y - self.min_y
def z_distance(self):
if not self.is_initialized:
return 0
return self.max_z - self.min_z
def average_distance(self):
return (self.x_distance() + self.y_distance() + self.z_distance()) / 3
def __sub__(self, other):
# return the difference between average distances
return abs(self.average_distance() - other.average_distance())
class VizDoomCustom:
def __init__(self, verbose: bool = False):
self.env = gymnasium.make("VizdoomCustom-v0")
self.game = self.env.env.env.game
self._prev_reward_features = None
self._current_reward_features = None
self.verbose = verbose
self.traveled_box = TraveledBox()
@property
def action_space(self):
return self.env.action_space
@property
def observation_space(self):
return self.env.observation_space
def reset(self):
observation, info = self.env.reset()
self._prev_reward_features = self._get_reward_features()
self._initial_reward_features = self._get_reward_features()
self.traveled_box = TraveledBox()
return observation, info
def step(self, action):
# Execute the action and observe the next state
observation, _, terminated, truncated, info = self.env.step(action)
self._current_reward_features = self._get_reward_features()
# Calculate custom reward
reward, deltas = self._get_reward()
self._prev_reward_features = self._current_reward_features # Update previous state
info["deltas"] = deltas
return observation, reward, terminated, truncated, info
def _get_reward_features(self) -> VizDoomRewardFeatures:
return VizDoomRewardFeatures.make_from_game(self.game, traveled_box=self.traveled_box)
def verbose_print(self, *args):
if self.verbose:
print(*args)
def _get_reward(self):
# https://vizdoom.farama.org/api/python/enums/#vizdoom.GameVariable
reward = 0
if self._prev_reward_features is None:
return reward
# NOTE: must be before deltas are calculated
# give some reward for the distance traveled from spawn
# spawn_x, spawn_y, spawn_z = self._initial_reward_features.POSITION_X, self._initial_reward_features.POSITION_Y, self._initial_reward_features.POSITION_Z
# last_x, last_y, last_z = self._prev_reward_features.POSITION_X, self._prev_reward_features.POSITION_Y, self._prev_reward_features.POSITION_Z
current_x, current_y, current_z = self._current_reward_features.POSITION_X, self._current_reward_features.POSITION_Y, self._current_reward_features.POSITION_Z
self.traveled_box.update(current_x, current_y, current_z)
# get deltas
deltas = self._current_reward_features.get_deltas(self._prev_reward_features)
# map exploration reward
# reward += deltas.TRAVELED_BOX
reward += 1 / (self.traveled_box.average_distance() + 1)
reward += deltas.KILLCOUNT * 1000
reward += deltas.ITEMCOUNT * 10
reward += deltas.SECRETCOUNT * 3000
# reward += deltas.HITCOUNT * 100
reward += deltas.DAMAGECOUNT * 10
reward += deltas.HEALTH * 10
reward += deltas.ARMOR * 10
# 10x negative reward to DAMAGE_TAKEN
reward -= deltas.DAMAGE_TAKEN * 10
# NOTE: this is buggy - goes negative when picking up a better weapon
# reward += deltas.SELECTED_WEAPON_AMMO * 10
# we need to use SELECTED_WEAPON to see if this value changed. if
# SELECTED_WEAPON is non-zero, then we know we picked up a new weapon, so
# any ammo decrease should be ignored.
if deltas.SELECTED_WEAPON != 0:
# if we changed weapons, ignore ammo change reward, but give a nice reward
reward += 1000
else:
# decrement reward for firing a weapon, unless we hit or killed an enemy
landed_shot = deltas.KILLCOUNT != 0 or deltas.HITCOUNT != 0
if not landed_shot:
reward += deltas.SELECTED_WEAPON_AMMO * 30
# decrement reward for taking damage (already covered in HEALTH and ARMOR)
# reward -= deltas.DAMAGE_TAKEN * 10
# decrement reward for dying
reward -= deltas.DEAD * 100
if reward != 0:
self.verbose_print(deltas.get_summary())
# return symlog(reward)
return reward, deltas
# Run an example game loop
if __name__ == "__main__":
agent = VizDoomCustom()
# Reset environment
observation, info = agent.reset()
# Initialize CV2 window
cv2.namedWindow("screen", cv2.WINDOW_NORMAL)
cv2.resizeWindow("screen", 640, 480)
# Run loop
for _ in range(1000000):
action = agent.env.action_space.sample()
observation, reward, terminated, truncated, info = agent.step(action)
# Display the reward and the screen
print(reward)
cv2.imshow("screen", observation["screen"])
cv2.waitKey(1)
if terminated or truncated:
observation, info = agent.reset()
agent.env.close()