Views
No views yet
CartPole-v1 environment.{'h_size': 16, 'lr': 0.01, 'gamma': 0.99, 'n_training_episodes': 1000, 'max_t': 1000, 'print_every': 100}1import gymnasium as gym
2import torch
3from your_policy_file import Policy # Assuming Policy class is in 'your_policy_file.py'
4
5# Load the environment
6env = gym.make("CartPole-v1")
7
8# Instantiate the policy network (adjust sizes as per your model)
9s_size = env.observation_space.shape[0]
10a_size = env.action_space.n
11h_size = 16
12policy = Policy(s_size, a_size, h_size)
13
14# Load the trained weights
15policy.load_state_dict(torch.load("model.pt"))
16policy.eval()
17
18# Test the agent
19state, info = env.reset()
20total_reward = 0
21terminated = False
22truncated = False
23while not terminated and not truncated:
24 action, _ = policy.act(state)
25 state, reward, terminated, truncated, info = env.step(action)
26 total_reward += reward
27print(f"Test Episode Reward: {total_reward}")
28env.close()