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

Fix: fix the gym box shape when using a grid based observation #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 6 additions & 11 deletions lbforaging/foraging/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def _get_observation_space(self):
high_obs = np.array(max_obs)
assert low_obs.shape == high_obs.shape
return gym.spaces.Box(
low=low_obs, high=high_obs, shape=[len(low_obs)], dtype=np.float32
low=low_obs, high=high_obs, shape=low_obs.shape, dtype=np.float32
)

@classmethod
Expand Down Expand Up @@ -436,7 +436,7 @@ def _is_valid_action(self, player, action):
elif action == Action.LOAD:
return self.adjacent_food(*player.position) > 0

self.logger.error("Undefined action {} from {}".format(action, player.name))
self.logger.error(f"Undefined action {action} from {player.name}")
raise ValueError("Undefined action")

def _transform_to_neighborhood(self, center, sight, position):
Expand Down Expand Up @@ -574,13 +574,11 @@ def get_agent_grid_bounds(agent_x, agent_y):
get_agent_grid_bounds(*player.position) for player in self.players
]
nobs = tuple(
[
layers[:, start_x:end_x, start_y:end_y]
for start_x, end_x, start_y, end_y in agents_bounds
]
layers[:, start_x:end_x, start_y:end_y]
for start_x, end_x, start_y, end_y in agents_bounds
)
else:
nobs = tuple([make_obs_array(obs) for obs in observations])
nobs = tuple(make_obs_array(obs) for obs in observations)

# check the space of obs
for i, obs in enumerate(nobs):
Expand Down Expand Up @@ -631,10 +629,7 @@ def step(self, actions):
for i, (player, action) in enumerate(zip(self.players, actions)):
if action not in self._valid_actions[player]:
self.logger.info(
"{}{} attempted invalid action {}.".format(
player.name, player.position, action
)
)
f"{player.name}{player.position} attempted invalid action {action}.")
actions[i] = Action.NONE

loading_players = set()
Expand Down
19 changes: 19 additions & 0 deletions lbforaging/foraging/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from environment import ForagingEnv
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Delete this file.


env = ForagingEnv(
grid_observation=True,
players=2,
max_player_level=2,
field_size=(8,8),
max_num_food=2,
sight=8,
force_coop=True,
min_player_level=1,
min_food_level=1,
max_food_level=10,
max_episode_steps=100)

nobs, infos = env.reset()
actions = [5,5,0]
nobs, rewards, done, truncated, info = env.step(actions=actions)
print(f"nobs: \n {nobs[0].shape}")