def should_raise_exception_if_keylength_is_invalid(): # given, when, then with pytest.raises(ValueError): UniformPreferenceDict('foo') # given, when, then with pytest.raises(ValueError): UniformPreferenceDict(-1)
def test_preferences(self): d = UniformPreferenceDict(3) a, b = (0, 1, 2), (3, 4, 5) d[a, b] = 1 e, f = (9, 9, 9), (1, 1, 1) d[e, f] = -1 self.assertEqual(sorted(d.preferences()), [(0, 1, 2), (1, 1, 1), (3, 4, 5), (9, 9, 9)])
def should_return_correct_preference_key_items(): # given a = (0, 1, 2) b = (3, 4, 5) e = (9, 9, 9) f = (1, 1, 1) preferences = UniformPreferenceDict(len(a)) # when preferences[a, b] = 1 preferences[e, f] = -1 # then assert sorted(preferences.preferences()) == [a, f, b, e]
def run(): # set RNG seed np.random.seed(0) tf.set_random_seed(0) # set up func = CosineMixture() bounds = func.bounds presenter = BenchmarkInputPresenter(func) output = BenchmarkOutputPresenter(func.__class__.__name__) optimizer = DirectOptimizer(bounds) model = BinaryPreferenceModel() data = UniformPreferenceDict(2) acquirer = ExpectedImprovementAcquirer(data, model, optimizer) ex = PreferenceExperiment(acquirer, presenter, output) # add initial observation n_initial_samples = 3 for _ in range(n_initial_samples): x1 = sample_uniformly(bounds) x2 = sample_uniformly(bounds) data[x1, x2] = presenter.get_choice(x1, x2) # run experiment n_iter = 3 for i in range(n_iter): ex.run()
def __init__(self): self.data = UniformPreferenceDict(2) self.data[(0, 0), (1, 1)] = 1 self._best = (0, 0) self._next = (2, 2) self._valuations = [((0, 0), 1), ((2, 2), 0)]
def test_preprocess_data(self): pd = UniformPreferenceDict(2) a = (0, 1) b = (2, 3) c = (4, 5) d = (6, 7) e = (8, 9) pd[a, b] = 1 pd[a, c] = 1 pd[a, d] = 1 pd[c, b] = -1 pd[d, e] = 0 X, y = preprocess_data(pd) A = np.array([ [0, 1], [2, 3], [4, 5], [6, 7], [8, 9], ]) self.assertTrue(np.allclose(A, X)) b = { (0, 1): 1, (0, 2): 1, (0, 3): 1, (1, 2): 1, (3, 4): 0, } self.assertEqual(y, b)
def test_basic(self): with self.assertRaises(ValueError): UniformPreferenceDict("foo") with self.assertRaises(ValueError): UniformPreferenceDict(-1) d = UniformPreferenceDict(3) a, b = (0, 1, 2), (3, 4, 5) d[a, b] = 1 self.assertEqual(d[a, b], 1) self.assertEqual(d[b, a], -1) e = (9, 9) with self.assertRaises(ValueError): d[e, a] = -1 self.assertEqual(len(d), 1)
def should_store_and_retrieve_preference_in_two_dimensions(): # given a = (0, 1) b = (2, 3) preferences = UniformPreferenceDict(len(a)) # when preferences[a, b] = 1 # then assert preferences[a, b] == 1 assert preferences[b, a] == -1
def should_raise_exception_if_dimension_is_different_than_keylength(): # given a = (0, 1, 2) e = (9, 9) preferences = UniformPreferenceDict(len(a)) # when, then with pytest.raises(ValueError): preferences[a, e] = 1 # when, then with pytest.raises(ValueError): preferences[e, a] = 1
def should_return_correct_number_of_preferences(): # given a = (0,) b = (1,) c = (2,) preferences = UniformPreferenceDict(len(a)) # when preferences[a, b] = 1 preferences[a, c] = 1 # then assert len(preferences) == 2
def test_run(self): # set RNG seed np.random.seed(0) tf.set_random_seed(0) # set up bounds = [ (-3, 6), (-3, 6), ] optimizer = DirectOptimizer(bounds) model = BinaryPreferenceModel() presenter = MockInputPresenter() output = MockOutputPresenter() data = UniformPreferenceDict(2) a = (0, 0) b = (1, 1) c = (2, 2) d = (3, 3) data[a, b] = presenter.get_choice(a, b) data[a, c] = presenter.get_choice(a, c) data[b, c] = presenter.get_choice(b, c) data[b, d] = presenter.get_choice(b, d) data[c, d] = presenter.get_choice(c, d) # construct experiment acquirer = ExpectedImprovementAcquirer(data, model, optimizer) ex = PreferenceExperiment( acquirer, presenter, output ) # verify data self.assertEqual(len(data), 5) self.assertTrue(verify_preferences(data, presenter)) # run experiment ex.run() # verify data self.assertEqual(len(data), 6) self.assertTrue(verify_preferences(data, presenter)) # run experiment ex.run() # verify data self.assertEqual(len(data), 7) self.assertTrue(verify_preferences(data, presenter))
def run_quadratic(): # set RNG seed np.random.seed(RNG_SEED) tf.set_random_seed(RNG_SEED) # set up bounds = [(-5, 5)] func = np.square input_presenter = ExampleInputPresenter(func) optimizer = GridSearchOptimizer(bounds) model = BinaryPreferenceModel( lengthscale=1.0, **CONFIG ) # initialization n_initial_observations = 3 data = UniformPreferenceDict(1) initial_observations = np.random.uniform( *bounds[0], size=n_initial_observations ).tolist() for a, b in itertools.combinations(initial_observations, 2): data[(a,), (b,)] = input_presenter.get_choice(a, b) # construct acquirer acquirer = ExpectedImprovementAcquirer(data, model, optimizer) output_file('1d_quadratic.html') n_gridpoints = 1000 grid = np.linspace(*bounds[0], num=n_gridpoints) x = grid[:, np.newaxis] n_iter = 5 plots = [] for i in range(n_iter): xn = acquirer.next xb = acquirer.best choice = input_presenter.get_choice(xn, xb) acquirer.update(xn, xb, choice) p = figure( title='learned (iteration {}/{})'.format(i + 1, n_iter), x_axis_label='x', y_axis_label='y', x_range=bounds[0], ) # plot mean +/- standard deviation mean = acquirer.model.mean(x) stddev = np.sqrt(acquirer.model.variance(x)) lower = mean - stddev upper = mean + stddev p.line( grid, mean, line_width=3, legend='mean', ) p.patch( np.concatenate([grid, grid[::-1]]), np.concatenate([lower, upper[::-1]]), alpha=0.5, line_width=1, legend='stddev' ) # plot data valuations = acquirer.valuations xs, ys = zip(*valuations) xs_flat = [e[0] for e in xs] p.asterisk( xs_flat, ys, size=20, color='black', legend='data' ) # plot query and incumbent point p.circle( xn, acquirer.model.mean([xn]), size=10, color='red', legend='query' ) p.circle( xb, acquirer.model.mean([xb]), size=10, color='green', legend='incumbent' ) # plot true valuation q = figure( title='true valuation', x_axis_label='x', y_axis_label='y', x_range=bounds[0], ) q.line( grid, func(grid), legend='valuation', line_width=3, ) # plot data q.asterisk( xs_flat, func(xs_flat), size=20, color='black', legend='data' ) # plot query and incumbent point q.circle( xn, func(xn), size=10, color='red', legend='query' ) q.circle( xb, func(xb), size=10, color='green', legend='incumbent' ) plots.append(row(p, q)) show(column(*plots))
def test_interface_with_grid_search_optimizer_ard_lengthscale(self): with self.test_session(): # set RNG seed np.random.seed(RNG_SEED) tf.set_random_seed(RNG_SEED) # set up bounds = [ (-3, 6), (-3, 6), ] optimizer = GridSearchOptimizer(bounds) model = BinaryPreferenceModel( ard=True, **CONFIG ) data = UniformPreferenceDict(2) a = (0, 0) b = (1, 0) c = (2, 0) d = (3, 0) data[a, b] = 1 data[a, c] = 1 data[b, c] = 1 data[b, d] = 1 data[c, d] = 1 ab = (0.5, 0) bc = (1.5, 0) cd = (2.5, 0) data[a, ab] = 1 data[ab, b] = 1 data[b, bc] = 1 data[bc, c] = 1 data[c, cd] = 1 data[cd, d] = 1 data[ab, bc] = 1 data[ab, cd] = 1 data[bc, cd] = 1 # construct acquirer acquirer = ExpectedImprovementAcquirer(data, model, optimizer) # test that `next` needs to be called before `best` with self.assertRaises(ValueError): acquirer.best # test that `next` needs to be called before `valuations` with self.assertRaises(ValueError): acquirer.valuations # test `next` a1, a2 = a b1, b2 = b xn = xn1, xn2 = acquirer.next eps = 1.0 self.assertTrue( (a1 - eps < xn1 < b1), ) self.assertTrue(np.allclose(xn, acquirer.next)) # test `best` best = acquirer.best self.assertTrue(np.allclose(a, best)) self.assertTrue(np.allclose(best, acquirer.best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:]))) # test `lengthscale` s1, s2 = model.lengthscale self.assertTrue(s1 < s2) # test `update` e = (-1, 0) acquirer.update(e, a, 1) acquirer.update(e, ab, 1) acquirer.update(e, b, 1) acquirer.update(e, bc, 1) # test `next` l1, l2 = (x[0] for x in bounds) xn1, xn2 = acquirer.next self.assertTrue( (l1 <= xn1 < a1), ) # test `best` best = acquirer.best self.assertTrue(best < b) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:]))) # test `lengthscale` s1, s2 = model.lengthscale self.assertTrue(s1 < s2)
def test_interface_with_direct_optimizer_scalar_lengthscale(self): with self.test_session(): # set RNG seed np.random.seed(RNG_SEED) tf.set_random_seed(RNG_SEED) # set up bounds = [ (-3, 6), (-3, 6), ] optimizer = DirectOptimizer(bounds) model = BinaryPreferenceModel( lengthscale=1.0, **CONFIG ) data = UniformPreferenceDict(2) a = (0, 0) b = (1, 1) c = (2, 2) d = (3, 3) data[a, b] = 1 data[a, c] = 1 data[b, c] = 1 data[b, d] = 1 data[c, d] = 1 # construct acquirer acquirer = ExpectedImprovementAcquirer(data, model, optimizer) # test that `next` needs to be called before `best` with self.assertRaises(ValueError): acquirer.best # test that `next` needs to be called before `valuations` with self.assertRaises(ValueError): acquirer.valuations # test `next` a1, a2 = a b1, b2 = b xn = xn1, xn2 = acquirer.next eps = 0.5 self.assertTrue( (a1 - eps < xn1 < b1) and (a2 - eps < xn2 < b2) ) self.assertTrue(np.allclose(xn, acquirer.next)) # test `best` best = acquirer.best self.assertTrue(np.allclose(a, best)) self.assertTrue(np.allclose(best, acquirer.best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:]))) # test `update` e = (-1, -1) acquirer.update(e, a, 1) acquirer.update(e, b, 1) # test `next` l1, l2 = (x[0] for x in bounds) xn1, xn2 = acquirer.next self.assertTrue( (l1 < xn1 < a1) and (l2 < xn2 < a2), ) # test `best` best = acquirer.best self.assertTrue(np.allclose(e, best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:])))
def test_interface_with_grid_search_optimizer_vector_lengthscale(self): with self.test_session(): # set RNG seed np.random.seed(RNG_SEED) tf.set_random_seed(RNG_SEED) # set up bounds = [ (-3, 6), (-3, 6), ] optimizer = GridSearchOptimizer(bounds) lengthscale = np.array([1, 10], np.float32) model = BinaryPreferenceModel(lengthscale=lengthscale, **CONFIG) data = UniformPreferenceDict(2) a = (0, 0) b = (1, 0) c = (2, 0) d = (3, 0) data[a, b] = 1 data[a, c] = 1 data[b, c] = 1 data[b, d] = 1 data[c, d] = 1 # construct acquirer acquirer = ExpectedImprovementAcquirer(data, model, optimizer) # test that `next` needs to be called before `best` with self.assertRaises(ValueError): acquirer.best # pylint: disable=pointless-statement # test that `next` needs to be called before `valuations` with self.assertRaises(ValueError): acquirer.valuations # pylint: disable=pointless-statement # test `next` a1, _ = a b1, _ = b xn = xn1, _ = acquirer.next eps = 0.5 self.assertTrue((a1 - eps < xn1 < b1)) self.assertTrue(np.allclose(xn, acquirer.next)) # test `best` best = acquirer.best self.assertTrue(np.allclose(a, best)) self.assertTrue(np.allclose(best, acquirer.best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:]))) # test `lengthscale` self.assertAllClose(model.lengthscale, lengthscale) # test `update` e = (-1, 0) acquirer.update(e, a, 1) acquirer.update(e, b, 1) # test `next` l1, _ = (x[0] for x in bounds) xn1, _ = acquirer.next self.assertTrue((l1 < xn1 < a1)) # test `best` best = acquirer.best self.assertTrue(np.allclose(e, best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:])))