# Buggy code
def choose_trial_to_run(self, trial_runner):
    """Fair scheduling within iteration by completion percentage.

    List of trials not used since all trials are tracked as state
    of scheduler. If iteration is occupied (ie, no trials to run),
    then look into next iteration.
    """

    for hyperband in self._hyperbands:
        # band will have None entries if no resources
        # are to be allocated to that bracket.
        scrubbed = [b for b in hyperband if b is not None]
        for bracket in scrubbed:
            for trial in bracket.current_trials():
                if trial.status == Trial.PENDING and trial_runner.has_resources(
                    trial.resources
                ):
                    return trial
    # MAIN CHANGE HERE!
    if not any(t.status == Trial.RUNNING for t in trial_runner.get_trials()):
        for hyperband in self._hyperbands:
            for bracket in hyperband:
                if bracket and any(
                    trial.status == Trial.PAUSED for trial in bracket.current_trials()
                ):
                    # This will change the trial state and let the
                    # trial runner retry.
                    self._process_bracket(trial_runner, bracket)
    # MAIN CHANGE HERE!
    return None


# Traceback (ray.tune.error.TuneError)
== Status ==
Memory usage on this node: 7.0/15.6 GiB
Using HyperBand: num_stopped=832 total_brackets=3
Round #0:
None
Bracket(Max Size (n)=2, Milestone (r)=1458, completed=100.0%): {RUNNING: 1, TERMINATED: 833}
Bracket(Max Size (n)=324, Milestone (r)=8, completed=47.3%): {PAUSED: 166}
Resources requested: 4/32 CPUs, 0/0 GPUs, 0.0/8.69 GiB heap, 0.0/2.98 GiB objects
Result logdir: /home/dl-user/ray_results/MCv0_DQN_BOHB
Number of trials: 1000 (166 PAUSED, 1 RUNNING, 833 TERMINATED)
+-----------------------------+------------+----------------------+-------------------+-------------+--------------------+--------+------------------+--------+----------+
| Trial name                  | status     | loc                  | batch_mode        |          lr |   train_batch_size |   iter |   total time (s) |     ts |   reward |
|-----------------------------+------------+----------------------+-------------------+-------------+--------------------+--------+------------------+--------+----------|
| DQN_MountainCar-v0_0428be42 | PAUSED     |                      | truncate_episodes | 1.99095e-05 |                408 |      2 |         25.6885  |   4032 |  -200    |
| DQN_MountainCar-v0_0428be45 | PAUSED     |                      | truncate_episodes | 0.000382289 |                211 |      2 |         24.7536  |   5040 |  -200    |
| DQN_MountainCar-v0_0428be48 | PAUSED     |                      | truncate_episodes | 0.000324929 |                233 |      2 |         25.5532  |   5040 |  -200    |
| DQN_MountainCar-v0_0747e5f2 | PAUSED     |                      | truncate_episodes | 0.000114766 |                 38 |      2 |         23.8492  |   7056 |  -200    |
| DQN_MountainCar-v0_0747e5f5 | PAUSED     |                      | truncate_episodes | 9.1226e-05  |                200 |      2 |         24.2349  |   5040 |  -200    |
| DQN_MountainCar-v0_08218bf0 | PAUSED     |                      | truncate_episodes | 0.000284028 |                 69 |      2 |         25.3671  |   7056 |  -200    |
| DQN_MountainCar-v0_093c0b8c | PAUSED     |                      | truncate_episodes | 0.00237606  |                114 |      2 |         23.3935  |   6048 |  -200    |
| DQN_MountainCar-v0_0a55eae6 | PAUSED     |                      | truncate_episodes | 0.000417829 |                111 |      2 |         23.4849  |   6048 |  -200    |
| DQN_MountainCar-v0_0b307d56 | PAUSED     |                      | truncate_episodes | 0.000196047 |                 59 |      2 |         23.1338  |   7056 |  -200    |
| DQN_MountainCar-v0_0eedea91 | PAUSED     |                      | truncate_episodes | 6.58278e-05 |                 59 |      2 |         24.0254  |   7056 |  -200    |
| DQN_MountainCar-v0_1fcd888b | RUNNING    | 172.16.160.219:47910 | truncate_episodes | 0.000237864 |                751 |     88 |       1638.34    | 199584 |  -122.05 |
| DQN_MountainCar-v0_0023f4f6 | TERMINATED |                      | truncate_episodes | 0.000255833 |                158 |      1 |          5.56779 |   1008 |  -200    |
| DQN_MountainCar-v0_0023f4f9 | TERMINATED |                      | complete_episodes | 0.000262904 |                156 |      1 |          5.43817 |   1200 |  -200    |
| DQN_MountainCar-v0_0023f4fc | TERMINATED |                      | complete_episodes | 0.0002605   |                260 |      1 |          5.33452 |   1200 |  -200    |
| DQN_MountainCar-v0_0108428e | TERMINATED |                      | truncate_episodes | 3.89327e-05 |                732 |      4 |         36.2218  |   5040 |  -200    |
| DQN_MountainCar-v0_01084291 | TERMINATED |                      | truncate_episodes | 2.39745e-05 |                714 |      4 |         36.2585  |   5040 |  -200    |
| DQN_MountainCar-v0_01084294 | TERMINATED |                      | truncate_episodes | 4.9252e-05  |                808 |      4 |         38.4182  |   5040 |  -200    |
| DQN_MountainCar-v0_01084297 | TERMINATED |                      | truncate_episodes | 7.42384e-05 |                804 |      4 |         38.0425  |   5040 |  -200    |
| DQN_MountainCar-v0_014223c0 | TERMINATED |                      | truncate_episodes | 0.0520328   |                 71 |      1 |          6.21906 |   1008 |  -200    |
| DQN_MountainCar-v0_01939ac4 | TERMINATED |                      | complete_episodes | 8.34678e-05 |                124 |      1 |          5.37302 |   1200 |  -200    |
| DQN_MountainCar-v0_01a4cc45 | TERMINATED |                      | complete_episodes | 0.00973094  |                373 |      3 |         27.2147  |  24000 |  -200    |
+-----------------------------+------------+----------------------+-------------------+-------------+--------------------+--------+------------------+--------+----------+
... 980 more trials not shown (156 PAUSED, 823 TERMINATED)


Traceback (most recent call last):
File "/home/dl-user/python-code/modularized_version_ray/ray_BOHB.py", line 123, in <module>
verbose=1,
File "/home/dl-user/.local/lib/python3.7/site-packages/ray/tune/tune.py", line 327, in run
runner.step()
File "/home/dl-user/.local/lib/python3.7/site-packages/ray/tune/trial_runner.py", line 342, in step
self.trial_executor.on_no_available_trials(self)
File "/home/dl-user/.local/lib/python3.7/site-packages/ray/tune/trial_executor.py", line 177, in on_no_available_trials
raise TuneError("There are paused trials, but no more pending "
ray.tune.error.TuneError: There are paused trials, but no more pending trials with sufficient resources.

Process finished with exit code 1

# Fixed code
def choose_trial_to_run(self, trial_runner, allow_recurse=True):
    """Fair scheduling within iteration by completion percentage.

    List of trials not used since all trials are tracked as state
    of scheduler. If iteration is occupied (ie, no trials to run),
    then look into next iteration.
    """

    for hyperband in self._hyperbands:
        # band will have None entries if no resources
        # are to be allocated to that bracket.
        scrubbed = [b for b in hyperband if b is not None]
        for bracket in scrubbed:
            for trial in bracket.current_trials():
                if trial.status == Trial.PENDING and trial_runner.has_resources(
                    trial.resources
                ):
                    return trial
    # MAIN CHANGE HERE!
    if not any(t.status == Trial.RUNNING for t in trial_runner.get_trials()):
        for hyperband in self._hyperbands:
            for bracket in hyperband:
                if bracket and any(
                    trial.status == Trial.PAUSED for trial in bracket.current_trials()
                ):
                    # This will change the trial state
                    self._process_bracket(trial_runner, bracket)

                    # If there are pending trials now, suggest one.
                    # This is because there might be both PENDING and
                    # PAUSED trials now, and PAUSED trials will raise
                    # an error before the trial runner tries again.
                    if allow_recurse and any(
                        trial.status == Trial.PENDING
                        for trial in bracket.current_trials()
                    ):
                        return self.choose_trial_to_run(
                            trial_runner, allow_recurse=False
                        )
    # MAIN CHANGE HERE!
    return None