# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.10 64-bit (''venv'': venv)'
#     language: python
#     name: python3
# ---

# +
import torch
import gym
from ppo import Agent
# import numpy

env = gym.make('CartPole-v1')

agent = Agent(env.action_space.n, env.observation_space.shape)
agent.load_models('model/actor','model/critic')


# +
import time
import numpy

obs = env.reset()

total_reward = 0
last_action = 1
counter = 0
env._max_episode_steps = 4000
while True:
    action, _, _ = agent.choose_action(obs)
    # print(action)
    if last_action == action:
        counter += 1
        if counter % 100 == 0:
            counter = 0
            action = 1
    else:
        counter = 0
    last_action = action
    n_obs, reward, done, _ = env.step(action)
    total_reward += reward
    env.render()
    time.sleep(0.1)
    obs = n_obs
    if done:
        print("reward:", total_reward)
        break

StarcoderdataJupyterScriptsDedupFiltered