-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgreedy_additive.hxx
More file actions
146 lines (134 loc) · 4.44 KB
/
Copy pathgreedy_additive.hxx
File metadata and controls
146 lines (134 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
#include "bioimage_cpp/detail/profile.hxx"
#include "bioimage_cpp/graph/lifted_multicut/detail.hxx"
#include "bioimage_cpp/graph/lifted_multicut/objective.hxx"
#include <cstddef>
#include <cstdint>
#include <vector>
namespace bioimage_cpp::graph::lifted_multicut {
// Reusable scratch state for `lifted_greedy_additive`.
struct GreedyAdditiveWorkspace {
detail::DynamicGraph dynamic_graph;
bioimage_cpp::util::UnionFind union_find{0};
detail::EdgeHeap heap;
void reset(const UndirectedGraph &lifted_graph) {
dynamic_graph.reset(lifted_graph);
union_find.reset(static_cast<std::size_t>(lifted_graph.number_of_nodes()));
heap.reset_capacity(static_cast<std::size_t>(lifted_graph.number_of_edges()));
}
};
// Greedy-additive multicut on the lifted graph. Identical contraction flow to
// multicut::greedy_additive, with one twist: lifted edges are never contracted
// directly. They influence the energy via weight accumulation during merges —
// a lifted edge between super-nodes contributes its weight to the contracted
// edge whenever the two super-nodes also share a non-lifted edge, and the
// combined edge stops being lifted as soon as a base-graph path connects its
// endpoints.
inline std::vector<std::uint64_t> greedy_additive(
const UndirectedGraph &lifted_graph,
const std::vector<double> &weights,
const std::uint64_t n_base_edges,
const double weight_stop,
const double node_num_stop,
const bool add_noise,
const int seed,
const double sigma,
GreedyAdditiveWorkspace &workspace
) {
BIOIMAGE_PROFILE_INIT(profile);
validate_weights(lifted_graph, weights);
{
BIOIMAGE_PROFILE_SCOPE(profile, "workspace_reset");
workspace.reset(lifted_graph);
}
auto &dynamic_graph = workspace.dynamic_graph;
auto &sets = workspace.union_find;
auto &heap = workspace.heap;
{
BIOIMAGE_PROFILE_SCOPE(profile, "initialize");
detail::initialize_dynamic_graph(
lifted_graph, weights, n_base_edges, dynamic_graph, heap, add_noise, seed, sigma
);
}
{
BIOIMAGE_PROFILE_SCOPE(profile, "contraction_loop");
while (!heap.empty() && dynamic_graph.alive_count > 1) {
const auto top = heap.top();
if (top.priority <= weight_stop) {
break;
}
if (node_num_stop > 0.0
&& dynamic_graph.alive_count <= detail::stop_node_count(lifted_graph, node_num_stop)) {
break;
}
const auto &edge = dynamic_graph.edges[top.key];
detail::merge_dynamic_nodes(dynamic_graph, sets, heap, edge.u, edge.v);
}
}
std::vector<std::uint64_t> labels;
{
BIOIMAGE_PROFILE_SCOPE(profile, "labels_from_sets");
labels = detail::labels_from_sets(sets, lifted_graph);
}
BIOIMAGE_PROFILE_REPORT(profile);
return labels;
}
inline std::vector<std::uint64_t> greedy_additive(
const UndirectedGraph &lifted_graph,
const std::vector<double> &weights,
const std::uint64_t n_base_edges,
const double weight_stop,
const double node_num_stop,
const bool add_noise,
const int seed,
const double sigma
) {
GreedyAdditiveWorkspace workspace;
return greedy_additive(
lifted_graph,
weights,
n_base_edges,
weight_stop,
node_num_stop,
add_noise,
seed,
sigma,
workspace
);
}
class GreedyAdditiveSolver final : public SolverBase {
public:
GreedyAdditiveSolver(
const double weight_stop = 0.0,
const double node_num_stop = -1.0,
const bool add_noise = false,
const int seed = 42,
const double sigma = 1.0
)
: weight_stop_(weight_stop),
node_num_stop_(node_num_stop),
add_noise_(add_noise),
seed_(seed),
sigma_(sigma) {}
std::vector<std::uint64_t> optimize(Objective &objective) const override {
auto labels = greedy_additive(
objective.lifted_graph(),
objective.weights(),
objective.number_of_base_edges(),
weight_stop_,
node_num_stop_,
add_noise_,
seed_,
sigma_
);
objective.set_labels(labels);
return labels;
}
private:
double weight_stop_;
double node_num_stop_;
bool add_noise_;
int seed_;
double sigma_;
};
} // namespace bioimage_cpp::graph::lifted_multicut