2023-11-23 11:44:23 +00:00
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
2023-11-19 03:27:47 +00:00
|
|
|
from game import Game
|
2023-11-17 02:19:03 +00:00
|
|
|
from tqdm import tqdm
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-11-19 03:27:47 +00:00
|
|
|
from os import environ
|
|
|
|
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
|
2023-09-27 18:03:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-11-23 11:44:23 +00:00
|
|
|
|
|
|
|
n_episodes = 1000
|
|
|
|
game_len = 10000
|
2023-11-17 02:19:03 +00:00
|
|
|
|
|
|
|
figure_file = 'plots/score.png'
|
|
|
|
best_score = 0
|
|
|
|
avg_score = 0
|
|
|
|
|
2023-09-27 18:03:37 +00:00
|
|
|
game = Game()
|
2023-11-17 02:19:03 +00:00
|
|
|
|
2023-11-23 11:44:23 +00:00
|
|
|
agent_list = []
|
|
|
|
exp_points_list = []
|
|
|
|
score_history = np.zeros(
|
|
|
|
shape=(len(game.level.player_sprites), n_episodes, ))
|
|
|
|
best_score = np.zeros(len(game.level.player_sprites))
|
|
|
|
avg_score = np.zeros(len(game.level.player_sprites))
|
2023-11-19 03:27:47 +00:00
|
|
|
for i in tqdm(range(n_episodes)):
|
2023-11-17 02:19:03 +00:00
|
|
|
# TODO: Make game.level.reset_map() so we don't __init__ everything all the time (such a waste)
|
2023-11-19 03:27:47 +00:00
|
|
|
if i != 0:
|
|
|
|
game.level.__init__(reset=True)
|
2023-11-17 02:19:03 +00:00
|
|
|
# TODO: Make game.level.reset_map() so we don't pull out and load the agent every time (There is -definitevly- a better way)
|
2023-11-23 11:44:23 +00:00
|
|
|
for player in game.level.player_sprites:
|
|
|
|
for agent in agent_list:
|
|
|
|
player.agent = agent_list[player.player_id]
|
|
|
|
player.stats.exp = score_history[player.player_id][i-1]
|
2023-11-19 03:27:47 +00:00
|
|
|
|
2023-11-17 02:19:03 +00:00
|
|
|
agent_list = []
|
2023-11-23 11:44:23 +00:00
|
|
|
|
|
|
|
for j in range(game_len):
|
2023-11-17 02:19:03 +00:00
|
|
|
if not game.level.done:
|
2023-11-23 11:44:23 +00:00
|
|
|
|
2023-11-17 02:19:03 +00:00
|
|
|
game.run()
|
2023-11-23 11:44:23 +00:00
|
|
|
game.calc_score()
|
|
|
|
|
|
|
|
if (j == game_len-1 or game.level.done) and game.level.enemy_sprites != []:
|
|
|
|
for player in game.level.player_sprites:
|
|
|
|
for enemy in game.level.enemy_sprites:
|
|
|
|
player.stats.exp *= .95
|
2023-11-17 02:19:03 +00:00
|
|
|
else:
|
|
|
|
break
|
2023-11-23 11:44:23 +00:00
|
|
|
|
2023-11-17 02:19:03 +00:00
|
|
|
for player in game.level.player_sprites:
|
2023-11-23 11:44:23 +00:00
|
|
|
agent_list.append(player.agent)
|
|
|
|
exp_points = player.stats.exp
|
|
|
|
score_history[player.player_id][i] = exp_points
|
|
|
|
avg_score[player.player_id] = np.mean(
|
|
|
|
score_history[player.player_id])
|
|
|
|
if avg_score[player.player_id] >= best_score[player.player_id]:
|
|
|
|
player.agent.save_models()
|
|
|
|
best_score[player.player_id] = avg_score[player.player_id]
|
2023-11-17 02:19:03 +00:00
|
|
|
|
2023-11-23 11:44:23 +00:00
|
|
|
print(
|
|
|
|
f"\nCumulative score for player {player.player_id}:\
|
|
|
|
{score_history[0][i]}\
|
|
|
|
\nAverage score for player {player.player_id}:\
|
|
|
|
{avg_score[player.player_id]}\
|
|
|
|
\nBest score for player {player.player_id}:\
|
|
|
|
{best_score[player.player_id]}")
|
2023-11-17 02:19:03 +00:00
|
|
|
|
2023-11-23 11:44:23 +00:00
|
|
|
plt.plot(score_history[0])
|
2023-11-17 02:19:03 +00:00
|
|
|
|
2023-11-23 11:44:23 +00:00
|
|
|
game.quit()
|
2023-11-17 02:19:03 +00:00
|
|
|
|
2023-11-23 11:44:23 +00:00
|
|
|
plt.show()
|