Documentation

Website


Installation

# Install from CRAN.
install.packages("reinforcelearn")

# Install development version from github.
devtools::install_github("markusdumke/reinforcelearn")

Get started

Reinforcement Learning with the package reinforcelearn is as easy as

library(reinforcelearn)

env = makeEnvironment("windy.gridworld")
agent = makeAgent("softmax", "table", "qlearning")

# Run interaction for 10 episodes.
interact(env, agent, n.episodes = 10L)
#> $returns
#>  [1] -1082 -2538  -209 -1821  -659  -106 -1722  -454  -500  -138
#> 
#> $steps
#>  [1] 1082 2538  209 1821  659  106 1722  454  500  138

Environments

With makeEnvironment you can create reinforcement learning environments.

# Create environment.
step = function(self, action) {
  state = list(mean = action + rnorm(1), sd = runif(1))
  reward = rnorm(1, state[[1]], state[[2]])
  done = FALSE
  list(state, reward, done)
}

reset = function(self) {
  state = list(mean = 0, sd = 1)
  state
}

env = makeEnvironment("custom", step = step, reset = reset)

The environment is an R6 class with a set of attributes and methods. You can interact with the environment via the reset and step method.

# Reset environment.
env$reset()
#> $mean
#> [1] 0
#> 
#> $sd
#> [1] 1

# Take action.
env$step(100)
#> $state
#> $state$mean
#> [1] 100.2982
#> 
#> $state$sd
#> [1] 0.4072189
#> 
#> 
#> $reward
#> [1] 99.82231
#> 
#> $done
#> [1] FALSE

There are some predefined environment classes, e.g. MDPEnvironment, which allows you to create a Markov Decision Process by passing on state transition array and reward matrix, or GymEnvironment, where you can use toy problems from OpenAI Gym.

# Create a gym environment.
# Make sure you have Python, gym and reticulate installed.
env = makeEnvironment("gym", gym.name = "MountainCar-v0")

# Take random actions for 200 steps.
env$reset()
for (i in 1:200) {
  action = sample(0:2, 1)
  env$step(action)
  env$visualize()
}
env$close()

This should open a window showing a graphical visualization of the environment during interaction.

For more details on how to create an environment have a look at the vignette: Environments


Agents

With makeAgent you can set up a reinforcement learning agent to solve the environment, i.e. to find the best action in each time step.

The first step is to set up the policy, which defines which action to choose. For example we could use a uniform random policy.

# Create the environment.
env = makeEnvironment("windy.gridworld")

# Create agent with uniform random policy.
policy = makePolicy("random")
agent = makeAgent(policy)

# Run interaction for 10 steps.
interact(env, agent, n.steps = 10L)
#> $returns
#> numeric(0)
#> 
#> $steps
#> integer(0)

In this scenario the agent chooses all actions with equal probability and will not learn anything from the interaction. Usually we want the agent to be able to learn something. Value-based algorithms learn a value function from interaction with the environment and adjust the policy according to the value function. For example we could set up Q-Learning with a softmax policy.

# Create the environment.
env = makeEnvironment("windy.gridworld")

# Create qlearning agent with softmax policy and tabular value function.
policy = makePolicy("softmax")
values = makeValueFunction("table", n.states = env$n.states, n.actions = env$n.actions)
algorithm = makeAlgorithm("qlearning")
agent = makeAgent(policy, values, algorithm)

# Run interaction for 10 steps.
interact(env, agent, n.episodes = 10L)
#> $returns
#>  [1]  -424 -4868  -267 -1513 -2035  -176  -433  -218  -317  -241
#> 
#> $steps
#>  [1]  424 4868  267 1513 2035  176  433  218  317  241

Vignettes

Also have a look at the vignettes for further examples.


Logo is a modification of https://www.r-project.org/logo/.