13 #ifndef MLPACK_CORE_TREE_RECTANGLE_TREE_MINIMAL_COVERAGE_SWEEP_IMPL_HPP 14 #define MLPACK_CORE_TREE_RECTANGLE_TREE_MINIMAL_COVERAGE_SWEEP_IMPL_HPP 21 template<
typename SplitPolicy>
22 template<
typename TreeType>
26 typename TreeType::ElemType& axisCut)
28 typedef typename TreeType::ElemType ElemType;
31 std::vector<std::pair<ElemType, size_t>> sorted(node->NumChildren());
33 for (
size_t i = 0; i < node->NumChildren(); ++i)
35 sorted[i].first = SplitPolicy::Bound(node->Child(i))[axis].Hi();
39 std::sort(sorted.begin(), sorted.end(),
40 [] (
const std::pair<ElemType, size_t>& s1,
41 const std::pair<ElemType, size_t>& s2)
43 return s1.first < s2.first;
46 size_t splitPointer = node->NumChildren() / 2;
48 axisCut = sorted[splitPointer - 1].first;
51 if (!CheckNonLeafSweep(node, axis, axisCut))
54 for (splitPointer = 1; splitPointer < sorted.size(); splitPointer++)
56 axisCut = sorted[splitPointer - 1].first;
57 if (CheckNonLeafSweep(node, axis, axisCut))
61 if (splitPointer == node->NumChildren())
62 return std::numeric_limits<ElemType>::max();
65 BoundType bound1(node->Bound().Dim());
66 BoundType bound2(node->Bound().Dim());
69 for (
size_t i = 0; i < splitPointer; ++i)
70 bound1 |= node->Child(sorted[i].second).Bound();
72 for (
size_t i = splitPointer; i < node->NumChildren(); ++i)
73 bound2 |= node->Child(sorted[i].second).Bound();
79 ElemType area1 = bound1.Volume();
80 ElemType area2 = bound2.Volume();
85 template<
typename SplitPolicy>
86 template<
typename TreeType>
90 typename TreeType::ElemType& axisCut)
92 typedef typename TreeType::ElemType ElemType;
95 std::vector<std::pair<ElemType, size_t>> sorted(node->Count());
97 sorted.resize(node->Count());
99 for (
size_t i = 0; i < node->NumPoints(); ++i)
101 sorted[i].first = node->Dataset().col(node->Point(i))[axis];
102 sorted[i].second = i;
106 std::sort(sorted.begin(), sorted.end(),
107 [] (
const std::pair<ElemType, size_t>& s1,
108 const std::pair<ElemType, size_t>& s2)
110 return s1.first < s2.first;
113 size_t splitPointer = node->Count() / 2;
115 axisCut = sorted[splitPointer - 1].first;
118 if (!CheckLeafSweep(node, axis, axisCut))
119 return std::numeric_limits<ElemType>::max();
121 BoundType bound1(node->Bound().Dim());
122 BoundType bound2(node->Bound().Dim());
125 for (
size_t i = 0; i < splitPointer; ++i)
126 bound1 |= node->Dataset().col(node->Point(sorted[i].second));
128 for (
size_t i = splitPointer; i < node->NumChildren(); ++i)
129 bound2 |= node->Dataset().col(node->Point(sorted[i].second));
134 return bound1.Volume() + bound2.Volume();
137 template<
typename SplitPolicy>
138 template<
typename TreeType,
typename ElemType>
141 const size_t cutAxis,
144 size_t numTreeOneChildren = 0;
145 size_t numTreeTwoChildren = 0;
148 for (
size_t i = 0; i < node->NumChildren(); ++i)
150 const TreeType& child = node->Child(i);
151 int policy = SplitPolicy::GetSplitPolicy(child, cutAxis, cut);
152 if (policy == SplitPolicy::AssignToFirstTree)
153 numTreeOneChildren++;
154 else if (policy == SplitPolicy::AssignToSecondTree)
155 numTreeTwoChildren++;
159 numTreeOneChildren++;
160 numTreeTwoChildren++;
164 if (numTreeOneChildren <= node->MaxNumChildren() && numTreeOneChildren > 0 &&
165 numTreeTwoChildren <= node->MaxNumChildren() && numTreeTwoChildren > 0)
170 template<
typename SplitPolicy>
171 template<
typename TreeType,
typename ElemType>
174 const size_t cutAxis,
177 size_t numTreeOnePoints = 0;
178 size_t numTreeTwoPoints = 0;
181 for (
size_t i = 0; i < node->NumPoints(); ++i)
183 if (node->Dataset().col(node->Point(i))[cutAxis] <= cut)
189 if (numTreeOnePoints <= node->MaxLeafSize() && numTreeOnePoints > 0 &&
190 numTreeTwoPoints <= node->MaxLeafSize() && numTreeTwoPoints > 0)
198 #endif // MLPACK_CORE_TREE_RECTANGLE_TREE_MINIMAL_COVERAGE_SWEEP_IMPL_HPP static TreeType::ElemType SweepLeafNode(const size_t axis, const TreeType *node, typename TreeType::ElemType &axisCut)
Find a suitable partition of a leaf node along the provided axis.
Definition: minimal_coverage_sweep_impl.hpp:88
static bool CheckNonLeafSweep(const TreeType *node, const size_t cutAxis, const ElemType cut)
Check if an intermediate node can be split along the axis at the provided coordinate.
Definition: minimal_coverage_sweep_impl.hpp:140
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
static TreeType::ElemType SweepNonLeafNode(const size_t axis, const TreeType *node, typename TreeType::ElemType &axisCut)
Find a suitable partition of a non-leaf node along the provided axis.
Definition: minimal_coverage_sweep_impl.hpp:24
static bool CheckLeafSweep(const TreeType *node, const size_t cutAxis, const ElemType cut)
Check if a leaf node can be split along the axis at the provided coordinate.
Definition: minimal_coverage_sweep_impl.hpp:173