forked from linyiLYi/street-fighter-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
street_fighter_custom_wrapper.py
116 lines (86 loc) · 4.71 KB
/
street_fighter_custom_wrapper.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
# Copyright 2023 LIN Yi. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import math
import time
import collections
import gym
import numpy as np
# Custom environment wrapper
class StreetFighterCustomWrapper(gym.Wrapper):
def __init__(self, env, reset_round=True, rendering=False):
super(StreetFighterCustomWrapper, self).__init__(env)
self.env = env
# Use a deque to store the last 9 frames
self.num_frames = 9
self.frame_stack = collections.deque(maxlen=self.num_frames)
self.num_step_frames = 6
self.reward_coeff = 3.0
self.total_timesteps = 0
self.full_hp = 176
self.prev_player_health = self.full_hp
self.prev_oppont_health = self.full_hp
self.observation_space = gym.spaces.Box(low=0, high=255, shape=(100, 128, 3), dtype=np.uint8)
self.reset_round = reset_round
self.rendering = rendering
def _stack_observation(self):
return np.stack([self.frame_stack[i * 3 + 2][:, :, i] for i in range(3)], axis=-1)
def reset(self):
observation = self.env.reset()
self.prev_player_health = self.full_hp
self.prev_oppont_health = self.full_hp
self.total_timesteps = 0
# Clear the frame stack and add the first observation [num_frames] times
self.frame_stack.clear()
for _ in range(self.num_frames):
self.frame_stack.append(observation[::2, ::2, :])
return np.stack([self.frame_stack[i * 3 + 2][:, :, i] for i in range(3)], axis=-1)
def step(self, action):
custom_done = False
obs, _reward, _done, info = self.env.step(action)
self.frame_stack.append(obs[::2, ::2, :])
# Render the game if rendering flag is set to True.
if self.rendering:
self.env.render()
time.sleep(0.01)
for _ in range(self.num_step_frames - 1):
# Keep the button pressed for (num_step_frames - 1) frames.
obs, _reward, _done, info = self.env.step(action)
self.frame_stack.append(obs[::2, ::2, :])
if self.rendering:
self.env.render()
time.sleep(0.01)
curr_player_health = info['agent_hp']
curr_oppont_health = info['enemy_hp']
self.total_timesteps += self.num_step_frames
# Game is over and player loses.
if curr_player_health < 0:
custom_reward = -math.pow(self.full_hp, (curr_oppont_health + 1) / (self.full_hp + 1)) # Use the remaining health points of opponent as penalty.
# If the opponent also has negative health points, it's a even game and the reward is +1.
custom_done = True
# Game is over and player wins.
elif curr_oppont_health < 0:
# custom_reward = curr_player_health * self.reward_coeff # Use the remaining health points of player as reward.
# Multiply by reward_coeff to make the reward larger than the penalty to avoid cowardice of agent.
# custom_reward = math.pow(self.full_hp, (5940 - self.total_timesteps) / 5940) * self.reward_coeff # Use the remaining time steps as reward.
custom_reward = math.pow(self.full_hp, (curr_player_health + 1) / (self.full_hp + 1)) * self.reward_coeff
custom_done = True
# While the fighting is still going on
else:
custom_reward = self.reward_coeff * (self.prev_oppont_health - curr_oppont_health) - (self.prev_player_health - curr_player_health)
self.prev_player_health = curr_player_health
self.prev_oppont_health = curr_oppont_health
custom_done = False
# When reset_round flag is set to False (never reset), the session should always keep going.
if not self.reset_round:
custom_done = False
# Max reward is 6 * full_hp = 1054 (damage * 3 + winning_reward * 3) norm_coefficient = 0.001
return self._stack_observation(), 0.001 * custom_reward, custom_done, info # reward normalization