-
Notifications
You must be signed in to change notification settings - Fork 20
Running Experiments
Sohan Rudra edited this page Jan 18, 2019
·
3 revisions
import gym
import MADRaS
env = gym.make('Madras-v0')
.
.
.
for i in range(num_episodes):
env.reset()
for i in range(num_steps):
a = policy(obs) # your algorithm
obs1, rew, done, info = env.step(a)
...
Install openai baselines.
Some slight changes are required before we can start to train.
- Add two lines in
baselines/run.py
.
import MADRAS # at the top
_game_envs['madras'] = {'Madras-v0'} #line 52
- If an error pops up while training
assert (np.abs(env.action_space.low) == env.action_space.high).all() # we assume symmetric actions.
then go to the desgnated line and comment it out (This error occurs as our action space is not symmetric).
python -m baselines.run --alg=algo --env='Madras-v0' #for single thread
mpirun -np 4 python -m baselines.run --alg=algo --env='Madras-v0' #for multi threaded(in this case 4)
Similarly other experiments can be run by following the instructions as specified in the [readme.md](https://github.com/openai/baselines/blob/master/README.md) of baselines.
Install rllab
For registering MADRaS with rllab add the line in the following file
import MADRaS
We show the example of TRPO here.
from rllab.algos.trpo import TRPO
from rllab.baselines.linear_feature_baseline import LinearFeatureBaseline
from rllab.envs.gym_env import GymEnv
from rllab.envs.normalized_env import normalize
from rllab.misc.instrument import run_experiment_lite
from rllab.policies.gaussian_mlp_policy import GaussianMLPPolicy
def run_task(*_):
# Please note that different environments with different action spaces may
# require different policies. For example with a Discrete action space, a
# CategoricalMLPPolicy works, but for a Box action space may need to use
# a GaussianMLPPolicy (see the trpo_gym_pendulum.py example)
env = normalize(GymEnv("Madras-v0"))
#policy = CategoricalMLPPolicy(
# env_spec=env.spec,
# The neural network policy should have two hidden layers, each with 32 hidden units.
# hidden_sizes=(32, 32)
#)
policy = GaussianMLPPolicy(
env_spec=env.spec,
# The neural network policy should have two hidden layers, each with 32 hidden units.
hidden_sizes=(32, 32)
)
baseline = LinearFeatureBaseline(env_spec=env.spec)
algo = TRPO(
env=env,
policy=policy,
baseline=baseline,
batch_size=4000,
max_path_length=env.horizon,
n_itr=50,
discount=0.99,
step_size=0.01,
# Uncomment both lines (this and the plot parameter below) to enable plotting
# plot=True,
)
algo.train()
run_experiment_lite(
run_task,
# Number of parallel workers for sampling
n_parallel=1,
# Only keep the snapshot parameters for the last iteration
snapshot_mode="last",
# Specifies the seed for the experiment. If this is not provided, a random seed
# will be used
seed=1,
# plot=True,
)