demo/min_route.cc
author deba
Tue, 13 Sep 2005 12:41:02 +0000
changeset 1683 13648409b596
parent 1435 8e85e6bbefdf
child 1775 f19e108cb286
permissions -rw-r--r--
Input file for coloring.
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@1358
    30
deba@1342
    31
using namespace lemon;
deba@1342
    32
deba@1342
    33
template <typename CoordMap>
deba@1342
    34
class PotentialMap {
deba@1342
    35
public:
deba@1342
    36
  typedef double Value;
deba@1342
    37
  typedef typename CoordMap::Key Key;
deba@1342
    38
deba@1342
    39
  PotentialMap(const CoordMap& _coord, const xy<double>& _target)
deba@1342
    40
    : coord(_coord), target(_target) {}
deba@1342
    41
deba@1342
    42
  double operator[](const Key& node) const {
deba@1342
    43
    return std::sqrt((coord[node].x - target.x) * (coord[node].x - target.x) + 
deba@1342
    44
		     (coord[node].y - target.y) * (coord[node].y - target.y));
deba@1342
    45
  }
deba@1342
    46
private:
deba@1358
    47
  const CoordMap& coord;
deba@1342
    48
  xy<double> target;
deba@1342
    49
};
deba@1342
    50
deba@1342
    51
template <typename Graph, typename LengthMap, typename PotentialMap>
deba@1342
    52
class ReducedLengthMap {
deba@1342
    53
public:
deba@1342
    54
  typedef double Value;
deba@1342
    55
  typedef typename LengthMap::Key Key;
deba@1342
    56
deba@1342
    57
  ReducedLengthMap(const Graph& _graph, const LengthMap& _length, 
deba@1342
    58
		   const PotentialMap& _pot) 
deba@1342
    59
    : graph(_graph), length(_length), pot(_pot) {}
deba@1342
    60
deba@1342
    61
  Value operator[](const Key& edge) const {
deba@1342
    62
    return length[edge] - (pot[graph.source(edge)] - pot[graph.target(edge)]);
deba@1342
    63
  }
deba@1342
    64
deba@1342
    65
private:
deba@1342
    66
  const Graph& graph;
deba@1342
    67
  const LengthMap& length;
deba@1342
    68
  const PotentialMap& pot;
deba@1342
    69
};
deba@1342
    70
deba@1342
    71
int main() {
deba@1342
    72
  typedef SmartGraph Graph;
deba@1342
    73
deba@1342
    74
  typedef Graph::Edge Edge;
deba@1342
    75
  typedef Graph::Node Node;
deba@1342
    76
  typedef Graph::EdgeIt EdgeIt;
deba@1342
    77
  typedef Graph::NodeIt NodeIt;
deba@1342
    78
  typedef Graph::EdgeMap<double> LengthMap;
deba@1342
    79
  typedef Graph::NodeMap<xy<double> > CoordMap;
deba@1342
    80
deba@1342
    81
  SmartGraph graph;
deba@1342
    82
deba@1358
    83
  std::ifstream is("route.lgf");
deba@1358
    84
  GraphReader<Graph> reader(is, graph);
deba@1342
    85
  
deba@1342
    86
  CoordMap coord(graph);
deba@1342
    87
  XMap<CoordMap> xcoord = xMap(coord);
deba@1394
    88
  reader.readNodeMap("coordinates_x", xcoord);
deba@1342
    89
  YMap<CoordMap> ycoord = yMap(coord);
deba@1394
    90
  reader.readNodeMap("coordinates_y", ycoord);
deba@1342
    91
deba@1342
    92
  LengthMap length(graph);
deba@1394
    93
  reader.readEdgeMap("length", length);
deba@1342
    94
deba@1342
    95
  Node source, target;
deba@1394
    96
  reader.readNode("source", source);
deba@1394
    97
  reader.readNode("target", target);
deba@1342
    98
deba@1342
    99
  reader.run();
deba@1342
   100
deba@1342
   101
  {
deba@1342
   102
    Timer timer;
deba@1342
   103
    Dijkstra<Graph, LengthMap> dijkstra(graph, length);
deba@1342
   104
    dijkstra.init();
deba@1342
   105
    dijkstra.addSource(source);
deba@1342
   106
    dijkstra.start(target);
deba@1342
   107
deba@1342
   108
    std::cout << dijkstra.dist(target) << std::endl;
deba@1342
   109
    std::cout << timer << std::endl;
deba@1342
   110
  }
deba@1342
   111
  {
deba@1342
   112
    Timer timer;
deba@1342
   113
    typedef PotentialMap<CoordMap> Potential;
deba@1342
   114
    Potential potential(coord, coord[target]);
deba@1342
   115
deba@1342
   116
    typedef ReducedLengthMap<Graph, LengthMap, Potential> ReducedLength;
deba@1342
   117
    ReducedLength reduced(graph, length, potential);
deba@1342
   118
deba@1342
   119
    Dijkstra<Graph, ReducedLength> dijkstra(graph, reduced);
deba@1342
   120
deba@1342
   121
    dijkstra.init();
deba@1342
   122
    dijkstra.addSource(source);
deba@1342
   123
    dijkstra.start(target);
deba@1342
   124
  
deba@1342
   125
    std::cout << dijkstra.dist(target) + potential[source] << std::endl;
deba@1342
   126
    std::cout << timer << std::endl;
deba@1342
   127
  }
deba@1342
   128
deba@1342
   129
  return 0;
deba@1342
   130
}