def test_update_with_new_lower_bound(self): tree = BabTree(MockNodeStorage(create_problem()), KSectionBranchingStrategy(), MockSelectionStrategy()) sol = create_solution(5.0, 10.0) tree.update_root(sol) root_children, _ = tree.branch_at_node(tree.root) a, b = root_children tree.update_node(a, create_solution(7.0, 11.0)) tree.branch_at_node(a) assert np.isclose(5.0, tree.lower_bound) assert np.isclose(10.0, tree.upper_bound) tree.update_node(b, create_solution(6.0, 10.0)) children, _ = tree.branch_at_node(b) assert np.isclose(6.0, tree.lower_bound) assert np.isclose(10.0, tree.upper_bound) a, b = children tree.update_node(a, create_solution(7.0, 10.0)) tree.branch_at_node(a) assert np.isclose(6.0, tree.lower_bound) assert np.isclose(10.0, tree.upper_bound)
def test_update_root(self): tree = BabTree(MockNodeStorage(create_problem()), KSectionBranchingStrategy(), MockSelectionStrategy()) sol = create_solution(5.0, 10.0) tree.update_root(sol) assert np.isclose(5.0, tree.lower_bound) assert np.isclose(10.0, tree.upper_bound)
def tree(): """Create a BaBTree like: 0 +----+----+ | | | 1 2 3 +-+-+ | | 4 5 with coordinates: 0 : [0] 1 : [0, 0] 2 : [0, 1] 3 : [0, 2] 4 : [0, 2, 0] 5 : [0, 2, 1] """ problem = create_problem() storage = MockNodeStorage(problem) t = BabTree(storage, KSectionBranchingStrategy(), MockSelectionStrategy()) t.update_root(create_solution(-30.0, 0.0)) root = t.root bp = BranchingPoint(problem.variable(0), [0.0, 0.5]) root.branch_at_point(bp) c = root.children[2] bp = BranchingPoint(problem.variable(1), [0.0]) c.branch_at_point(bp) return t
def test_branch_on_list_of_points(self, problem): tree = MockTree() node = Node(MockNodeStorage(problem), tree, coordinate=[0]) node.update(create_solution(0.0, 1.0)) new_nodes, _ = node.branch(MockBranching([-0.5, 0.0, 0.5, 1.0, 1.5])) assert len(new_nodes) == 6 expected_bounds = [(-1, -0.5), (-0.5, 0.0), (0.0, 0.5), (0.5, 1.0), (1.0, 1.5), (1.5, 2.0)] self._assert_bounds_are_correct(new_nodes, expected_bounds)
def test_ksection(self, problem, solution): ksection_strat = KSectionBranchingStrategy(7) tree = BabTree( MockNodeStorage(problem), ksection_strat, FakeSelectionStrategy(), ) node = tree.root for i in range(5): node.update(create_solution(0.0, 1.0)) children, _ = node.branch(ksection_strat) assert len(children) == 7 for child in children: assert child.variable.idx == i node = children[0]
def solution(): return create_solution(0.0, 1.0)
def solution(): return create_solution(-20.0, -10.0)