Skip to content
Extraits de code Groupes Projets
plot.py 1,64 ko
Newer Older
  • Learn to ignore specific revisions
  • Adrien Payen's avatar
    Adrien Payen a validé
    import numpy as np
    import random as rd
    import matplotlib.pyplot as plt
    from tmc import TransitionMatrixCalculator as tmc
    
    Adrien Payen's avatar
    Adrien Payen a validé
    from markovDecision import MarkovDecisionSolver as mD
    
    Adrien Payen's avatar
    Adrien Payen a validé
    from validation import Validation
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    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()
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # 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
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        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")
    
    Adrien Payen's avatar
    Adrien Payen a validé
        plt.show()
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
        # 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()
    
    Adrien Payen's avatar
    Adrien Payen a validé
    
    
    Adrien Payen's avatar
    Adrien Payen a validé
    # Call make_plots function
    if __name__ == "__main__":
        make_plots()