You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.8 KiB

12 months ago
  1. from typing import Dict, Optional
  2. from ray.rllib.env.env_context import EnvContext
  3. from ray.rllib.policy import Policy
  4. from ray.rllib.utils.typing import EnvType, PolicyID
  5. from ray.rllib.algorithms.algorithm import Algorithm
  6. from ray.rllib.env.base_env import BaseEnv
  7. from ray.rllib.evaluation import RolloutWorker
  8. from ray.rllib.evaluation.episode import Episode
  9. from ray.rllib.evaluation.episode_v2 import EpisodeV2
  10. from ray.rllib.algorithms.callbacks import DefaultCallbacks, make_multi_callbacks
  11. import matplotlib.pyplot as plt
  12. import tensorflow as tf
  13. class MyCallbacks(DefaultCallbacks):
  14. def on_episode_start(self, *, worker: RolloutWorker, base_env: BaseEnv, policies: Dict[PolicyID, Policy], episode, env_index, **kwargs) -> None:
  15. # print(F"Epsiode started Environment: {base_env.get_sub_environments()}")
  16. env = base_env.get_sub_environments()[0]
  17. episode.user_data["count"] = 0
  18. episode.user_data["ran_into_lava"] = []
  19. episode.user_data["goals_reached"] = []
  20. episode.user_data["ran_into_adversary"] = []
  21. episode.hist_data["ran_into_lava"] = []
  22. episode.hist_data["goals_reached"] = []
  23. episode.hist_data["ran_into_adversary"] = []
  24. # print("On episode start print")
  25. # print(env.printGrid())
  26. # print(worker)
  27. # print(env.action_space.n)
  28. # print(env.actions)
  29. # print(env.mission)
  30. # print(env.observation_space)
  31. # plt.imshow(img)
  32. # plt.show()
  33. def on_episode_step(self, *, worker: RolloutWorker, base_env: BaseEnv, policies, episode, env_index, **kwargs) -> None:
  34. episode.user_data["count"] = episode.user_data["count"] + 1
  35. env = base_env.get_sub_environments()[0]
  36. # print(env.printGrid())
  37. if hasattr(env, "adversaries"):
  38. for adversary in env.adversaries.values():
  39. if adversary.cur_pos[0] == env.agent_pos[0] and adversary.cur_pos[1] == env.agent_pos[1]:
  40. print(F"Adversary ran into agent. Adversary {adversary.cur_pos}, Agent {env.agent_pos}")
  41. # assert False
  42. def on_episode_end(self, *, worker: RolloutWorker, base_env: BaseEnv, policies, episode, env_index, **kwargs) -> None:
  43. # print(F"Epsiode end Environment: {base_env.get_sub_environments()}")
  44. env = base_env.get_sub_environments()[0]
  45. agent_tile = env.grid.get(env.agent_pos[0], env.agent_pos[1])
  46. ran_into_adversary = False
  47. if hasattr(env, "adversaries"):
  48. adversaries = env.adversaries.values()
  49. for adversary in adversaries:
  50. if adversary.cur_pos[0] == env.agent_pos[0] and adversary.cur_pos[1] == env.agent_pos[1]:
  51. ran_into_adversary = True
  52. break
  53. episode.user_data["goals_reached"].append(agent_tile is not None and agent_tile.type == "goal")
  54. episode.user_data["ran_into_lava"].append(agent_tile is not None and agent_tile.type == "lava")
  55. episode.user_data["ran_into_adversary"].append(ran_into_adversary)
  56. episode.custom_metrics["reached_goal"] = agent_tile is not None and agent_tile.type == "goal"
  57. episode.custom_metrics["ran_into_lava"] = agent_tile is not None and agent_tile.type == "lava"
  58. episode.custom_metrics["ran_into_adversary"] = ran_into_adversary
  59. #print("On episode end print")
  60. # print(env.printGrid())
  61. episode.hist_data["goals_reached"] = episode.user_data["goals_reached"]
  62. episode.hist_data["ran_into_lava"] = episode.user_data["ran_into_lava"]
  63. episode.hist_data["ran_into_adversary"] = episode.user_data["ran_into_adversary"]
  64. def on_evaluate_start(self, *, algorithm: Algorithm, **kwargs) -> None:
  65. print("Evaluate Start")
  66. def on_evaluate_end(self, *, algorithm: Algorithm, evaluation_metrics: dict, **kwargs) -> None:
  67. print("Evaluate End")