All Projects → markusdumke → reinforcelearn

markusdumke / reinforcelearn

Licence: other
R Package for Reinforcement Learning

Programming Languages

r
7636 projects

Projects that are alternatives of or similar to reinforcelearn

rfishbase
R interface to the fishbase.org database
Stars: ✭ 79 (+139.39%)
Mutual labels:  r-package
WikidataQueryServiceR
An R package for the Wikidata Query Service API
Stars: ✭ 23 (-30.3%)
Mutual labels:  r-package
covidseir
Bayesian SEIR model to estimate the effects of social-distancing on COVID-19
Stars: ✭ 23 (-30.3%)
Mutual labels:  r-package
tabula
📊 Analysis, Seriation and Visualization of Archaeological Count Data
Stars: ✭ 25 (-24.24%)
Mutual labels:  r-package
powerlmm
powerlmm R package for power calculations for two- and three-level longitudinal multilevel/linear mixed models.
Stars: ✭ 86 (+160.61%)
Mutual labels:  r-package
reportfactory
Lightweight infrastructure to handle multiple rmarkdown reports
Stars: ✭ 68 (+106.06%)
Mutual labels:  r-package
lcars
LCARS aesthetic for Shiny
Stars: ✭ 54 (+63.64%)
Mutual labels:  r-package
miner
R package for controlling Minecraft via API
Stars: ✭ 74 (+124.24%)
Mutual labels:  r-package
reproducible
A set of tools for R that enhance reproducibility beyond package management
Stars: ✭ 33 (+0%)
Mutual labels:  r-package
xfun
Miscellaneous R functions
Stars: ✭ 102 (+209.09%)
Mutual labels:  r-package
rinat
A programmatic interface to iNaturalist
Stars: ✭ 36 (+9.09%)
Mutual labels:  r-package
UCSCXenaShiny
📊 An R package for interactively exploring UCSC Xena https://xenabrowser.net/datapages/
Stars: ✭ 52 (+57.58%)
Mutual labels:  r-package
bh
R package providing Boost Header files
Stars: ✭ 73 (+121.21%)
Mutual labels:  r-package
brmstools
Helper functions for brmsfit objects (DEPRECATED)
Stars: ✭ 24 (-27.27%)
Mutual labels:  r-package
paleobioDB
R interface to the Paleobiology Database
Stars: ✭ 36 (+9.09%)
Mutual labels:  r-package
PackageDevelopment
Task View: PackageDevelopment
Stars: ✭ 38 (+15.15%)
Mutual labels:  r-package
NLMR
📦 R package to simulate neutral landscape models 🏔
Stars: ✭ 57 (+72.73%)
Mutual labels:  r-package
datatoolbox
🎓 Data Toolbox Training
Stars: ✭ 18 (-45.45%)
Mutual labels:  r-package
rdryad
R client for Dryad web services
Stars: ✭ 25 (-24.24%)
Mutual labels:  r-package
microbiomeMarker
R package for microbiome biomarker discovery
Stars: ✭ 89 (+169.7%)
Mutual labels:  r-package

Reinforcement Learning in R

Travis-CI Build Status CRAN_Status_Badge Coverage Status

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] -3244 -2335 -1734  -169  -879  -798  -216  -176  -699  -232
#> 
#> $steps
#>  [1] 3244 2335 1734  169  879  798  216  176  699  232

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] 99.56104
#> 
#> $state$sd
#> [1] 0.5495179
#> 
#> 
#> $reward
#> [1] 99.40968
#> 
#> $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] -1524 -3496  -621  -374  -173 -1424 -1742  -468  -184   -39
#> 
#> $steps
#>  [1] 1524 3496  621  374  173 1424 1742  468  184   39

Vignettes

Also have a look at the vignettes for further examples.


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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].