Cosmetics.
[mtp.git] / mtp_graph.cc
index c7e6b98..1dd6ff2 100644 (file)
@@ -27,7 +27,7 @@ class Edge {
 public:
   int id, occupied;
   scalar_t length, work_length;
-  Vertex *terminal_vertex;
+  Vertex *origin_vertex, *terminal_vertex;
   Edge *next, *pred;
 };
 
@@ -56,29 +56,31 @@ public:
 };
 
 void MTPGraph::print() {
-  for(int n = 0; n < _nb_vertices; n++) {
-    for(Edge *e = vertices[n].root_edge; e; e = e->next) {
-      cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
-      if(e->occupied) {
-        cout << " *";
-      }
-      cout << endl;
+  for(int k = 0; k < _nb_edges; k++) {
+    Edge *e = edges + k;
+    cout << e->origin_vertex->id
+         << " -> "
+         << e->terminal_vertex->id
+         << " "
+         << e->length;
+    if(e->occupied) {
+      cout << " *";
     }
+    cout << endl;
   }
 }
 
 void MTPGraph::print_dot() {
   cout << "digraph {" << endl;
   cout << "  node[shape=circle];" << endl;
-  for(int n = 0; n < _nb_vertices; n++) {
-    int a = vertices[n].id;
-    for(Edge *e = vertices[n].root_edge; e; e = e->next) {
-      int b = e->terminal_vertex->id;
-      if(e->occupied) {
-        cout << "  " << b << " -> " << a << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
-      } else {
-        cout << "  " << a << " -> " << b << " [color=gray,label=\"" << e->length << "\"];" << endl;
-      }
+  for(int k = 0; k < _nb_edges; k++) {
+    Edge *e = edges + k;
+    if(e->occupied) {
+      cout << "  " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
+           << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
+    } else {
+      cout << "  " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
+           << " [color=gray,label=\"" << e->length << "\"];" << endl;
     }
   }
   cout << "}" << endl;
@@ -106,6 +108,7 @@ MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
     vertices[from[e]].add_edge(&edges[e]);
     edges[e].occupied = 0;
     edges[e].id = e;
+    edges[e].origin_vertex = &vertices[from[e]];
     edges[e].terminal_vertex = &vertices[to[e]];
   }
 
@@ -133,20 +136,13 @@ void MTPGraph::initialize_work_lengths() {
 }
 
 void MTPGraph::update_work_lengths() {
-  for(int n = 0; n < _nb_vertices; n++) {
-    scalar_t d = vertices[n].distance_from_source;
-    for(Edge *e = vertices[n].root_edge; e; e = e->next) {
-      e->work_length += d - e->terminal_vertex->distance_from_source;
-    }
+  for(int k = 0; k < _nb_edges; k++) {
+    Edge *e = edges + k;
+    e->work_length += e->terminal_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
   }
 }
 
-void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
-  Vertex **tmp_front;
-  int tmp_front_size;
-  Vertex *v, *tv;
-  scalar_t d;
-
+void MTPGraph::force_positive_work_lengths() {
 #ifdef VERBOSE
   scalar_t residual_error = 0.0;
 #endif
@@ -163,6 +159,13 @@ void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
 #ifdef VERBOSE
   cerr << "residual_error " << residual_error << endl;
 #endif
+}
+
+void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
+  Vertex **tmp_front;
+  int tmp_front_size;
+  Vertex *v, *tv;
+  scalar_t d;
 
   for(int v = 0; v < _nb_vertices; v++) {
     vertices[v].distance_from_source = FLT_MAX;
@@ -212,39 +215,87 @@ void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
 
   for(int e = 0; e < _nb_edges; e++) {
     edges[e].length = lengths[e];
+    edges[e].work_length = edges[e].length;
   }
 
+#warning
+  // find_shortest_path(_front, _new_front);
+  // update_work_lengths();
+
   initialize_work_lengths();
 
   do {
-    total_length = 0.0;
+    force_positive_work_lengths();
     find_shortest_path(_front, _new_front);
     update_work_lengths();
 
+    total_length = 0.0;
+
     // Do we reach the _sink?
     if(_sink->pred_edge) {
 
       // If yes, compute the length of the best path
-      for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
+      for(Vertex *v = _sink; v->pred_vertex; v = v->pred_vertex) {
         total_length += v->pred_edge->length;
       }
 
       // If that length is negative
       if(total_length < 0.0) {
+#ifdef VERBOSE
+        cout << "Found a path of length " << total_length << endl;
+#endif
         // Invert all the edges along the best path
         for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
           Edge *e = v->pred_edge;
-          e->terminal_vertex = v->pred_vertex;
           e->occupied = 1 - e->occupied;
           e->length = - e->length;
           e->work_length = - e->work_length;
-          v->pred_vertex->del_edge(e);
-          v->add_edge(e);
+          e->origin_vertex->del_edge(e);
+          e->terminal_vertex->add_edge(e);
+          Vertex *t = e->terminal_vertex;
+          e->terminal_vertex = e->origin_vertex;
+          e->origin_vertex = t;
         }
       }
     }
+
   } while(total_length < 0.0);
 
+  for(int k = 0; k < _nb_edges; k++) {
+    Edge *e = edges + k;
+    if(e->occupied) {
+      e->length = - e->length;
+      e->work_length = 0;
+      e->origin_vertex->del_edge(e);
+      e->terminal_vertex->add_edge(e);
+      Vertex *t = e->terminal_vertex;
+      e->terminal_vertex = e->origin_vertex;
+      e->origin_vertex = t;
+    }
+  }
+
+  // for(Edge *e = _sink->root_edge; e; e = e->next) {
+    // if(e->occupied) {
+      // Edge *f = e;
+      // cout << "PATH " << _sink->id;
+      // while(f) {
+        // cout << " " << f->terminal_vertex->id;
+        // for(f = f->terminal_vertex->root_edge; f && !f->occupied; f = f->next);
+      // }
+      // cout << endl;
+    // }
+  // }
+
+  // int nb_occupied = 0;
+  // for(int e = 0; e < _nb_edges; e++) {
+    // for(int n = 0; n < _nb_vertices; n++) {
+      // Vertex *v = &vertices[n];
+      // for(Edge *e = v->root_edge; e; e = e->next) {
+        // if(e->occupied) nb_occupied++;
+      // }
+    // }
+  // }
+
   for(int n = 0; n < _nb_vertices; n++) {
     Vertex *v = &vertices[n];
     for(Edge *e = v->root_edge; e; e = e->next) {