def test_branch_on_candidate_returns_2_simplices(self): np.random.seed(1053) simplex = make_simplex(dimension=10) optimizer = make_branch_bound_optimizer(simplex.dimension) branched = optimizer.branch_on_candidate(simplex) self.assertEqual(len(branched), 2)
def make_branch_bound_optimizer(dimension=3): initial_simplices = [make_simplex(dimension=dimension) for _ in range(10)] simplex_bound_calculator = make_simplex_bound_calculator() optimizer = BranchBoundOptimizer( square_distance_from_center, initial_simplices, simplex_bound_calculator) return optimizer
def test_init_stores_objective_function(self): objective_function = square_distance_from_center initial_simplices = [make_simplex() for _ in range(10)] simplex_bound_calculator = make_simplex_bound_calculator() optimizer = BranchBoundOptimizer( objective_function, initial_simplices, simplex_bound_calculator) self.assertIs(optimizer.objective_function, objective_function)
def test_init_sets_up_heap(self): np.random.seed(1024) initial_simplices = [make_simplex() for _ in range(10)] simplex_bound_calculator = make_simplex_bound_calculator() optimizer = BranchBoundOptimizer( square_distance_from_center, initial_simplices, simplex_bound_calculator) self.assertIsInstance(optimizer._heap, Heap)
def test_branch_on_candidate_puts_max_vertex_in_only_1_child(self): np.random.seed(1157) simplex = make_simplex(dimension=10) vertex_max = simplex.vertex_with_max_value optimizer = make_branch_bound_optimizer(simplex.dimension) branched = optimizer.branch_on_candidate(simplex) max_in_branched = [vertex_max in b.function_points for b in branched] self.assertTrue(any(max_in_branched)) self.assertFalse(all(max_in_branched))
def test_branch_on_candidate_keeps_other_vertices(self): np.random.seed(1159) simplex = make_simplex(dimension=10) optimizer = make_branch_bound_optimizer(simplex.dimension) branched = optimizer.branch_on_candidate(simplex) for b in branched: each_in_b = [ v in b.function_points for v in simplex.function_points] count_in_b = sum(each_in_b) self.assertEqual(count_in_b, len(simplex.function_points) - 1)
def test_branch_on_candidate_decreases_distance_from_max_vertex(self): np.random.seed(1159) simplex = make_simplex(dimension=10) optimizer = make_branch_bound_optimizer(simplex.dimension) branched = optimizer.branch_on_candidate(simplex) get_max_distance = lambda simplex: max([ np.linalg.norm(simplex.vertex_with_max_value.point - v.point) for v in simplex.function_points]) max_old = get_max_distance(simplex) for b in branched: max_new = get_max_distance(b) self.assertLess(max_new, max_old)
def test_bounds_correctly(self): np.random.seed(1459) point_bounder = bound.OrdinaryPointBoundCalculator(*np.random.randn(2)) simplex_bounder = bound.MaxPointSimplexBoundCalculator(point_bounder) simplex = make_simplex(dimension=10) result = simplex_bounder.bound(simplex) index = np.argmax([fp.value for fp in simplex.function_points]) vertex_max_f = simplex.function_points[index].value max_dist_from_vertex = max([ np.linalg.norm(fp.point - simplex.function_points[index].point) for fp in simplex.function_points]) correct = vertex_max_f - point_bounder.bound(max_dist_from_vertex) self.assertAlmostEqual(result, correct, places=13)
def test_init_sets_current_min_function_point(self): np.random.seed(1045) initial_simplices = [make_simplex() for _ in range(10)] simplex_bound_calculator = make_simplex_bound_calculator() optimizer = BranchBoundOptimizer( square_distance_from_center, initial_simplices, simplex_bound_calculator) function_values = [ simplex.vertex_with_min_value.value for simplex in initial_simplices] true_min_found = min(function_values) stored_min_found = optimizer.current_min_function_point.value self.assertEqual(true_min_found, stored_min_found)
def test_setup_heap_includes_all_simplices(self): np.random.seed(1024) initial_simplices = [make_simplex() for _ in range(10)] simplex_bound_calculator = make_simplex_bound_calculator() optimizer = BranchBoundOptimizer( square_distance_from_center, initial_simplices, simplex_bound_calculator) heap = optimizer._setup_heap(initial_simplices) self.assertEqual(len(initial_simplices), heap.num_in_heap) heap_entries = [] while len(heap) > 0: heap_entries.append(heap.pop_min()) simplices_in_heap = [entry.object for entry in heap_entries] for simplex in initial_simplices: self.assertIn(simplex, simplices_in_heap)
def test_bound_checks_if_simplex(self): bounder = bound.SimplexBoundCalculator() simplex = make_simplex() function_point = simplex.function_points[0] self.assertRaises(ValueError, bounder.bound, function_point)
def test_under_bound_raises_notimplementederror(self): bounder = bound.SimplexBoundCalculator() simplex = make_simplex() self.assertRaises(NotImplementedError, bounder._bound, simplex)