The code found on the internet is easy to use in the test. Share the following.
Import gymimport timeenv = gym. make ('cartpole-v0') # obtain the game environment observation = Env. reset () # reset the game environment and print the game in a new game ('initial game observation = {}'. format (observation) for T in range (0, 200): Env. render () Action = Env. action_space.sample () # randomly select an action print ('{}: action = {}'. format (T, Action) observation, reward, done, info = Env. STEP (Action) # print the execution behavior ('{}: observation ={}, this step score ={}, end indicator ={}, other information = {}'. format (T, observation, reward, done, Info) if done: break time. sleep (1) # You can add or not, and then you can see the figure Env. close ()
The code for multiple rounds is as follows:
Import gymenv = gym. make ('cartpole-v0') n_episode = 20for I _episode in range (n_episode): Observation = Env. reset () episode_reward = 0 while true: # Env. render () Action = Env. action_space.sample () # randomly select observation, reward, done, _ = Env. STEP (Action) episode_reward + = reward state = observation if done: Break print ('score {}'. format (I _episode, episode_reward) ENV. close ()
This multi-round game has not been added to the drawing. If you need to draw, you can add env. Render.
Enhanced learning Roman Games