COIN-OR::LEMON - Graph Library

Changeset 607:327f7cf13843 in lemon-0.x


Ignore:
Timestamp:
05/11/04 17:42:11 (16 years ago)
Author:
athos
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@789
Message:

Finished MinLengthPaths?: a specialization of MinCostFlows?.

Location:
src/work
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/work/athos/makefile

    r551 r607  
    1 BINARIES = suurballe minlength_demo mincostflows_test
     1BINARIES = minlengthpaths_test minlength_demo
    22INCLUDEDIRS= -I../.. -I.. -I../{athos,klao,marci,jacint,alpar,johanna,akos}
    33include ../makefile
  • src/work/athos/mincostflows.h

    r554 r607  
    99#include <iostream>
    1010#include <hugo/dijkstra.h>
    11 #include <graph_wrapper.h>
     11#include <hugo/graph_wrapper.h>
    1212#include <hugo/maps.h>
    1313#include <vector>
     
    7878
    7979
     80  protected:
    8081   
    8182    //Input
     
    8485    const CapacityMap& capacity;
    8586
     87
    8688    //auxiliary variables
    8789
     
    99101    Length total_length;
    100102
     103
    101104  public :
    102105
     
    111114    ///Returns k if there are at least k edge-disjoint paths from s to t.
    112115    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
     116    ///\todo May be it does make sense to be able to start with a nonzero
     117    /// feasible primal-dual solution pair as well.
    113118    int run(Node s, Node t, int k) {
    114119
     
    186191    }
    187192
    188     //This function checks, whether the given solution is optimal
    189     //Running after a \c run() should return with true
    190     //In this "state of the art" this only check optimality, doesn't bother with feasibility
    191     bool checkSolution(){
     193    ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
     194    ///be called before using this function.
     195    const EdgeIntMap &getFlow() const { return flow;}
     196
     197  ///Returns a const reference to the NodeMap \c potential (the dual solution).
     198    /// \pre \ref run() must be called before using this function.
     199    const EdgeIntMap &getPotential() const { return potential;}
     200
     201    ///This function checks, whether the given solution is optimal
     202    ///Running after a \c run() should return with true
     203    ///In this "state of the art" this only check optimality, doesn't bother with feasibility
     204    ///
     205    ///\todo Is this OK here?
     206    bool checkComplementarySlackness(){
    192207      Length mod_pot;
    193208      Length fl_e;
  • src/work/athos/minlength_demo.cc

    r511 r607  
    33
    44#include <list_graph.h>
    5 #include <dimacs.h>
    6 #include <minlengthpaths.h>
     5#include <hugo/dimacs.h>
     6#include <hugo/time_measure.h>
     7#include "minlengthpaths.h"
    78//#include <time_measure.h>
    89
     
    2021  Node s, t;
    2122  Graph::EdgeMap<int> cap(G);
    22   readDimacsMaxFlow(std::cin, G, s, t, cap);
     23  readDimacs(std::cin, G, cap, s, t);
    2324
    24   std::cout << "preflow demo (ATHOS)..." << std::endl;
     25  std::cout << "Minlengthpaths demo (ATHOS)..." << std::endl;
    2526  //Graph::EdgeMap<int> flow(G); //0 flow
    2627
     
    3233  MinLengthPaths<Graph, Graph::EdgeMap<int> >
    3334    surb_test(G,cap);
    34   std::cout << surb_test.run(s,t,k) << std::endl;
    35   std::cout << surb_test.totalLength() << std::endl;
     35  Timer ts;
     36  ts.reset();
     37  std::cout << "Number of found paths: " << surb_test.run(s,t,k) << std::endl;
     38  std::cout << "elapsed time: " << ts << std::endl;
     39 
     40  std::cout << "Total length of found paths: " << surb_test.totalLength() << std::endl;
     41  //std::cout << (surb_test.checkComplementarySlackness() ? "OK (compl. slackn.)." : "Problem (compl. slackn.)!!!") << std::endl;
     42
    3643  //preflow_push<Graph, int> max_flow_test(G, s, t, cap);
    3744  //int flow_value=max_flow_test.run();
  • src/work/athos/minlengthpaths.h

    r520 r607  
    88
    99#include <iostream>
    10 #include <dijkstra.h>
    11 #include <graph_wrapper.h>
    12 #include <maps.h>
    13 #include <vector.h>
    14 
     10//#include <hugo/dijkstra.h>
     11//#include <hugo/graph_wrapper.h>
     12#include <hugo/maps.h>
     13#include <vector>
     14#include <mincostflows.h>
     15#include <for_each_macros.h>
    1516
    1617namespace hugo {
     
    2728  /// edge-weighted directed graph having minimal total weigth (length).
    2829  ///
     30  ///\warning It is assumed that the lengths are positive, since the
     31  /// general flow-decomposition is not implemented yet.
     32  ///
    2933  ///\author Attila Bernath
    3034  template <typename Graph, typename LengthMap>
    31   class MinLengthPaths {
     35  class MinLengthPaths{
     36
    3237
    3338    typedef typename LengthMap::ValueType Length;
     
    4146    typedef ConstMap<Edge,int> ConstMap;
    4247
    43     typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
     48    //Input
     49    const Graph& G;
    4450
    45     class ModLengthMap {   
    46       typedef typename ResGraphType::template NodeMap<Length> NodeMap;
    47       const ResGraphType& G;
    48       const EdgeIntMap& rev;
    49       const LengthMap &ol;
    50       const NodeMap &pot;
    51     public :
    52       typedef typename LengthMap::KeyType KeyType;
    53       typedef typename LengthMap::ValueType ValueType;
    54        
    55       ValueType operator[](typename ResGraphType::Edge e) const {     
    56         //if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
    57         //  std::cout<<"Negative length!!"<<std::endl;
    58         //}
    59         return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
    60       }     
    61        
    62       ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
    63                    const LengthMap &o,  const NodeMap &p) :
    64         G(_G), rev(_rev), ol(o), pot(p){};
    65     };//ModLengthMap
     51    //Auxiliary variables
     52    //This is the capacity map for the mincostflow problem
     53    ConstMap const1map;
     54    //This MinCostFlows instance will actually solve the problem
     55    MinCostFlows<Graph, LengthMap, ConstMap> mincost_flow;
    6656
    67 
    68    
    69 
    70     const Graph& G;
    71     const LengthMap& length;
    72 
    73     //auxiliary variables
    74 
    75     //The value is 1 iff the edge is reversed.
    76     //If the algorithm has finished, the edges of the seeked paths are
    77     //exactly those that are reversed
    78     EdgeIntMap reversed;
    79    
    8057    //Container to store found paths
    8158    std::vector< std::vector<Edge> > paths;
    82     //typedef DirPath<Graph> DPath;
    83     //DPath paths;
    84 
    85 
    86     Length total_length;
    8759
    8860  public :
    8961
    9062
    91     MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), 
    92       length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
     63    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
     64      const1map(1), mincost_flow(_G, _length, const1map){}
    9365
    94    
    9566    ///Runs the algorithm.
    9667
    9768    ///Runs the algorithm.
    9869    ///Returns k if there are at least k edge-disjoint paths from s to t.
    99     ///Otherwise it returns the number of found edge-disjoint paths from s to t.
     70   ///Otherwise it returns the number of found edge-disjoint paths from s to t.
    10071    int run(Node s, Node t, int k) {
    101       ConstMap const1map(1);
     72
     73      int i = mincost_flow.run(s,t,k);
     74     
    10275
    10376
    104       //We need a residual graph, in which some of the edges are reversed
    105       ResGraphType res_graph(G, const1map, reversed);
    106 
    107       //Initialize the copy of the Dijkstra potential to zero
    108       typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
    109       ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
    110 
    111       Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
    112 
    113       int i;
    114       for (i=0; i<k; ++i){
    115         dijkstra.run(s);
    116         if (!dijkstra.reached(t)){
    117           //There are no k paths from s to t
    118           break;
    119         };
    120        
    121         {
    122           //We have to copy the potential
    123           typename ResGraphType::NodeIt n;
    124           for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
    125               dijkstra_dist[n] += dijkstra.distMap()[n];
    126           }
    127         }
    128 
    129 
    130         //Reversing the sortest path
    131         Node n=t;
    132         Edge e;
    133         while (n!=s){
    134           e = dijkstra.pred(n);
    135           n = dijkstra.predNode(n);
    136           reversed[e] = 1-reversed[e];
    137         }
    138 
    139          
    140       }
    141      
    14277      //Let's find the paths
    14378      //We put the paths into stl vectors (as an inner representation).
     
    14580      //We suppose the lengths to be positive now.
    14681
    147       //Meanwhile we put the total length of the found paths
    148       //in the member variable total_length
     82      //We don't want to change the flow of mincost_flow, so we make a copy
     83      //The name here suggests that the flow has only 0/1 values.
     84      EdgeIntMap reversed(G);
     85
     86      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
     87        reversed[e] = mincost_flow.getFlow()[e];
     88      }
     89     
    14990      paths.clear();
    150       total_length=0;
     91      //total_length=0;
    15192      paths.resize(k);
    15293      for (int j=0; j<i; ++j){
     
    164105          n = G.head(e);
    165106          paths[j].push_back(e);
    166           total_length += length[e];
     107          //total_length += length[e];
    167108          reversed[e] = 1-reversed[e];
    168109        }
    169110       
    170111      }
    171 
    172112      return i;
    173113    }
    174114
     115   
    175116    ///This function gives back the total length of the found paths.
    176117    ///Assumes that \c run() has been run and nothing changed since then.
    177118    Length totalLength(){
    178       return total_length;
     119      return mincost_flow.totalLength();
     120    }
     121
     122    ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
     123    ///be called before using this function.
     124    const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
     125
     126  ///Returns a const reference to the NodeMap \c potential (the dual solution).
     127    /// \pre \ref run() must be called before using this function.
     128    const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
     129
     130    ///This function checks, whether the given solution is optimal
     131    ///Running after a \c run() should return with true
     132    ///In this "state of the art" this only checks optimality, doesn't bother with feasibility
     133    ///
     134    ///\todo Is this OK here?
     135    bool checkComplementarySlackness(){
     136      return mincost_flow.checkComplementarySlackness();
    179137    }
    180138
    181139    ///This function gives back the \c j-th path in argument p.
    182140    ///Assumes that \c run() has been run and nothing changed since then.
    183     /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path.
     141    /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is not less than the result of previous \c run, then the result here will be an empty path (\c j can be 0 as well).
    184142    template<typename DirPath>
    185     void getPath(DirPath& p, int j){
     143    void getPath(DirPath& p, size_t j){
     144     
    186145      p.clear();
     146      if (j>paths.size()-1){
     147        return;
     148      }
    187149      typename DirPath::Builder B(p);
    188150      for(typename std::vector<Edge>::iterator i=paths[j].begin();
  • src/work/athos/minlengthpaths_test.cc

    r520 r607  
    7070  check(  surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 46,"Two paths, total length should be 46");
    7171
     72  check(  surb_test.checkComplementarySlackness(), "Complementary slackness conditions are not met.");
     73
    7274  typedef DirPath<ListGraph> DPath;
    7375  DPath P(graph);
     
    8183  k=1;
    8284  check(  surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19");
     85
     86  check(  surb_test.checkComplementarySlackness(), "Complementary slackness conditions are not met.");
    8387 
    8488  surb_test.getPath(P,0);
  • src/work/athos/old/minlengthpaths.h

    r601 r607  
    88
    99#include <iostream>
    10 #include <dijkstra.h>
    11 #include <graph_wrapper.h>
    12 #include <maps.h>
    13 #include <vector.h>
     10#include <hugo/dijkstra.h>
     11#include <hugo/graph_wrapper.h>
     12#include <hugo/maps.h>
     13#include <vector>
    1414
    1515
  • src/work/klao/path.h

    r493 r607  
    1212#include <algorithm>
    1313
    14 #include <invalid.h>
    15 #include <error.h>
     14#include <hugo/invalid.h>
     15#include <hugo/error.h>
    1616#include <debug.h>
    1717
Note: See TracChangeset for help on using the changeset viewer.