in data/envs/metaworld/generate_random_score.py [0:0]
def generate_random_score(task_name):
# Make the environment
env_name = TASK_NAME_TO_ENV_NAME[task_name]
env = gym.make(env_name)
env.reset()
# Initialize the variables
all_episode_rewards = []
tot_episode_rewards = 0 # for one episode
num_timesteps = 0
terminated = truncated = False
while num_timesteps < TOT_NUM_TIMESTEPS or not (terminated or truncated):
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)
tot_episode_rewards += reward
num_timesteps += 1
if terminated or truncated:
env.reset()
all_episode_rewards.append(tot_episode_rewards)
tot_episode_rewards = 0
# Load the scores dictionary
if not os.path.exists(FILENAME):
scores_dict = {}
else:
with open(FILENAME, "r") as file:
scores_dict = json.load(file)
# Add the random scores to the dictionary
if task_name not in scores_dict:
scores_dict[task_name] = {}
scores_dict[task_name]["random"] = {"mean": np.mean(all_episode_rewards), "std": np.std(all_episode_rewards)}
# Save the dictionary to a file
with open(FILENAME, "w") as file:
scores_dict = {
task: {agent: scores_dict[task][agent] for agent in sorted(scores_dict[task])}
for task in sorted(scores_dict)
}
json.dump(scores_dict, file, indent=4)