Commit f0387bae authored by Jonas Seidel's avatar Jonas Seidel

scanning graphs from file

parent f460d26b
......@@ -7,3 +7,4 @@ linear_program_test
maintenance_problem_test
.test
*.lp
*.netw
......@@ -65,6 +65,7 @@ public:
}
// _advanced.cpp:
Graph(std::istream& is);
void conditional_bfs_all_components(std::function<void(Node<N,E>* from, Edge<N,E>* via, bool used_in_traversal)> edge_exec, std::function<void(Node<N,E>*)> node_exec,std::function<bool(Node<N,E>* from, Edge<N,E>* via)> guide = [](Node<N,E>* n, Edge<N,E>* e)->bool {return e->from() == n;});
// _special_members_and_operators:
......@@ -75,6 +76,9 @@ public:
template <typename N, typename E>
std::ostream& operator<<(std::ostream& os, Graph<N,E>& g);
template <typename N, typename E>
std::istream& operator>>(std::istream& is, Graph<N,E>& g){g = Graph(is); return is;}
#include "Basic_Graph.ipp"
#include "Basic_Graph_advanced.ipp"
#include "Basic_Graph_special_members_and_operators.ipp"
......
......@@ -75,18 +75,30 @@ void Graph<N, E>::reset_attribute_values(std::set<E> edge_attr, std::set<N> node
template <typename N, typename E>
std::ostream& operator<<(std::ostream& os, Graph<N,E>& g){
os << "\033[0;31m";
os << "Graph{ Nodes: { \n";
for(Node<N, E>* n : g.nodes()){
os << *n << "\n";
}
os << "\033[0;31m";
os << "} Edges: {" << "\n";
for(Edge<N, E>* e : g.edges()){
os << *e << "\n";
if(&os == &std::cout){
os << "\033[0;31m";
os << "Graph { Nodes { \n";
for(Node<N, E>* n : g.nodes()){
os << *n << "\n";
}
os << "\033[0;31m";
os << "} Edges {" << "\n";
for(Edge<N, E>* e : g.edges()){
os << *e << "\n";
}
os << "\033[0;31m";
os << "} }";
os << "\033[0m";
}else{
os << "Graph { Nodes { \n";
for(Node<N, E>* n : g.nodes()){
os << *n << "\n";
}
os << "} Edges {" << "\n";
for(Edge<N, E>* e : g.edges()){
os << *e << "\n";
}
os << "} }";
}
os << "\033[0;31m";
os << "} }";
os << "\033[0m";
return os;
}
template <typename N, typename E>
Graph<N,E>::Graph(std::istream& is){
std::map<std::string, Node<N,E>* > name_lookup;
std::string curr;
is >> curr;
assert(curr == "Graph");
is >> curr;
assert(curr == "{");
is >> curr;
assert(curr == "Nodes");
is >> curr;
assert(curr == "{");
auto pos = is.tellg();
is >> curr;
while(curr == "Node"){
is.seekg(pos);
Node<N,E>* tmp = new Node<N,E>(is);
name_lookup.insert({tmp->description(), tmp});
this->_nodes.insert(tmp);
pos = is.tellg();
is >> curr;
}
assert(curr == "}");
is >> curr;
assert(curr == "Edges");
is >> curr;
assert(curr == "{");
pos = is.tellg();
is >> curr;
while(curr == "Edge"){
is.seekg(pos);
Edge<N,E>* tmp = new Edge(is, name_lookup);
this->_edges.insert(tmp);
pos = is.tellg();
is >> curr;
}
}
template <typename N, typename E>
void Graph<N,E>::conditional_bfs_all_components(std::function<void(Node<N,E>* from, Edge<N,E>* via, bool used_in_traversal)> edge_exec, std::function<void(Node<N,E>*)> node_exec,std::function<bool(Node<N,E>* from, Edge<N,E>* via)> guide){
/*
......
......@@ -8,6 +8,7 @@
#include <stdexcept>
#include <string>
#include <sstream>
#include <cassert>
#include "Attribute.h"
......@@ -42,6 +43,8 @@ public:
to->add_incident(this);
};
Edge(std::istream& is, std::map<std::string, Node<N,E>* >& name_lookup);
std::string description(){
if(this->_description == ""){
std::stringstream name;
......
template <typename N, typename E>
Edge<N,E>::Edge(std::istream& is, std::map<std::string, Node<N,E>* >& name_lookup){
std::string curr;
is >> curr;
assert(curr == "Edge");
is >> this->_description;
is >> curr;
assert(curr == "(");
is >> curr;
while(curr != ")"){
assert(curr.length() == 1);
E attribute = static_cast<E>(curr.c_str()[0]);
is >> curr;
assert(curr == "=");
is >> curr;
this->_attributes.insert({attribute, {fix, std::stod(curr), Continuous}});
is >> curr;
}
is >> curr;
assert(curr == "{");
is >> curr;
this->_from = name_lookup.find(curr)->second;
this->_from->add_incident(this);
is >> curr;
this->_to = name_lookup.find(curr)->second;
this->_to->add_incident(this);
is >> curr;
assert(curr == "}");
}
template <typename N, typename E>
std::ostream& operator<<(std::ostream& os, Edge<N, E>& e){
os << "\033[0;32m";
os << "Edge " << e.description() << " (";
for(std::pair<E, Attribute> pair : e.attributes()){
os << pair.second.value() << ", ";
if(&os == &std::cout){
os << "\033[0;32m";
os << "Edge " << e.description() << " ( ";
for(std::pair<E, Attribute> pair : e.attributes()){
os << static_cast<char>(pair.first) << " = " << pair.second.value() << ", ";
}
os << ") {" << e.from()->description() << ", " << e.to()->description() << "}";
os << "\033[0m";
}else{
os << "Edge " << e.description() << " ( ";
for(std::pair<E, Attribute> pair : e.attributes()){
os << static_cast<char>(pair.first) << " = " << pair.second.value() << " ";
}
os << " ) { " << e.from()->description() << " " << e.to()->description() << " }";
}
os << ") {" << e.from()->description() << ", " << e.to()->description() << "}";
os << "\033[0m";
return os;
}
......@@ -25,6 +25,7 @@ public:
}
Node(Node& node) : _description(node.description()), _incident({}), _attributes(node.attributes()) {}
Node(Node&& node) : _description(std::move(node._description)), _incident(std::move(node._incident)), _attributes(std::move(node._attributes)) {}
Node(std::istream& is);
std::string description(){
if(this->_description == ""){
......@@ -62,6 +63,8 @@ public:
template <typename N, typename E>
std::ostream& operator<<(std::ostream& os, Node<N, E>& n);
template <typename N, typename E>
std::istream& operator>>(std::istream& is, Node<N,E>& n) { n = Node<N,E>(is); return is;}
#include "Node.ipp"
......
template <typename N, typename E>
std::ostream& operator<<(std::ostream& os, Node<N, E>& n){
os << "\033[0;36m";
os << "Node " << n.description() << " { (";
for(std::pair<N, Attribute> pair : n.attributes()){
os << pair.second.value() << ", ";
Node<N,E>::Node(std::istream& is){
std::string curr;
is >> curr;
assert(curr == "Node");
is >> this->_description;
is >> curr;
assert(curr == "(");
is >> curr;
while(curr != ")"){
assert(curr.length() == 1);
N attribute = static_cast<N>(curr.c_str()[0]);
is >> curr;
assert(curr == "=");
is >> curr;
this->_attributes.insert({attribute, {fix, std::stod(curr), Continuous}});
is >> curr;
}
is >> curr;
assert(curr == "{");
size_t counter = 1;
while(counter != 0){
is >> curr;
if(curr == "{"){
++counter;
}else if(curr == "}"){
--counter;
}
}
os << ")\n \tEdges : {\n";
for(Edge<N, E>* e : n.incident()){
os << *e << "\n";
}
template <typename N, typename E>
std::ostream& operator<<(std::ostream& os, Node<N, E>& n){
if(&os == &std::cout){
os << "\033[0;36m";
os << "Node " << n.description() << " ( ";
for(std::pair<N, Attribute> pair : n.attributes()){
os << static_cast<char>(pair.first) << " = " << pair.second.value() << ", ";
}
os << ") {\n \tEdges {\n";
for(Edge<N, E>* e : n.incident()){
os << *e << "\n";
}
os << "\033[0;36m";
os << "} }";
os << "\033[0m";
}else{
os << "Node " << n.description() << " ( ";
for(std::pair<N, Attribute> pair : n.attributes()){
os << static_cast<char>(pair.first) << " = " << pair.second.value() << ", ";
}
os << " ) {\n \tEdges {\n";
for(Edge<N, E>* e : n.incident()){
os << *e << "\n";
}
os << "} }";
}
os << "\033[0;36m";
os << "} }";
os << "\033[0m";
return os;
}
#include "Specialization/Graph_Specialization/Circulation_Network.h"
#include "Graphtheory/Generators/random_graph_generator.h"
#include <fstream>
#include <sstream>
int main(){
CirculationNetwork g({{Flow, Attribute(max, 0)}, {Demand, Attribute(fix, 0)}, {Capacity, Attribute(fix, 1)},{Cost, Attribute(fix, 0)}});
......@@ -23,5 +25,16 @@ int main(){
std::cout << "MaxFlow: " << ford_fulkerson(g, m.first, m.second, Flow, Demand, Capacity) << std::endl;
std::cout << g << std::endl;
std::stringstream path;
path << "./.data/.cache/" << &g << ".netw";
std::ofstream ofs_graph(path.str());
ofs_graph << g << std::endl;
ofs_graph.close();
std::ifstream ifs_graph(path.str());
CirculationNetwork g2 (ifs_graph);
std::cout << g2 << std::endl;
return 0;
}
......@@ -23,10 +23,14 @@ int main(){
Maintenance_Problem mp;
mpg >> mp;
std::ofstream ofs("./.data/Maintenance_Problem/mp.lp");
ofs << mp << std::endl;
std::ofstream ofs_graph("./.data/Maintenance_Problem/mp.netw");
ofs_graph << mp.network() << std::endl;
ofs_graph.close();
std::ofstream ofs_program("./.data/Maintenance_Problem/mp.lp");
ofs_program << mp << std::endl;
std::cout << mp << std::endl;
ofs.close();
ofs_program.close();
system("polymake --script .data/Maintenance_Problem/pm_script_lp2facets");
......
Markdown is supported
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