Files
ray/python/ray/tune/tests/test_automl_searcher.py
T
Sven 60d4d5e1aa Remove future imports (#6724)
* Remove all __future__ imports from RLlib.

* Remove (object) again from tf_run_builder.py::TFRunBuilder.

* Fix 2xLINT warnings.

* Fix broken appo_policy import (must be appo_tf_policy)

* Remove future imports from all other ray files (not just RLlib).

* Remove future imports from all other ray files (not just RLlib).

* Remove future import blocks that contain `unicode_literals` as well.
Revert appo_tf_policy.py to appo_policy.py (belongs to another PR).

* Add two empty lines before Schedule class.

* Put back __future__ imports into determine_tests_to_run.py. Fails otherwise on a py2/print related error.
2020-01-09 00:15:48 -08:00

72 lines
2.4 KiB
Python

import random
import unittest
from ray.tune import register_trainable
from ray.tune.automl import SearchSpace, DiscreteSpace, GridSearch
class AutoMLSearcherTest(unittest.TestCase):
def setUp(self):
def dummy_train(config, reporter):
reporter(timesteps_total=100, done=True)
register_trainable("f1", dummy_train)
def testExpandSearchSpace(self):
exp = {"test-exp": {"run": "f1", "config": {"a": {"d": "dummy"}}}}
space = SearchSpace([
DiscreteSpace("a.b.c", [1, 2]),
DiscreteSpace("a.d", ["a", "b"]),
])
searcher = GridSearch(space, "reward")
searcher.add_configurations(exp)
trials = searcher.next_trials()
self.assertEqual(len(trials), 4)
self.assertTrue(trials[0].config["a"]["b"]["c"] in [1, 2])
self.assertTrue(trials[1].config["a"]["d"] in ["a", "b"])
def testSearchRound(self):
exp = {"test-exp": {"run": "f1", "config": {"a": {"d": "dummy"}}}}
space = SearchSpace([
DiscreteSpace("a.b.c", [1, 2]),
DiscreteSpace("a.d", ["a", "b"]),
])
searcher = GridSearch(space, "reward")
searcher.add_configurations(exp)
trials = searcher.next_trials()
self.assertEqual(len(searcher.next_trials()), 0)
for trial in trials[1:]:
searcher.on_trial_complete(trial.trial_id)
searcher.on_trial_complete(trials[0].trial_id, error=True)
self.assertTrue(searcher.is_finished())
def testBestTrial(self):
exp = {"test-exp": {"run": "f1", "config": {"a": {"d": "dummy"}}}}
space = SearchSpace([
DiscreteSpace("a.b.c", [1, 2]),
DiscreteSpace("a.d", ["a", "b"]),
])
searcher = GridSearch(space, "reward")
searcher.add_configurations(exp)
trials = searcher.next_trials()
self.assertEqual(len(searcher.next_trials()), 0)
for i, trial in enumerate(trials):
rewards = list(range(i, i + 10))
random.shuffle(rewards)
for reward in rewards:
searcher.on_trial_result(trial.trial_id, {"reward": reward})
best_trial = searcher.get_best_trial()
self.assertEqual(best_trial, trials[-1])
self.assertEqual(best_trial.best_result["reward"], 3 + 10 - 1)
if __name__ == "__main__":
import pytest
import sys
sys.exit(pytest.main(["-v", __file__]))