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