demo/min_route.cc
author alpar
Thu, 17 Nov 2005 10:16:29 +0000
changeset 1812 a6f019fa6e7a
parent 1636 260ac104190f
child 1875 98698b69a902
permissions -rw-r--r--
split(Edge) member function added.
ladanyi@1636
     1
/* -*- C++ -*-
ladanyi@1636
     2
 * demo/min_route.cc - Part of LEMON, a generic C++ optimization library
ladanyi@1636
     3
 *
ladanyi@1636
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
ladanyi@1636
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
ladanyi@1636
     6
 *
ladanyi@1636
     7
 * Permission to use, modify and distribute this software is granted
ladanyi@1636
     8
 * provided that this copyright notice appears in all copies. For
ladanyi@1636
     9
 * precise terms see the accompanying LICENSE file.
ladanyi@1636
    10
 *
ladanyi@1636
    11
 * This software is provided "AS IS" with no warranty of any kind,
ladanyi@1636
    12
 * express or implied, and with no claim as to its suitability for any
ladanyi@1636
    13
 * purpose.
ladanyi@1636
    14
 *
ladanyi@1636
    15
 */
ladanyi@1636
    16
deba@1358
    17
#include <iostream>
deba@1358
    18
#include <fstream>
deba@1358
    19
deba@1342
    20
#include <lemon/smart_graph.h>
deba@1342
    21
#include <lemon/dijkstra.h>
deba@1342
    22
#include <lemon/maps.h>
deba@1342
    23
#include <lemon/xy.h>
deba@1342
    24
#include <lemon/graph_reader.h>
deba@1342
    25
deba@1342
    26
#include <lemon/time_measure.h>
deba@1342
    27
deba@1342
    28
#include <cmath>
deba@1342
    29
deba@1775
    30
/// \ingroup demos
deba@1775
    31
/// \file
deba@1775
    32
/// \brief Minimal route on a plan graph with eucledian distances.
deba@1775
    33
///
deba@1775
    34
/// Minimal route on a plan graph with eucledian distances.
deba@1775
    35
///
deba@1775
    36
/// \include min_route.cc
deba@1775
    37
deba@1358
    38
deba@1342
    39
using namespace lemon;
deba@1342
    40
deba@1342
    41
template <typename CoordMap>
deba@1342
    42
class PotentialMap {
deba@1342
    43
public:
deba@1342
    44
  typedef double Value;
deba@1342
    45
  typedef typename CoordMap::Key Key;
deba@1342
    46
deba@1342
    47
  PotentialMap(const CoordMap& _coord, const xy<double>& _target)
deba@1342
    48
    : coord(_coord), target(_target) {}
deba@1342
    49
deba@1342
    50
  double operator[](const Key& node) const {
deba@1342
    51
    return std::sqrt((coord[node].x - target.x) * (coord[node].x - target.x) + 
deba@1342
    52
		     (coord[node].y - target.y) * (coord[node].y - target.y));
deba@1342
    53
  }
deba@1342
    54
private:
deba@1358
    55
  const CoordMap& coord;
deba@1342
    56
  xy<double> target;
deba@1342
    57
};
deba@1342
    58
deba@1342
    59
template <typename Graph, typename LengthMap, typename PotentialMap>
deba@1342
    60
class ReducedLengthMap {
deba@1342
    61
public:
deba@1342
    62
  typedef double Value;
deba@1342
    63
  typedef typename LengthMap::Key Key;
deba@1342
    64
deba@1342
    65
  ReducedLengthMap(const Graph& _graph, const LengthMap& _length, 
deba@1342
    66
		   const PotentialMap& _pot) 
deba@1342
    67
    : graph(_graph), length(_length), pot(_pot) {}
deba@1342
    68
deba@1342
    69
  Value operator[](const Key& edge) const {
deba@1342
    70
    return length[edge] - (pot[graph.source(edge)] - pot[graph.target(edge)]);
deba@1342
    71
  }
deba@1342
    72
deba@1342
    73
private:
deba@1342
    74
  const Graph& graph;
deba@1342
    75
  const LengthMap& length;
deba@1342
    76
  const PotentialMap& pot;
deba@1342
    77
};
deba@1342
    78
deba@1342
    79
int main() {
deba@1342
    80
  typedef SmartGraph Graph;
deba@1342
    81
deba@1342
    82
  typedef Graph::Edge Edge;
deba@1342
    83
  typedef Graph::Node Node;
deba@1342
    84
  typedef Graph::EdgeIt EdgeIt;
deba@1342
    85
  typedef Graph::NodeIt NodeIt;
deba@1342
    86
  typedef Graph::EdgeMap<double> LengthMap;
deba@1342
    87
  typedef Graph::NodeMap<xy<double> > CoordMap;
deba@1342
    88
deba@1342
    89
  SmartGraph graph;
deba@1342
    90
deba@1358
    91
  std::ifstream is("route.lgf");
deba@1358
    92
  GraphReader<Graph> reader(is, graph);
deba@1342
    93
  
deba@1342
    94
  CoordMap coord(graph);
deba@1342
    95
  XMap<CoordMap> xcoord = xMap(coord);
deba@1394
    96
  reader.readNodeMap("coordinates_x", xcoord);
deba@1342
    97
  YMap<CoordMap> ycoord = yMap(coord);
deba@1394
    98
  reader.readNodeMap("coordinates_y", ycoord);
deba@1342
    99
deba@1342
   100
  LengthMap length(graph);
deba@1394
   101
  reader.readEdgeMap("length", length);
deba@1342
   102
deba@1342
   103
  Node source, target;
deba@1394
   104
  reader.readNode("source", source);
deba@1394
   105
  reader.readNode("target", target);
deba@1342
   106
deba@1342
   107
  reader.run();
deba@1342
   108
deba@1342
   109
  {
deba@1342
   110
    Timer timer;
deba@1342
   111
    Dijkstra<Graph, LengthMap> dijkstra(graph, length);
deba@1342
   112
    dijkstra.init();
deba@1342
   113
    dijkstra.addSource(source);
deba@1342
   114
    dijkstra.start(target);
deba@1342
   115
deba@1342
   116
    std::cout << dijkstra.dist(target) << std::endl;
deba@1342
   117
    std::cout << timer << std::endl;
deba@1342
   118
  }
deba@1342
   119
  {
deba@1342
   120
    Timer timer;
deba@1342
   121
    typedef PotentialMap<CoordMap> Potential;
deba@1342
   122
    Potential potential(coord, coord[target]);
deba@1342
   123
deba@1342
   124
    typedef ReducedLengthMap<Graph, LengthMap, Potential> ReducedLength;
deba@1342
   125
    ReducedLength reduced(graph, length, potential);
deba@1342
   126
deba@1342
   127
    Dijkstra<Graph, ReducedLength> dijkstra(graph, reduced);
deba@1342
   128
deba@1342
   129
    dijkstra.init();
deba@1342
   130
    dijkstra.addSource(source);
deba@1342
   131
    dijkstra.start(target);
deba@1342
   132
  
deba@1342
   133
    std::cout << dijkstra.dist(target) + potential[source] << std::endl;
deba@1342
   134
    std::cout << timer << std::endl;
deba@1342
   135
  }
deba@1342
   136
deba@1342
   137
  return 0;
deba@1342
   138
}