diff --git a/Graphtheory/Basic_Graph.h b/Graphtheory/Basic_Graph.h index 4e4afc46ff1cbe015e9eb633c83e6035b0f1c280..fa647fd93432bda7ed1efa34eb3d32ec140355ad 100644 --- a/Graphtheory/Basic_Graph.h +++ b/Graphtheory/Basic_Graph.h @@ -66,7 +66,7 @@ public: // _advanced.cpp: Graph(std::istream& is); - void conditional_bfs_all_components(std::function* from, Edge* via, bool used_in_traversal)> edge_exec, std::function*)> node_exec,std::function* from, Edge* via)> guide = [](Node* n, Edge* e)->bool {return e->from() == n;}); + void conditional_bfs_all_components(std::function* from, Edge* via, bool used_in_traversal)> edge_exec, std::function* via, Node* node)> node_exec, std::deque*> starting_nodes = {}, bool all_paths = false, std::function* from, Edge* via)> guide = [](Node* n, Edge* e)->bool {return e->from() == n;}); // _special_members_and_operators: void operator=(Graph& graph); @@ -77,7 +77,7 @@ template std::ostream& operator<<(std::ostream& os, Graph& g); template -std::istream& operator>>(std::istream& is, Graph& g){g = Graph(is); return is;} +std::istream& operator>>(std::istream& is, Graph& g){g = Graph(is); return is;} #include "Basic_Graph.ipp" #include "Basic_Graph_advanced.ipp" diff --git a/Graphtheory/Basic_Graph_advanced.ipp b/Graphtheory/Basic_Graph_advanced.ipp index f58adf8d8c06e5fdb766a1e1e9d81c07f4f7ae02..318088e6f7da3c749d514cbd68c54ced9d143735 100644 --- a/Graphtheory/Basic_Graph_advanced.ipp +++ b/Graphtheory/Basic_Graph_advanced.ipp @@ -30,7 +30,7 @@ Graph::Graph(std::istream& is){ is >> curr; while(curr == "Edge"){ is.seekg(pos); - Edge* tmp = new Edge(is, name_lookup); + Edge* tmp = new Edge(is, name_lookup); this->_edges.insert(tmp); pos = is.tellg(); is >> curr; @@ -38,29 +38,44 @@ Graph::Graph(std::istream& is){ } template -void Graph::conditional_bfs_all_components(std::function* from, Edge* via, bool used_in_traversal)> edge_exec, std::function*)> node_exec,std::function* from, Edge* via)> guide){ +void Graph::conditional_bfs_all_components( + std::function* from, Edge* via, bool used_in_traversal)> edge_exec, + std::function* via, Node* node)> node_exec, + std::deque*> starting_nodes, + bool all_paths, + std::function* from, Edge* via)> guide){ /* executes edge_exec and node_exec for every edge or node in visiting order. Also executes for unused edges that test positive w.r.t guide !! possibly executes twice if guide allows !! + node_exec will be called with nullptr for via for start node + node_exec might be called with nullptr for via if the graph is not strongly connected + + node_exec need to return true in order to continue traversal over its incident edges */ std::set*> uncharted = this->nodes(); - std::queue*> active; + std::deque*> active = starting_nodes; while(!uncharted.empty()){ - active.push(*uncharted.begin()); - uncharted.erase(*uncharted.begin()); + if(active.empty()){ + active.push_back(*uncharted.begin()); + uncharted.erase(*uncharted.begin()); + } + for(Node* manually_inserted_node : active){ + node_exec(nullptr, manually_inserted_node); + } while(!active.empty()){ - Node* n = active.front(); active.pop(); - node_exec(n); + Node* n = active.front(); active.pop_front(); for(Edge* e : n->incident()){ bool used_in_traversal = false; if(guide(n, e)){ - if(uncharted.find(e->to(n)) != uncharted.end()){ + if(uncharted.find(e->to(n)) != uncharted.end() || all_paths){ used_in_traversal = true; - active.push(e->to(n)); + if(node_exec(e, e->to(n))){ + active.push_back(e->to(n)); + } uncharted.erase(e->to(n)); } diff --git a/Linear_Programming/lp_generator.ipp b/Linear_Programming/lp_generator.ipp index 3e43445a3f18380832f0fe290cfc885b43ef805c..645d5e18ee53c698786928c7b5882c7a92f3e40e 100644 --- a/Linear_Programming/lp_generator.ipp +++ b/Linear_Programming/lp_generator.ipp @@ -37,12 +37,13 @@ std::pair*,N>, std::pair>, std:: edge_lookup.insert({{via, edge_prop.first}, p.add_variable(Variable(name.str(), std::get<0>(edge_prop.second), std::get<1>(edge_prop.second), std::get<2>(edge_prop.second)))}); } }; - std::function*)> node_function = [&p, &node_lookup, &node_var_data, start_index, name_appendix](Node* n) -> void { + std::function*, Node*)> node_function = [&p, &node_lookup, &node_var_data, start_index, name_appendix](Edge* via, Node* n) -> bool { for(std::pair, std::pair>> node_prop : node_var_data){ std::stringstream name; name << "node_" << n->description() << "_" << static_cast(node_prop.first) << "_" << name_appendix; node_lookup.insert({{n,node_prop.first}, p.add_variable(Variable(name.str(), std::get<0>(node_prop.second), std::get<1>(node_prop.second), std::get<2>(node_prop.second)))}); } + return true; }; g.conditional_bfs_all_components( diff --git a/maintenance_problem_test.cpp b/maintenance_problem_test.cpp index a7ef6790eb8ef92afdc1e9909be0c22650e0268f..914bffd0a942e3dca5bf30189db67815dbd39d1f 100644 --- a/maintenance_problem_test.cpp +++ b/maintenance_problem_test.cpp @@ -20,12 +20,14 @@ int main(){ ); while(true){ + system("killall graph_display"); Maintenance_Problem mp; mpg >> mp; std::ofstream ofs_graph("./.data/Maintenance_Problem/mp.netw"); ofs_graph << mp.network() << std::endl; ofs_graph.close(); + system("cd ../display/graph_display/ && (./graph_display --file ../../discrete_optimization_library/.data/Maintenance_Problem/mp.netw &) && cd ../../discrete_optimization_library"); std::ofstream ofs_program("./.data/Maintenance_Problem/mp.lp"); ofs_program << mp << std::endl;