# Use Closed-Form Policy to Play CartPole-v1¶

In [1]:

    import sys
    import logging
    import itertools

    import numpy as np
    np.random.seed(0)
    import gym

    logging.basicConfig(level=logging.INFO,
            format='%(asctime)s [%(levelname)s] %(message)s',
            stream=sys.stdout, datefmt='%H:%M:%S')

In [2]:

    env = gym.make('CartPole-v1')
    for key in vars(env.spec):
        logging.info('%s: %s', key, vars(env.spec)[key])
    for key in vars(env.unwrapped):
        logging.info('%s: %s', key, vars(env.unwrapped)[key])

    00:00:00 [INFO] id: CartPole-v1
    00:00:00 [INFO] entry_point: gym.envs.classic_control:CartPoleEnv
    00:00:00 [INFO] reward_threshold: 475.0
    00:00:00 [INFO] nondeterministic: False
    00:00:00 [INFO] max_episode_steps: 500
    00:00:00 [INFO] order_enforce: True
    00:00:00 [INFO] _kwargs: {}
    00:00:00 [INFO] _env_name: CartPole
    00:00:00 [INFO] gravity: 9.8
    00:00:00 [INFO] masscart: 1.0
    00:00:00 [INFO] masspole: 0.1
    00:00:00 [INFO] total_mass: 1.1
    00:00:00 [INFO] length: 0.5
    00:00:00 [INFO] polemass_length: 0.05
    00:00:00 [INFO] force_mag: 10.0
    00:00:00 [INFO] tau: 0.02
    00:00:00 [INFO] kinematics_integrator: euler
    00:00:00 [INFO] theta_threshold_radians: 0.20943951023931953
    00:00:00 [INFO] x_threshold: 2.4
    00:00:00 [INFO] action_space: Discrete(2)
    00:00:00 [INFO] observation_space: Box([-4.8000002e+00 -3.4028235e+38 -4.1887903e-01 -3.4028235e+38], [4.8000002e+00 3.4028235e+38 4.1887903e-01 3.4028235e+38], (4,), float32)
    00:00:00 [INFO] np_random: RandomState(MT19937)
    00:00:00 [INFO] viewer: None
    00:00:00 [INFO] state: None
    00:00:00 [INFO] steps_beyond_done: None
    00:00:00 [INFO] spec: EnvSpec(CartPole-v1)

In [3]:

    class ClosedFormAgent:
        def __init__(self, _):
            pass

        def reset(self, mode=None):
            pass

        def step(self, observation, reward, terminated):
            position, velocity, angle, angle_velocity = observation
            action = int(3. * angle + angle_velocity > 0.)
            return action

        def close(self):
            pass

    agent = ClosedFormAgent(env)

In [4]:

    def play_episode(env, agent, seed=None, mode=None, render=False):
        observation, _ = env.reset(seed=seed)
        reward, terminated, truncated = 0., False, False
        agent.reset(mode=mode)
        episode_reward, elapsed_steps = 0., 0
        while True:
            action = agent.step(observation, reward, terminated)
            if render:
                env.render()
            if terminated or truncated:
                break
            observation, reward, terminated, truncated, _ = env.step(action)
            episode_reward += reward
            elapsed_steps += 1
        agent.close()
        return episode_reward, elapsed_steps

    logging.info('==== test ====')
    episode_rewards = []
    for episode in range(100):
        episode_reward, elapsed_steps = play_episode(env, agent)
        episode_rewards.append(episode_reward)
        logging.info('test episode %d: reward = %.2f, steps = %d',
                episode, episode_reward, elapsed_steps)
    logging.info('average episode reward = %.2f ± %.2f',
            np.mean(episode_rewards), np.std(episode_rewards))

    00:00:00 [INFO] ==== test ====
    00:00:00 [INFO] test episode 0: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 1: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 2: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 3: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 4: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 5: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 6: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 7: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 8: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 9: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 10: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 11: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 12: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 13: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 14: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 15: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 16: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 17: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 18: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 19: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 20: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 21: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 22: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 23: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 24: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 25: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 26: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 27: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 28: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 29: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 30: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 31: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 32: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 33: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 34: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 35: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 36: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 37: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 38: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 39: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 40: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 41: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 42: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 43: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 44: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 45: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 46: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 47: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 48: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 49: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 50: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 51: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 52: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 53: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 54: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 55: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 56: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 57: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 58: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 59: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 60: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 61: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 62: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 63: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 64: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 65: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 66: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 67: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 68: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 69: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 70: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 71: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 72: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 73: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 74: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 75: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 76: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 77: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 78: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 79: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 80: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 81: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 82: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 83: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 84: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 85: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 86: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 87: reward = 500.00, steps = 500
    00:00:00 [INFO] test episode 88: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 89: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 90: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 91: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 92: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 93: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 94: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 95: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 96: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 97: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 98: reward = 500.00, steps = 500
    00:00:01 [INFO] test episode 99: reward = 500.00, steps = 500
    00:00:01 [INFO] average episode reward = 500.00 ± 0.00

In [5]:

    env.close()