Beispiel #1
0
    def test_getitem(self):
        s = {"a": quantized_uniform(1, 2, 1)}
        grid = ParameterGrid(Space(s))

        r = grid[0]
        self.assertAlmostEqual(r[0], 0.0)
        r = grid[-1]
        self.assertAlmostEqual(r[0], 0.0)

        s = {"a": quantized_uniform(1, 5, 1),
             "b": quantized_log(1, 5, 1, 10)}
        grid = ParameterGrid(Space(s))

        r = grid[0]
        self.assertAlmostEqual(r[0], 0.0)
        self.assertAlmostEqual(r[1], 0.0)

        r = grid[1]
        self.assertAlmostEqual(r[0], 0.25)
        self.assertAlmostEqual(r[1], 0.0)

        s = {"a": quantized_uniform(1, 5, 1),
             "b": {"c": quantized_log(1, 5, 1, 10),
                   "d": quantized_uniform(0, 10, 1)}}
        grid = ParameterGrid(Space(s))

        r = grid[0]
        self.assertEqual(len(r), 4)
        self.assertAlmostEqual(r[0], 0.0)
        self.assertAlmostEqual(r[1], 0.0)
        self.assertAlmostEqual(r[2], 0.0)
        self.assertAlmostEqual(r[3], None)

        r = grid[1]
        self.assertEqual(len(r), 4)
        self.assertAlmostEqual(r[0], 0.25)
        self.assertAlmostEqual(r[1], 0.0)
        self.assertAlmostEqual(r[2], 0.0)
        self.assertAlmostEqual(r[3], None)

        r = grid[16]
        self.assertEqual(len(r), 4)
        self.assertAlmostEqual(r[0], 0.0)
        self.assertAlmostEqual(r[1], 0.5)
        self.assertAlmostEqual(r[2], None)
        self.assertAlmostEqual(r[3], 0.0)

        r = grid[20]
        self.assertEqual(len(r), 4)
        self.assertAlmostEqual(r[0], 0.0)
        self.assertAlmostEqual(r[1], 0.5)
        self.assertAlmostEqual(r[2], None)
        self.assertAlmostEqual(r[3], 0.1)

        r = grid[-1]
        self.assertEqual(len(r), 4)
        self.assertAlmostEqual(r[0], 0.75)
        self.assertAlmostEqual(r[1], 0.5)
        self.assertAlmostEqual(r[2], None)
        self.assertAlmostEqual(r[3], 0.9)
Beispiel #2
0
    def setUp(self):
        self.qa = quantized_uniform(0, 3, 1)
        self.qb = quantized_uniform(1, 4, 1)
        s = {"a": self.qa, "b": self.qb}

        self.mock_conn = MagicMock(name="connection")
        self.mock_conn.get_space.return_value = Space(s)

        self.space = Space(s)
        self.search = Random(self.mock_conn, s)
Beispiel #3
0
    def setUp(self):
        s = {"a": uniform(1, 10), "b": uniform(5, 15)}

        self.mock_conn = MagicMock(name="connection")
        self.mock_conn.get_space.return_value = Space(s)

        self.search = CMAES(self.mock_conn, s)
Beispiel #4
0
    def setUp(self):
        self.space = {"a": uniform(1, 10), "b": uniform(5, 15)}

        self.mock_conn = MagicMock(name="connection")
        self.mock_conn.get_space.return_value = Space(self.space)

        self.search = QuasiRandom(self.mock_conn, self.space)
Beispiel #5
0
    def setUp(self):
        s = {"a": quantized_uniform(1, 10, 1)}

        self.mock_conn = MagicMock(name="connection")
        self.mock_conn.get_space.return_value = Space(s)

        self.grid = Grid(self.mock_conn, s)
    def test_all_results(self):
        mock_conn = MagicMock(name="connection")
        space = Space({"a": uniform(1, 2)})
        repeat_col_name = "rep"

        data = [{
            "a": 0,
            "_chocolate_id": 0,
            repeat_col_name: 0,
            "_loss": 0.3
        }, {
            "a": 0,
            "_chocolate_id": 0,
            repeat_col_name: 1,
            "_loss": 0.3
        }, {
            "a": 0,
            "_chocolate_id": 0,
            repeat_col_name: 2,
            "_loss": 0.4
        }, {
            "a": 0.6,
            "_chocolate_id": 1,
            repeat_col_name: 0,
            "_loss": 0.3
        }, {
            "a": 0.6,
            "_chocolate_id": 1,
            repeat_col_name: 1,
            "_loss": 0.4
        }, {
            "a": 0.6,
            "_chocolate_id": 1,
            repeat_col_name: 2,
            "_loss": 0.5
        }]

        mock_conn.all_results.return_value = data
        mock_conn.count_results = len(data)
        mock_conn.pop_id = lambda x: x

        cv = Repeat(3, rep_col=repeat_col_name)
        cv.wrap_connection(mock_conn)
        cv.space = space

        self.assertEqual(cv.count_results(), 2)

        results = cv.all_results()
        for entry in results:
            if entry["_chocolate_id"] == 0:
                self.assertAlmostEqual(entry["_loss"], 1 / 3)
            elif entry["_chocolate_id"] == 1:
                self.assertAlmostEqual(entry["_loss"], 0.4)
Beispiel #7
0
    def test_len(self):
        s = {"a": quantized_uniform(1, 2, 1)}
        grid = ParameterGrid(Space(s))
        self.assertEqual(len(grid), 1)

        s = {"a": quantized_uniform(1, 5, 1),
             "b": quantized_log(1, 5, 1, 10)}
        grid = ParameterGrid(Space(s))
        self.assertEqual(len(grid), 16)

        s = {"a": quantized_uniform(1, 5, 1),
             "b": {"c": quantized_log(1, 5, 1, 10),
                   "d": quantized_uniform(0, 1, 1)}}
        grid = ParameterGrid(Space(s))
        self.assertEqual(len(grid), 20)

        s = {"a": quantized_uniform(1, 5, 1),
             "b": {"c": quantized_log(1, 5, 1, 10),
                   "d": quantized_uniform(0, 10, 1)}}

        grid = ParameterGrid(Space(s))
        self.assertEqual(len(grid), 56)
    def test_next_new(self):
        mock_conn = MagicMock(name="connection")
        space = Space({"a": uniform(1, 2)})
        repeat_col_name = "rep"

        data = [{
            "a": 0,
            "_chocolate_id": 0,
            repeat_col_name: 0,
            "_loss": 0.3
        }, {
            "a": 0,
            "_chocolate_id": 0,
            repeat_col_name: 1,
            "_loss": 0.3
        }, {
            "a": 0,
            "_chocolate_id": 0,
            repeat_col_name: 2,
            "_loss": 0.4
        }, {
            "a": 0.6,
            "_chocolate_id": 1,
            repeat_col_name: 0,
            "_loss": 0.3
        }, {
            "a": 0.6,
            "_chocolate_id": 1,
            repeat_col_name: 1,
            "_loss": 0.4
        }, {
            "a": 0.6,
            "_chocolate_id": 1,
            repeat_col_name: 2,
            "_loss": 0.5
        }]

        mock_conn.all_results.return_value = data
        mock_conn.count_results = len(data)
        mock_conn.pop_id = lambda x: x
        mock_conn.get_space.return_value = space

        cv = Repeat(3, rep_col=repeat_col_name)
        s = Random(mock_conn, space, cv)

        token, params = s.next()
        self.assertEqual(token["_chocolate_id"], 2)
    def setUp(self):
        self.repeat_col_name = "rep"
        self.mock_conn = MagicMock(name="connection")
        self.space = Space({"a": uniform(1, 2)})
        self.data = [{
            "a": 0,
            "_chocolate_id": 0,
            "_arm_id": 0,
            self.repeat_col_name: 0,
            "_loss": 0.3
        }, {
            "a": 0,
            "_chocolate_id": 0,
            "_arm_id": 0,
            self.repeat_col_name: 1,
            "_loss": 0.3
        }, {
            "a": 0,
            "_chocolate_id": 0,
            "_arm_id": 0,
            self.repeat_col_name: 2,
            "_loss": 0.4
        }, {
            "a": 0.6,
            "_chocolate_id": 1,
            "_arm_id": 0,
            self.repeat_col_name: 0,
            "_loss": 0.4
        }, {
            "a": 0.6,
            "_chocolate_id": 1,
            "_arm_id": 0,
            self.repeat_col_name: 1,
            "_loss": 0.5
        }]

        self.mock_conn.all_results.return_value = self.data
        self.mock_conn.count_results = len(self.data)
        self.mock_conn.pop_id = lambda x: x
        self.mock_conn.get_space.return_value = self.space
Beispiel #10
0
    def test_conditional_n_steps(self):
        s = [{
            "a": uniform(1, 10),
            "b": uniform(5, 15),
            "C": 0
        }, {
            "c": uniform(2, 3),
            "C": 1
        }]

        db = list()
        comp = list()

        self.mock_conn = MagicMock(name="connection")
        self.mock_conn.get_space.return_value = Space(s)
        self.mock_conn.all_results.return_value = db
        self.mock_conn.count_results.return_value = len(db)
        self.mock_conn.all_complementary.return_value = comp

        self.search = MOCMAES(self.mock_conn, s, mu=2)
        for i in range(25):
            token, p = self.search.next()

            self.assertIn("_chocolate_id", token)
            self.assertEqual(token["_chocolate_id"], i)

            entry = self.mock_conn.insert_result.call_args[0][0]
            entry["_loss_0"] = numpy.random.randn()
            entry["_loss_1"] = numpy.random.randn()
            db.append(entry)
            self.mock_conn.count_results.return_value = len(db)
            self.mock_conn.insert_result.reset_mock()

            if self.mock_conn.insert_complementary.call_count > 0:
                c = self.mock_conn.insert_complementary.call_args[0][0]
                comp.append(c)
                self.mock_conn.insert_complementary.reset_mock()
Beispiel #11
0
 def test_invalid_getitem(self):
     s = {"a": quantized_uniform(1, 2, 1)}
     grid = ParameterGrid(Space(s))
     self.assertRaises(IndexError, grid.__getitem__, 1)
     self.assertRaises(IndexError, grid.__getitem__, -2)
Beispiel #12
0
 def test_init_continuous(self):
     s = {"a": uniform(1, 2)}
     self.assertRaises(AssertionError, ParameterGrid, Space(s))
Beispiel #13
0
 def test_empty_subspace(self):
     grid = ParameterGrid(Space({"a": {"b": None}}))
     self.assertRaises(IndexError, grid.__getitem__, 0)
Beispiel #14
0
 def test_empty_grid(self):
     grid = ParameterGrid(Space(dict()))
     self.assertRaises(IndexError, grid.__getitem__, 0)