Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New protobuf #33

Merged
merged 2 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions diambra/arena/arena_gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def env_info_process(self, env_info_dict):
self.max_stage = env_info_dict["max_stage"]

# Min-Max reward
self.minmax_reward = env_info_dict["min_max_rew"]
self.cumulative_reward_bounds = env_info_dict["cumulative_reward_bounds"]

# Characters names list
self.char_names = env_info_dict["char_list"]
Expand All @@ -78,11 +78,12 @@ def env_info_process(self, env_info_dict):
# Action dict
self.print_actions_dict = env_info_dict["actions_dict"]

# Additional Obs map
self.add_obs = {}
for idx in range(len(env_info_dict["additional_obs"])):
elem = env_info_dict["additional_obs"][idx]
self.add_obs[elem["name"]] = [elem["type"], elem["min"], elem["max"]]
# Ram states map
self.ram_states = {}
for k in sorted(env_info_dict["ram_states"].keys()):
self.ram_states[k] = [env_info_dict["ram_states"][k].type,
env_info_dict["ram_states"][k].min,
env_info_dict["ram_states"][k].max]

# Return env action list
def action_list(self):
Expand All @@ -98,10 +99,10 @@ def print_actions(self):
for k, v in self.print_actions_dict[1].items():
print(" {}: {}".format(k, v))

# Return min max rewards for the environment
def get_min_max_reward(self):
return [self.minmax_reward[0] / (self.reward_normalization_value),
self.minmax_reward[1] / (self.reward_normalization_value)]
# Return cumulative reward bounds for the environment
def get_cumulative_reward_bounds(self):
return [self.cumulative_reward_bounds[0] / (self.reward_normalization_value),
self.cumulative_reward_bounds[1] / (self.reward_normalization_value)]

# Step method to be implemented in derived classes
def step(self, action):
Expand Down Expand Up @@ -295,7 +296,7 @@ def __init__(self, env_settings):
player_spec_dict = {}

# Adding env additional observations (side-specific)
for k, v in self.add_obs.items():
for k, v in self.ram_states.items():

if k == "stage":
continue
Expand Down Expand Up @@ -323,13 +324,13 @@ def __init__(self, env_settings):

player_spec_dict["actions"] = spaces.Dict(actions_dict)
observation_space_dict["P1"] = spaces.Dict(player_spec_dict)
observation_space_dict["stage"] = spaces.Box(low=self.add_obs["stage"][1],
high=self.add_obs["stage"][2],
observation_space_dict["stage"] = spaces.Box(low=self.ram_states["stage"][1],
high=self.ram_states["stage"][2],
shape=(1,), dtype=np.int8)

self.observation_space = spaces.Dict(observation_space_dict)

def add_obs_integration(self, frame, data):
def ram_states_integration(self, frame, data):

observation = {}
observation["frame"] = frame
Expand All @@ -338,7 +339,7 @@ def add_obs_integration(self, frame, data):
player_spec_dict = {}

# Adding env additional observations (side-specific)
for k, v in self.add_obs.items():
for k, v in self.ram_states.items():

if k == "stage":
continue
Expand All @@ -364,7 +365,7 @@ def step(self, action):

self.frame, reward, done, data = self.step_complete(action)

observation = self.add_obs_integration(self.frame, data)
observation = self.ram_states_integration(self.frame, data)

return observation, reward, done,\
{"round_done": data["round_done"], "stage_done": data["stage_done"],
Expand All @@ -373,7 +374,7 @@ def step(self, action):
# Reset the environment
def reset(self):
self.frame, data, self.player_side = self.arena_engine.reset()
observation = self.add_obs_integration(self.frame, data)
observation = self.ram_states_integration(self.frame, data)
return observation

# DIAMBRA Gym base class providing frame and additional info as observations
Expand All @@ -393,7 +394,7 @@ def __init__(self, env_settings):
player_spec_dict = {}

# Adding env additional observations (side-specific)
for k, v in self.add_obs.items():
for k, v in self.ram_states.items():

if k == "stage":
continue
Expand Down Expand Up @@ -430,13 +431,13 @@ def __init__(self, env_settings):
player_dict_p2 = player_spec_dict.copy()
observation_space_dict["P2"] = spaces.Dict(player_dict_p2)

observation_space_dict["stage"] = spaces.Box(low=self.add_obs["stage"][1],
high=self.add_obs["stage"][2],
observation_space_dict["stage"] = spaces.Box(low=self.ram_states["stage"][1],
high=self.ram_states["stage"][2],
shape=(1,), dtype=np.int8)

self.observation_space = spaces.Dict(observation_space_dict)

def add_obs_integration(self, frame, data):
def ram_states_integration(self, frame, data):

observation = {}
observation["frame"] = frame
Expand All @@ -447,7 +448,7 @@ def add_obs_integration(self, frame, data):
player_spec_dict = {}

# Adding env additional observations (side-specific)
for k, v in self.add_obs.items():
for k, v in self.ram_states.items():

if k == "stage":
continue
Expand All @@ -474,7 +475,7 @@ def step(self, action):

self.frame, reward, done, data = self.step_complete(action)

observation = self.add_obs_integration(self.frame, data)
observation = self.ram_states_integration(self.frame, data)

return observation, reward, done,\
{"round_done": data["round_done"], "stage_done": data["stage_done"],
Expand All @@ -483,5 +484,5 @@ def step(self, action):
# Reset the environment
def reset(self):
self.frame, data, self.player_side = self.arena_engine.reset()
observation = self.add_obs_integration(self.frame, data)
observation = self.ram_states_integration(self.frame, data)
return observation
23 changes: 11 additions & 12 deletions diambra/arena/arena_imitation_learning_gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def step(self, dummy_action):
done_flags = self.rl_traj_dict["done_flags"][self.step_idx]

if (done_flags[0] or done_flags[1] or done_flags[2]) and not done:
self.shift_counter += self.frame_n_channels-1
self.shift_counter += self.frame_n_channels - 1

# Observation retrieval
observation = self.obs_retrieval()
Expand Down Expand Up @@ -306,7 +306,7 @@ def obs_retrieval(self, reset_shift=0):
observation[:, :, iframe] = self.rl_traj_dict["frames"][self.step_idx +
self.shift_counter + iframe - reset_shift]
# Storing last observation for rendering
self.last_obs = observation[:, :, self.frame_n_channels-1]
self.last_obs = observation[:, :, self.frame_n_channels - 1]

return observation

Expand Down Expand Up @@ -338,8 +338,7 @@ def __init__(self, traj_files_list, rank=0, total_cpus=1):
# Specific observation retrieval
def obs_retrieval(self, reset_shift=0):
# Observation retrieval
observation = self.rl_traj_dict["add_obs"][self.step_idx +
1 - reset_shift].copy()
observation = self.rl_traj_dict["ram_states"][self.step_idx + 1 - reset_shift].copy()

# Frame
observation["frame"] = np.zeros(
Expand All @@ -348,7 +347,7 @@ def obs_retrieval(self, reset_shift=0):
observation["frame"][:, :, iframe] = self.rl_traj_dict["frames"][self.step_idx +
self.shift_counter + iframe - reset_shift]
# Storing last observation for rendering
self.last_obs = observation["frame"][:, :, self.frame_n_channels-1]
self.last_obs = observation["frame"][:, :, self.frame_n_channels - 1]

return observation

Expand All @@ -366,12 +365,12 @@ def generate_p2_experience_from_p1(self):
super().generate_p2_experience_from_p1()

# Process Additiona Obs for P2 (copy them in P1 position)
for add_obs in self.rl_traj_dict_p2["add_obs"]:
add_obs.pop("P1")
add_obs["P1"] = add_obs.pop("P2")
add_obs["stage"] = 0
for ram_states in self.rl_traj_dict_p2["ram_states"]:
ram_states.pop("P1")
ram_states["P1"] = ram_states.pop("P2")
ram_states["stage"] = 0

# Remove P2 info from P1 Observation
for add_obs in self.rl_traj_dict["add_obs"]:
add_obs.pop("P2")
add_obs["stage"] = 0
for ram_states in self.rl_traj_dict["ram_states"]:
ram_states.pop("P2")
ram_states["stage"] = 0
Loading