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@2553
|
5 |
* Copyright (C) 2003-2008
|
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 |
#ifndef LEMON_CSP_H
|
alpar@2360
|
20 |
#define LEMON_CSP_H
|
alpar@2360
|
21 |
|
deba@2376
|
22 |
///\ingroup approx
|
alpar@2360
|
23 |
///\file
|
alpar@2360
|
24 |
///\brief Algorithm for the Resource Constrained Shortest Path problem.
|
alpar@2360
|
25 |
///
|
alpar@2360
|
26 |
|
alpar@2360
|
27 |
#include <lemon/list_graph.h>
|
alpar@2360
|
28 |
#include <lemon/graph_utils.h>
|
alpar@2360
|
29 |
#include <lemon/error.h>
|
alpar@2360
|
30 |
#include <lemon/maps.h>
|
alpar@2360
|
31 |
#include <lemon/tolerance.h>
|
alpar@2360
|
32 |
#include <lemon/dijkstra.h>
|
alpar@2360
|
33 |
#include <lemon/path.h>
|
alpar@2360
|
34 |
#include <lemon/counter.h>
|
alpar@2360
|
35 |
namespace lemon {
|
deba@2377
|
36 |
|
deba@2377
|
37 |
///\ingroup approx
|
deba@2486
|
38 |
///
|
deba@2486
|
39 |
///\brief Algorithms for the Resource Constrained Shortest Path Problem
|
deba@2486
|
40 |
///
|
alpar@2373
|
41 |
///The Resource Constrained Shortest (Least Cost) Path problem is the
|
alpar@2373
|
42 |
///following. We are given a directed graph with two additive weightings
|
alpar@2373
|
43 |
///on the edges, referred as \e cost and \e delay. In addition,
|
alpar@2373
|
44 |
///a source and a destination node \e s and \e t and a delay
|
alpar@2373
|
45 |
///constraint \e D is given. A path \e p is called \e feasible
|
alpar@2373
|
46 |
///if <em>delay(p)\<=D</em>. Then, the task is to find the least cost
|
alpar@2373
|
47 |
///feasible path.
|
alpar@2360
|
48 |
///
|
alpar@2360
|
49 |
template<class Graph,
|
alpar@2360
|
50 |
class CM=typename Graph:: template EdgeMap<double>,
|
alpar@2360
|
51 |
class DM=CM>
|
alpar@2360
|
52 |
class ConstrainedShortestPath
|
alpar@2360
|
53 |
{
|
alpar@2360
|
54 |
public:
|
alpar@2360
|
55 |
|
alpar@2360
|
56 |
GRAPH_TYPEDEFS(typename Graph);
|
alpar@2360
|
57 |
|
alpar@2360
|
58 |
typedef SimplePath<Graph> Path;
|
alpar@2360
|
59 |
|
deba@2401
|
60 |
private:
|
deba@2401
|
61 |
|
deba@2401
|
62 |
const Graph &_g;
|
alpar@2360
|
63 |
Tolerance<double> tol;
|
alpar@2360
|
64 |
|
deba@2401
|
65 |
const CM &_cost;
|
deba@2401
|
66 |
const DM &_delay;
|
alpar@2360
|
67 |
|
alpar@2360
|
68 |
class CoMap
|
alpar@2360
|
69 |
{
|
deba@2401
|
70 |
const CM &_cost;
|
deba@2401
|
71 |
const DM &_delay;
|
alpar@2360
|
72 |
double _lambda;
|
alpar@2360
|
73 |
public:
|
alpar@2360
|
74 |
typedef typename CM::Key Key;
|
alpar@2360
|
75 |
typedef double Value;
|
deba@2401
|
76 |
CoMap(const CM &c, const DM &d) :_cost(c), _delay(d), _lambda(0) {}
|
alpar@2360
|
77 |
double lambda() const { return _lambda; }
|
alpar@2360
|
78 |
void lambda(double l) { _lambda=l; }
|
alpar@2360
|
79 |
Value operator[](Key &e) const
|
alpar@2360
|
80 |
{
|
alpar@2360
|
81 |
return _cost[e]+_lambda*_delay[e];
|
alpar@2360
|
82 |
}
|
deba@2401
|
83 |
};
|
deba@2401
|
84 |
|
deba@2401
|
85 |
CoMap _co_map;
|
alpar@2360
|
86 |
|
alpar@2360
|
87 |
|
alpar@2360
|
88 |
Dijkstra<Graph, CoMap> _dij;
|
deba@2401
|
89 |
|
deba@2401
|
90 |
public:
|
deba@2401
|
91 |
|
deba@2486
|
92 |
/// \brief Constructor
|
alpar@2360
|
93 |
|
deba@2486
|
94 |
///Constructor
|
alpar@2360
|
95 |
///
|
deba@2401
|
96 |
ConstrainedShortestPath(const Graph &g, const CM &ct, const DM &dl)
|
deba@2386
|
97 |
: _g(g), _cost(ct), _delay(dl),
|
deba@2401
|
98 |
_co_map(ct, dl), _dij(_g,_co_map) {}
|
alpar@2360
|
99 |
|
alpar@2360
|
100 |
|
alpar@2360
|
101 |
///Compute the cost of a path
|
deba@2401
|
102 |
double cost(const Path &p) const
|
alpar@2360
|
103 |
{
|
alpar@2360
|
104 |
double s=0;
|
alpar@2360
|
105 |
// Path r;
|
alpar@2360
|
106 |
for(typename Path::EdgeIt e(p);e!=INVALID;++e) s+=_cost[e];
|
alpar@2360
|
107 |
return s;
|
alpar@2360
|
108 |
}
|
alpar@2360
|
109 |
|
alpar@2360
|
110 |
///Compute the delay of a path
|
deba@2401
|
111 |
double delay(const Path &p) const
|
alpar@2360
|
112 |
{
|
alpar@2360
|
113 |
double s=0;
|
alpar@2360
|
114 |
for(typename Path::EdgeIt e(p);e!=INVALID;++e) s+=_delay[e];
|
alpar@2360
|
115 |
return s;
|
alpar@2360
|
116 |
}
|
alpar@2360
|
117 |
|
alpar@2360
|
118 |
///Runs the LARAC algorithm
|
alpar@2360
|
119 |
|
alpar@2360
|
120 |
///This function runs a Lagrange relaxation based heuristic to find
|
alpar@2360
|
121 |
///a delay constrained least cost path.
|
alpar@2360
|
122 |
///\param s source node
|
alpar@2360
|
123 |
///\param t target node
|
deba@2486
|
124 |
///\param delta upper bound on the delta
|
alpar@2360
|
125 |
///\retval lo_bo a lower bound on the optimal solution
|
alpar@2360
|
126 |
///\return the found path or an empty
|
alpar@2360
|
127 |
Path larac(Node s, Node t, double delta, double &lo_bo)
|
alpar@2360
|
128 |
{
|
alpar@2360
|
129 |
double lambda=0;
|
alpar@2360
|
130 |
double cp,cq,dp,dq,cr,dr;
|
alpar@2360
|
131 |
Path p;
|
alpar@2360
|
132 |
Path q;
|
alpar@2360
|
133 |
Path r;
|
alpar@2360
|
134 |
{
|
alpar@2360
|
135 |
Dijkstra<Graph,CM> dij(_g,_cost);
|
alpar@2360
|
136 |
dij.run(s,t);
|
alpar@2360
|
137 |
if(!dij.reached(t)) return Path();
|
alpar@2360
|
138 |
p=dij.path(t);
|
alpar@2360
|
139 |
cp=cost(p);
|
alpar@2360
|
140 |
dp=delay(p);
|
alpar@2360
|
141 |
}
|
alpar@2360
|
142 |
if(delay(p)<=delta) return p;
|
alpar@2360
|
143 |
{
|
alpar@2360
|
144 |
Dijkstra<Graph,DM> dij(_g,_delay);
|
alpar@2360
|
145 |
dij.run(s,t);
|
alpar@2360
|
146 |
q=dij.path(t);
|
alpar@2360
|
147 |
cq=cost(q);
|
alpar@2360
|
148 |
dq=delay(q);
|
alpar@2360
|
149 |
}
|
alpar@2360
|
150 |
if(delay(q)>delta) return Path();
|
alpar@2360
|
151 |
while (true) {
|
alpar@2360
|
152 |
lambda=(cp-cq)/(dq-dp);
|
alpar@2360
|
153 |
_co_map.lambda(lambda);
|
alpar@2360
|
154 |
_dij.run(s,t);
|
alpar@2360
|
155 |
r=_dij.path(t);
|
alpar@2360
|
156 |
cr=cost(r);
|
alpar@2360
|
157 |
dr=delay(r);
|
alpar@2360
|
158 |
if(!tol.less(cr+lambda*dr,cp+lambda*dp)) {
|
alpar@2360
|
159 |
lo_bo=cq+lambda*(dq-delta);
|
alpar@2360
|
160 |
return q;
|
alpar@2360
|
161 |
}
|
alpar@2360
|
162 |
else if(tol.less(dr,delta))
|
alpar@2360
|
163 |
{
|
alpar@2360
|
164 |
q=r;
|
alpar@2360
|
165 |
cq=cr;
|
alpar@2360
|
166 |
dq=dr;
|
alpar@2360
|
167 |
}
|
alpar@2360
|
168 |
else if(tol.less(delta,dr))
|
alpar@2360
|
169 |
{
|
alpar@2360
|
170 |
p=r;
|
alpar@2360
|
171 |
cp=cr;
|
alpar@2360
|
172 |
dp=dr;
|
alpar@2360
|
173 |
}
|
alpar@2360
|
174 |
else return r;
|
alpar@2360
|
175 |
}
|
alpar@2360
|
176 |
}
|
alpar@2360
|
177 |
};
|
alpar@2360
|
178 |
|
alpar@2360
|
179 |
|
alpar@2360
|
180 |
} //END OF NAMESPACE LEMON
|
alpar@2360
|
181 |
|
alpar@2360
|
182 |
#endif
|