YEAR: 2016 90 | COPYRIGHT HOLDER: Paul Hendricks 91 |92 | 93 |
gym: Provides Access to the OpenAI Gym API
91 | 92 | 93 | 94 | 95 |A sample random discrete agent.
91 | 92 | 93 |random_discrete_agent(n)94 | 95 |
| n | 100 |The number of discrete action spaces available. |
101 |
|---|
NULL.
107 | 108 | 109 |111 |agent <- random_discrete_agent(10)
This function instantiates a GymClient instance to integrate with an OpenAI Gym server.
91 | 92 | 93 |GymClient(remote_base)94 | 95 |
| remote_base | 100 |The URL of the OpenAI gym server. This value is usually "http://127.0.0.1:5000". |
101 |
|---|
An instance of class "GymClient"; this object has "remote_base" as an attribute.
107 | 108 | 109 |# NOT RUN { 111 | remote_base <- "http://127.0.0.1:5000" 112 | client <- GymClient(remote_base) 113 | # }114 |
This function instantiates a GymClient instance to integrate with an OpenAI Gym server.
91 | 92 | 93 |create_GymClient(remote_base)94 | 95 |
| remote_base | 100 |The URL of the OpenAI gym server. This value is usually "http://127.0.0.1:5000". |
101 |
|---|
An instance of class "GymClient"; this object has "remote_base" as an attribute.
107 | 108 | 109 |# NOT RUN { 111 | remote_base <- "http://127.0.0.1:5000" 112 | client <- create_GymClient(remote_base) 113 | # }114 |
Request a server shutdown.
91 | 92 | 93 |shutdown_server(x)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|
NULL Currently used by the integration tests to repeatedly create and destroy fresh copies of the server running in a separate thread.
107 | 108 | 109 |# NOT RUN { 111 | remote_base <- "http://127.0.0.1:5000" 112 | client <- create_GymClient(remote_base) 113 | shutdown_server(client) 114 | # }115 |
List all environments running on the server.
91 | 92 | 93 |env_list_all(x)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|
A list mapping instance_id to env_id e.g. list("3c657dbc" = "CartPole-v0") for every env on the server.
# NOT RUN { 111 | remote_base <- "http://127.0.0.1:5000" 112 | client <- create_GymClient(remote_base) 113 | env_list_all(client) 114 | # }115 |
Represent a GymClient instance on the command line.
91 | 92 | 93 |# S3 method for GymClient 94 | print(x, ...)95 | 96 |
| x | 101 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
102 |
|---|---|
| ... | 105 |Further arguments passed to or from other methods. |
106 |
x A GymClient instance.
112 | 113 | 114 |# NOT RUN { 116 | remote_base <- "http://127.0.0.1:5000" 117 | client <- create_GymClient(remote_base) 118 | print(client) 119 | # }120 |
Flush all monitor data to disk.
91 | 92 | 93 |env_close(x, instance_id)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
NULL.
111 | 112 | 113 |# NOT RUN { 115 | remote_base <- "http://127.0.0.1:5000" 116 | client <- create_GymClient(remote_base) 117 | env_id <- "CartPole-v0" 118 | instance_id <- env_create(client, env_id) 119 | env_close(client, instance_id) 120 | # }121 |
Parse the server error or raise for status.
91 | 92 | 93 |parse_server_error_or_raise_for_status(response)94 | 95 |
| response | 100 |A response object from |
101 |
|---|
If the response code is 200 or 204, a parsed response. Else, a server error or raised exception.
107 | 108 | 109 |# NOT RUN { 111 | b2 <- "http://httpbin.org/post" 112 | response <- httr::POST(b2, body = "A simple text string") 113 | parse_server_error_or_raise_for_status(response) 114 | # }115 |
Flush all monitor data to disk.
91 | 92 | 93 |env_monitor_close(x, instance_id)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
NULL.
111 | 112 | 113 |# NOT RUN { 115 | remote_base <- "http://127.0.0.1:5000" 116 | client <- create_GymClient(remote_base) 117 | env_id <- "CartPole-v0" 118 | instance_id <- env_create(client, env_id) 119 | env_monitor_close(client, instance_id) 120 | # }121 |
Submit a GET request to an OpenAI Gym server.
91 | 92 | 93 |get_request(x, route, data = NULL)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| route | 104 |The URL path or endpoint. |
105 |
| data | 108 |URL query arguments. Default value is NULL. |
109 |
If the response code is 200 or 204, a parsed response. Else, a server error or raised exception.
115 | 116 | 117 |# NOT RUN { 119 | remote_base <- "http://127.0.0.1:5000" 120 | client <- create_GymClient(remote_base) 121 | route <- "/v1/envs/" 122 | get_request(client, route) 123 | # }124 |
Create an instance of the specified environment.
91 | 92 | 93 |env_create(x, env_id)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| env_id | 104 |A short identifier (such as "3c657dbc") for the created environment instance. The instance_id is used in future API calls to identify the environment to be manipulated. |
105 |
A short identifier (such as "3c657dbc") for the created environment instance. The instance_id is used in future API calls to identify the environment to be manipulated.
111 | 112 | 113 |# NOT RUN { 115 | remote_base <- "http://127.0.0.1:5000" 116 | client <- create_GymClient(remote_base) 117 | env_id <- "CartPole-v0" 118 | env_create(client, env_id) 119 | # }120 |
Reset the state of the environment and return an initial observation.
91 | 92 | 93 |env_reset(x, instance_id)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
The initial observation of the space.
111 | 112 | 113 |# NOT RUN { 115 | remote_base <- "http://127.0.0.1:5000" 116 | client <- create_GymClient(remote_base) 117 | env_id <- "CartPole-v0" 118 | instance_id <- env_create(client, env_id) 119 | env_reset(client, instance_id) 120 | # }121 |
Sample an action from the environments's action space.
91 | 92 | 93 |env_action_space_sample(x, instance_id)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
An action sampled from a space (such as "Discrete"), which varies from space to space.
111 | 112 | 113 |# NOT RUN { 115 | remote_base <- "http://127.0.0.1:5000" 116 | client <- create_GymClient(remote_base) 117 | env_id <- "CartPole-v0" 118 | instance_id <- env_create(client, env_id) 119 | env_action_space_sample(client, instance_id) 120 | # }121 |
Flush all monitor data to disk.
91 | 92 | 93 |upload(x, training_dir, api_key = NULL, algorithm_id = NULL)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| training_dir | 104 |A directory containing the results of a training run. |
105 |
| api_key | 108 |Your OpenAI API key. |
109 |
| algorithm_id | 112 |An arbitrary string indicating the paricular version of the algorithm (including choices of parameters) you are running. |
113 |
NULL.
119 | 120 | 121 |# NOT RUN { 123 | remote_base <- "http://127.0.0.1:5000" 124 | client <- create_GymClient(remote_base) 125 | outdir <- "/tmp/random-agent-results" 126 | upload(client, outdir) 127 | # }128 |
Get information (name and dimensions/bounds) of the environments's action space.
91 | 92 | 93 |env_action_space_info(x, instance_id)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
A list containing "name" (such as "Discrete"), and additional dimensional info (such as "n") which varies from space to space.
111 | 112 | 113 |# NOT RUN { 115 | remote_base <- "http://127.0.0.1:5000" 116 | client <- create_GymClient(remote_base) 117 | env_id <- "CartPole-v0" 118 | instance_id <- env_create(client, env_id) 119 | env_action_space_info(client, instance_id) 120 | # }121 |
Get information (name and dimensions/bounds) of the environment's observation space.
91 | 92 | 93 |env_observation_space_info(x, instance_id)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
A list containing "name" (such as "Discrete"), and additional dimensional info (such as "n") which varies from space to space.
111 | 112 | 113 |# NOT RUN { 115 | remote_base <- "http://127.0.0.1:5000" 116 | client <- create_GymClient(remote_base) 117 | env_id <- "CartPole-v0" 118 | instance_id <- env_create(client, env_id) 119 | env_observation_space_info(client, instance_id) 120 | # }121 |
Submit a POST request to an OpenAI Gym server.
91 | 92 | 93 |post_request(x, route, data = NULL)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| route | 104 |The URL path or endpoint. |
105 |
| data | 108 |URL query arguments. Default value is NULL. |
109 |
If the response code is 200 or 204, a parsed response. Else, a server error or raised exception.
115 | 116 | 117 |# NOT RUN { 119 | remote_base <- "http://127.0.0.1:5000" 120 | client <- create_GymClient(remote_base) 121 | route <- "/v1/envs/" 122 | env_id <- "CartPole-v0" 123 | data <- list(env_id = env_id) 124 | post_request(client, route, data) 125 | # }126 |
Evaluate whether an action is a member of an environments's action space.
91 | 92 | 93 |env_action_space_contains(x, instance_id, action)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
| action | 108 |An action to take in the environment. |
109 |
A boolean atomic vector of length one indicating if the action is a member of an environments's action space.
115 | 116 | 117 |# NOT RUN { 119 | remote_base <- "http://127.0.0.1:5000" 120 | client <- create_GymClient(remote_base) 121 | env_id <- "CartPole-v0" 122 | instance_id <- env_create(client, env_id) 123 | action <- env_action_space_sample(client, instance_id) 124 | env_action_space_contains(client, instance_id, action) 125 | # }126 |
Step though an environment using an action.
91 | 92 | 93 |env_step(x, instance_id, action, render = FALSE)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
| action | 108 |An action to take in the environment. |
109 |
| render | 112 |Whether to render the environment. Defaults to FALSE. |
113 |
A list consisting of the following: action; an action to take in the environment, observation; an agent's observation of the current environment, reward; the amount of reward returned after previous action, done; whether the episode has ended, and info; a list containing auxiliary diagnostic information.
119 | 120 | 121 |# NOT RUN { 123 | remote_base <- "http://127.0.0.1:5000" 124 | client <- create_GymClient(remote_base) 125 | env_id <- "CartPole-v0" 126 | instance_id <- env_create(client, env_id) 127 | action <- env_action_space_sample(client, instance_id) 128 | env_step(client, instance_id, action) 129 | # }130 |
Start monitoring.
91 | 92 | 93 |env_monitor_start(x, instance_id, directory, force = FALSE, resume = FALSE)94 | 95 |
| x | 100 |An instance of class "GymClient"; this object has "remote_base" as an attribute. |
101 |
|---|---|
| instance_id | 104 |A short identifier (such as "3c657dbc") for the environment instance. |
105 |
| directory | 108 |The directory to write the training data to. Defaults to FALSE. |
109 |
| force | 112 |Clear out existing training data from this directory (by deleting every file prefixed with "openaigym"). Defaults to NULL. |
113 |
| resume | 116 |Retain the training data already in this directory, which will be merged with our new data. Defaults to FALSE. |
117 |
NULL.
123 | 124 | 125 |# NOT RUN { 127 | remote_base <- "http://127.0.0.1:5000" 128 | client <- create_GymClient(remote_base) 129 | env_id <- "CartPole-v0" 130 | instance_id <- env_create(client, env_id) 131 | outdir <- "/tmp/random-agent-results" 132 | env_monitor_start(client, instance_id, outdir, force = TRUE, resume = FALSE) 133 | # }134 |
OpenAI Gym is a open-source Python toolkit for developing and comparing reinforcement learning algorithms. This R package is a wrapper for the OpenAI Gym API, and enables access to an ever-growing variety of environments.
73 |You can install the latest development version from CRAN:
77 |install.packages("gym")Or from GitHub with:
79 |if (packageVersion("devtools") < 1.6) {
80 | install.packages("devtools")
81 | }
82 | devtools::install_github("paulhendricks/gym-R", subdir = "R")If you encounter a clear bug, please file a minimal reproducible example on GitHub.
84 |To download the code and install the requirements, you can run the following shell commands:
92 |git clone https://github.com/openai/gym-http-api
93 | cd gym-http-api
94 | pip install -r requirements.txtThis code is intended to be run locally by a single user. The server runs in python.
96 |To start the server from the command line, run this:
97 |python gym_http_server.pyFor more details, please see here: https://github.com/openai/gym-http-api.
99 |In a separate R terminal, you can then try running the example agent and see what happens:
104 |library(gym)
105 |
106 | remote_base <- "http://127.0.0.1:5000"
107 | client <- create_GymClient(remote_base)
108 | print(client)
109 |
110 | # Create environment
111 | env_id <- "CartPole-v0"
112 | instance_id <- env_create(client, env_id)
113 | print(instance_id)
114 |
115 | # List all environments
116 | all_envs <- env_list_all(client)
117 | print(all_envs)
118 |
119 | # Set up agent
120 | action_space_info <- env_action_space_info(client, instance_id)
121 | print(action_space_info)
122 | agent <- random_discrete_agent(action_space_info[["n"]])
123 |
124 | # Run experiment, with monitor
125 | outdir <- "/tmp/random-agent-results"
126 | env_monitor_start(client, instance_id, outdir, force = TRUE, resume = FALSE)
127 |
128 | episode_count <- 100
129 | max_steps <- 200
130 | reward <- 0
131 | done <- FALSE
132 |
133 | for (i in 1:episode_count) {
134 | ob <- env_reset(client, instance_id)
135 | for (i in 1:max_steps) {
136 | action <- env_action_space_sample(client, instance_id)
137 | results <- env_step(client, instance_id, action, render = TRUE)
138 | if (results[["done"]]) break
139 | }
140 | }
141 |
142 | # Dump result info to disk
143 | env_monitor_close(client, instance_id)To cite package ‘gym’ in publications use:
150 |Paul Hendricks (2016). gym: Provides Access to the OpenAI Gym API. R package version 0.1.0. https://CRAN.R-project.org/package=gym
151 | A BibTeX entry for LaTeX users is
152 |@Manual{,
153 | title = {gym: Provides Access to the OpenAI Gym API},
154 | author = {Paul Hendricks},
155 | year = {2016},
156 | note = {R package version 0.1.0},
157 | url = {https://CRAN.R-project.org/package=gym},
158 | }
159 |