# Buggy code
def mtsac_metaworld_mt50(ctxt=None, seed=1, use_gpu=False, _gpu=0):
    """Train MTSAC with MT50 environment.

    Args:
        ctxt (garage.experiment.ExperimentContext): The experiment
            configuration used by LocalRunner to create the snapshotter.
        seed (int): Used to seed the random number generator to produce
            determinism.
        use_gpu (bool): Used to enable ussage of GPU in training.
        _gpu (int): The ID of the gpu (used on multi-gpu machines).

    """
    deterministic.set_seed(seed)
    runner = LocalRunner(ctxt)
    task_names = mwb.MT50.get_train_tasks().all_task_names
    train_envs = []
    test_envs = []
    for task_name in task_names:
        train_env = normalize(
            GymEnv(mwb.MT50.from_task(task_name)), normalize_reward=True
        )
        test_env = normalize(GymEnv(mwb.MT50.from_task(task_name)))
        train_envs.append(train_env)
        test_envs.append(test_env)
    mt50_train_envs = MultiEnvWrapper(
        train_envs, sample_strategy=round_robin_strategy, mode="vanilla"
    )
    mt50_test_envs = MultiEnvWrapper(
        test_envs, sample_strategy=round_robin_strategy, mode="vanilla"
    )
    policy = TanhGaussianMLPPolicy(
        env_spec=mt50_train_envs.spec,
        hidden_sizes=[400, 400, 400],
        hidden_nonlinearity=nn.ReLU,
        output_nonlinearity=None,
        min_std=np.exp(-20.0),
        max_std=np.exp(2.0),
    )

    qf1 = ContinuousMLPQFunction(
        env_spec=mt50_train_envs.spec,
        hidden_sizes=[400, 400, 400],
        hidden_nonlinearity=F.relu,
    )

    qf2 = ContinuousMLPQFunction(
        env_spec=mt50_train_envs.spec,
        hidden_sizes=[400, 400, 400],
        hidden_nonlinearity=F.relu,
    )

    replay_buffer = PathBuffer(
        capacity_in_transitions=int(1e6),
    )

    timesteps = 100000000
    batch_size = int(150 * mt50_train_envs.num_tasks)
    num_evaluation_points = 500
    epochs = timesteps // batch_size
    epoch_cycles = epochs // num_evaluation_points
    epochs = epochs // epoch_cycles
    mtsac = MTSAC(
        policy=policy,
        qf1=qf1,
        qf2=qf2,
        gradient_steps_per_itr=150,
        max_episode_length=250,
        eval_env=mt50_test_envs,
        env_spec=mt50_train_envs.spec,
        num_tasks=10,
        steps_per_epoch=epoch_cycles,
        replay_buffer=replay_buffer,
        min_buffer_size=7500,
        target_update_tau=5e-3,
        discount=0.99,
        buffer_batch_size=6400,
    )
    set_gpu_mode(use_gpu, _gpu)
    mtsac.to()
    runner.setup(algo=mtsac, env=mt50_train_envs, sampler_cls=LocalSampler)
    runner.train(n_epochs=epochs, batch_size=batch_size)


# Traceback (ValueError)
2020-08-15 02:07:45 | [mtsac_metaworld_mt50] Setting seed to 1
^T2020-08-15 02:09:26 | [mtsac_metaworld_mt50] Obtaining samples...
Traceback (most recent call last):
File "examples/torch/mtsac_metaworld_mt50.py", line 103, in <module>
mtsac_metaworld_mt50()
File "/home/eholly/venv/lib/python3.5/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/eholly/venv/lib/python3.5/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/eholly/venv/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/eholly/venv/lib/python3.5/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/experiment/experiment.py", line 553, in __call__
result = self.function(ctxt, **kwargs)
File "examples/torch/mtsac_metaworld_mt50.py", line 100, in mtsac_metaworld_mt50
runner.train(n_epochs=epochs, batch_size=batch_size)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/experiment/local_runner.py", line 485, in train
average_return = self._algo.train(self)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/torch/algos/sac.py", line 206, in train
last_return = self._evaluate_policy(runner.step_itr)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/torch/algos/mtsac.py", line 189, in _evaluate_policy
num_trajs=self._num_evaluation_trajectories))
File "/home/eholly/venv/lib/python3.5/site-packages/garage/np/_functions.py", line 62, in obtain_evaluation_samples
deterministic=True)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/sampler/utils.py", line 66, in rollout
next_o, r, d, env_info = env.step(a)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/envs/multi_env_wrapper.py", line 231, in step
obs, reward, done, info = self.env.step(action)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/envs/garage_env.py", line 154, in step
observation, reward, done, info = self.env.step(action)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/envs/normalized_env.py", line 153, in step
next_obs, reward, done, info = self.env.step(scaled_action)
File "/home/eholly/venv/lib/python3.5/site-packages/garage/envs/garage_env.py", line 154, in stpe
observation, reward, done, info = self.env.step(action)
File "/home/eholly/venv/lib/python3.5/site-packages/metaworld/envs/mujoco/multitask_env.py", line 161, in step
obs, reward, done, info = self.active_env.step(action)
File "/home/eholly/venv/lib/python3.5/site-packages/metaworld/envs/mujoco/sawyer_xyz/sawyer_plate_slide_back_side.py", line 124, in step
self.do_simulation([action[-1], -action[-1]])
File "/home/eholly/venv/lib/python3.5/site-packages/metaworld/envs/mujoco/mujoco_env.py", line 118, in do_simulation
raise ValueError('Maximum path length allowed by the benchmark has been exceeded')
ValueError: Maximum path length allowed by the benchmark has been exceeded
Makefile:187: recipe for target 'run-headless' failed
make: *** [run-headless] Error 1

# Fixed code
def mtsac_metaworld_mt50(ctxt=None, seed=1, use_gpu=False, _gpu=0):
    """Train MTSAC with MT50 environment.

    Args:
        ctxt (garage.experiment.ExperimentContext): The experiment
            configuration used by LocalRunner to create the snapshotter.
        seed (int): Used to seed the random number generator to produce
            determinism.
        use_gpu (bool): Used to enable ussage of GPU in training.
        _gpu (int): The ID of the gpu (used on multi-gpu machines).

    """
    deterministic.set_seed(seed)
    runner = LocalRunner(ctxt)
    task_names = mwb.MT50.get_train_tasks().all_task_names
    train_envs = []
    test_envs = []
    for task_name in task_names:
        train_env = normalize(
            GymEnv(mwb.MT50.from_task(task_name)), normalize_reward=True
        )
        test_env = normalize(GymEnv(mwb.MT50.from_task(task_name)))
        train_envs.append(train_env)
        test_envs.append(test_env)
    mt50_train_envs = MultiEnvWrapper(
        train_envs, sample_strategy=round_robin_strategy, mode="vanilla"
    )
    mt50_test_envs = MultiEnvWrapper(
        test_envs, sample_strategy=round_robin_strategy, mode="vanilla"
    )
    policy = TanhGaussianMLPPolicy(
        env_spec=mt50_train_envs.spec,
        hidden_sizes=[400, 400, 400],
        hidden_nonlinearity=nn.ReLU,
        output_nonlinearity=None,
        min_std=np.exp(-20.0),
        max_std=np.exp(2.0),
    )

    qf1 = ContinuousMLPQFunction(
        env_spec=mt50_train_envs.spec,
        hidden_sizes=[400, 400, 400],
        hidden_nonlinearity=F.relu,
    )

    qf2 = ContinuousMLPQFunction(
        env_spec=mt50_train_envs.spec,
        hidden_sizes=[400, 400, 400],
        hidden_nonlinearity=F.relu,
    )

    replay_buffer = PathBuffer(
        capacity_in_transitions=int(1e6),
    )

    timesteps = 100000000
    batch_size = int(150 * mt50_train_envs.num_tasks)
    num_evaluation_points = 500
    epochs = timesteps // batch_size
    epoch_cycles = epochs // num_evaluation_points
    epochs = epochs // epoch_cycles
    mtsac = MTSAC(
        policy=policy,
        qf1=qf1,
        qf2=qf2,
        gradient_steps_per_itr=150,
        max_episode_length=150,
        eval_env=mt50_test_envs,
        env_spec=mt50_train_envs.spec,
        num_tasks=10,
        steps_per_epoch=epoch_cycles,
        replay_buffer=replay_buffer,
        min_buffer_size=7500,
        target_update_tau=5e-3,
        discount=0.99,
        buffer_batch_size=6400,
    )
    set_gpu_mode(use_gpu, _gpu)
    mtsac.to()
    runner.setup(algo=mtsac, env=mt50_train_envs, sampler_cls=LocalSampler)
    runner.train(n_epochs=epochs, batch_size=batch_size)