Skip to content

Instantly share code, notes, and snippets.

@tsu-nera
Last active June 9, 2017 18:49
Show Gist options
  • Select an option

  • Save tsu-nera/a3fa51cf6ab27c34e4456038ef5055da to your computer and use it in GitHub Desktop.

Select an option

Save tsu-nera/a3fa51cf6ab27c34e4456038ef5055da to your computer and use it in GitHub Desktop.
OpenAI Gym FrozenLake-v0 random search https://gym.openai.com/evaluations/eval_iinFj8fUSvOOYHWXwjjAw
import gym
import numpy as np
from gym import wrappers
env = gym.make("FrozenLake-v0")
env = wrappers.Monitor(env, '/tmp/cartpole-experiment-2')
env.reset();
n_states = env.observation_space.n
n_actions = env.action_space.n
def get_random_policy():
return np.random.choice(n_actions, size=n_states)
def sample_reward(env, policy, t_max=100):
s = env.reset()
total_reward = 0
for _ in range(t_max):
s, reward, is_done, _ = env.step(policy[s])
total_reward += reward
if is_done:
break
return total_reward
def evaluate(policy, n_times=100):
rewards = [sample_reward(env, policy) for _ in range(n_times)]
return float(np.mean(rewards))
def print_policy(policy):
lake = "SFFFFHFHFFFHHFFG"
arrows = ['>^v<'[a] for a in policy]
signs = [arrow if tile in "SF" else tile for arrow, tile in zip(arrows, lake)]
for i in range(0, 16, 4):
print(' '.join(signs[i:i+4]))
best_policy = None
best_score = -float('inf')
from tqdm import tqdm
for i in tqdm(range(10000)):
policy = get_random_policy()
score = evaluate(policy)
if score > best_score:
best_score = score
best_policy = policy
print("\n")
print("New best score:", score)
print("Best policy:")
print_policy(best_policy)
env.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment