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

Avoid multiple generations of greedy packing

parent 5fb51690
Branches
1 merge request!9Perf optimization
...@@ -25,7 +25,7 @@ void trail_state::pop() { ...@@ -25,7 +25,7 @@ void trail_state::pop() {
// solver implementation. // solver implementation.
// -------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------
solver::solver(graph& g, lb lb_algorithm) : graph_(g), partition_(partition{g}), lb_algorithm_(lb_algorithm), i_bfs_(incremental_bfs(g)), ek_(edmonds_karp(g)), pr_(push_relabel(g)) { solver::solver(graph& g, lb lb_algorithm) : graph_(g), partition_(partition{g}), lb_algorithm_(lb_algorithm), i_bfs_(incremental_bfs(g)), ek_(edmonds_karp(g)), pr_(push_relabel(g)), gp_(greedy_packing(g, i_bfs_, true)) {
} }
unsigned int solver::get_lower(){ unsigned int solver::get_lower(){
...@@ -58,12 +58,9 @@ unsigned int solver::get_lower(){ ...@@ -58,12 +58,9 @@ unsigned int solver::get_lower(){
return pr_.get_max_flow(); return pr_.get_max_flow();
} }
else if(lb_algorithm_ == lb::gp){ else if(lb_algorithm_ == lb::gp){
bool with_flow = true; gp_.reset(sources, sinks);
auto g_p = greedy_packing(graph_, sources, sinks, i_bfs_, with_flow); gp_.run();
g_p.run(); return gp_.get_max_flow();
if(with_flow)
return g_p.get_max_flow();
return g_p.get_max_flow() + partition_.current_objective();
} }
return partition_.current_objective(); return partition_.current_objective();
} }
......
...@@ -4,27 +4,37 @@ ...@@ -4,27 +4,37 @@
bool do_swap = true; bool do_swap = true;
greedy_packing::greedy_packing(graph& g, std::vector<node_id>& a, std::vector<node_id>& b, incremental_bfs& ibfs, bool with_flow) greedy_packing::greedy_packing(graph& g, incremental_bfs& ibfs, bool with_flow)
: graph_(g), a_(a), b_(b), i_bfs(ibfs), with_flow_(with_flow){ : graph_(g), i_bfs(ibfs), with_flow_(with_flow){
if(do_swap && a.size() > b.size()){
std::swap(a_, b_);
}
visited.resize(graph_.num_nodes() +1, false);
max_a = (graph_.num_nodes()+1)/2; max_a = (graph_.num_nodes()+1)/2;
}; };
void greedy_packing::reset(std::vector<node_id>& a, std::vector<node_id>& b) {
a_ = &a;
b_ = &b;
x_.clear();
flow_edges.clear();
partitioning.clear();
a_count = 0;
visited.assign(graph_.num_nodes() +1, false);
if(do_swap && a.size() > b.size()){
std::swap(a_, b_);
}
}
//breadth-first-search to determine neighbors of B //breadth-first-search to determine neighbors of B
void greedy_packing::bfs(){ void greedy_packing::bfs(){
for(node_id node : b_){ for (unsigned int i = 0; i < b_->size(); ++i) {
node_id node = b_->operator[](i);
q.push(node); q.push(node);
visited[node] = true; visited[node] = true;
} }
for(node_id node : a_){ for (unsigned int i = 0; i < a_->size(); ++i) {
visited[node] = true; visited[a_->operator[](i)] = true;
} }
while(!q.empty()){ while(!q.empty()){
...@@ -51,13 +61,13 @@ void greedy_packing::run(){ ...@@ -51,13 +61,13 @@ void greedy_packing::run(){
if(with_flow_){ if(with_flow_){
//get all flow edges and the flow //get all flow edges and the flow
i_bfs.reset(a_, b_); i_bfs.reset(*a_, *b_);
i_bfs.run(); i_bfs.run();
flow_ = i_bfs.get_max_flow(); flow_ = i_bfs.get_max_flow();
flow_edges = i_bfs.get_flow_edges(); flow_edges = i_bfs.get_flow_edges();
} }
a_count = a_.size(); a_count = a_->size();
if(a_count >= max_a) if(a_count >= max_a)
return; return;
...@@ -91,8 +101,8 @@ void greedy_packing::run(){ ...@@ -91,8 +101,8 @@ void greedy_packing::run(){
//danach lokale suche um balance zu verbessern ? //danach lokale suche um balance zu verbessern ?
std::sort(partitioning.begin(), partitioning.end(), [](std::vector<node_id> a_, std::vector<node_id> b_) { std::sort(partitioning.begin(), partitioning.end(), [](std::vector<node_id> x, std::vector<node_id> y) {
return a_.size() > b_.size(); return x.size() > y.size();
}); });
// behandle von B aus unnerreichbare Knoten // behandle von B aus unnerreichbare Knoten
......
...@@ -92,6 +92,7 @@ private: ...@@ -92,6 +92,7 @@ private:
incremental_bfs i_bfs_; incremental_bfs i_bfs_;
edmonds_karp ek_; edmonds_karp ek_;
push_relabel pr_; push_relabel pr_;
greedy_packing gp_;
// Value of best solution seen so far. // Value of best solution seen so far.
std::vector<subgraph> best_solution_; std::vector<subgraph> best_solution_;
......
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
class greedy_packing{ class greedy_packing{
public: public:
greedy_packing(graph& g, std::vector<node_id>& a, std::vector<node_id>& b, incremental_bfs& ibfs, bool with_flow); greedy_packing(graph& g, incremental_bfs& ibfs, bool with_flow);
void reset(std::vector<node_id>& a, std::vector<node_id>& b);
void run(); void run();
...@@ -26,7 +28,9 @@ private: ...@@ -26,7 +28,9 @@ private:
//partial partition into blocks a and b //partial partition into blocks a and b
//x is block with the neighbors of B //x is block with the neighbors of B
std::vector<node_id>& a_, b_, x_; std::vector<node_id>* a_;
std::vector<node_id>* b_;
std::vector<node_id> x_;
incremental_bfs& i_bfs; incremental_bfs& i_bfs;
......
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