Starts to look decent.
authorFrancois Fleuret <francois@fleuret.org>
Wed, 22 Aug 2012 18:13:53 +0000 (11:13 -0700)
committerFrancois Fleuret <francois@fleuret.org>
Wed, 22 Aug 2012 18:13:53 +0000 (11:13 -0700)
misc.h
mtp_graph.cc

diff --git a/misc.h b/misc.h
index f9bc8e5..f28305a 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -11,7 +11,7 @@
 #ifndef MISC_H
 #define MISC_H
 
-#define VERBOSE
+// #define VERBOSE
 
 typedef float scalar_t;
 
index 1dd6ff2..5100c1d 100644 (file)
@@ -29,6 +29,8 @@ public:
   scalar_t length, work_length;
   Vertex *origin_vertex, *terminal_vertex;
   Edge *next, *pred;
+
+  inline void revert();
 };
 
 class Vertex {
@@ -36,24 +38,45 @@ public:
   int id, iteration;
   Edge *root_edge;
   scalar_t distance_from_source;
-  Vertex *pred_vertex;
   Edge *pred_edge;
 
-  Vertex() { root_edge = 0; }
+  Vertex();
+  inline void add_edge(Edge *e);
+  inline void del_edge(Edge *e);
+};
 
-  inline void add_edge(Edge *e) {
-    e->next = root_edge;
-    e->pred = 0;
-    if(root_edge) { root_edge->pred = e; }
-    root_edge = e;
-  }
+//////////////////////////////////////////////////////////////////////
 
-  inline void del_edge(Edge *e) {
-    if(e == root_edge) { root_edge = e->next; }
-    if(e->pred) { e->pred->next = e->next; }
-    if(e->next) { e->next->pred = e->pred; }
-  }
-};
+void Edge::revert() {
+  length = - length;
+  work_length = 0;
+  origin_vertex->del_edge(this);
+  terminal_vertex->add_edge(this);
+  Vertex *t = terminal_vertex;
+  terminal_vertex = origin_vertex;
+  origin_vertex = t;
+}
+
+//////////////////////////////////////////////////////////////////////
+
+Vertex::Vertex() {
+  root_edge = 0;
+}
+
+void Vertex::add_edge(Edge *e) {
+  e->next = root_edge;
+  e->pred = 0;
+  if(root_edge) { root_edge->pred = e; }
+  root_edge = e;
+}
+
+void Vertex::del_edge(Edge *e) {
+  if(e == root_edge) { root_edge = e->next; }
+  if(e->pred) { e->pred->next = e->next; }
+  if(e->next) { e->next->pred = e->pred; }
+}
+
+//////////////////////////////////////////////////////////////////////
 
 void MTPGraph::print() {
   for(int k = 0; k < _nb_edges; k++) {
@@ -169,7 +192,6 @@ void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
 
   for(int v = 0; v < _nb_vertices; v++) {
     vertices[v].distance_from_source = FLT_MAX;
-    vertices[v].pred_vertex = 0;
     vertices[v].pred_edge = 0;
     vertices[v].iteration = 0;
   }
@@ -190,7 +212,6 @@ void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
         tv = e->terminal_vertex;
         if(d < tv->distance_from_source) {
           tv->distance_from_source = d;
-          tv->pred_vertex = v;
           tv->pred_edge = e;
           if(tv->iteration < iteration) {
             _new_front[_new_front_size++] = tv;
@@ -212,17 +233,19 @@ void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
 
 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
   scalar_t total_length;
+  Vertex *v;
+  Edge *e;
 
   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();
+  find_shortest_path(_front, _new_front);
+  update_work_lengths();
 
-  initialize_work_lengths();
+  // #warning
+  // initialize_work_lengths();
 
   do {
     force_positive_work_lengths();
@@ -233,28 +256,24 @@ void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
 
     // Do we reach the _sink?
     if(_sink->pred_edge) {
-
       // If yes, compute the length of the best path
-      for(Vertex *v = _sink; v->pred_vertex; v = v->pred_vertex) {
+      v = _sink;
+      while(v->pred_edge) {
         total_length += v->pred_edge->length;
+        v = v->pred_edge->origin_vertex;
       }
-
       // 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;
+        v = _sink;
+        while(v->pred_edge) {
+          e = v->pred_edge;
+          v = e->origin_vertex;
+          e->revert();
           e->occupied = 1 - e->occupied;
-          e->length = - e->length;
-          e->work_length = - e->work_length;
-          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;
         }
       }
     }
@@ -263,39 +282,9 @@ void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
 
   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;
-    }
+    if(e->occupied) { e->revert(); }
   }
 
-  // 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) {