|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "bioimage_cpp/graph/detail/fusion_contract.hxx" |
| 4 | +#include "bioimage_cpp/graph/multicut/greedy_additive.hxx" |
| 5 | +#include "bioimage_cpp/graph/multicut/objective.hxx" |
| 6 | +#include "bioimage_cpp/graph/proposal_generator.hxx" |
| 7 | +#include "bioimage_cpp/graph/undirected_graph.hxx" |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <cstddef> |
| 11 | +#include <cstdint> |
| 12 | +#include <memory> |
| 13 | +#include <stdexcept> |
| 14 | +#include <utility> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +namespace bioimage_cpp::graph::multicut { |
| 18 | + |
| 19 | +class FusionMoveSolver final : public SolverBase { |
| 20 | +public: |
| 21 | + // The proposal generator is borrowed; the caller owns its lifetime. |
| 22 | + // The sub-solver pointer is optional: when null, the driver uses a default |
| 23 | + // greedy-additive sub-solver internally. |
| 24 | + // |
| 25 | + // `number_of_threads` and `number_of_parallel_proposals` are reserved for |
| 26 | + // future use; v1 only supports the single-threaded pairwise |
| 27 | + // (proposal, current) fuse and rejects other values. |
| 28 | + FusionMoveSolver( |
| 29 | + ProposalGeneratorBase &proposal_generator, |
| 30 | + const SolverBase *sub_solver = nullptr, |
| 31 | + const std::size_t number_of_iterations = 10, |
| 32 | + const std::size_t stop_if_no_improvement = 4, |
| 33 | + const std::size_t number_of_threads = 1, |
| 34 | + const std::size_t number_of_parallel_proposals = 2 |
| 35 | + ) |
| 36 | + : proposal_generator_(proposal_generator), |
| 37 | + sub_solver_(sub_solver), |
| 38 | + number_of_iterations_(number_of_iterations), |
| 39 | + stop_if_no_improvement_(stop_if_no_improvement) { |
| 40 | + if (number_of_threads != 1) { |
| 41 | + throw std::invalid_argument( |
| 42 | + "FusionMoveSolver currently supports number_of_threads=1 only" |
| 43 | + ); |
| 44 | + } |
| 45 | + if (number_of_parallel_proposals != 2) { |
| 46 | + throw std::invalid_argument( |
| 47 | + "FusionMoveSolver currently supports number_of_parallel_proposals=2 only" |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + std::vector<std::uint64_t> optimize(Objective &objective) const override { |
| 53 | + const auto &graph = objective.graph(); |
| 54 | + const auto &costs = objective.costs(); |
| 55 | + const auto number_of_nodes = graph.number_of_nodes(); |
| 56 | + |
| 57 | + std::vector<std::uint64_t> current = objective.labels(); |
| 58 | + if (number_of_nodes == 0 || graph.number_of_edges() == 0) { |
| 59 | + objective.set_labels(current); |
| 60 | + return objective.labels(); |
| 61 | + } |
| 62 | + |
| 63 | + const auto default_sub_solver = GreedyAdditiveSolver(); |
| 64 | + const auto &sub_solver = sub_solver_ != nullptr |
| 65 | + ? *sub_solver_ |
| 66 | + : static_cast<const SolverBase &>(default_sub_solver); |
| 67 | + |
| 68 | + // Warm start from greedy-additive if the caller passed the trivial |
| 69 | + // singleton labeling. |
| 70 | + if (is_singleton_labeling(current)) { |
| 71 | + Objective warm_objective(graph, costs); |
| 72 | + current = default_sub_solver.optimize(warm_objective); |
| 73 | + } |
| 74 | + |
| 75 | + double current_energy = energy(graph, costs, current); |
| 76 | + |
| 77 | + std::vector<std::uint64_t> proposal(static_cast<std::size_t>(number_of_nodes)); |
| 78 | + std::size_t iterations_without_improvement = 0; |
| 79 | + |
| 80 | + for (std::size_t iteration = 0; iteration < number_of_iterations_; ++iteration) { |
| 81 | + proposal_generator_.generate(current, proposal); |
| 82 | + |
| 83 | + const auto fused = fuse_pair(graph, costs, current, proposal, sub_solver); |
| 84 | + const auto fused_energy = energy(graph, costs, fused); |
| 85 | + const auto proposal_energy = energy(graph, costs, proposal); |
| 86 | + |
| 87 | + // Best-of safety net across the three candidates so an iteration |
| 88 | + // can never raise the running energy. |
| 89 | + double best_energy = current_energy; |
| 90 | + const std::vector<std::uint64_t> *best = ¤t; |
| 91 | + if (fused_energy < best_energy) { |
| 92 | + best_energy = fused_energy; |
| 93 | + best = &fused; |
| 94 | + } |
| 95 | + if (proposal_energy < best_energy) { |
| 96 | + best_energy = proposal_energy; |
| 97 | + best = &proposal; |
| 98 | + } |
| 99 | + |
| 100 | + if (best_energy < current_energy) { |
| 101 | + current = *best; |
| 102 | + current_energy = best_energy; |
| 103 | + iterations_without_improvement = 0; |
| 104 | + } else { |
| 105 | + ++iterations_without_improvement; |
| 106 | + if (iterations_without_improvement >= stop_if_no_improvement_) { |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + objective.set_labels(current); |
| 113 | + return objective.labels(); |
| 114 | + } |
| 115 | + |
| 116 | +private: |
| 117 | + static bool is_singleton_labeling(const std::vector<std::uint64_t> &labels) { |
| 118 | + for (std::size_t index = 0; index < labels.size(); ++index) { |
| 119 | + if (labels[index] != static_cast<std::uint64_t>(index)) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + static std::vector<std::uint64_t> fuse_pair( |
| 127 | + const UndirectedGraph &graph, |
| 128 | + const std::vector<double> &costs, |
| 129 | + const std::vector<std::uint64_t> ¤t, |
| 130 | + const std::vector<std::uint64_t> &proposal, |
| 131 | + const SolverBase &sub_solver |
| 132 | + ) { |
| 133 | + const auto number_of_nodes = static_cast<std::size_t>(graph.number_of_nodes()); |
| 134 | + std::vector<std::uint64_t> stacked(2 * number_of_nodes); |
| 135 | + std::copy(current.begin(), current.end(), stacked.begin()); |
| 136 | + std::copy(proposal.begin(), proposal.end(), stacked.begin() + number_of_nodes); |
| 137 | + |
| 138 | + auto contraction = ::bioimage_cpp::graph::detail::contract_by_agreement( |
| 139 | + graph, stacked.data(), 2, number_of_nodes |
| 140 | + ); |
| 141 | + |
| 142 | + const auto &contracted_graph = contraction.contracted_graph; |
| 143 | + const auto number_of_contracted_edges = contracted_graph.number_of_edges(); |
| 144 | + |
| 145 | + std::vector<double> contracted_costs( |
| 146 | + static_cast<std::size_t>(number_of_contracted_edges), 0.0 |
| 147 | + ); |
| 148 | + for (std::uint64_t edge = 0; edge < graph.number_of_edges(); ++edge) { |
| 149 | + const auto target = contraction.contracted_edge_of_original[ |
| 150 | + static_cast<std::size_t>(edge) |
| 151 | + ]; |
| 152 | + if (target < 0) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + contracted_costs[static_cast<std::size_t>(target)] += |
| 156 | + costs[static_cast<std::size_t>(edge)]; |
| 157 | + } |
| 158 | + |
| 159 | + if (number_of_contracted_edges == 0) { |
| 160 | + std::vector<std::uint64_t> result(number_of_nodes); |
| 161 | + for (std::uint64_t node = 0; node < graph.number_of_nodes(); ++node) { |
| 162 | + result[static_cast<std::size_t>(node)] = contraction.root_of_node[ |
| 163 | + static_cast<std::size_t>(node) |
| 164 | + ]; |
| 165 | + } |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | + Objective sub_objective(contracted_graph, std::move(contracted_costs)); |
| 170 | + const auto sub_labels = sub_solver.optimize(sub_objective); |
| 171 | + |
| 172 | + std::vector<std::uint64_t> result(number_of_nodes); |
| 173 | + for (std::uint64_t node = 0; node < graph.number_of_nodes(); ++node) { |
| 174 | + const auto root = contraction.root_of_node[static_cast<std::size_t>(node)]; |
| 175 | + result[static_cast<std::size_t>(node)] = sub_labels[ |
| 176 | + static_cast<std::size_t>(root) |
| 177 | + ]; |
| 178 | + } |
| 179 | + return result; |
| 180 | + } |
| 181 | + |
| 182 | + ProposalGeneratorBase &proposal_generator_; |
| 183 | + const SolverBase *sub_solver_; |
| 184 | + std::size_t number_of_iterations_; |
| 185 | + std::size_t stop_if_no_improvement_; |
| 186 | +}; |
| 187 | + |
| 188 | +} // namespace bioimage_cpp::graph::multicut |
0 commit comments