forked from Code-Bullet/Car-QLearning
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotValues.py
More file actions
57 lines (46 loc) · 1.62 KB
/
plotValues.py
File metadata and controls
57 lines (46 loc) · 1.62 KB
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
import polars as pl
import matplotlib.pyplot as plt
# Define the function to read and process the data
def process_data(file_path, variable, window_size):
# Read the file
with open(file_path, 'r') as file:
lines = file.readlines()
# Initialize lists to hold the extracted data
episodes = []
values = []
# Extract the relevant data
for line in lines:
parts = line.split('\t')
episode = int(parts[0].split(': ')[1])
value = float([part.split(': ')[1] for part in parts if part.startswith(variable)][0])
episodes.append(episode)
values.append(value)
# Create a Polars DataFrame
df = pl.DataFrame({
"Episode": episodes,
variable: values
})
# Compute the rolling average
df = df.with_columns([
pl.col(variable).rolling_mean(window_size).alias('Rolling Average')
])
return df
# Define the function to plot the data
def plot_data(df, variable):
plt.figure(figsize=(10, 6))
plt.plot(df['Episode'], df[variable], label=variable)
plt.plot(df['Episode'], df['Rolling Average'], label='Rolling Average', color='orange')
plt.xlabel('Episode')
plt.ylabel(variable)
plt.title(f'{variable} and its Rolling Average')
plt.legend()
plt.grid(True)
plt.show()
# Path to your file
file_path = 'QLearningFromOldMate_Input.txt'
variable = 'lifespan' # Change this to the variable you want to visualize
window_size = 100 # Adjust the window size for the rolling average
# Process the data
df = process_data(file_path, variable, window_size)
# Plot the data
plot_data(df, variable)