Spaces:
Sleeping
Sleeping
File size: 1,055 Bytes
01901c5 |
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 |
import numpy as np
import gymnasium as gym
from tqdm import tqdm
policy_file = "policy.npy"
n_steps = 500
n_test_episodes = 10
def main():
print("=" * 80)
print("# Cliff Walking - Monte Carlo Test")
print("=" * 80)
# save the policy
print(f"Loading policy from file: '{policy_file}'...")
Pi = np.load(policy_file)
print("Policy:")
print(Pi)
print(f"shape: {Pi.shape}")
_, n_actions = Pi.shape
print("=" * 80)
print(f"Testing policy for {n_test_episodes} episodes...")
env = gym.make("CliffWalking-v0", render_mode="human")
for e in range(n_test_episodes):
print(f"Test #{e + 1}:", end=" ")
state, _ = env.reset()
for _ in range(n_steps):
action = np.random.choice(n_actions, p=Pi[state])
next_state, reward, done, _, _ = env.step(action)
state = next_state
if done:
print("Success!")
break
else:
print("Failed!")
env.close()
if __name__ == "__main__":
main()
|