Peano
Loading...
Searching...
No Matches
test_splittrees_example3_offline_triple_split.py
Go to the documentation of this file.
1import pytest
2
3import toolbox.loadbalancing.api.strategies.SplitTrees as split_strategy
4import toolbox.loadbalancing.api.strategies.Decomposition as decomposition_strategy
5from tree_builders import build_visualise_tree_split_example3_2d
6
7
8# ---------------------------------------------------------------------------
9# Stage I integration test
10# ---------------------------------------------------------------------------
11
12
13@pytest.mark.integration
15 """
16 Integration test for the example shown in visualise_tree_split_example3.png.
17
18 The final tree contains 10 leaves arranged as a single refined branch:
19 - 4 deepest leaves on the left-most branch
20 - 3 middle-level leaves on the same branch
21 - 3 top-level leaves directly under the root
22
23 With 3 subtrees and bfs_finest_first traversal, Stage I should distribute
24 the leaves as [4, 3, 3].
25 """
26 tree = build_visualise_tree_split_example3_2d()
27 decomposition = decomposition_strategy.equidistant_splitting(
28 peano_tree=tree,
29 number_of_subtrees=3,
30 )
31
32 split_strategy.split_tree(
33 peano_tree=tree,
34 decomposition=decomposition,
35 traversal="bfs_finest_first",
36 enable_propagate_bottom_up=False,
37 enable_optimize_hierarchy=False,
38 )
39
40 # Root and internal nodes remain in subtree 0 during Stage I.
41 assert tree._tree_arrays[0][0][0] == (1, 0), "Root must stay subtree 0"
42 assert tree._tree_arrays[1][0][0] == (1, 0), "Level-1 internal node must stay subtree 0"
43 assert tree._tree_arrays[2][0][0] == (1, 0), "Level-2 internal node must stay subtree 0"
44
45 # Finest-level leaves of the refined branch go to subtree 0.
46 for coords in ((0, 0), (0, 2), (2, 0), (2, 2)):
47 assert tree._tree_arrays[3][coords[0]][coords[1]] == (1, 0), (
48 f"Deep leaf {coords} must be subtree 0"
49 )
50
51 # Middle-level leaves go to subtree 1.
52 for coords in ((0, 2), (2, 0), (2, 2)):
53 assert tree._tree_arrays[2][coords[0]][coords[1]] == (1, 1), (
54 f"Middle leaf {coords} must be subtree 1"
55 )
56
57 # Top-level leaves go to subtree 2.
58 for coords in ((0, 2), (2, 0), (2, 2)):
59 assert tree._tree_arrays[1][coords[0]][coords[1]] == (1, 2), (
60 f"Top-level leaf {coords} must be subtree 2"
61 )
62
63 tree.update_split_pattern()
64 assert list(tree._split_pattern) == [4.0, 3.0, 3.0], (
65 f"Expected [4,3,3] leaf distribution, got {list(tree._split_pattern)}"
66 )
67
68
69# ---------------------------------------------------------------------------
70# Stage I + II + III integration test
71# ---------------------------------------------------------------------------
72
73
74@pytest.mark.integration
76 """
77 Integration test for the full staged pipeline on example 3.
78
79 Stage II should compact the deepest branch onto Level 2.
80
81 For this sparse example, Stage III keeps the upper mixed parents unchanged
82 (their child set is incomplete), while preserving the same leaf split.
83 """
84 tree_stage2 = build_visualise_tree_split_example3_2d()
85 decomposition_stage2 = decomposition_strategy.equidistant_splitting(
86 peano_tree=tree_stage2,
87 number_of_subtrees=3,
88 )
89 split_strategy.split_tree(
90 peano_tree=tree_stage2,
91 decomposition=decomposition_stage2,
92 traversal="bfs_finest_first",
93 enable_propagate_bottom_up=True,
94 enable_optimize_hierarchy=False,
95 )
96
97 tree_stage3 = build_visualise_tree_split_example3_2d()
98 decomposition_stage3 = decomposition_strategy.equidistant_splitting(
99 peano_tree=tree_stage3,
100 number_of_subtrees=3,
101 )
102 split_strategy.split_tree(
103 peano_tree=tree_stage3,
104 decomposition=decomposition_stage3,
105 traversal="bfs_finest_first",
106 enable_propagate_bottom_up=True,
107 enable_optimize_hierarchy=True,
108 )
109
110 # Stage II only: the deepest refined node is compacted, but the upper
111 # topology is still unresolved.
112 assert tree_stage2._tree_arrays[0][0][0] == (1, 0), "Root must stay 0 after Stage II only"
113 assert tree_stage2._tree_arrays[1][0][0] == (1, 0), "Level-1 internal node must stay 0 after Stage II only"
114 assert tree_stage2._tree_arrays[2][0][0] == (1, 0), "Level-2 internal node must stay 0 after Stage II only"
115
116 # Stage III: for this sparse case, upper mixed parents stay unchanged.
117 assert tree_stage3._tree_arrays[0][0][0] == (1, 0), "Root must remain subtree 0"
118 assert tree_stage3._tree_arrays[1][0][0] == (1, 0), "Level-1 internal node must remain subtree 0"
119 assert tree_stage3._tree_arrays[2][0][0] == (1, 0), "Level-2 internal node stays subtree 0"
120
121 # The leaf mapping remains the same 4-3-3 split.
122 for coords in ((0, 0), (0, 2), (2, 0), (2, 2)):
123 assert tree_stage3._tree_arrays[3][coords[0]][coords[1]] == (1, 0)
124 for coords in ((0, 2), (2, 0), (2, 2)):
125 assert tree_stage3._tree_arrays[2][coords[0]][coords[1]] == (1, 1)
126 for coords in ((0, 2), (2, 0), (2, 2)):
127 assert tree_stage3._tree_arrays[1][coords[0]][coords[1]] == (1, 2)
128
129 tree_stage3.update_split_pattern()
130 assert list(tree_stage3._split_pattern) == [4.0, 3.0, 3.0]
131
132
133@pytest.mark.integration
135 """
136 With relaxed Stage III gating, sparse mixed parents in example 3 can be
137 promoted without requiring a complete child set.
138 """
139 tree_stage3_relaxed = build_visualise_tree_split_example3_2d()
140 decomposition_relaxed = decomposition_strategy.equidistant_splitting(
141 peano_tree=tree_stage3_relaxed,
142 number_of_subtrees=3,
143 )
144 split_strategy.split_tree(
145 peano_tree=tree_stage3_relaxed,
146 decomposition=decomposition_relaxed,
147 traversal="bfs_finest_first",
148 enable_propagate_bottom_up=True,
149 enable_optimize_hierarchy=True,
150 require_all_children_for_hierarchy_optimization=False,
151 )
152
153 # Relaxed Stage III promotes sparse mixed parents along the branch.
154 assert tree_stage3_relaxed._tree_arrays[0][0][0] == (1, 2), "Root should be promoted to subtree 2"
155 assert tree_stage3_relaxed._tree_arrays[1][0][0] == (1, 1), "Level-1 internal node should be promoted to subtree 1"
156 assert tree_stage3_relaxed._tree_arrays[2][0][0] == (1, 0), "Level-2 internal node remains subtree 0"
157
158 # Leaf split remains unchanged.
159 tree_stage3_relaxed.update_split_pattern()
160 assert list(tree_stage3_relaxed._split_pattern) == [4.0, 3.0, 3.0]
test_example3_stage1_bfs_finest_first_leaf_distribution()
Integration test for the example shown in visualise_tree_split_example3.png.
test_example3_stage3_promotes_root_and_level1_node()
Integration test for the full staged pipeline on example 3.
test_example3_stage3_can_promote_sparse_mixed_parents_when_enabled()
With relaxed Stage III gating, sparse mixed parents in example 3 can be promoted without requiring a ...