lemon/network_simplex.h
branch1.1
changeset 1081 f1398882a928
parent 976 5205145fabf6
equal deleted inserted replaced
30:832716532a52 37:f98bddf787e5
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2009
     5  * Copyright (C) 2003-2011
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
     9  * Permission to use, modify and distribute this software is granted
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    10  * provided that this copyright notice appears in all copies. For
    93       /// The objective function of the problem is unbounded, i.e.
    93       /// The objective function of the problem is unbounded, i.e.
    94       /// there is a directed cycle having negative total cost and
    94       /// there is a directed cycle having negative total cost and
    95       /// infinite upper bound.
    95       /// infinite upper bound.
    96       UNBOUNDED
    96       UNBOUNDED
    97     };
    97     };
    98     
    98 
    99     /// \brief Constants for selecting the type of the supply constraints.
    99     /// \brief Constants for selecting the type of the supply constraints.
   100     ///
   100     ///
   101     /// Enum type containing constants for selecting the supply type,
   101     /// Enum type containing constants for selecting the supply type,
   102     /// i.e. the direction of the inequalities in the supply/demand
   102     /// i.e. the direction of the inequalities in the supply/demand
   103     /// constraints of the \ref min_cost_flow "minimum cost flow problem".
   103     /// constraints of the \ref min_cost_flow "minimum cost flow problem".
   111       GEQ,
   111       GEQ,
   112       /// This option means that there are <em>"less or equal"</em>
   112       /// This option means that there are <em>"less or equal"</em>
   113       /// supply/demand constraints in the definition of the problem.
   113       /// supply/demand constraints in the definition of the problem.
   114       LEQ
   114       LEQ
   115     };
   115     };
   116     
   116 
   117     /// \brief Constants for selecting the pivot rule.
   117     /// \brief Constants for selecting the pivot rule.
   118     ///
   118     ///
   119     /// Enum type containing constants for selecting the pivot rule for
   119     /// Enum type containing constants for selecting the pivot rule for
   120     /// the \ref run() function.
   120     /// the \ref run() function.
   121     ///
   121     ///
   154       /// It is a modified version of the Candidate List method.
   154       /// It is a modified version of the Candidate List method.
   155       /// It keeps only the several best eligible arcs from the former
   155       /// It keeps only the several best eligible arcs from the former
   156       /// candidate list and extends this list in every iteration.
   156       /// candidate list and extends this list in every iteration.
   157       ALTERING_LIST
   157       ALTERING_LIST
   158     };
   158     };
   159     
   159 
   160   private:
   160   private:
   161 
   161 
   162     TEMPLATE_DIGRAPH_TYPEDEFS(GR);
   162     TEMPLATE_DIGRAPH_TYPEDEFS(GR);
   163 
   163 
   164     typedef std::vector<Arc> ArcVector;
   164     typedef std::vector<Arc> ArcVector;
   221     int first, second, right, last;
   221     int first, second, right, last;
   222     int stem, par_stem, new_stem;
   222     int stem, par_stem, new_stem;
   223     Value delta;
   223     Value delta;
   224 
   224 
   225   public:
   225   public:
   226   
   226 
   227     /// \brief Constant for infinite upper bounds (capacities).
   227     /// \brief Constant for infinite upper bounds (capacities).
   228     ///
   228     ///
   229     /// Constant for infinite upper bounds (capacities).
   229     /// Constant for infinite upper bounds (capacities).
   230     /// It is \c std::numeric_limits<Value>::infinity() if available,
   230     /// It is \c std::numeric_limits<Value>::infinity() if available,
   231     /// \c std::numeric_limits<Value>::max() otherwise.
   231     /// \c std::numeric_limits<Value>::max() otherwise.
   642       // Check the value types
   642       // Check the value types
   643       LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
   643       LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
   644         "The flow type of NetworkSimplex must be signed");
   644         "The flow type of NetworkSimplex must be signed");
   645       LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
   645       LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
   646         "The cost type of NetworkSimplex must be signed");
   646         "The cost type of NetworkSimplex must be signed");
   647         
   647 
   648       // Resize vectors
   648       // Resize vectors
   649       _node_num = countNodes(_graph);
   649       _node_num = countNodes(_graph);
   650       _arc_num = countArcs(_graph);
   650       _arc_num = countArcs(_graph);
   651       int all_node_num = _node_num + 1;
   651       int all_node_num = _node_num + 1;
   652       int max_arc_num = _arc_num + 2 * _node_num;
   652       int max_arc_num = _arc_num + 2 * _node_num;
   682         _arc_id[a] = i;
   682         _arc_id[a] = i;
   683         _source[i] = _node_id[_graph.source(a)];
   683         _source[i] = _node_id[_graph.source(a)];
   684         _target[i] = _node_id[_graph.target(a)];
   684         _target[i] = _node_id[_graph.target(a)];
   685         if ((i += k) >= _arc_num) i = (i % k) + 1;
   685         if ((i += k) >= _arc_num) i = (i % k) + 1;
   686       }
   686       }
   687       
   687 
   688       // Initialize maps
   688       // Initialize maps
   689       for (int i = 0; i != _node_num; ++i) {
   689       for (int i = 0; i != _node_num; ++i) {
   690         _supply[i] = 0;
   690         _supply[i] = 0;
   691       }
   691       }
   692       for (int i = 0; i != _arc_num; ++i) {
   692       for (int i = 0; i != _arc_num; ++i) {
   807       }
   807       }
   808       _supply[_node_id[s]] =  k;
   808       _supply[_node_id[s]] =  k;
   809       _supply[_node_id[t]] = -k;
   809       _supply[_node_id[t]] = -k;
   810       return *this;
   810       return *this;
   811     }
   811     }
   812     
   812 
   813     /// \brief Set the type of the supply constraints.
   813     /// \brief Set the type of the supply constraints.
   814     ///
   814     ///
   815     /// This function sets the type of the supply/demand constraints.
   815     /// This function sets the type of the supply/demand constraints.
   816     /// If it is not used before calling \ref run(), the \ref GEQ supply
   816     /// If it is not used before calling \ref run(), the \ref GEQ supply
   817     /// type will be used.
   817     /// type will be used.
   833 
   833 
   834     /// \brief Run the algorithm.
   834     /// \brief Run the algorithm.
   835     ///
   835     ///
   836     /// This function runs the algorithm.
   836     /// This function runs the algorithm.
   837     /// The paramters can be specified using functions \ref lowerMap(),
   837     /// The paramters can be specified using functions \ref lowerMap(),
   838     /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), 
   838     /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(),
   839     /// \ref supplyType().
   839     /// \ref supplyType().
   840     /// For example,
   840     /// For example,
   841     /// \code
   841     /// \code
   842     ///   NetworkSimplex<ListDigraph> ns(graph);
   842     ///   NetworkSimplex<ListDigraph> ns(graph);
   843     ///   ns.lowerMap(lower).upperMap(upper).costMap(cost)
   843     ///   ns.lowerMap(lower).upperMap(upper).costMap(cost)
  1052       // Initialize arc maps
  1052       // Initialize arc maps
  1053       for (int i = 0; i != _arc_num; ++i) {
  1053       for (int i = 0; i != _arc_num; ++i) {
  1054         _flow[i] = 0;
  1054         _flow[i] = 0;
  1055         _state[i] = STATE_LOWER;
  1055         _state[i] = STATE_LOWER;
  1056       }
  1056       }
  1057       
  1057 
  1058       // Set data for the artificial root node
  1058       // Set data for the artificial root node
  1059       _root = _node_num;
  1059       _root = _node_num;
  1060       _parent[_root] = -1;
  1060       _parent[_root] = -1;
  1061       _pred[_root] = -1;
  1061       _pred[_root] = -1;
  1062       _thread[_root] = 0;
  1062       _thread[_root] = 0;
  1226         }
  1226         }
  1227       }
  1227       }
  1228       // Search the cycle along the path form the second node to the root
  1228       // Search the cycle along the path form the second node to the root
  1229       for (int u = second; u != join; u = _parent[u]) {
  1229       for (int u = second; u != join; u = _parent[u]) {
  1230         e = _pred[u];
  1230         e = _pred[u];
  1231         d = _forward[u] ? 
  1231         d = _forward[u] ?
  1232           (_cap[e] == INF ? INF : _cap[e] - _flow[e]) : _flow[e];
  1232           (_cap[e] == INF ? INF : _cap[e] - _flow[e]) : _flow[e];
  1233         if (d <= delta) {
  1233         if (d <= delta) {
  1234           delta = d;
  1234           delta = d;
  1235           u_out = u;
  1235           u_out = u;
  1236           result = 2;
  1236           result = 2;
  1433         if (change) {
  1433         if (change) {
  1434           updateTreeStructure();
  1434           updateTreeStructure();
  1435           updatePotential();
  1435           updatePotential();
  1436         }
  1436         }
  1437       }
  1437       }
  1438       
  1438 
  1439       // Check feasibility
  1439       // Check feasibility
  1440       for (int e = _search_arc_num; e != _all_arc_num; ++e) {
  1440       for (int e = _search_arc_num; e != _all_arc_num; ++e) {
  1441         if (_flow[e] != 0) return INFEASIBLE;
  1441         if (_flow[e] != 0) return INFEASIBLE;
  1442       }
  1442       }
  1443 
  1443 
  1450             _supply[_source[i]] += c;
  1450             _supply[_source[i]] += c;
  1451             _supply[_target[i]] -= c;
  1451             _supply[_target[i]] -= c;
  1452           }
  1452           }
  1453         }
  1453         }
  1454       }
  1454       }
  1455       
  1455 
  1456       // Shift potentials to meet the requirements of the GEQ/LEQ type
  1456       // Shift potentials to meet the requirements of the GEQ/LEQ type
  1457       // optimality conditions
  1457       // optimality conditions
  1458       if (_sum_supply == 0) {
  1458       if (_sum_supply == 0) {
  1459         if (_stype == GEQ) {
  1459         if (_stype == GEQ) {
  1460           Cost max_pot = -std::numeric_limits<Cost>::max();
  1460           Cost max_pot = -std::numeric_limits<Cost>::max();