File size: 4,456 Bytes
ba246bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import matplotlib.pyplot as plt
from numpy import ndarray
import seaborn as sns
import numpy as np
from scipy.stats import gaussian_kde

def plot_test_results(rewards: ndarray, wins: ndarray, steps: ndarray, normalized_return: ndarray) -> None:
    """
    Plot the rewards obtained on the test data, the number of times the agent wins, and the length (number of time steps)
    of each episode.
    :param rewards: list of rewards obtained on the test data
    :param wins: list of wins obtained on the test data
    :param steps: list of steps of each episode obtained on the test data
    :param normalized_return: list of normalized return obtained on the test data
    """
    nb_episodes = len(rewards)
    cat = [i for i in range(nb_episodes)]
    plt.figure(figsize=(10, 6))
    plt.subplot(4, 1, 1)  # 4 lignes, 1 colonne, graphique 1
    plt.bar(cat, rewards, label='avg_rewards')
    plt.title('Average rewards on test data')
    plt.legend()

    # Ajouter le deuxième sous-graphe
    plt.subplot(4, 1, 2)  # 4 lignes, 1 colonne, graphique 2
    plt.bar(cat, wins, label='avg_wins', color='orange')
    plt.title('Average wins on test data')
    plt.legend()

    # Ajouter le troisième sous-graphe
    plt.subplot(4, 1, 3)  # 4 lignes, 1 colonne, graphique 3
    plt.bar(cat, steps, label='avg_steps', color='green')
    plt.title('Average length of episodes on test data')
    plt.legend()

    # Ajouter le quatrième sous-graphe
    plt.subplot(4, 1, 4)  # 4 lignes, 1 colonne, graphique 4
    plt.bar(cat, normalized_return, label='avg_normalized_return', color='green')
    plt.title('Average normalized return obtained on test data')
    plt.legend()
    # Ajuster l'espacement entre les sous-graphes
    plt.tight_layout()
    # Afficher les graphiques
    plt.show()


def plot_density(normalized_return: ndarray):

    # Figure
    plt.figure(figsize=(15, 4))

    # --- 1. Histogramme brut ---
    plt.subplot(1, 3, 1)
    plt.hist(normalized_return, bins=10, color="green", edgecolor="black")
    plt.title("Histogramme brut")
    plt.xlabel("Retour normalisé")
    plt.ylabel("Nombre de maillages")

    # --- 2. Histogramme + densité normalisée ---
    plt.subplot(1, 3, 2)
    sns.histplot(normalized_return, bins=10, color="green", edgecolor="black", stat="density", kde=False,
                 label="Histogramme (densité)")
    sns.kdeplot(normalized_return, color="red", linewidth=2, label="Densité KDE", clip=(0,1))
    plt.title("Histogramme + densité normalisée")
    plt.xlabel("Retour normalisé")
    plt.ylabel("Densité")
    plt.legend()

    # --- 3. Histogramme + densité mise à l’échelle ---
    plt.subplot(1, 3, 3)
    counts, bins, _ = plt.hist(normalized_return, bins=10, color="green", edgecolor="black", label="Histogramme")

    # KDE avec mise à l'échelle
    x = np.linspace(0, 1, 200)
    kde = gaussian_kde(normalized_return)
    kde_scaled = kde(x) * len(normalized_return) * (bins[1] - bins[0])
    plt.plot(x, kde_scaled, color="red", linewidth=2, label="Densité mise à l’échelle")

    plt.title("Histogramme + densité à l’échelle")
    plt.xlabel("Retour normalisé")
    plt.ylabel("Nombre de maillages")
    plt.xlim(0, 1)
    plt.legend()

    plt.tight_layout()
    plt.show()


def plot_training_results(rewards: ndarray, wins: ndarray, steps: ndarray) -> None:
    """
    Plot the rewards obtained during training, the number of times the agent wins, and the length (number of time steps)
    of each episode.
    :param rewards: list of rewards obtained during training
    :param wins: list of wins obtained during training
    :param steps: list of steps obtained during training
    """
    nb_episodes = len(rewards)
    real_runs = 1

    plt.figure()
    plt.plot(rewards)
    plt.xlabel('Episodes')
    plt.ylabel('Rewards')
    plt.title('Learning Rewards, nb_episodes={}'.format(nb_episodes) + ', nb_runs={}'.format(real_runs))
    plt.legend(loc="best")
    plt.show()
    plt.figure()
    plt.plot(wins)
    plt.xlabel('Episodes')
    plt.ylabel('Wins')
    plt.title('Learning Wins, nb_episodes={}'.format(nb_episodes) + ', nb_runs={}'.format(real_runs))
    plt.legend(loc="best")
    plt.show()
    plt.figure()
    plt.plot(steps)
    plt.xlabel('Episodes')
    plt.ylabel('Number of steps per episode')
    plt.title('Learning steps, nb_episodes={}'.format(nb_episodes) + ', nb_runs={}'.format(real_runs))
    plt.legend(loc="best")
    plt.show()