lemon/csp.h
author deba
Thu, 15 Feb 2007 13:06:23 +0000
changeset 2362 eb37b9774ef6
child 2373 134639e6ea45
permissions -rw-r--r--
Small changes
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
alpar@2360
    22
///\ingroup flowalgs
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 {
alpar@2360
    38
  
alpar@2360
    39
  ///Algorithms for the Resource Constrained Shortest Path Problem
alpar@2360
    40
  
alpar@2360
    41
  ///\e
alpar@2360
    42
  ///
alpar@2360
    43
  template<class Graph,
alpar@2360
    44
	   class CM=typename Graph:: template EdgeMap<double>,
alpar@2360
    45
	   class DM=CM>
alpar@2360
    46
  class ConstrainedShortestPath 
alpar@2360
    47
  {
alpar@2360
    48
  public:
alpar@2360
    49
    
alpar@2360
    50
    GRAPH_TYPEDEFS(typename Graph);
alpar@2360
    51
    
alpar@2360
    52
    typedef SimplePath<Graph> Path;
alpar@2360
    53
    
alpar@2360
    54
    Graph &_g;
alpar@2360
    55
    Tolerance<double> tol;
alpar@2360
    56
alpar@2360
    57
    CM &_cost;
alpar@2360
    58
    DM &_delay;
alpar@2360
    59
alpar@2360
    60
    class CoMap 
alpar@2360
    61
    {
alpar@2360
    62
      CM &_cost;
alpar@2360
    63
      DM &_delay;
alpar@2360
    64
      double _lambda;
alpar@2360
    65
    public:
alpar@2360
    66
      typedef typename CM::Key Key;
alpar@2360
    67
      typedef double Value;
alpar@2360
    68
      CoMap(CM &c,DM &d) :_cost(c), _delay(d), _lambda(0) {}
alpar@2360
    69
      double lambda() const { return _lambda; }
alpar@2360
    70
      void lambda(double l)  { _lambda=l; }
alpar@2360
    71
      Value operator[](Key &e) const 
alpar@2360
    72
      {
alpar@2360
    73
	return _cost[e]+_lambda*_delay[e];
alpar@2360
    74
      }
alpar@2360
    75
    } _co_map;
alpar@2360
    76
    
alpar@2360
    77
    
alpar@2360
    78
    Dijkstra<Graph, CoMap> _dij;
alpar@2360
    79
    ///\e
alpar@2360
    80
    
alpar@2360
    81
    ///\e
alpar@2360
    82
    ///
alpar@2360
    83
    ConstrainedShortestPath(Graph &g, CM &cost, DM &delay)
alpar@2360
    84
      : _g(g), _cost(cost), _delay(delay),
alpar@2360
    85
	_co_map(cost,delay), _dij(_g,_co_map) {}
alpar@2360
    86
    
alpar@2360
    87
alpar@2360
    88
    ///Compute the cost of a path
alpar@2360
    89
    double cost(const Path &p) 
alpar@2360
    90
    {
alpar@2360
    91
      double s=0;
alpar@2360
    92
      //      Path r;  
alpar@2360
    93
      for(typename Path::EdgeIt e(p);e!=INVALID;++e) s+=_cost[e];
alpar@2360
    94
      return s;
alpar@2360
    95
    }
alpar@2360
    96
alpar@2360
    97
    ///Compute the delay of a path
alpar@2360
    98
    double delay(const Path &p) 
alpar@2360
    99
    {
alpar@2360
   100
      double s=0;
alpar@2360
   101
      for(typename Path::EdgeIt e(p);e!=INVALID;++e) s+=_delay[e];
alpar@2360
   102
      return s;
alpar@2360
   103
    }
alpar@2360
   104
    
alpar@2360
   105
    ///Runs the LARAC algorithm
alpar@2360
   106
    
alpar@2360
   107
    ///This function runs a Lagrange relaxation based heuristic to find
alpar@2360
   108
    ///a delay constrained least cost path.
alpar@2360
   109
    ///\param s source node
alpar@2360
   110
    ///\param t target node
alpar@2360
   111
    ///\retval lo_bo a lower bound on the optimal solution
alpar@2360
   112
    ///\return the found path or an empty 
alpar@2360
   113
    Path larac(Node s, Node t, double delta, double &lo_bo) 
alpar@2360
   114
    {
alpar@2360
   115
      NoCounter cnt("LARAC iterations: ");
alpar@2360
   116
      double lambda=0;
alpar@2360
   117
      double cp,cq,dp,dq,cr,dr;
alpar@2360
   118
      Path p;
alpar@2360
   119
      Path q;
alpar@2360
   120
      Path r;
alpar@2360
   121
      {
alpar@2360
   122
	Dijkstra<Graph,CM> dij(_g,_cost);
alpar@2360
   123
	dij.run(s,t);
alpar@2360
   124
	cnt++;
alpar@2360
   125
	if(!dij.reached(t)) return Path();
alpar@2360
   126
	p=dij.path(t);
alpar@2360
   127
	cp=cost(p);
alpar@2360
   128
	dp=delay(p);
alpar@2360
   129
      }
alpar@2360
   130
      if(delay(p)<=delta) return p;
alpar@2360
   131
      {
alpar@2360
   132
	Dijkstra<Graph,DM> dij(_g,_delay);
alpar@2360
   133
	dij.run(s,t);
alpar@2360
   134
	cnt++;
alpar@2360
   135
	q=dij.path(t);
alpar@2360
   136
	cq=cost(q);
alpar@2360
   137
	dq=delay(q);
alpar@2360
   138
      }
alpar@2360
   139
      if(delay(q)>delta) return Path();
alpar@2360
   140
      while (true) {
alpar@2360
   141
	lambda=(cp-cq)/(dq-dp);
alpar@2360
   142
	_co_map.lambda(lambda);
alpar@2360
   143
	_dij.run(s,t);
alpar@2360
   144
	cnt++;
alpar@2360
   145
	r=_dij.path(t);
alpar@2360
   146
	cr=cost(r);
alpar@2360
   147
	dr=delay(r);
alpar@2360
   148
	if(!tol.less(cr+lambda*dr,cp+lambda*dp)) {
alpar@2360
   149
	  lo_bo=cq+lambda*(dq-delta);
alpar@2360
   150
	  return q;
alpar@2360
   151
	}
alpar@2360
   152
	else if(tol.less(dr,delta)) 
alpar@2360
   153
	  {
alpar@2360
   154
	    q=r;
alpar@2360
   155
	    cq=cr;
alpar@2360
   156
	    dq=dr;
alpar@2360
   157
	  }
alpar@2360
   158
	else if(tol.less(delta,dr))
alpar@2360
   159
	  {
alpar@2360
   160
	    p=r;
alpar@2360
   161
	    cp=cr;
alpar@2360
   162
	    dp=dr;
alpar@2360
   163
	  }
alpar@2360
   164
	else return r;
alpar@2360
   165
      }
alpar@2360
   166
    }
alpar@2360
   167
  };
alpar@2360
   168
  
alpar@2360
   169
alpar@2360
   170
} //END OF NAMESPACE LEMON
alpar@2360
   171
alpar@2360
   172
#endif