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