Skip to content


Folders and files

Last commit message
Last commit date

Latest commit



8 Commits

Repository files navigation


A proxy app for EXARL

Software Requirement

  • Python 3.7
  • Additional python packages are defined in the
  • This document assumes you are running at the top directory

Directory Organization

├──                          : Python setup file with requirements files
├── config                	          : folder containing configurations
    └── agent_cfg                     : agent configuration folder
    └── model_cfg                     : model configuration folder
    └── env_cfg                       : env configuration folder
    └── workflow_cfg                  : workflow configuration folder
    └── learner_cfg.json              : learner configuration
├── exarl                	            : folder with EXARL code
    └──                   : make base classes visible
    ├── base         	                : folder containing EXARL base classes
        └──               : make base classes visible
        └──             : agent base class
        └──               : environment base class
        └──          : workflow base class
        └──           : learner base class
    ├── driver                        : folder containing RL MPI steering scripts
        └──                 : Run scipt
    ├── candlelib                     : folder containing library for CANDLE functionality
    ├── agents         	              : folder containing EXARL agents and registration scripts
        └──               : agent registry
        └──           : script to handle registration
        ├── agent_vault               : folder containing agents
            └──           : script to make agents visible
            └── <RLagent>.py          : RL agents (such as DQN, DDPG, etc.)
    ├── envs         	                : folder containing EXARL environments
        └──               : environment registry
        ├── env_vault                 : folder containing environments
        └──               : script to make environments visible
            └── <RLenv>.py            : RL environments (physics simulations, interfaces to experiments, etc.)
    ├── workflows      	              : folder containing EXARL workflows and registration scripts
        └──               : workflow registry
        └──           : script to handle registration
        ├── workflow_vault            : folder containing workflows
            └──           : script to make workflows visible
            └── <RLworkflow>.py       : RL workflows (such as SEED, IMPALA, etc.)
    ├── utils                         : folder containing utilities
        └──               : make classes and functions visible
        └──           : Supporting CANDLE script
        └──         : script for plotting results
        └──                    : central place to set logging levels
        └──                : provides function decorators for profiling, timing, and debugging


  • Pull code from repo
git clone --recursive
cd miniRL
  • Install dependencies for miniRL:
pip install -e . --user

Configuration Files

Configuration files such as exarl/config/learner_cfg.json are searched for in the following directories:

  1. (current working directory)/exarl/config
  2. ~/.exarl/config
  3. (site-packages dir)/exarl/config

If you would like to run miniRL from outside the source directory, you may install the config files with exarl or copy them into miniRL's config directory in your home directory like so:

$ mkdir -p ~/.exarl/config
$ cd EXARL
$ cp config/* ~/.exarl/config

CANDLE functionality is built into miniRL

  • Add/modify the learner parameters in miniRL/exarl/config/learner_cfg.json
    "agent": "DQN-v0",
    "env": "ExaCartPoleStatic-v0",
    "workflow": "async",
    "n_episodes": 10,
    "n_steps": 10,
    "output_dir": "./exa_results_dir"
  • Add/modify the agent parameters in miniRL/exarl/config/agent_cfg/<AgentName>.json
    "gamma": 0.75,
    "epsilon": 1.0,
    "epsilon_min" : 0.01,
    "epsilon_decay" : 0.999,
    "learning_rate" : 0.001,
    "batch_size" : 5,
    "tau" : 0.5

Currently, DQN agent takes either MLP or LSTM as model_type.

  • Add/modify the model parameters in miniRL/exarl/config/model_cfg/<ModelName>.json
    "dense" : [64, 128],
    "activation" : "relu",
    "optimizer" : "adam",
    "out_activation" : "linear",
    "loss" : "mse"
  • Add/modify the environment parameters in miniRL/exarl/config/env_cfg/<EnvName>.json
        "worker_app": "./exarl/envs/env_vault/"
  • Add/modify the workflow parameters in miniRL/exarl/config/workflow_cfg/<WorkflowName>.json
        "process_per_env": "1"
  • Please note the agent, model, environment, and workflow configuration file (json file) name must match the agent, model, environment, and workflow ID specified in miniRL/exarl/config/learner_cfg.json.
    E.g.:- miniRL/exarl/config/agent_cfg/DQN-v0.json, miniRL/exarl/config/model_cfg/MLP.json, miniRL/exarl/config/env_cfg/ExaCartPole-v1.json, and miniRL/exarl/config/workflow_cfg/async.json

Running EXARL using MPI

  • Existing environment can be paired with an available agent
  • The following script is provided for convenience: miniRL/exarl/driver/
from mpi4py import MPI
import utils.analyze_reward as ar
import time
import exarl as erl
import mpi4py.rc
mpi4py.rc.threads = False
mpi4py.rc.recv_mprobe = False

# MPI communicator
rank = comm.Get_rank()
size = comm.Get_size()

# Get run parameters using CANDLE
# run_params = initialize_parameters()

# Create learner object and run
exa_learner = erl.ExaLearner(comm)

# Run the learner, measure time
start = time.time()
elapse = time.time() - start

# Compute and print average time
max_elapse = comm.reduce(elapse, op=MPI.MAX, root=0)
elapse = comm.reduce(elapse, op=MPI.SUM, root=0)

if rank == 0:
    print("Average elapsed time = ", elapse / size)
    print("Maximum elapsed time = ", max_elapse)
    # Save rewards vs. episodes plot
  • Write your own script or modify the above as needed
  • Run the following command:
mpiexec -np <num_parent_processes> python exarl/driver/ --<run_params>=<param_value>

Using parameters set in CANDLE configuration/get parameters from terminal

  • To obtain the parameters from JSON file/set in terminal using CANDLE, use the following lines:
import exarl.utils.candleDriver as cd
cd.run_params # dictionary containing all parameters
  • Individual parameters are accessed using the corresponding key
self.search_method =  cd.run_params['search_method']
self.gamma =  cd.run_params['gamma']

Creating custom environments

  • miniRL uses OpenAI gym environments
  • The ExaEnv class in miniRL/exarl/ inherits from OpenAI GYM Wrapper class for including added functionality.
  • Environments inherit from gym.Env
    class envName(gym.Env):
  • Register the environment in EXARL/exarl/envs/
from gym.envs.registration import register

  • The id variable will be passed to exarl.make() to call the environment

  • The file miniRL/exarl/env/env_vault/ should include

from exarl.envs.env_vault.foo_env import FooEnv

where miniRL/exarl/envs/env_vault/ is the file containing your envirnoment

Using environment written in a lower level language

  • The following example illustrates using the C function of computing the value of PI in miniRL
#include <mpi.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
  extern void compute_pi(int, MPI_Comm);
#ifdef __cplusplus


#include <stdio.h>
#include <mpi.h>

double compute_pi(int N, MPI_Comm new_comm)
  int rank, size;
  MPI_Comm_rank(new_comm, &rank);
  MPI_Comm_size(new_comm, &size);

  double h, s, x;
  h = 1.0 / (double) N;
  s = 0.0;
  for(int i=rank; i<N; i+=size)
    x = h * ((double)i + 0.5);
    s += 4.0 / (1.0 + x*x);
  return (s * h);
  • Compile the C/C++ code and create a shared object (*.so file)
  • Create a python wrapper (Ctypes wrapper is shown)
from mpi4py import MPI
import ctypes
import os

_libdir = os.path.dirname(__file__)

if MPI._sizeof(MPI.Comm) == ctypes.sizeof(ctypes.c_int):
    MPI_Comm = ctypes.c_int
    MPI_Comm = ctypes.c_void_p
_lib = ctypes.CDLL(os.path.join(_libdir, ""))
_lib.compute_pi.restype = ctypes.c_double
_lib.compute_pi.argtypes = [ctypes.c_int, MPI_Comm]

def compute_pi(N, comm):
    comm_ptr = MPI._addressof(comm)
    comm_val = MPI_Comm.from_address(comm_ptr)
    myPI = _lib.compute_pi(ctypes.c_int(N), comm_val)
    return myPI
  • In your environment code, just import the function and use it regularly
from mpi4py import MPI
import numpy as np
import pdb
import computePI as cp

def main():
    comm = MPI.COMM_WORLD
    myrank = comm.Get_rank()
    nprocs = comm.Get_size()

    if myrank == 0:
        N = 100
        N = None

    N = comm.bcast(N, root=0)
    num = 4
    color = int(myrank/num)
    newcomm = comm.Split(color, myrank)

    mypi = cp.compute_pi(N, newcomm)
    pi = newcomm.reduce(mypi, op=MPI.SUM, root=0)

    newrank = newcomm.rank
    if newrank==0:

if __name__ == '__main__':

Creating custom agents

  • miniRL extends OpenAI gym's environment registration to agents
  • Agents inherit from exarl.ExaAgent
    import exarl
    class agentName(exarl.ExaAgent):
          def __init__(self, env, is_learner):
  • Agents must include the following functions:
get_weights()   # get target model weights
set_weights()   # set target model weights
train()         # train the agent
update()        # update target model
action()        # Next action based on current state
load()          # load weights from memory
save()          # save weights to memory
monitor()       # monitor progress of learning
  • Register the agent in miniRL/exarl/agents/
from exarl.agents.registration import register, make

  • The id variable will be passed to exarl.make() to call the agent

  • The file miniRL/exarl/agents/agent_vault/ should include

from exarl.agents.agent_vault.foo_agent import FooAgent

where miniRL/agents/agent_vault/ is the file containing your agent

Creating custom workflows

  • miniRL also extends OpenAI gym's environment registration to workflows
  • Workflows inherit from exarl.ExaWorkflow
    class workflowName(exarl.ExaWorkflow):
  • Workflows must include the following functions:
run()   # run the workflow
  • Register the workflow in miniRL/exarl/workflows/
from exarl.agents.registration import register, make

  • The id variable will be passed to exarl.make() to call the agent

  • The file miniRL/exarl/workflows/workflow_vault/ should include

from exarl.workflows.workflow_vault.foo_workflow import FooWorkflow

where miniRL/workflows/workflow_vault/ is the file containing your workflow

Base classes

  • Base classes are provided for agents, environments, workflows, and learner in the directory miniRL/exarl/
  • Users can inherit from the correspoding agent, environment, and workflow base classes

Debugging, Timing, and Profiling

  • Function decorators are provided for debugging, timing, and profiling miniRL.
  • Debugger captures the function signature and return values.
  • Timer prints execution time in seconds.
  • Either line_profiler or memory_profiler can be used for profiling the code.
    • Profiler can be selected in learner_cfg.json or using the command line argument --profile.
    • Options for profiling are line, mem, or none.
  • Function decorators can be used as shown below:
from exarl.utils.profile import *

def my_func(*args, **kwargs):

def my_func(*args, **kwargs):

def my_func(*args, **kwargs):
  • Profiling results are written to: results_dir + '/Profile/<line/memory>_profile.txt.

Cite this software

  author = {Vinay Ramakrishnaiah, Malachi Schram, Jamal Mohd-Yusof, Sayan Ghosh, Yunzhi Huang, Ai Kagawa, Christine Sweeney, Shinjae Yoo},
  title = {Easily eXtendable Architecture for Reinforcement Learning (EXARL)},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{}},


If you have any questions or concerns regarding EXARL, please contact Vinay Ramakrishnaiah ( and/or Malachi Schram (