Nem tudom, a hugo-n miert nem megy.
authorathos
Tue, 04 May 2004 14:54:21 +0000
changeset 5277550fed0cd91
parent 526 def920ddaba7
child 528 c00f6ebbe1e6
Nem tudom, a hugo-n miert nem megy.
src/work/athos/makefile
src/work/athos/mincostflows.h
src/work/athos/mincostflows_test.cc
     1.1 --- a/src/work/athos/makefile	Tue May 04 14:06:00 2004 +0000
     1.2 +++ b/src/work/athos/makefile	Tue May 04 14:54:21 2004 +0000
     1.3 @@ -1,4 +1,4 @@
     1.4 -BINARIES = suurballe minlength_demo
     1.5 +BINARIES = suurballe minlength_demo mincostflows_test
     1.6  INCLUDEDIRS= -I../../include -I.. -I../{athos,klao,marci,jacint,alpar,johanna,akos} 
     1.7  include ../makefile
     1.8  
     2.1 --- a/src/work/athos/mincostflows.h	Tue May 04 14:06:00 2004 +0000
     2.2 +++ b/src/work/athos/mincostflows.h	Tue May 04 14:54:21 2004 +0000
     2.3 @@ -38,6 +38,8 @@
     2.4    class MinCostFlows {
     2.5  
     2.6      typedef typename LengthMap::ValueType Length;
     2.7 +
     2.8 +    typedef typename LengthMap::ValueType Length;
     2.9      
    2.10      typedef typename Graph::Node Node;
    2.11      typedef typename Graph::NodeIt NodeIt;
    2.12 @@ -45,14 +47,14 @@
    2.13      typedef typename Graph::OutEdgeIt OutEdgeIt;
    2.14      typedef typename Graph::template EdgeMap<int> EdgeIntMap;
    2.15  
    2.16 -    typedef ConstMap<Edge,int> ConstMap;
    2.17 +    //    typedef ConstMap<Edge,int> ConstMap;
    2.18  
    2.19 -    typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
    2.20 +    typedef ResGraphWrapper<const Graph,int,EdgeIntMap,EdgeIntMap> ResGraphType;
    2.21  
    2.22      class ModLengthMap {   
    2.23        typedef typename ResGraphType::template NodeMap<Length> NodeMap;
    2.24        const ResGraphType& G;
    2.25 -      const EdgeIntMap& rev;
    2.26 +      //      const EdgeIntMap& rev;
    2.27        const LengthMap &ol;
    2.28        const NodeMap &pot;
    2.29      public :
    2.30 @@ -60,29 +62,30 @@
    2.31        typedef typename LengthMap::ValueType ValueType;
    2.32  	
    2.33        ValueType operator[](typename ResGraphType::Edge e) const {     
    2.34 -	//if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
    2.35 -	//  std::cout<<"Negative length!!"<<std::endl;
    2.36 -	//}
    2.37 -	return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
    2.38 +	if (G.forward(e))
    2.39 +	  return  ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
    2.40 +	else
    2.41 +	  return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
    2.42        }     
    2.43  	
    2.44        ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev, 
    2.45  		   const LengthMap &o,  const NodeMap &p) : 
    2.46 -	G(_G), rev(_rev), ol(o), pot(p){}; 
    2.47 +	G(_G), /*rev(_rev),*/ ol(o), pot(p){}; 
    2.48      };//ModLengthMap
    2.49  
    2.50  
    2.51      
    2.52 -
    2.53 +    //Input
    2.54      const Graph& G;
    2.55      const LengthMap& length;
    2.56 +    const EdgeIntMap& capacity;
    2.57  
    2.58      //auxiliary variables
    2.59  
    2.60      //The value is 1 iff the edge is reversed. 
    2.61      //If the algorithm has finished, the edges of the seeked paths are 
    2.62      //exactly those that are reversed 
    2.63 -    EdgeIntMap reversed; 
    2.64 +    EdgeIntMap flow; 
    2.65      
    2.66      //Container to store found paths
    2.67      std::vector< std::vector<Edge> > paths;
    2.68 @@ -95,8 +98,8 @@
    2.69    public :
    2.70  
    2.71  
    2.72 -    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), 
    2.73 -      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
    2.74 +    MinLengthPaths(Graph& _G, LengthMap& _length, EdgeIntMap& _cap) : G(_G), 
    2.75 +      length(_length), capacity(_cap), flow(_G)/*, dijkstra_dist(_G)*/{ }
    2.76  
    2.77      
    2.78      ///Runs the algorithm.
    2.79 @@ -105,15 +108,14 @@
    2.80      ///Returns k if there are at least k edge-disjoint paths from s to t.
    2.81      ///Otherwise it returns the number of found edge-disjoint paths from s to t.
    2.82      int run(Node s, Node t, int k) {
    2.83 -      ConstMap const1map(1);
    2.84  
    2.85  
    2.86 -      //We need a residual graph, in which some of the edges are reversed
    2.87 -      ResGraphType res_graph(G, const1map, reversed);
    2.88 +      //We need a residual graph
    2.89 +      ResGraphType res_graph(G, capacity, flow);
    2.90  
    2.91        //Initialize the copy of the Dijkstra potential to zero
    2.92        typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
    2.93 -      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
    2.94 +      ModLengthMap mod_length(res_graph, length, dijkstra_dist);
    2.95  
    2.96        Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
    2.97  
    2.98 @@ -134,18 +136,21 @@
    2.99  	}
   2.100  
   2.101  
   2.102 -	//Reversing the sortest path
   2.103 +	//Augmenting on the sortest path
   2.104  	Node n=t;
   2.105  	Edge e;
   2.106  	while (n!=s){
   2.107  	  e = dijkstra.pred(n);
   2.108  	  n = dijkstra.predNode(n);
   2.109 -	  reversed[e] = 1-reversed[e];
   2.110 +	  G.augment(e,1);
   2.111  	}
   2.112  
   2.113  	  
   2.114        }
   2.115        
   2.116 +      /*
   2.117 +	///\TODO To be implemented later
   2.118 +
   2.119        //Let's find the paths
   2.120        //We put the paths into stl vectors (as an inner representation). 
   2.121        //In the meantime we lose the information stored in 'reversed'.
   2.122 @@ -175,6 +180,7 @@
   2.123  	}
   2.124  	
   2.125        }
   2.126 +      */
   2.127  
   2.128        return i;
   2.129      }
   2.130 @@ -206,4 +212,4 @@
   2.131  
   2.132  } //namespace hugo
   2.133  
   2.134 -#endif //HUGO_MINLENGTHPATHS_H
   2.135 +#endif //HUGO_MINCOSTFLOW_H
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/work/athos/mincostflows_test.cc	Tue May 04 14:54:21 2004 +0000
     3.3 @@ -0,0 +1,94 @@
     3.4 +#include <iostream>
     3.5 +#include <list_graph.h>
     3.6 +#include <mincostflows.h>
     3.7 +//#include <path.h>
     3.8 +#include <maps.h>
     3.9 +
    3.10 +using namespace std;
    3.11 +using namespace hugo;
    3.12 +
    3.13 +
    3.14 +
    3.15 +bool passed = true;
    3.16 +
    3.17 +void check(bool rc, char *msg="") {
    3.18 +  passed = passed && rc;
    3.19 +  if(!rc) {
    3.20 +    std::cerr << "Test failed! ("<< msg << ")" << std::endl; \
    3.21 + 
    3.22 +
    3.23 +  }
    3.24 +}
    3.25 +
    3.26 +
    3.27 +
    3.28 +int main()
    3.29 +{
    3.30 +
    3.31 +  typedef ListGraph::Node Node;
    3.32 +  typedef ListGraph::Edge Edge;
    3.33 +
    3.34 +  ListGraph graph;
    3.35 +
    3.36 +  //Ahuja könyv példája
    3.37 +
    3.38 +  Node s=graph.addNode();
    3.39 +  Node v1=graph.addNode();  
    3.40 +  Node v2=graph.addNode();
    3.41 +  Node v3=graph.addNode();
    3.42 +  Node v4=graph.addNode();
    3.43 +  Node v5=graph.addNode();
    3.44 +  Node t=graph.addNode();
    3.45 +
    3.46 +  Edge s_v1=graph.addEdge(s, v1);
    3.47 +  Edge v1_v2=graph.addEdge(v1, v2);
    3.48 +  Edge s_v3=graph.addEdge(s, v3);
    3.49 +  Edge v2_v4=graph.addEdge(v2, v4);
    3.50 +  Edge v2_v5=graph.addEdge(v2, v5);
    3.51 +  Edge v3_v5=graph.addEdge(v3, v5);
    3.52 +  Edge v4_t=graph.addEdge(v4, t);
    3.53 +  Edge v5_t=graph.addEdge(v5, t);
    3.54 +  
    3.55 +
    3.56 +  ListGraph::EdgeMap<int> length(graph);
    3.57 +
    3.58 +  length.set(s_v1, 6);
    3.59 +  length.set(v1_v2, 4);
    3.60 +  length.set(s_v3, 10);
    3.61 +  length.set(v2_v4, 5);
    3.62 +  length.set(v2_v5, 1);
    3.63 +  length.set(v3_v5, 5);
    3.64 +  length.set(v4_t, 8);
    3.65 +  length.set(v5_t, 8);
    3.66 +
    3.67 +  ConstMap const1map(1);
    3.68 +  std::cout << "Minlengthpaths algorithm test..." << std::endl;
    3.69 +
    3.70 +  
    3.71 +  int k=3;
    3.72 +  MinCostFlows< ListGraph, ListGraph::EdgeMap<int> >
    3.73 +    surb_test(graph, length, const1map);
    3.74 +
    3.75 +  check(  surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 46,"Two paths, total length should be 46");
    3.76 +
    3.77 +  typedef DirPath<ListGraph> DPath;
    3.78 +  DPath P(graph);
    3.79 +
    3.80 +  surb_test.getPath(P,0);
    3.81 +  check(P.length() == 4, "First path should contain 4 edges.");  
    3.82 +
    3.83 +  surb_test.getPath(P,1);
    3.84 +  check(P.length() == 3, "Second path: 3 edges.");
    3.85 +  
    3.86 +  k=1;
    3.87 +  check(  surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19");
    3.88 + 
    3.89 +  surb_test.getPath(P,0);
    3.90 +  check(P.length() == 4, "First path should contain 4 edges.");  
    3.91 +
    3.92 +  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
    3.93 +       << endl;
    3.94 +
    3.95 +  return passed ? 0 : 1;
    3.96 +
    3.97 +}