Skip to content
Snippets Groups Projects
Commit 7d283333 authored by p-hamann's avatar p-hamann
Browse files

Adjust sources and sinks dynamically in bnb

parent 8ca8c755
Branches
1 merge request!9Perf optimization
#include <algorithm>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <time.h> #include <time.h>
...@@ -29,36 +30,27 @@ solver::solver(graph& g, lb lb_algorithm) : graph_(g), partition_(partition{g}), ...@@ -29,36 +30,27 @@ solver::solver(graph& g, lb lb_algorithm) : graph_(g), partition_(partition{g}),
} }
unsigned int solver::get_lower(){ unsigned int solver::get_lower(){
std::vector<node_id> sources, sinks; if (current_sources_.empty() || current_sinks_.empty()) {
for(node_id node = 1; node <= graph_.num_nodes(); node++){
if(partition_.assigned_subgraph_of(node) == partition::sg_a)
sources.push_back(node);
else if(partition_.assigned_subgraph_of(node) == partition::sg_b)
sinks.push_back(node);
}
if (sources.empty() || sinks.empty()) {
return partition_.current_objective(); return partition_.current_objective();
} }
if(lb_algorithm_ == lb::ek){ if(lb_algorithm_ == lb::ek){
ek_.reset(sources, sinks); ek_.reset(current_sources_, current_sinks_);
ek_.run(); ek_.run();
return ek_.get_max_flow(); return ek_.get_max_flow();
} }
else if(lb_algorithm_ == lb::ibfs){ else if(lb_algorithm_ == lb::ibfs){
i_bfs_.reset(sources, sinks); i_bfs_.reset(current_sources_, current_sinks_);
i_bfs_.run(); i_bfs_.run();
return i_bfs_.get_max_flow(); return i_bfs_.get_max_flow();
} }
else if(lb_algorithm_ == lb::pr){ else if(lb_algorithm_ == lb::pr){
pr_.reset(sources, sinks); pr_.reset(current_sources_, current_sinks_);
pr_.run(); pr_.run();
return pr_.get_max_flow(); return pr_.get_max_flow();
} }
else if(lb_algorithm_ == lb::gp){ else if(lb_algorithm_ == lb::gp){
gp_.reset(sources, sinks); gp_.reset(current_sources_, current_sinks_);
gp_.run(); gp_.run();
return gp_.get_max_flow(); return gp_.get_max_flow();
} }
...@@ -70,6 +62,17 @@ void solver::solve() { ...@@ -70,6 +62,17 @@ void solver::solve() {
best_objective_ = graph_.num_nodes(); best_objective_ = graph_.num_nodes();
clock_t begin = clock(); clock_t begin = clock();
/*
// builds a vector of node ids with descending degree
std::vector<node_id> sorted_nodes;
for (node_id i = 1; i <= graph_.num_nodes(); ++i) {
sorted_nodes.push_back(i);
}
std::sort(sorted_nodes.begin(), sorted_nodes.end(), [this](node_id a, node_id b) {
return graph_.get_adjacency(a).size() > graph_.get_adjacency(b).size();
});
*/
while(true) { while(true) {
//falls current sol schlechter als bisher beste lösung: //falls current sol schlechter als bisher beste lösung:
//zähle schritte bis zur nächsten alternative und //zähle schritte bis zur nächsten alternative und
...@@ -125,6 +128,7 @@ void solver::solve() { ...@@ -125,6 +128,7 @@ void solver::solve() {
next_node = trail_.length() +1; next_node = trail_.length() +1;
//next_node = sorted_nodes[trail_.length()];
expand(next_node, next_possible_subgraph(next_node, subgraph::sg_a)); expand(next_node, next_possible_subgraph(next_node, subgraph::sg_a));
} }
...@@ -149,6 +153,8 @@ void solver::expand(node_id node, subgraph sg) { ...@@ -149,6 +153,8 @@ void solver::expand(node_id node, subgraph sg) {
partition_.assign_node(node, sg); partition_.assign_node(node, sg);
trail_.push_node(node, alt); trail_.push_node(node, alt);
if (sg == partition::sg_a) current_sources_.push_back(node);
else current_sinks_.push_back(node);
} }
void solver::backtrack() { void solver::backtrack() {
...@@ -160,6 +166,8 @@ void solver::backtrack() { ...@@ -160,6 +166,8 @@ void solver::backtrack() {
trail_.pop(); trail_.pop();
partition_.unassign_node(node); partition_.unassign_node(node);
if (current_sources_.back() == node) current_sources_.erase(current_sources_.begin() + current_sources_.size() - 1);
else if (current_sinks_.back() == node) current_sinks_.erase(current_sinks_.begin() + current_sinks_.size() - 1);
} }
// Finds the next partition in which the given node can be placed. // Finds the next partition in which the given node can be placed.
......
...@@ -93,6 +93,8 @@ private: ...@@ -93,6 +93,8 @@ private:
edmonds_karp ek_; edmonds_karp ek_;
push_relabel pr_; push_relabel pr_;
greedy_packing gp_; greedy_packing gp_;
std::vector<node_id> current_sources_;
std::vector<node_id> current_sinks_;
// Value of best solution seen so far. // Value of best solution seen so far.
std::vector<subgraph> best_solution_; std::vector<subgraph> best_solution_;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment