MLE-Pacman/learning.py

59 lines
1.4 KiB
Python

import matplotlib.pyplot as plt
import pandas as pd
from game import run_game
from util import initial_q_fill
EPSILON = 0.5
ALPHA = 0.3
GAMMA = 0.8
def runTry(EPSILON, ALPHA, GAMMA):
"""
state: (x_distance_to_ghost, y_distance_to_ghost, next_cookie_Direction)
action: Direction
q_value: (state, action)
"""
AMOUNT_RUNS = 5000
q_values = {}
initial_q_fill(q_values)
cookies_per_run = []
# Amount of single runs
for x in range(AMOUNT_RUNS):
amount_cookies_ate = run_game(q_values, EPSILON, ALPHA, GAMMA)
cookies_per_run.append(amount_cookies_ate)
# print(f"Run {x}: {amount_cookies_ate} cookies ate\n")
wins = 0
for element in cookies_per_run:
if element == 20:
wins += 1
print(f"Win percentage: {wins/AMOUNT_RUNS}%")
return cookies_per_run
cookies_per_run = runTry(EPSILON, ALPHA, GAMMA)
window_size = 100 # Adjust based on your needs
rolling_avg = pd.Series(cookies_per_run).rolling(window=window_size, center=True).mean()
plt.figure(figsize=(12, 6))
plt.plot(cookies_per_run, alpha=0.2, label='Raw Data', linewidth=0.5, color='gray')
plt.plot(rolling_avg, label=f'{window_size}-point Moving Average',
linewidth=2, color='blue')
plt.title("Data with Rolling Average")
plt.xlabel("Index")
plt.ylabel("Value")
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()