Skip to content

Commit

Permalink
logging and test created for close_before_reset, gamma and SISL pass …
Browse files Browse the repository at this point in the history
…new test
  • Loading branch information
MarioJayakumar committed Apr 29, 2020
1 parent b78845c commit 76694b5
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ def render(self, mode="human"):
pygame.display.flip()

def close(self):
if not self.closed:
if not self.has_reset:
EnvLogger.warn_close_before_reset()
elif not self.closed:
self.closed = True
if not self.render_on:
EnvLogger.warn_close_unrendered_env()
Expand Down
4 changes: 3 additions & 1 deletion pettingzoo/gamma/pistonball/pistonball.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ def enable_render(self):
self.reset()

def close(self):
if not self.closed:
if not self.has_reset:
EnvLogger.warn_close_before_reset()
elif not self.closed:
self.closed = True
if not self.renderOn:
EnvLogger.warn_close_unrendered_env()
Expand Down
4 changes: 3 additions & 1 deletion pettingzoo/gamma/prison/prison.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ def convert_coord_to_prisoner_id(self, c):
return self.prisoner_mapping[c]

def close(self):
if not self.closed:
if not self.has_reset:
EnvLogger.warn_close_before_reset()
elif not self.closed:
self.closed = True
if self.rendering:
pygame.event.pump()
Expand Down
5 changes: 4 additions & 1 deletion pettingzoo/sisl/multiwalker/multiwalker.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def reset(self, observe=True):
return self.observe(self.agent_selection)

def close(self):
self.env.close()
if not self.has_reset:
EnvLogger.warn_close_before_reset()
else:
self.env.close()

def render(self, mode="human"):
if not self.has_reset:
Expand Down
4 changes: 3 additions & 1 deletion pettingzoo/sisl/pursuit/pursuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def reset(self, observe=True):
return self.observe(self.agent_selection)

def close(self):
if not self.closed and self.has_reset:
if not self.has_reset:
EnvLogger.warn_close_before_reset()
elif not self.closed and self.has_reset:
self.closed = True
self.env.close()

Expand Down
5 changes: 4 additions & 1 deletion pettingzoo/sisl/waterworld/waterworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def reset(self, observe=True):
return self.observe(self.agent_selection)

def close(self):
self.env.close()
if not self.has_reset:
EnvLogger.warn_close_before_reset()
else:
self.env.close()

def render(self, mode="human"):
if not self.has_reset:
Expand Down
2 changes: 2 additions & 0 deletions pettingzoo/tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ def test_requires_reset(env):
if "render.modes" in env.metadata and len(env.metadata["render.modes"]) > 0:
if not check_asserts(lambda: env.render(), "reset() needs to be called before render"):
warnings.warn("env.render should call EnvLogger.error_render_before_reset if it is called before reset")
if not check_warns(lambda: env.close(), "reset() needs to be called before close."):
warnings.warn("env should warn_close_before_reset() if closing before reset()")


def test_bad_actions(env):
Expand Down
7 changes: 4 additions & 3 deletions pettingzoo/utils/env_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def warn_action_is_NaN(backup_policy):
def warn_close_unrendered_env():
EnvLogger._generic_warning("[WARNING]: Called close on an unrendered environment.")

@staticmethod
def warn_close_before_reset():
EnvLogger._generic_warning("[WARNING]: reset() needs to be called before close.")

@staticmethod
def warn_on_illegal_move():
EnvLogger._generic_warning("[WARNING]: Illegal move made, game terminating with current player losing. \nenv.infos[player]['legal_moves'] contains a list of all legal moves that can be chosen.")
Expand All @@ -54,9 +58,6 @@ def error_observe_before_reset():
def error_step_before_reset():
assert False, "reset() needs to be called before step"

@staticmethod
def error_close_before_reset():
assert False, "reset() needs to be called before close"

@staticmethod
def error_render_before_reset():
Expand Down

0 comments on commit 76694b5

Please sign in to comment.