Newer
Older
import numpy as np
import random as rd
import matplotlib.pyplot as plt
from tmc import TransitionMatrixCalculator as tmc
def make_plots():
layout = [0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 1, 0]
circle = False
validation = Validation(layout, circle)
expec, optimal_policy = mD(layout, circle).solve()
# Plot 1: Theoretical vs Empirical Cost
expected_costs = np.zeros(len(expec))
for start_square in range(len(expec)):
total_turns = 0
for _ in range(10000):
total_turns += validation.play_one_game(start_square)
expected_costs[start_square] = total_turns / 10000
squares = np.arange(len(expec))
plt.plot(squares, expec, label="Theoretical cost")
plt.plot(squares, expected_costs, label="Empirical cost")
plt.xticks(np.arange(0, len(expec), step=1))
plt.grid(True)
plt.xlabel("Square")
plt.ylabel("Cost")
plt.legend()
plt.title("Comparison between the expected cost and the actual cost")
# Plot 2: Expected number of turns for different policies
policies = [optimal_policy, np.ones(len(expec)), np.ones(len(expec)) * 2, np.ones(len(expec)) * 3, np.random.randint(1, 4, len(expec))]
avgn_turns = [Validation(layout, circle).empirical_results() for policy in policies]
names = ["optimal", "safe", "normal", "risky", "random"]
plt.bar(names, avgn_turns)
plt.xlabel("Policy")
plt.ylabel("Cost")
plt.title("Expected number of turns for different policies")
plt.show()
# Call make_plots function
if __name__ == "__main__":
make_plots()