<jupyter_start><jupyter_text>Random CartPole-v1 evaluation (baseline)  Code to reset matplotlib plotting to defaults<jupyter_code># Checking matplotlib defaults
# inline_default_rc = dict(mpl.rcParams)
# default_rc = dict(mpl.rcParamsDefault)
# print( {k:[v,default_rc[k]] for k,v in inline_default_rc.items() if v != default_rc[k]} )

# mpl.rcParams.update(mpl.rcParamsDefault)

import gym
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline

import seaborn as sns
sns.set_style("whitegrid")
from tqdm import tqdm
from utils import stringify_dict
ENV_NAME = "CartPole-v1"
NUM_EPISODES = 100000

env = gym.make(ENV_NAME)
env.seed(0)
total_reward_all_episodes = []

HYPERPARAMETERS = {"num_episodes": NUM_EPISODES}
# For saving results
from datetime import datetime
import os

dt_now = datetime.now()
dt_str = dt_now.strftime("%Y%m%d-%H%M%S")

RESULTS_PATH = "results"
EXP_TYPE = "train-random"
HYPERPARAMETERS_STR = stringify_dict(HYPERPARAMETERS)
FILE_NAME = f"{HYPERPARAMETERS_STR}"
LOG_FILE_NAME = FILE_NAME + ".txt"
PLOT_FILE_NAME = FILE_NAME + ".png"
OUTPUT_PATH = os.path.join(RESULTS_PATH, ENV_NAME, EXP_TYPE, dt_str)
OUTPUT_PATH

if not os.path.isdir(OUTPUT_PATH):
    os.makedirs(OUTPUT_PATH)
    
# Time code
import timeit
start_time = timeit.default_timer()

# Start random training
episode_counter = 0

for _ in tqdm(range(NUM_EPISODES)):
    total_reward = 0.0
    episode_counter+= 1
    obs = env.reset()

    while True:
        action = env.action_space.sample()
        observation, reward, done, _ = env.step(action)
        total_reward += reward
        if done:
            break

    if (episode_counter % 1000 == 0):
        total_reward_all_episodes.append(total_reward)

env.close()

# End timer
WALL_TIME = timeit.default_timer() - start_time<jupyter_output>100%|██████████| 100000/100000 [00:36<00:00, 2742.85it/s]<jupyter_text>Plot and save results<jupyter_code># Preview results
results_df = pd.DataFrame.from_dict({"total_rewards": total_reward_all_episodes})
results_df["episode_id"] = results_df.index
results_df.head(5)
plt.plot(results_df["episode_id"], results_df["total_rewards"]);

plt.title(f"Random agent on {ENV_NAME} for {NUM_EPISODES} episodes");
plt.xlabel("Per 1000 episodes");
plt.ylabel("Reward");
plt.ylim([0, 500]);
plt.xlim([0, 100]);

last_reward = (100, results_df["total_rewards"].iloc[-1])
max_reward = (results_df["total_rewards"].idxmax(), results_df["total_rewards"].max())
min_reward = (results_df["total_rewards"].idxmin(), results_df["total_rewards"].min())

plt.annotate(f"Last {last_reward[1]}", (last_reward[0], last_reward[1]))
plt.annotate(f"Min {min_reward[1]}", (min_reward[0], min_reward[1]))
plt.annotate(f"Max {max_reward[1]}", (max_reward[0], max_reward[1]))

plt.savefig(os.path.join(OUTPUT_PATH, PLOT_FILE_NAME), dpi=300, bbox_inches="tight")
plt.show()
from collections import namedtuple

Results = namedtuple("Results", "min_reward, max_reward, reward_variance, mean_reward")

results = Results(min(results_df.total_rewards), max(results_df.total_rewards), np.std(results_df.total_rewards), np.mean(results_df.total_rewards))

with open(os.path.join(OUTPUT_PATH, LOG_FILE_NAME), "w") as f:
    f.write(f"datetime:{dt_str}\n")
    f.write(f"hyperparameters:{HYPERPARAMETERS_STR}\n")
    tidy_results = stringify_dict(results._asdict()).replace("-", ":")
    f.write(f"results:{tidy_results}\n")
    f.write(f"wall_time:{WALL_TIME}\n")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1419594401599543, 'fraction_numerical': 0.021708083404741504, 'mean_word_length': 6.161554192229039, 'pattern_counts': {'":': 3, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '24230', 'license': 'MIT', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1326, 'n_words': 237, 'path': 'random_evaluation.ipynb', 'repo_name': 'EduardErnest/Deep-Learning-INM707', 'size': '3501'}