27 #ifndef SRC_METHODS_BP_BEAM_IPP_ 28 #define SRC_METHODS_BP_BEAM_IPP_ 33 template<
class UserNodeLabel,
class UserEdgeLabel>
34 BPBeam<UserNodeLabel, UserEdgeLabel>::
37 template<
class UserNodeLabel,
class UserEdgeLabel>
38 BPBeam<UserNodeLabel, UserEdgeLabel>::
39 BPBeam(
const GEDData<UserNodeLabel, UserEdgeLabel> & ged_data) :
40 LSBasedMethod<UserNodeLabel, UserEdgeLabel>(ged_data),
45 template<
class UserNodeLabel,
class UserEdgeLabel>
51 std::vector<NodeMap::Assignment> assignments;
56 output_node_map = initial_node_map;
59 for (std::size_t iteration{0}; iteration < num_orderings_; iteration++) {
62 shuffle_assignments_(assignments);
65 std::vector<TreeNode_> open;
66 open.emplace_back(initial_node_map, assignments);
69 while (not open.empty()) {
72 TreeNode_ top(open.at(0));
73 open.erase(open.begin());
74 if (top.node_map().induced_cost() < output_node_map.
induced_cost()) {
75 output_node_map = top.node_map();
79 if (top.depth() == assignments.size() - 1) {
84 for (std::size_t pos_swapped_assignment{top.depth()}; pos_swapped_assignment < assignments.size(); pos_swapped_assignment++) {
85 open.emplace_back(top, this->
ged_data_, g, h, pos_swapped_assignment);
89 sort_and_shrink_to_beam_size_(open);
94 template<
class UserNodeLabel,
class UserEdgeLabel>
102 template<
class UserNodeLabel,
class UserEdgeLabel>
106 bool return_value{
false};
107 std::string ordering_options(
"");
108 if (option ==
"beam-size") {
110 beam_size_ = std::stoul(arg);
113 throw Error(std::string(
"Invalid argument \"") + arg +
"\" for option beam-size. Usage: options = \"[--beam-size <convertible to int greater 0>]\"");
115 if (beam_size_ <= 0) {
116 throw Error(std::string(
"Invalid argument \"") + arg +
"\" for option beam-size. Usage: options = \"[--beam-size <convertible to int greater 0>]\"");
120 else if (option ==
"num-orderings") {
122 num_orderings_ = std::stoul(arg);
125 throw Error(std::string(
"Invalid argument \"") + arg +
"\" for option num-random-orderings. Usage: options = \"[--num-orderings <convertible to int greater 0>]\"");
127 if (num_orderings_ < 1) {
128 throw Error(std::string(
"Invalid argument \"") + arg +
"\" for option num-random-orderings. Usage: options = \"[--num-orderings <convertible to int greater 0>]\"");
135 template<
class UserNodeLabel,
class UserEdgeLabel>
139 return "[--beam-size <arg>] [--num-orderings <arg>]";
143 template<
class UserNodeLabel,
class UserEdgeLabel>
147 std::sort(open.begin(), open.end());
148 while (open.size() > beam_size_) {
153 template<
class UserNodeLabel,
class UserEdgeLabel>
157 if (num_orderings_ > 1) {
158 std::random_device rng;
159 std::mt19937 urng(rng());
160 std::shuffle(assignments.begin(), assignments.end(), urng);
165 template<
class UserNodeLabel,
class UserEdgeLabel>
169 node_map_(tree_node.node_map_),
170 assignments_(tree_node.assignments_),
171 depth_{tree_node.depth_} {}
173 template<
class UserNodeLabel,
class UserEdgeLabel>
176 TreeNode_(
const NodeMap & node_map,
const std::vector<NodeMap::Assignment> & assignments) :
178 assignments_(assignments),
181 template<
class UserNodeLabel,
class UserEdgeLabel>
185 node_map_(tree_node.node_map_),
186 assignments_(tree_node.assignments_),
187 depth_{tree_node.depth_ + 1} {
188 NodeMap::Assignment assignment_1(assignments_.at(depth_ - 1));
189 NodeMap::Assignment assignment_2(assignments_.at(pos_swapped_assignment));
190 double swap_cost{ged_data.
swap_cost(g, h, assignment_1, assignment_2, node_map_)};
191 ged_data.
swap_assignments(assignment_1, assignment_2, swap_cost, node_map_);
192 assignments_[depth_ - 1].second = assignment_2.second;
193 assignments_[pos_swapped_assignment].second = assignment_1.second;
196 template<
class UserNodeLabel,
class UserEdgeLabel>
204 template<
class UserNodeLabel,
class UserEdgeLabel>
212 template<
class UserNodeLabel,
class UserEdgeLabel>
216 operator<(
const TreeNode_ & tree_node)
const {
217 return (node_map_ < tree_node.node_map_);
Contains the standardized input data along with basic functionality.
Computes an upper bound for general edit costs.
void as_relation(std::vector< Assignment > &relation) const
Constructs the representation as relation.
virtual void ls_set_default_options_() final
Sets all options that are not among the ones shared by all derived classes of ged::LSBasedMethod to d...
const GEDData< UserNodeLabel, UserEdgeLabel > & ged_data_
The data on which the method is run.
void swap_assignments(const NodeMap::Assignment &assignment_1, const NodeMap::Assignment &assignment_2, double swap_cost, NodeMap &node_map) const
Swaps two assignments in a node map.
double induced_cost() const
Returns the induced cost.
virtual void ls_run_from_initial_solution_(const GEDGraph &g, const GEDGraph &h, double lower_bound, const NodeMap &initial_node_map, NodeMap &output_node_map) final
Runs the local search from an initial node map.
static NodeID dummy_node()
Returns a dummy node.
double swap_cost(const GEDGraph &g, const GEDGraph &h, const NodeMap::Assignment &assignment_1, const NodeMap::Assignment &assignment_2, NodeMap &node_map) const
Computes the cost of swapping two assignments in a node map while leaving the node map unchanged...
virtual std::string ls_valid_options_string_() const final
Returns string of all valid options that are not among the ones shared by all derived classes of ged:...
The normalized input graphs used by GEDLIB. All labels are integers.
virtual bool ls_parse_option_(const std::string &option, const std::string &arg) final
Parses one option that is not among the ones shared by all derived classes of ged::LSBasedMethod.
Global namespace for GEDLIB.