exp per player now resets for each episode

This commit is contained in:
Vasilis Valatsos 2023-12-06 13:03:59 +01:00
parent ca29a0e6dc
commit 0aea150454
9 changed files with 54 additions and 40 deletions

Binary file not shown.

Binary file not shown.

View file

@ -231,6 +231,8 @@ class Player(pygame.sprite.Sprite):
self.get_current_state() self.get_current_state()
print(self.reward_features)
def update(self): def update(self):
if not self.is_dead(): if not self.is_dead():

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

@ -205,13 +205,18 @@ class Level:
self.obstacle_sprites) self.obstacle_sprites)
for player in self.player_sprites: for player in self.player_sprites:
player.animation.import_assets( player.animation.import_assets(
choice(self.possible_player_locations)) choice(self.possible_player_locations))
player.stats.health\ player.stats.health\
= player.stats.stats['health'] = player.stats.stats['health']
player.stats.energy\ player.stats.energy\
= player.stats.stats['energy'] = player.stats.stats['energy']
player.stats.exp = 0
self.get_entities() self.get_entities()
self.get_distance_direction() self.get_distance_direction()
self.dead_players = np.zeros(self.n_players) self.dead_players = np.zeros(self.n_players)

View file

@ -189,8 +189,11 @@ if __name__ == "__main__":
# Gather information about the episode # Gather information about the episode
for player in game.level.player_sprites: for player in game.level.player_sprites:
score = np.mean(player.reward_features)
print(player.reward_features)
# Update score # Update score
score_history[player.player_id][episode] = player.stats.exp score_history[player.player_id][episode] = np.mean(score)
# Update actor/critic loss # Update actor/critic loss
actor_loss[player.player_id][episode] = np.mean( actor_loss[player.player_id][episode] = np.mean(
@ -203,13 +206,13 @@ if __name__ == "__main__":
episode_total_loss) episode_total_loss)
# Check for new best score # Check for new best score
if player.stats.exp > best_score[player.player_id]: if score > best_score[player.player_id]:
print(f"\nNew best score for player {player.player_id}:\ print(f"\nNew best score for player {player.player_id}:\
{player.stats.exp}\ {score}\
\nOld best score for player {player.player_id}: \ \nOld best score for player {player.player_id}: \
{best_score[player.player_id]}") {best_score[player.player_id]}")
best_score[player.player_id] = player.stats.exp best_score[player.player_id] = score
print(f"Saving models for player {player.player_id}...") print(f"Saving models for player {player.player_id}...")
@ -220,44 +223,48 @@ if __name__ == "__main__":
print(f"Models saved to {chkpt_path}") print(f"Models saved to {chkpt_path}")
plt.figure()
plt.title("Player Performance")
plt.xlabel("Episode")
plt.ylabel("Score")
plt.legend([f"Player {num}" for num in range(n_players)])
for player_score in score_history:
plt.plot(player_score)
plt.savefig(f"{figure_folder}/score.png")
plt.close()
plt.figure()
plt.suptitle("Actor Loss")
plt.xlabel("Episode")
plt.ylabel("Loss")
plt.legend([f"Agent {num}" for num in range(n_players)])
for actor in actor_loss:
plt.plot(actor)
plt.savefig(f"{figure_folder}/actor_loss.png")
plt.close()
plt.figure()
plt.suptitle("Critic Loss")
plt.xlabel("Episode")
plt.ylabel("Loss")
plt.legend([f"Agent {num}" for num in range(n_players)])
for critic in critic_loss:
plt.plot(critic)
plt.savefig(f"{figure_folder}/critic_loss.png")
plt.close()
plt.figure()
plt.suptitle("Total Loss")
plt.xlabel("Episode")
plt.ylabel("Loss")
plt.legend([f"Agent {num}" for num in range(n_players)])
for total in total_loss:
plt.plot(total)
plt.savefig(f"{figure_folder}/total_loss.png")
plt.close()
# End of training session # End of training session
print("End of episodes.\ print("End of episodes.\
\nExiting game...") \nExiting game...")
plt.figure()
plt.title("Player Performance")
plt.xlabel("Episode")
plt.ylabel("Score")
plt.legend([f"Player {num}" for num in range(n_players)])
for player_score in score_history:
plt.plot(player_score)
plt.savefig(f"{figure_folder}/score.png")
plt.figure()
plt.suptitle("Actor Loss")
plt.xlabel("Episode")
plt.ylabel("Loss")
plt.legend([f"Agent {num}" for num in range(n_players)])
for actor in actor_loss:
plt.plot(actor)
plt.savefig(f"{figure_folder}/actor_loss.png")
plt.figure()
plt.suptitle("Critic Loss")
plt.xlabel("Episode")
plt.ylabel("Loss")
plt.legend([f"Agent {num}" for num in range(n_players)])
for critic in critic_loss:
plt.plot(critic)
plt.savefig(f"{figure_folder}/critic_loss.png")
plt.figure()
plt.suptitle("Total Loss")
plt.xlabel("Episode")
plt.ylabel("Loss")
plt.legend([f"Agent {num}" for num in range(n_players)])
for total in total_loss:
plt.plot(total)
plt.savefig(f"{figure_folder}/total_loss.png")
game.quit() game.quit()