alpar@2360
|
1 |
/* -*- C++ -*-
|
alpar@2360
|
2 |
*
|
alpar@2360
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@2360
|
4 |
*
|
alpar@2360
|
5 |
* Copyright (C) 2003-2006
|
alpar@2360
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@2360
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@2360
|
8 |
*
|
alpar@2360
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@2360
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@2360
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@2360
|
12 |
*
|
alpar@2360
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@2360
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@2360
|
15 |
* purpose.
|
alpar@2360
|
16 |
*
|
alpar@2360
|
17 |
*/
|
alpar@2360
|
18 |
|
alpar@2360
|
19 |
///\ingroup demos
|
alpar@2360
|
20 |
///\file
|
alpar@2360
|
21 |
///\brief Demonstrating the usage of LEMON's Dijkstra algorithm
|
alpar@2360
|
22 |
///
|
alpar@2360
|
23 |
/// Dijkstra's algorithm computes shortest paths between two nodes in
|
alpar@2360
|
24 |
/// a graph with edge lengths. Here we only show some of the
|
alpar@2360
|
25 |
/// facilities supplied by our implementation: for the detailed
|
alpar@2360
|
26 |
/// documentation of the LEMON Dijkstra class read \ref lemon::Dijkstra "this".
|
alpar@2360
|
27 |
///
|
alpar@2360
|
28 |
/// \include dijkstra_demo.cc
|
alpar@2360
|
29 |
|
alpar@2360
|
30 |
#include <iostream>
|
alpar@2360
|
31 |
#include <lemon/list_graph.h>
|
alpar@2360
|
32 |
#include <lemon/csp.h>
|
alpar@2360
|
33 |
#include <lemon/random.h>
|
alpar@2360
|
34 |
|
alpar@2360
|
35 |
|
alpar@2360
|
36 |
using namespace lemon;
|
alpar@2360
|
37 |
|
alpar@2360
|
38 |
|
alpar@2360
|
39 |
int main (int, char*[])
|
alpar@2360
|
40 |
{
|
alpar@2360
|
41 |
|
alpar@2360
|
42 |
typedef ListGraph Graph;
|
alpar@2360
|
43 |
typedef Graph::Node Node;
|
alpar@2360
|
44 |
typedef Graph::Edge Edge;
|
alpar@2360
|
45 |
typedef Graph::EdgeIt EdgeIt;
|
alpar@2360
|
46 |
typedef Graph::EdgeMap<double> LengthMap;
|
alpar@2360
|
47 |
|
alpar@2360
|
48 |
Graph g;
|
alpar@2360
|
49 |
|
alpar@2360
|
50 |
const int N=100;
|
alpar@2360
|
51 |
const int M=20000;
|
alpar@2360
|
52 |
std::vector<Node> nodes;
|
alpar@2360
|
53 |
std::vector<Edge> edges;
|
alpar@2360
|
54 |
|
alpar@2360
|
55 |
for(int i=0;i<N;i++) nodes.push_back(g.addNode());
|
alpar@2360
|
56 |
|
alpar@2360
|
57 |
for(int i=0;i<M;i++)
|
alpar@2360
|
58 |
edges.push_back(g.addEdge(nodes[rnd[N]],nodes[rnd[N]]));
|
alpar@2360
|
59 |
|
alpar@2360
|
60 |
LengthMap cost(g);
|
alpar@2360
|
61 |
LengthMap delay(g);
|
alpar@2360
|
62 |
for(EdgeIt e(g);e!=INVALID;++e)
|
alpar@2360
|
63 |
{
|
alpar@2360
|
64 |
cost[e]=0.01+rnd(.99);
|
alpar@2360
|
65 |
delay[e]=0.01+rnd(.99);
|
alpar@2360
|
66 |
}
|
alpar@2360
|
67 |
|
alpar@2360
|
68 |
ConstrainedShortestPath<Graph,LengthMap,LengthMap> csp(g,cost,delay);
|
alpar@2360
|
69 |
for(double d=0;d<2;d+=.01)
|
alpar@2360
|
70 |
{
|
alpar@2360
|
71 |
double lo_bo;
|
alpar@2360
|
72 |
std::cout << d << ": ";
|
alpar@2360
|
73 |
SimplePath<Graph> r = csp.larac(nodes[0],nodes[1],d,lo_bo);
|
alpar@2360
|
74 |
if(r.empty()) std::cout << "INFEASIBLE\n";
|
alpar@2360
|
75 |
else std::cout << "delay: " << csp.delay(r)
|
alpar@2360
|
76 |
<< " cost: " << csp.cost(r)
|
alpar@2360
|
77 |
<< " (lb: " << lo_bo
|
alpar@2360
|
78 |
<< " gap: " << csp.cost(r)-lo_bo
|
alpar@2360
|
79 |
<< " , " << (csp.cost(r)-lo_bo)/csp.cost(r)*100
|
alpar@2360
|
80 |
<< "%)" << std::endl;
|
alpar@2360
|
81 |
}
|
alpar@2360
|
82 |
|
alpar@2360
|
83 |
return 0;
|
alpar@2360
|
84 |
}
|