COIN-OR::LEMON - Graph Library

Changes in / [829:7762cab7f372:831:cc9e0c15d747] in lemon-main


Ignore:
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • lemon/capacity_scaling.h

    r825 r831  
    320320        "The cost type of CapacityScaling must be signed");
    321321
     322      // Reset data structures
     323      reset();
     324    }
     325
     326    /// \name Parameters
     327    /// The parameters of the algorithm can be specified using these
     328    /// functions.
     329
     330    /// @{
     331
     332    /// \brief Set the lower bounds on the arcs.
     333    ///
     334    /// This function sets the lower bounds on the arcs.
     335    /// If it is not used before calling \ref run(), the lower bounds
     336    /// will be set to zero on all arcs.
     337    ///
     338    /// \param map An arc map storing the lower bounds.
     339    /// Its \c Value type must be convertible to the \c Value type
     340    /// of the algorithm.
     341    ///
     342    /// \return <tt>(*this)</tt>
     343    template <typename LowerMap>
     344    CapacityScaling& lowerMap(const LowerMap& map) {
     345      _have_lower = true;
     346      for (ArcIt a(_graph); a != INVALID; ++a) {
     347        _lower[_arc_idf[a]] = map[a];
     348        _lower[_arc_idb[a]] = map[a];
     349      }
     350      return *this;
     351    }
     352
     353    /// \brief Set the upper bounds (capacities) on the arcs.
     354    ///
     355    /// This function sets the upper bounds (capacities) on the arcs.
     356    /// If it is not used before calling \ref run(), the upper bounds
     357    /// will be set to \ref INF on all arcs (i.e. the flow value will be
     358    /// unbounded from above).
     359    ///
     360    /// \param map An arc map storing the upper bounds.
     361    /// Its \c Value type must be convertible to the \c Value type
     362    /// of the algorithm.
     363    ///
     364    /// \return <tt>(*this)</tt>
     365    template<typename UpperMap>
     366    CapacityScaling& upperMap(const UpperMap& map) {
     367      for (ArcIt a(_graph); a != INVALID; ++a) {
     368        _upper[_arc_idf[a]] = map[a];
     369      }
     370      return *this;
     371    }
     372
     373    /// \brief Set the costs of the arcs.
     374    ///
     375    /// This function sets the costs of the arcs.
     376    /// If it is not used before calling \ref run(), the costs
     377    /// will be set to \c 1 on all arcs.
     378    ///
     379    /// \param map An arc map storing the costs.
     380    /// Its \c Value type must be convertible to the \c Cost type
     381    /// of the algorithm.
     382    ///
     383    /// \return <tt>(*this)</tt>
     384    template<typename CostMap>
     385    CapacityScaling& costMap(const CostMap& map) {
     386      for (ArcIt a(_graph); a != INVALID; ++a) {
     387        _cost[_arc_idf[a]] =  map[a];
     388        _cost[_arc_idb[a]] = -map[a];
     389      }
     390      return *this;
     391    }
     392
     393    /// \brief Set the supply values of the nodes.
     394    ///
     395    /// This function sets the supply values of the nodes.
     396    /// If neither this function nor \ref stSupply() is used before
     397    /// calling \ref run(), the supply of each node will be set to zero.
     398    ///
     399    /// \param map A node map storing the supply values.
     400    /// Its \c Value type must be convertible to the \c Value type
     401    /// of the algorithm.
     402    ///
     403    /// \return <tt>(*this)</tt>
     404    template<typename SupplyMap>
     405    CapacityScaling& supplyMap(const SupplyMap& map) {
     406      for (NodeIt n(_graph); n != INVALID; ++n) {
     407        _supply[_node_id[n]] = map[n];
     408      }
     409      return *this;
     410    }
     411
     412    /// \brief Set single source and target nodes and a supply value.
     413    ///
     414    /// This function sets a single source node and a single target node
     415    /// and the required flow value.
     416    /// If neither this function nor \ref supplyMap() is used before
     417    /// calling \ref run(), the supply of each node will be set to zero.
     418    ///
     419    /// Using this function has the same effect as using \ref supplyMap()
     420    /// with such a map in which \c k is assigned to \c s, \c -k is
     421    /// assigned to \c t and all other nodes have zero supply value.
     422    ///
     423    /// \param s The source node.
     424    /// \param t The target node.
     425    /// \param k The required amount of flow from node \c s to node \c t
     426    /// (i.e. the supply of \c s and the demand of \c t).
     427    ///
     428    /// \return <tt>(*this)</tt>
     429    CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
     430      for (int i = 0; i != _node_num; ++i) {
     431        _supply[i] = 0;
     432      }
     433      _supply[_node_id[s]] =  k;
     434      _supply[_node_id[t]] = -k;
     435      return *this;
     436    }
     437   
     438    /// @}
     439
     440    /// \name Execution control
     441    /// The algorithm can be executed using \ref run().
     442
     443    /// @{
     444
     445    /// \brief Run the algorithm.
     446    ///
     447    /// This function runs the algorithm.
     448    /// The paramters can be specified using functions \ref lowerMap(),
     449    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
     450    /// For example,
     451    /// \code
     452    ///   CapacityScaling<ListDigraph> cs(graph);
     453    ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
     454    ///     .supplyMap(sup).run();
     455    /// \endcode
     456    ///
     457    /// This function can be called more than once. All the given parameters
     458    /// are kept for the next call, unless \ref resetParams() or \ref reset()
     459    /// is used, thus only the modified parameters have to be set again.
     460    /// If the underlying digraph was also modified after the construction
     461    /// of the class (or the last \ref reset() call), then the \ref reset()
     462    /// function must be called.
     463    ///
     464    /// \param factor The capacity scaling factor. It must be larger than
     465    /// one to use scaling. If it is less or equal to one, then scaling
     466    /// will be disabled.
     467    ///
     468    /// \return \c INFEASIBLE if no feasible flow exists,
     469    /// \n \c OPTIMAL if the problem has optimal solution
     470    /// (i.e. it is feasible and bounded), and the algorithm has found
     471    /// optimal flow and node potentials (primal and dual solutions),
     472    /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
     473    /// and infinite upper bound. It means that the objective function
     474    /// is unbounded on that arc, however, note that it could actually be
     475    /// bounded over the feasible flows, but this algroithm cannot handle
     476    /// these cases.
     477    ///
     478    /// \see ProblemType
     479    /// \see resetParams(), reset()
     480    ProblemType run(int factor = 4) {
     481      _factor = factor;
     482      ProblemType pt = init();
     483      if (pt != OPTIMAL) return pt;
     484      return start();
     485    }
     486
     487    /// \brief Reset all the parameters that have been given before.
     488    ///
     489    /// This function resets all the paramaters that have been given
     490    /// before using functions \ref lowerMap(), \ref upperMap(),
     491    /// \ref costMap(), \ref supplyMap(), \ref stSupply().
     492    ///
     493    /// It is useful for multiple \ref run() calls. Basically, all the given
     494    /// parameters are kept for the next \ref run() call, unless
     495    /// \ref resetParams() or \ref reset() is used.
     496    /// If the underlying digraph was also modified after the construction
     497    /// of the class or the last \ref reset() call, then the \ref reset()
     498    /// function must be used, otherwise \ref resetParams() is sufficient.
     499    ///
     500    /// For example,
     501    /// \code
     502    ///   CapacityScaling<ListDigraph> cs(graph);
     503    ///
     504    ///   // First run
     505    ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
     506    ///     .supplyMap(sup).run();
     507    ///
     508    ///   // Run again with modified cost map (resetParams() is not called,
     509    ///   // so only the cost map have to be set again)
     510    ///   cost[e] += 100;
     511    ///   cs.costMap(cost).run();
     512    ///
     513    ///   // Run again from scratch using resetParams()
     514    ///   // (the lower bounds will be set to zero on all arcs)
     515    ///   cs.resetParams();
     516    ///   cs.upperMap(capacity).costMap(cost)
     517    ///     .supplyMap(sup).run();
     518    /// \endcode
     519    ///
     520    /// \return <tt>(*this)</tt>
     521    ///
     522    /// \see reset(), run()
     523    CapacityScaling& resetParams() {
     524      for (int i = 0; i != _node_num; ++i) {
     525        _supply[i] = 0;
     526      }
     527      for (int j = 0; j != _res_arc_num; ++j) {
     528        _lower[j] = 0;
     529        _upper[j] = INF;
     530        _cost[j] = _forward[j] ? 1 : -1;
     531      }
     532      _have_lower = false;
     533      return *this;
     534    }
     535
     536    /// \brief Reset the internal data structures and all the parameters
     537    /// that have been given before.
     538    ///
     539    /// This function resets the internal data structures and all the
     540    /// paramaters that have been given before using functions \ref lowerMap(),
     541    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
     542    ///
     543    /// It is useful for multiple \ref run() calls. Basically, all the given
     544    /// parameters are kept for the next \ref run() call, unless
     545    /// \ref resetParams() or \ref reset() is used.
     546    /// If the underlying digraph was also modified after the construction
     547    /// of the class or the last \ref reset() call, then the \ref reset()
     548    /// function must be used, otherwise \ref resetParams() is sufficient.
     549    ///
     550    /// See \ref resetParams() for examples.
     551    ///
     552    /// \return <tt>(*this)</tt>
     553    ///
     554    /// \see resetParams(), run()
     555    CapacityScaling& reset() {
    322556      // Resize vectors
    323557      _node_num = countNodes(_graph);
     
    383617     
    384618      // Reset parameters
    385       reset();
    386     }
    387 
    388     /// \name Parameters
    389     /// The parameters of the algorithm can be specified using these
    390     /// functions.
    391 
    392     /// @{
    393 
    394     /// \brief Set the lower bounds on the arcs.
    395     ///
    396     /// This function sets the lower bounds on the arcs.
    397     /// If it is not used before calling \ref run(), the lower bounds
    398     /// will be set to zero on all arcs.
    399     ///
    400     /// \param map An arc map storing the lower bounds.
    401     /// Its \c Value type must be convertible to the \c Value type
    402     /// of the algorithm.
    403     ///
    404     /// \return <tt>(*this)</tt>
    405     template <typename LowerMap>
    406     CapacityScaling& lowerMap(const LowerMap& map) {
    407       _have_lower = true;
    408       for (ArcIt a(_graph); a != INVALID; ++a) {
    409         _lower[_arc_idf[a]] = map[a];
    410         _lower[_arc_idb[a]] = map[a];
    411       }
    412       return *this;
    413     }
    414 
    415     /// \brief Set the upper bounds (capacities) on the arcs.
    416     ///
    417     /// This function sets the upper bounds (capacities) on the arcs.
    418     /// If it is not used before calling \ref run(), the upper bounds
    419     /// will be set to \ref INF on all arcs (i.e. the flow value will be
    420     /// unbounded from above).
    421     ///
    422     /// \param map An arc map storing the upper bounds.
    423     /// Its \c Value type must be convertible to the \c Value type
    424     /// of the algorithm.
    425     ///
    426     /// \return <tt>(*this)</tt>
    427     template<typename UpperMap>
    428     CapacityScaling& upperMap(const UpperMap& map) {
    429       for (ArcIt a(_graph); a != INVALID; ++a) {
    430         _upper[_arc_idf[a]] = map[a];
    431       }
    432       return *this;
    433     }
    434 
    435     /// \brief Set the costs of the arcs.
    436     ///
    437     /// This function sets the costs of the arcs.
    438     /// If it is not used before calling \ref run(), the costs
    439     /// will be set to \c 1 on all arcs.
    440     ///
    441     /// \param map An arc map storing the costs.
    442     /// Its \c Value type must be convertible to the \c Cost type
    443     /// of the algorithm.
    444     ///
    445     /// \return <tt>(*this)</tt>
    446     template<typename CostMap>
    447     CapacityScaling& costMap(const CostMap& map) {
    448       for (ArcIt a(_graph); a != INVALID; ++a) {
    449         _cost[_arc_idf[a]] =  map[a];
    450         _cost[_arc_idb[a]] = -map[a];
    451       }
    452       return *this;
    453     }
    454 
    455     /// \brief Set the supply values of the nodes.
    456     ///
    457     /// This function sets the supply values of the nodes.
    458     /// If neither this function nor \ref stSupply() is used before
    459     /// calling \ref run(), the supply of each node will be set to zero.
    460     ///
    461     /// \param map A node map storing the supply values.
    462     /// Its \c Value type must be convertible to the \c Value type
    463     /// of the algorithm.
    464     ///
    465     /// \return <tt>(*this)</tt>
    466     template<typename SupplyMap>
    467     CapacityScaling& supplyMap(const SupplyMap& map) {
    468       for (NodeIt n(_graph); n != INVALID; ++n) {
    469         _supply[_node_id[n]] = map[n];
    470       }
    471       return *this;
    472     }
    473 
    474     /// \brief Set single source and target nodes and a supply value.
    475     ///
    476     /// This function sets a single source node and a single target node
    477     /// and the required flow value.
    478     /// If neither this function nor \ref supplyMap() is used before
    479     /// calling \ref run(), the supply of each node will be set to zero.
    480     ///
    481     /// Using this function has the same effect as using \ref supplyMap()
    482     /// with such a map in which \c k is assigned to \c s, \c -k is
    483     /// assigned to \c t and all other nodes have zero supply value.
    484     ///
    485     /// \param s The source node.
    486     /// \param t The target node.
    487     /// \param k The required amount of flow from node \c s to node \c t
    488     /// (i.e. the supply of \c s and the demand of \c t).
    489     ///
    490     /// \return <tt>(*this)</tt>
    491     CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
    492       for (int i = 0; i != _node_num; ++i) {
    493         _supply[i] = 0;
    494       }
    495       _supply[_node_id[s]] =  k;
    496       _supply[_node_id[t]] = -k;
    497       return *this;
    498     }
    499    
    500     /// @}
    501 
    502     /// \name Execution control
    503     /// The algorithm can be executed using \ref run().
    504 
    505     /// @{
    506 
    507     /// \brief Run the algorithm.
    508     ///
    509     /// This function runs the algorithm.
    510     /// The paramters can be specified using functions \ref lowerMap(),
    511     /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
    512     /// For example,
    513     /// \code
    514     ///   CapacityScaling<ListDigraph> cs(graph);
    515     ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
    516     ///     .supplyMap(sup).run();
    517     /// \endcode
    518     ///
    519     /// This function can be called more than once. All the parameters
    520     /// that have been given are kept for the next call, unless
    521     /// \ref reset() is called, thus only the modified parameters
    522     /// have to be set again. See \ref reset() for examples.
    523     /// However, the underlying digraph must not be modified after this
    524     /// class have been constructed, since it copies and extends the graph.
    525     ///
    526     /// \param factor The capacity scaling factor. It must be larger than
    527     /// one to use scaling. If it is less or equal to one, then scaling
    528     /// will be disabled.
    529     ///
    530     /// \return \c INFEASIBLE if no feasible flow exists,
    531     /// \n \c OPTIMAL if the problem has optimal solution
    532     /// (i.e. it is feasible and bounded), and the algorithm has found
    533     /// optimal flow and node potentials (primal and dual solutions),
    534     /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
    535     /// and infinite upper bound. It means that the objective function
    536     /// is unbounded on that arc, however, note that it could actually be
    537     /// bounded over the feasible flows, but this algroithm cannot handle
    538     /// these cases.
    539     ///
    540     /// \see ProblemType
    541     ProblemType run(int factor = 4) {
    542       _factor = factor;
    543       ProblemType pt = init();
    544       if (pt != OPTIMAL) return pt;
    545       return start();
    546     }
    547 
    548     /// \brief Reset all the parameters that have been given before.
    549     ///
    550     /// This function resets all the paramaters that have been given
    551     /// before using functions \ref lowerMap(), \ref upperMap(),
    552     /// \ref costMap(), \ref supplyMap(), \ref stSupply().
    553     ///
    554     /// It is useful for multiple run() calls. If this function is not
    555     /// used, all the parameters given before are kept for the next
    556     /// \ref run() call.
    557     /// However, the underlying digraph must not be modified after this
    558     /// class have been constructed, since it copies and extends the graph.
    559     ///
    560     /// For example,
    561     /// \code
    562     ///   CapacityScaling<ListDigraph> cs(graph);
    563     ///
    564     ///   // First run
    565     ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
    566     ///     .supplyMap(sup).run();
    567     ///
    568     ///   // Run again with modified cost map (reset() is not called,
    569     ///   // so only the cost map have to be set again)
    570     ///   cost[e] += 100;
    571     ///   cs.costMap(cost).run();
    572     ///
    573     ///   // Run again from scratch using reset()
    574     ///   // (the lower bounds will be set to zero on all arcs)
    575     ///   cs.reset();
    576     ///   cs.upperMap(capacity).costMap(cost)
    577     ///     .supplyMap(sup).run();
    578     /// \endcode
    579     ///
    580     /// \return <tt>(*this)</tt>
    581     CapacityScaling& reset() {
    582       for (int i = 0; i != _node_num; ++i) {
    583         _supply[i] = 0;
    584       }
    585       for (int j = 0; j != _res_arc_num; ++j) {
    586         _lower[j] = 0;
    587         _upper[j] = INF;
    588         _cost[j] = _forward[j] ? 1 : -1;
    589       }
    590       _have_lower = false;
     619      resetParams();
    591620      return *this;
    592621    }
  • lemon/cost_scaling.h

    r825 r831  
    337337      LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
    338338        "The cost type of CostScaling must be signed");
    339 
     339     
     340      // Reset data structures
     341      reset();
     342    }
     343
     344    /// \name Parameters
     345    /// The parameters of the algorithm can be specified using these
     346    /// functions.
     347
     348    /// @{
     349
     350    /// \brief Set the lower bounds on the arcs.
     351    ///
     352    /// This function sets the lower bounds on the arcs.
     353    /// If it is not used before calling \ref run(), the lower bounds
     354    /// will be set to zero on all arcs.
     355    ///
     356    /// \param map An arc map storing the lower bounds.
     357    /// Its \c Value type must be convertible to the \c Value type
     358    /// of the algorithm.
     359    ///
     360    /// \return <tt>(*this)</tt>
     361    template <typename LowerMap>
     362    CostScaling& lowerMap(const LowerMap& map) {
     363      _have_lower = true;
     364      for (ArcIt a(_graph); a != INVALID; ++a) {
     365        _lower[_arc_idf[a]] = map[a];
     366        _lower[_arc_idb[a]] = map[a];
     367      }
     368      return *this;
     369    }
     370
     371    /// \brief Set the upper bounds (capacities) on the arcs.
     372    ///
     373    /// This function sets the upper bounds (capacities) on the arcs.
     374    /// If it is not used before calling \ref run(), the upper bounds
     375    /// will be set to \ref INF on all arcs (i.e. the flow value will be
     376    /// unbounded from above).
     377    ///
     378    /// \param map An arc map storing the upper bounds.
     379    /// Its \c Value type must be convertible to the \c Value type
     380    /// of the algorithm.
     381    ///
     382    /// \return <tt>(*this)</tt>
     383    template<typename UpperMap>
     384    CostScaling& upperMap(const UpperMap& map) {
     385      for (ArcIt a(_graph); a != INVALID; ++a) {
     386        _upper[_arc_idf[a]] = map[a];
     387      }
     388      return *this;
     389    }
     390
     391    /// \brief Set the costs of the arcs.
     392    ///
     393    /// This function sets the costs of the arcs.
     394    /// If it is not used before calling \ref run(), the costs
     395    /// will be set to \c 1 on all arcs.
     396    ///
     397    /// \param map An arc map storing the costs.
     398    /// Its \c Value type must be convertible to the \c Cost type
     399    /// of the algorithm.
     400    ///
     401    /// \return <tt>(*this)</tt>
     402    template<typename CostMap>
     403    CostScaling& costMap(const CostMap& map) {
     404      for (ArcIt a(_graph); a != INVALID; ++a) {
     405        _scost[_arc_idf[a]] =  map[a];
     406        _scost[_arc_idb[a]] = -map[a];
     407      }
     408      return *this;
     409    }
     410
     411    /// \brief Set the supply values of the nodes.
     412    ///
     413    /// This function sets the supply values of the nodes.
     414    /// If neither this function nor \ref stSupply() is used before
     415    /// calling \ref run(), the supply of each node will be set to zero.
     416    ///
     417    /// \param map A node map storing the supply values.
     418    /// Its \c Value type must be convertible to the \c Value type
     419    /// of the algorithm.
     420    ///
     421    /// \return <tt>(*this)</tt>
     422    template<typename SupplyMap>
     423    CostScaling& supplyMap(const SupplyMap& map) {
     424      for (NodeIt n(_graph); n != INVALID; ++n) {
     425        _supply[_node_id[n]] = map[n];
     426      }
     427      return *this;
     428    }
     429
     430    /// \brief Set single source and target nodes and a supply value.
     431    ///
     432    /// This function sets a single source node and a single target node
     433    /// and the required flow value.
     434    /// If neither this function nor \ref supplyMap() is used before
     435    /// calling \ref run(), the supply of each node will be set to zero.
     436    ///
     437    /// Using this function has the same effect as using \ref supplyMap()
     438    /// with such a map in which \c k is assigned to \c s, \c -k is
     439    /// assigned to \c t and all other nodes have zero supply value.
     440    ///
     441    /// \param s The source node.
     442    /// \param t The target node.
     443    /// \param k The required amount of flow from node \c s to node \c t
     444    /// (i.e. the supply of \c s and the demand of \c t).
     445    ///
     446    /// \return <tt>(*this)</tt>
     447    CostScaling& stSupply(const Node& s, const Node& t, Value k) {
     448      for (int i = 0; i != _res_node_num; ++i) {
     449        _supply[i] = 0;
     450      }
     451      _supply[_node_id[s]] =  k;
     452      _supply[_node_id[t]] = -k;
     453      return *this;
     454    }
     455   
     456    /// @}
     457
     458    /// \name Execution control
     459    /// The algorithm can be executed using \ref run().
     460
     461    /// @{
     462
     463    /// \brief Run the algorithm.
     464    ///
     465    /// This function runs the algorithm.
     466    /// The paramters can be specified using functions \ref lowerMap(),
     467    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
     468    /// For example,
     469    /// \code
     470    ///   CostScaling<ListDigraph> cs(graph);
     471    ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
     472    ///     .supplyMap(sup).run();
     473    /// \endcode
     474    ///
     475    /// This function can be called more than once. All the given parameters
     476    /// are kept for the next call, unless \ref resetParams() or \ref reset()
     477    /// is used, thus only the modified parameters have to be set again.
     478    /// If the underlying digraph was also modified after the construction
     479    /// of the class (or the last \ref reset() call), then the \ref reset()
     480    /// function must be called.
     481    ///
     482    /// \param method The internal method that will be used in the
     483    /// algorithm. For more information, see \ref Method.
     484    /// \param factor The cost scaling factor. It must be larger than one.
     485    ///
     486    /// \return \c INFEASIBLE if no feasible flow exists,
     487    /// \n \c OPTIMAL if the problem has optimal solution
     488    /// (i.e. it is feasible and bounded), and the algorithm has found
     489    /// optimal flow and node potentials (primal and dual solutions),
     490    /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
     491    /// and infinite upper bound. It means that the objective function
     492    /// is unbounded on that arc, however, note that it could actually be
     493    /// bounded over the feasible flows, but this algroithm cannot handle
     494    /// these cases.
     495    ///
     496    /// \see ProblemType, Method
     497    /// \see resetParams(), reset()
     498    ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) {
     499      _alpha = factor;
     500      ProblemType pt = init();
     501      if (pt != OPTIMAL) return pt;
     502      start(method);
     503      return OPTIMAL;
     504    }
     505
     506    /// \brief Reset all the parameters that have been given before.
     507    ///
     508    /// This function resets all the paramaters that have been given
     509    /// before using functions \ref lowerMap(), \ref upperMap(),
     510    /// \ref costMap(), \ref supplyMap(), \ref stSupply().
     511    ///
     512    /// It is useful for multiple \ref run() calls. Basically, all the given
     513    /// parameters are kept for the next \ref run() call, unless
     514    /// \ref resetParams() or \ref reset() is used.
     515    /// If the underlying digraph was also modified after the construction
     516    /// of the class or the last \ref reset() call, then the \ref reset()
     517    /// function must be used, otherwise \ref resetParams() is sufficient.
     518    ///
     519    /// For example,
     520    /// \code
     521    ///   CostScaling<ListDigraph> cs(graph);
     522    ///
     523    ///   // First run
     524    ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
     525    ///     .supplyMap(sup).run();
     526    ///
     527    ///   // Run again with modified cost map (resetParams() is not called,
     528    ///   // so only the cost map have to be set again)
     529    ///   cost[e] += 100;
     530    ///   cs.costMap(cost).run();
     531    ///
     532    ///   // Run again from scratch using resetParams()
     533    ///   // (the lower bounds will be set to zero on all arcs)
     534    ///   cs.resetParams();
     535    ///   cs.upperMap(capacity).costMap(cost)
     536    ///     .supplyMap(sup).run();
     537    /// \endcode
     538    ///
     539    /// \return <tt>(*this)</tt>
     540    ///
     541    /// \see reset(), run()
     542    CostScaling& resetParams() {
     543      for (int i = 0; i != _res_node_num; ++i) {
     544        _supply[i] = 0;
     545      }
     546      int limit = _first_out[_root];
     547      for (int j = 0; j != limit; ++j) {
     548        _lower[j] = 0;
     549        _upper[j] = INF;
     550        _scost[j] = _forward[j] ? 1 : -1;
     551      }
     552      for (int j = limit; j != _res_arc_num; ++j) {
     553        _lower[j] = 0;
     554        _upper[j] = INF;
     555        _scost[j] = 0;
     556        _scost[_reverse[j]] = 0;
     557      }     
     558      _have_lower = false;
     559      return *this;
     560    }
     561
     562    /// \brief Reset all the parameters that have been given before.
     563    ///
     564    /// This function resets all the paramaters that have been given
     565    /// before using functions \ref lowerMap(), \ref upperMap(),
     566    /// \ref costMap(), \ref supplyMap(), \ref stSupply().
     567    ///
     568    /// It is useful for multiple run() calls. If this function is not
     569    /// used, all the parameters given before are kept for the next
     570    /// \ref run() call.
     571    /// However, the underlying digraph must not be modified after this
     572    /// class have been constructed, since it copies and extends the graph.
     573    /// \return <tt>(*this)</tt>
     574    CostScaling& reset() {
    340575      // Resize vectors
    341576      _node_num = countNodes(_graph);
     
    405640     
    406641      // Reset parameters
    407       reset();
    408     }
    409 
    410     /// \name Parameters
    411     /// The parameters of the algorithm can be specified using these
    412     /// functions.
    413 
    414     /// @{
    415 
    416     /// \brief Set the lower bounds on the arcs.
    417     ///
    418     /// This function sets the lower bounds on the arcs.
    419     /// If it is not used before calling \ref run(), the lower bounds
    420     /// will be set to zero on all arcs.
    421     ///
    422     /// \param map An arc map storing the lower bounds.
    423     /// Its \c Value type must be convertible to the \c Value type
    424     /// of the algorithm.
    425     ///
    426     /// \return <tt>(*this)</tt>
    427     template <typename LowerMap>
    428     CostScaling& lowerMap(const LowerMap& map) {
    429       _have_lower = true;
    430       for (ArcIt a(_graph); a != INVALID; ++a) {
    431         _lower[_arc_idf[a]] = map[a];
    432         _lower[_arc_idb[a]] = map[a];
    433       }
    434       return *this;
    435     }
    436 
    437     /// \brief Set the upper bounds (capacities) on the arcs.
    438     ///
    439     /// This function sets the upper bounds (capacities) on the arcs.
    440     /// If it is not used before calling \ref run(), the upper bounds
    441     /// will be set to \ref INF on all arcs (i.e. the flow value will be
    442     /// unbounded from above).
    443     ///
    444     /// \param map An arc map storing the upper bounds.
    445     /// Its \c Value type must be convertible to the \c Value type
    446     /// of the algorithm.
    447     ///
    448     /// \return <tt>(*this)</tt>
    449     template<typename UpperMap>
    450     CostScaling& upperMap(const UpperMap& map) {
    451       for (ArcIt a(_graph); a != INVALID; ++a) {
    452         _upper[_arc_idf[a]] = map[a];
    453       }
    454       return *this;
    455     }
    456 
    457     /// \brief Set the costs of the arcs.
    458     ///
    459     /// This function sets the costs of the arcs.
    460     /// If it is not used before calling \ref run(), the costs
    461     /// will be set to \c 1 on all arcs.
    462     ///
    463     /// \param map An arc map storing the costs.
    464     /// Its \c Value type must be convertible to the \c Cost type
    465     /// of the algorithm.
    466     ///
    467     /// \return <tt>(*this)</tt>
    468     template<typename CostMap>
    469     CostScaling& costMap(const CostMap& map) {
    470       for (ArcIt a(_graph); a != INVALID; ++a) {
    471         _scost[_arc_idf[a]] =  map[a];
    472         _scost[_arc_idb[a]] = -map[a];
    473       }
    474       return *this;
    475     }
    476 
    477     /// \brief Set the supply values of the nodes.
    478     ///
    479     /// This function sets the supply values of the nodes.
    480     /// If neither this function nor \ref stSupply() is used before
    481     /// calling \ref run(), the supply of each node will be set to zero.
    482     ///
    483     /// \param map A node map storing the supply values.
    484     /// Its \c Value type must be convertible to the \c Value type
    485     /// of the algorithm.
    486     ///
    487     /// \return <tt>(*this)</tt>
    488     template<typename SupplyMap>
    489     CostScaling& supplyMap(const SupplyMap& map) {
    490       for (NodeIt n(_graph); n != INVALID; ++n) {
    491         _supply[_node_id[n]] = map[n];
    492       }
    493       return *this;
    494     }
    495 
    496     /// \brief Set single source and target nodes and a supply value.
    497     ///
    498     /// This function sets a single source node and a single target node
    499     /// and the required flow value.
    500     /// If neither this function nor \ref supplyMap() is used before
    501     /// calling \ref run(), the supply of each node will be set to zero.
    502     ///
    503     /// Using this function has the same effect as using \ref supplyMap()
    504     /// with such a map in which \c k is assigned to \c s, \c -k is
    505     /// assigned to \c t and all other nodes have zero supply value.
    506     ///
    507     /// \param s The source node.
    508     /// \param t The target node.
    509     /// \param k The required amount of flow from node \c s to node \c t
    510     /// (i.e. the supply of \c s and the demand of \c t).
    511     ///
    512     /// \return <tt>(*this)</tt>
    513     CostScaling& stSupply(const Node& s, const Node& t, Value k) {
    514       for (int i = 0; i != _res_node_num; ++i) {
    515         _supply[i] = 0;
    516       }
    517       _supply[_node_id[s]] =  k;
    518       _supply[_node_id[t]] = -k;
    519       return *this;
    520     }
    521    
    522     /// @}
    523 
    524     /// \name Execution control
    525     /// The algorithm can be executed using \ref run().
    526 
    527     /// @{
    528 
    529     /// \brief Run the algorithm.
    530     ///
    531     /// This function runs the algorithm.
    532     /// The paramters can be specified using functions \ref lowerMap(),
    533     /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
    534     /// For example,
    535     /// \code
    536     ///   CostScaling<ListDigraph> cs(graph);
    537     ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
    538     ///     .supplyMap(sup).run();
    539     /// \endcode
    540     ///
    541     /// This function can be called more than once. All the parameters
    542     /// that have been given are kept for the next call, unless
    543     /// \ref reset() is called, thus only the modified parameters
    544     /// have to be set again. See \ref reset() for examples.
    545     /// However, the underlying digraph must not be modified after this
    546     /// class have been constructed, since it copies and extends the graph.
    547     ///
    548     /// \param method The internal method that will be used in the
    549     /// algorithm. For more information, see \ref Method.
    550     /// \param factor The cost scaling factor. It must be larger than one.
    551     ///
    552     /// \return \c INFEASIBLE if no feasible flow exists,
    553     /// \n \c OPTIMAL if the problem has optimal solution
    554     /// (i.e. it is feasible and bounded), and the algorithm has found
    555     /// optimal flow and node potentials (primal and dual solutions),
    556     /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
    557     /// and infinite upper bound. It means that the objective function
    558     /// is unbounded on that arc, however, note that it could actually be
    559     /// bounded over the feasible flows, but this algroithm cannot handle
    560     /// these cases.
    561     ///
    562     /// \see ProblemType, Method
    563     ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) {
    564       _alpha = factor;
    565       ProblemType pt = init();
    566       if (pt != OPTIMAL) return pt;
    567       start(method);
    568       return OPTIMAL;
    569     }
    570 
    571     /// \brief Reset all the parameters that have been given before.
    572     ///
    573     /// This function resets all the paramaters that have been given
    574     /// before using functions \ref lowerMap(), \ref upperMap(),
    575     /// \ref costMap(), \ref supplyMap(), \ref stSupply().
    576     ///
    577     /// It is useful for multiple run() calls. If this function is not
    578     /// used, all the parameters given before are kept for the next
    579     /// \ref run() call.
    580     /// However, the underlying digraph must not be modified after this
    581     /// class have been constructed, since it copies and extends the graph.
    582     ///
    583     /// For example,
    584     /// \code
    585     ///   CostScaling<ListDigraph> cs(graph);
    586     ///
    587     ///   // First run
    588     ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
    589     ///     .supplyMap(sup).run();
    590     ///
    591     ///   // Run again with modified cost map (reset() is not called,
    592     ///   // so only the cost map have to be set again)
    593     ///   cost[e] += 100;
    594     ///   cs.costMap(cost).run();
    595     ///
    596     ///   // Run again from scratch using reset()
    597     ///   // (the lower bounds will be set to zero on all arcs)
    598     ///   cs.reset();
    599     ///   cs.upperMap(capacity).costMap(cost)
    600     ///     .supplyMap(sup).run();
    601     /// \endcode
    602     ///
    603     /// \return <tt>(*this)</tt>
    604     CostScaling& reset() {
    605       for (int i = 0; i != _res_node_num; ++i) {
    606         _supply[i] = 0;
    607       }
    608       int limit = _first_out[_root];
    609       for (int j = 0; j != limit; ++j) {
    610         _lower[j] = 0;
    611         _upper[j] = INF;
    612         _scost[j] = _forward[j] ? 1 : -1;
    613       }
    614       for (int j = limit; j != _res_arc_num; ++j) {
    615         _lower[j] = 0;
    616         _upper[j] = INF;
    617         _scost[j] = 0;
    618         _scost[_reverse[j]] = 0;
    619       }     
    620       _have_lower = false;
     642      resetParams();
    621643      return *this;
    622644    }
  • lemon/cycle_canceling.h

    r820 r830  
    251251        "The cost type of CycleCanceling must be signed");
    252252
     253      // Reset data structures
     254      reset();
     255    }
     256
     257    /// \name Parameters
     258    /// The parameters of the algorithm can be specified using these
     259    /// functions.
     260
     261    /// @{
     262
     263    /// \brief Set the lower bounds on the arcs.
     264    ///
     265    /// This function sets the lower bounds on the arcs.
     266    /// If it is not used before calling \ref run(), the lower bounds
     267    /// will be set to zero on all arcs.
     268    ///
     269    /// \param map An arc map storing the lower bounds.
     270    /// Its \c Value type must be convertible to the \c Value type
     271    /// of the algorithm.
     272    ///
     273    /// \return <tt>(*this)</tt>
     274    template <typename LowerMap>
     275    CycleCanceling& lowerMap(const LowerMap& map) {
     276      _have_lower = true;
     277      for (ArcIt a(_graph); a != INVALID; ++a) {
     278        _lower[_arc_idf[a]] = map[a];
     279        _lower[_arc_idb[a]] = map[a];
     280      }
     281      return *this;
     282    }
     283
     284    /// \brief Set the upper bounds (capacities) on the arcs.
     285    ///
     286    /// This function sets the upper bounds (capacities) on the arcs.
     287    /// If it is not used before calling \ref run(), the upper bounds
     288    /// will be set to \ref INF on all arcs (i.e. the flow value will be
     289    /// unbounded from above).
     290    ///
     291    /// \param map An arc map storing the upper bounds.
     292    /// Its \c Value type must be convertible to the \c Value type
     293    /// of the algorithm.
     294    ///
     295    /// \return <tt>(*this)</tt>
     296    template<typename UpperMap>
     297    CycleCanceling& upperMap(const UpperMap& map) {
     298      for (ArcIt a(_graph); a != INVALID; ++a) {
     299        _upper[_arc_idf[a]] = map[a];
     300      }
     301      return *this;
     302    }
     303
     304    /// \brief Set the costs of the arcs.
     305    ///
     306    /// This function sets the costs of the arcs.
     307    /// If it is not used before calling \ref run(), the costs
     308    /// will be set to \c 1 on all arcs.
     309    ///
     310    /// \param map An arc map storing the costs.
     311    /// Its \c Value type must be convertible to the \c Cost type
     312    /// of the algorithm.
     313    ///
     314    /// \return <tt>(*this)</tt>
     315    template<typename CostMap>
     316    CycleCanceling& costMap(const CostMap& map) {
     317      for (ArcIt a(_graph); a != INVALID; ++a) {
     318        _cost[_arc_idf[a]] =  map[a];
     319        _cost[_arc_idb[a]] = -map[a];
     320      }
     321      return *this;
     322    }
     323
     324    /// \brief Set the supply values of the nodes.
     325    ///
     326    /// This function sets the supply values of the nodes.
     327    /// If neither this function nor \ref stSupply() is used before
     328    /// calling \ref run(), the supply of each node will be set to zero.
     329    ///
     330    /// \param map A node map storing the supply values.
     331    /// Its \c Value type must be convertible to the \c Value type
     332    /// of the algorithm.
     333    ///
     334    /// \return <tt>(*this)</tt>
     335    template<typename SupplyMap>
     336    CycleCanceling& supplyMap(const SupplyMap& map) {
     337      for (NodeIt n(_graph); n != INVALID; ++n) {
     338        _supply[_node_id[n]] = map[n];
     339      }
     340      return *this;
     341    }
     342
     343    /// \brief Set single source and target nodes and a supply value.
     344    ///
     345    /// This function sets a single source node and a single target node
     346    /// and the required flow value.
     347    /// If neither this function nor \ref supplyMap() is used before
     348    /// calling \ref run(), the supply of each node will be set to zero.
     349    ///
     350    /// Using this function has the same effect as using \ref supplyMap()
     351    /// with such a map in which \c k is assigned to \c s, \c -k is
     352    /// assigned to \c t and all other nodes have zero supply value.
     353    ///
     354    /// \param s The source node.
     355    /// \param t The target node.
     356    /// \param k The required amount of flow from node \c s to node \c t
     357    /// (i.e. the supply of \c s and the demand of \c t).
     358    ///
     359    /// \return <tt>(*this)</tt>
     360    CycleCanceling& stSupply(const Node& s, const Node& t, Value k) {
     361      for (int i = 0; i != _res_node_num; ++i) {
     362        _supply[i] = 0;
     363      }
     364      _supply[_node_id[s]] =  k;
     365      _supply[_node_id[t]] = -k;
     366      return *this;
     367    }
     368   
     369    /// @}
     370
     371    /// \name Execution control
     372    /// The algorithm can be executed using \ref run().
     373
     374    /// @{
     375
     376    /// \brief Run the algorithm.
     377    ///
     378    /// This function runs the algorithm.
     379    /// The paramters can be specified using functions \ref lowerMap(),
     380    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
     381    /// For example,
     382    /// \code
     383    ///   CycleCanceling<ListDigraph> cc(graph);
     384    ///   cc.lowerMap(lower).upperMap(upper).costMap(cost)
     385    ///     .supplyMap(sup).run();
     386    /// \endcode
     387    ///
     388    /// This function can be called more than once. All the given parameters
     389    /// are kept for the next call, unless \ref resetParams() or \ref reset()
     390    /// is used, thus only the modified parameters have to be set again.
     391    /// If the underlying digraph was also modified after the construction
     392    /// of the class (or the last \ref reset() call), then the \ref reset()
     393    /// function must be called.
     394    ///
     395    /// \param method The cycle-canceling method that will be used.
     396    /// For more information, see \ref Method.
     397    ///
     398    /// \return \c INFEASIBLE if no feasible flow exists,
     399    /// \n \c OPTIMAL if the problem has optimal solution
     400    /// (i.e. it is feasible and bounded), and the algorithm has found
     401    /// optimal flow and node potentials (primal and dual solutions),
     402    /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
     403    /// and infinite upper bound. It means that the objective function
     404    /// is unbounded on that arc, however, note that it could actually be
     405    /// bounded over the feasible flows, but this algroithm cannot handle
     406    /// these cases.
     407    ///
     408    /// \see ProblemType, Method
     409    /// \see resetParams(), reset()
     410    ProblemType run(Method method = CANCEL_AND_TIGHTEN) {
     411      ProblemType pt = init();
     412      if (pt != OPTIMAL) return pt;
     413      start(method);
     414      return OPTIMAL;
     415    }
     416
     417    /// \brief Reset all the parameters that have been given before.
     418    ///
     419    /// This function resets all the paramaters that have been given
     420    /// before using functions \ref lowerMap(), \ref upperMap(),
     421    /// \ref costMap(), \ref supplyMap(), \ref stSupply().
     422    ///
     423    /// It is useful for multiple \ref run() calls. Basically, all the given
     424    /// parameters are kept for the next \ref run() call, unless
     425    /// \ref resetParams() or \ref reset() is used.
     426    /// If the underlying digraph was also modified after the construction
     427    /// of the class or the last \ref reset() call, then the \ref reset()
     428    /// function must be used, otherwise \ref resetParams() is sufficient.
     429    ///
     430    /// For example,
     431    /// \code
     432    ///   CycleCanceling<ListDigraph> cs(graph);
     433    ///
     434    ///   // First run
     435    ///   cc.lowerMap(lower).upperMap(upper).costMap(cost)
     436    ///     .supplyMap(sup).run();
     437    ///
     438    ///   // Run again with modified cost map (resetParams() is not called,
     439    ///   // so only the cost map have to be set again)
     440    ///   cost[e] += 100;
     441    ///   cc.costMap(cost).run();
     442    ///
     443    ///   // Run again from scratch using resetParams()
     444    ///   // (the lower bounds will be set to zero on all arcs)
     445    ///   cc.resetParams();
     446    ///   cc.upperMap(capacity).costMap(cost)
     447    ///     .supplyMap(sup).run();
     448    /// \endcode
     449    ///
     450    /// \return <tt>(*this)</tt>
     451    ///
     452    /// \see reset(), run()
     453    CycleCanceling& resetParams() {
     454      for (int i = 0; i != _res_node_num; ++i) {
     455        _supply[i] = 0;
     456      }
     457      int limit = _first_out[_root];
     458      for (int j = 0; j != limit; ++j) {
     459        _lower[j] = 0;
     460        _upper[j] = INF;
     461        _cost[j] = _forward[j] ? 1 : -1;
     462      }
     463      for (int j = limit; j != _res_arc_num; ++j) {
     464        _lower[j] = 0;
     465        _upper[j] = INF;
     466        _cost[j] = 0;
     467        _cost[_reverse[j]] = 0;
     468      }     
     469      _have_lower = false;
     470      return *this;
     471    }
     472
     473    /// \brief Reset the internal data structures and all the parameters
     474    /// that have been given before.
     475    ///
     476    /// This function resets the internal data structures and all the
     477    /// paramaters that have been given before using functions \ref lowerMap(),
     478    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
     479    ///
     480    /// It is useful for multiple \ref run() calls. Basically, all the given
     481    /// parameters are kept for the next \ref run() call, unless
     482    /// \ref resetParams() or \ref reset() is used.
     483    /// If the underlying digraph was also modified after the construction
     484    /// of the class or the last \ref reset() call, then the \ref reset()
     485    /// function must be used, otherwise \ref resetParams() is sufficient.
     486    ///
     487    /// See \ref resetParams() for examples.
     488    ///
     489    /// \return <tt>(*this)</tt>
     490    ///
     491    /// \see resetParams(), run()
     492    CycleCanceling& reset() {
    253493      // Resize vectors
    254494      _node_num = countNodes(_graph);
     
    316556     
    317557      // Reset parameters
    318       reset();
    319     }
    320 
    321     /// \name Parameters
    322     /// The parameters of the algorithm can be specified using these
    323     /// functions.
    324 
    325     /// @{
    326 
    327     /// \brief Set the lower bounds on the arcs.
    328     ///
    329     /// This function sets the lower bounds on the arcs.
    330     /// If it is not used before calling \ref run(), the lower bounds
    331     /// will be set to zero on all arcs.
    332     ///
    333     /// \param map An arc map storing the lower bounds.
    334     /// Its \c Value type must be convertible to the \c Value type
    335     /// of the algorithm.
    336     ///
    337     /// \return <tt>(*this)</tt>
    338     template <typename LowerMap>
    339     CycleCanceling& lowerMap(const LowerMap& map) {
    340       _have_lower = true;
    341       for (ArcIt a(_graph); a != INVALID; ++a) {
    342         _lower[_arc_idf[a]] = map[a];
    343         _lower[_arc_idb[a]] = map[a];
    344       }
    345       return *this;
    346     }
    347 
    348     /// \brief Set the upper bounds (capacities) on the arcs.
    349     ///
    350     /// This function sets the upper bounds (capacities) on the arcs.
    351     /// If it is not used before calling \ref run(), the upper bounds
    352     /// will be set to \ref INF on all arcs (i.e. the flow value will be
    353     /// unbounded from above).
    354     ///
    355     /// \param map An arc map storing the upper bounds.
    356     /// Its \c Value type must be convertible to the \c Value type
    357     /// of the algorithm.
    358     ///
    359     /// \return <tt>(*this)</tt>
    360     template<typename UpperMap>
    361     CycleCanceling& upperMap(const UpperMap& map) {
    362       for (ArcIt a(_graph); a != INVALID; ++a) {
    363         _upper[_arc_idf[a]] = map[a];
    364       }
    365       return *this;
    366     }
    367 
    368     /// \brief Set the costs of the arcs.
    369     ///
    370     /// This function sets the costs of the arcs.
    371     /// If it is not used before calling \ref run(), the costs
    372     /// will be set to \c 1 on all arcs.
    373     ///
    374     /// \param map An arc map storing the costs.
    375     /// Its \c Value type must be convertible to the \c Cost type
    376     /// of the algorithm.
    377     ///
    378     /// \return <tt>(*this)</tt>
    379     template<typename CostMap>
    380     CycleCanceling& costMap(const CostMap& map) {
    381       for (ArcIt a(_graph); a != INVALID; ++a) {
    382         _cost[_arc_idf[a]] =  map[a];
    383         _cost[_arc_idb[a]] = -map[a];
    384       }
    385       return *this;
    386     }
    387 
    388     /// \brief Set the supply values of the nodes.
    389     ///
    390     /// This function sets the supply values of the nodes.
    391     /// If neither this function nor \ref stSupply() is used before
    392     /// calling \ref run(), the supply of each node will be set to zero.
    393     ///
    394     /// \param map A node map storing the supply values.
    395     /// Its \c Value type must be convertible to the \c Value type
    396     /// of the algorithm.
    397     ///
    398     /// \return <tt>(*this)</tt>
    399     template<typename SupplyMap>
    400     CycleCanceling& supplyMap(const SupplyMap& map) {
    401       for (NodeIt n(_graph); n != INVALID; ++n) {
    402         _supply[_node_id[n]] = map[n];
    403       }
    404       return *this;
    405     }
    406 
    407     /// \brief Set single source and target nodes and a supply value.
    408     ///
    409     /// This function sets a single source node and a single target node
    410     /// and the required flow value.
    411     /// If neither this function nor \ref supplyMap() is used before
    412     /// calling \ref run(), the supply of each node will be set to zero.
    413     ///
    414     /// Using this function has the same effect as using \ref supplyMap()
    415     /// with such a map in which \c k is assigned to \c s, \c -k is
    416     /// assigned to \c t and all other nodes have zero supply value.
    417     ///
    418     /// \param s The source node.
    419     /// \param t The target node.
    420     /// \param k The required amount of flow from node \c s to node \c t
    421     /// (i.e. the supply of \c s and the demand of \c t).
    422     ///
    423     /// \return <tt>(*this)</tt>
    424     CycleCanceling& stSupply(const Node& s, const Node& t, Value k) {
    425       for (int i = 0; i != _res_node_num; ++i) {
    426         _supply[i] = 0;
    427       }
    428       _supply[_node_id[s]] =  k;
    429       _supply[_node_id[t]] = -k;
    430       return *this;
    431     }
    432    
    433     /// @}
    434 
    435     /// \name Execution control
    436     /// The algorithm can be executed using \ref run().
    437 
    438     /// @{
    439 
    440     /// \brief Run the algorithm.
    441     ///
    442     /// This function runs the algorithm.
    443     /// The paramters can be specified using functions \ref lowerMap(),
    444     /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
    445     /// For example,
    446     /// \code
    447     ///   CycleCanceling<ListDigraph> cc(graph);
    448     ///   cc.lowerMap(lower).upperMap(upper).costMap(cost)
    449     ///     .supplyMap(sup).run();
    450     /// \endcode
    451     ///
    452     /// This function can be called more than once. All the parameters
    453     /// that have been given are kept for the next call, unless
    454     /// \ref reset() is called, thus only the modified parameters
    455     /// have to be set again. See \ref reset() for examples.
    456     /// However, the underlying digraph must not be modified after this
    457     /// class have been constructed, since it copies and extends the graph.
    458     ///
    459     /// \param method The cycle-canceling method that will be used.
    460     /// For more information, see \ref Method.
    461     ///
    462     /// \return \c INFEASIBLE if no feasible flow exists,
    463     /// \n \c OPTIMAL if the problem has optimal solution
    464     /// (i.e. it is feasible and bounded), and the algorithm has found
    465     /// optimal flow and node potentials (primal and dual solutions),
    466     /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
    467     /// and infinite upper bound. It means that the objective function
    468     /// is unbounded on that arc, however, note that it could actually be
    469     /// bounded over the feasible flows, but this algroithm cannot handle
    470     /// these cases.
    471     ///
    472     /// \see ProblemType, Method
    473     ProblemType run(Method method = CANCEL_AND_TIGHTEN) {
    474       ProblemType pt = init();
    475       if (pt != OPTIMAL) return pt;
    476       start(method);
    477       return OPTIMAL;
    478     }
    479 
    480     /// \brief Reset all the parameters that have been given before.
    481     ///
    482     /// This function resets all the paramaters that have been given
    483     /// before using functions \ref lowerMap(), \ref upperMap(),
    484     /// \ref costMap(), \ref supplyMap(), \ref stSupply().
    485     ///
    486     /// It is useful for multiple run() calls. If this function is not
    487     /// used, all the parameters given before are kept for the next
    488     /// \ref run() call.
    489     /// However, the underlying digraph must not be modified after this
    490     /// class have been constructed, since it copies and extends the graph.
    491     ///
    492     /// For example,
    493     /// \code
    494     ///   CycleCanceling<ListDigraph> cs(graph);
    495     ///
    496     ///   // First run
    497     ///   cc.lowerMap(lower).upperMap(upper).costMap(cost)
    498     ///     .supplyMap(sup).run();
    499     ///
    500     ///   // Run again with modified cost map (reset() is not called,
    501     ///   // so only the cost map have to be set again)
    502     ///   cost[e] += 100;
    503     ///   cc.costMap(cost).run();
    504     ///
    505     ///   // Run again from scratch using reset()
    506     ///   // (the lower bounds will be set to zero on all arcs)
    507     ///   cc.reset();
    508     ///   cc.upperMap(capacity).costMap(cost)
    509     ///     .supplyMap(sup).run();
    510     /// \endcode
    511     ///
    512     /// \return <tt>(*this)</tt>
    513     CycleCanceling& reset() {
    514       for (int i = 0; i != _res_node_num; ++i) {
    515         _supply[i] = 0;
    516       }
    517       int limit = _first_out[_root];
    518       for (int j = 0; j != limit; ++j) {
    519         _lower[j] = 0;
    520         _upper[j] = INF;
    521         _cost[j] = _forward[j] ? 1 : -1;
    522       }
    523       for (int j = limit; j != _res_arc_num; ++j) {
    524         _lower[j] = 0;
    525         _upper[j] = INF;
    526         _cost[j] = 0;
    527         _cost[_reverse[j]] = 0;
    528       }     
    529       _have_lower = false;
     558      resetParams();
    530559      return *this;
    531560    }
  • lemon/network_simplex.h

    r812 r830  
    195195    IntVector _source;
    196196    IntVector _target;
     197    bool _arc_mixing;
    197198
    198199    // Node and arc data
     
    634635    NetworkSimplex(const GR& graph, bool arc_mixing = false) :
    635636      _graph(graph), _node_id(graph), _arc_id(graph),
     637      _arc_mixing(arc_mixing),
    636638      MAX(std::numeric_limits<Value>::max()),
    637639      INF(std::numeric_limits<Value>::has_infinity ?
     
    644646        "The cost type of NetworkSimplex must be signed");
    645647       
    646       // Resize vectors
    647       _node_num = countNodes(_graph);
    648       _arc_num = countArcs(_graph);
    649       int all_node_num = _node_num + 1;
    650       int max_arc_num = _arc_num + 2 * _node_num;
    651 
    652       _source.resize(max_arc_num);
    653       _target.resize(max_arc_num);
    654 
    655       _lower.resize(_arc_num);
    656       _upper.resize(_arc_num);
    657       _cap.resize(max_arc_num);
    658       _cost.resize(max_arc_num);
    659       _supply.resize(all_node_num);
    660       _flow.resize(max_arc_num);
    661       _pi.resize(all_node_num);
    662 
    663       _parent.resize(all_node_num);
    664       _pred.resize(all_node_num);
    665       _forward.resize(all_node_num);
    666       _thread.resize(all_node_num);
    667       _rev_thread.resize(all_node_num);
    668       _succ_num.resize(all_node_num);
    669       _last_succ.resize(all_node_num);
    670       _state.resize(max_arc_num);
    671 
    672       // Copy the graph
    673       int i = 0;
    674       for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
    675         _node_id[n] = i;
    676       }
    677       if (arc_mixing) {
    678         // Store the arcs in a mixed order
    679         int k = std::max(int(std::sqrt(double(_arc_num))), 10);
    680         int i = 0, j = 0;
    681         for (ArcIt a(_graph); a != INVALID; ++a) {
    682           _arc_id[a] = i;
    683           _source[i] = _node_id[_graph.source(a)];
    684           _target[i] = _node_id[_graph.target(a)];
    685           if ((i += k) >= _arc_num) i = ++j;
    686         }
    687       } else {
    688         // Store the arcs in the original order
    689         int i = 0;
    690         for (ArcIt a(_graph); a != INVALID; ++a, ++i) {
    691           _arc_id[a] = i;
    692           _source[i] = _node_id[_graph.source(a)];
    693           _target[i] = _node_id[_graph.target(a)];
    694         }
    695       }
    696      
    697       // Reset parameters
     648      // Reset data structures
    698649      reset();
    699650    }
     
    843794    /// \endcode
    844795    ///
    845     /// This function can be called more than once. All the parameters
    846     /// that have been given are kept for the next call, unless
    847     /// \ref reset() is called, thus only the modified parameters
    848     /// have to be set again. See \ref reset() for examples.
    849     /// However, the underlying digraph must not be modified after this
    850     /// class have been constructed, since it copies and extends the graph.
     796    /// This function can be called more than once. All the given parameters
     797    /// are kept for the next call, unless \ref resetParams() or \ref reset()
     798    /// is used, thus only the modified parameters have to be set again.
     799    /// If the underlying digraph was also modified after the construction
     800    /// of the class (or the last \ref reset() call), then the \ref reset()
     801    /// function must be called.
    851802    ///
    852803    /// \param pivot_rule The pivot rule that will be used during the
     
    862813    ///
    863814    /// \see ProblemType, PivotRule
     815    /// \see resetParams(), reset()
    864816    ProblemType run(PivotRule pivot_rule = BLOCK_SEARCH) {
    865817      if (!init()) return INFEASIBLE;
     
    873825    /// \ref costMap(), \ref supplyMap(), \ref stSupply(), \ref supplyType().
    874826    ///
    875     /// It is useful for multiple run() calls. If this function is not
    876     /// used, all the parameters given before are kept for the next
    877     /// \ref run() call.
    878     /// However, the underlying digraph must not be modified after this
    879     /// class have been constructed, since it copies and extends the graph.
     827    /// It is useful for multiple \ref run() calls. Basically, all the given
     828    /// parameters are kept for the next \ref run() call, unless
     829    /// \ref resetParams() or \ref reset() is used.
     830    /// If the underlying digraph was also modified after the construction
     831    /// of the class or the last \ref reset() call, then the \ref reset()
     832    /// function must be used, otherwise \ref resetParams() is sufficient.
    880833    ///
    881834    /// For example,
     
    887840    ///     .supplyMap(sup).run();
    888841    ///
    889     ///   // Run again with modified cost map (reset() is not called,
     842    ///   // Run again with modified cost map (resetParams() is not called,
    890843    ///   // so only the cost map have to be set again)
    891844    ///   cost[e] += 100;
    892845    ///   ns.costMap(cost).run();
    893846    ///
    894     ///   // Run again from scratch using reset()
     847    ///   // Run again from scratch using resetParams()
    895848    ///   // (the lower bounds will be set to zero on all arcs)
    896     ///   ns.reset();
     849    ///   ns.resetParams();
    897850    ///   ns.upperMap(capacity).costMap(cost)
    898851    ///     .supplyMap(sup).run();
     
    900853    ///
    901854    /// \return <tt>(*this)</tt>
    902     NetworkSimplex& reset() {
     855    ///
     856    /// \see reset(), run()
     857    NetworkSimplex& resetParams() {
    903858      for (int i = 0; i != _node_num; ++i) {
    904859        _supply[i] = 0;
     
    914869    }
    915870
     871    /// \brief Reset the internal data structures and all the parameters
     872    /// that have been given before.
     873    ///
     874    /// This function resets the internal data structures and all the
     875    /// paramaters that have been given before using functions \ref lowerMap(),
     876    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(),
     877    /// \ref supplyType().
     878    ///
     879    /// It is useful for multiple \ref run() calls. Basically, all the given
     880    /// parameters are kept for the next \ref run() call, unless
     881    /// \ref resetParams() or \ref reset() is used.
     882    /// If the underlying digraph was also modified after the construction
     883    /// of the class or the last \ref reset() call, then the \ref reset()
     884    /// function must be used, otherwise \ref resetParams() is sufficient.
     885    ///
     886    /// See \ref resetParams() for examples.
     887    ///
     888    /// \return <tt>(*this)</tt>
     889    ///
     890    /// \see resetParams(), run()
     891    NetworkSimplex& reset() {
     892      // Resize vectors
     893      _node_num = countNodes(_graph);
     894      _arc_num = countArcs(_graph);
     895      int all_node_num = _node_num + 1;
     896      int max_arc_num = _arc_num + 2 * _node_num;
     897
     898      _source.resize(max_arc_num);
     899      _target.resize(max_arc_num);
     900
     901      _lower.resize(_arc_num);
     902      _upper.resize(_arc_num);
     903      _cap.resize(max_arc_num);
     904      _cost.resize(max_arc_num);
     905      _supply.resize(all_node_num);
     906      _flow.resize(max_arc_num);
     907      _pi.resize(all_node_num);
     908
     909      _parent.resize(all_node_num);
     910      _pred.resize(all_node_num);
     911      _forward.resize(all_node_num);
     912      _thread.resize(all_node_num);
     913      _rev_thread.resize(all_node_num);
     914      _succ_num.resize(all_node_num);
     915      _last_succ.resize(all_node_num);
     916      _state.resize(max_arc_num);
     917
     918      // Copy the graph
     919      int i = 0;
     920      for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
     921        _node_id[n] = i;
     922      }
     923      if (_arc_mixing) {
     924        // Store the arcs in a mixed order
     925        int k = std::max(int(std::sqrt(double(_arc_num))), 10);
     926        int i = 0, j = 0;
     927        for (ArcIt a(_graph); a != INVALID; ++a) {
     928          _arc_id[a] = i;
     929          _source[i] = _node_id[_graph.source(a)];
     930          _target[i] = _node_id[_graph.target(a)];
     931          if ((i += k) >= _arc_num) i = ++j;
     932        }
     933      } else {
     934        // Store the arcs in the original order
     935        int i = 0;
     936        for (ArcIt a(_graph); a != INVALID; ++a, ++i) {
     937          _arc_id[a] = i;
     938          _source[i] = _node_id[_graph.source(a)];
     939          _target[i] = _node_id[_graph.target(a)];
     940        }
     941      }
     942     
     943      // Reset parameters
     944      resetParams();
     945      return *this;
     946    }
     947   
    916948    /// @}
    917949
  • test/min_cost_flow_test.cc

    r819 r830  
    158158      const MCF& const_mcf = mcf;
    159159
    160       b = mcf.reset()
     160      b = mcf.reset().resetParams()
    161161             .lowerMap(me.lower)
    162162             .upperMap(me.upper)
     
    347347  checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s2,
    348348           mcf1.OPTIMAL, true,     8010, test_str + "-4");
    349   mcf1.reset().supplyMap(s1);
     349  mcf1.resetParams().supplyMap(s1);
    350350  checkMcf(mcf1, mcf1.run(param), gr, l1, cu, cc, s1,
    351351           mcf1.OPTIMAL, true,       74, test_str + "-5");
     
    364364
    365365  // Tests for the GEQ form
    366   mcf1.reset().upperMap(u).costMap(c).supplyMap(s5);
     366  mcf1.resetParams().upperMap(u).costMap(c).supplyMap(s5);
    367367  checkMcf(mcf1, mcf1.run(param), gr, l1, u, c, s5,
    368368           mcf1.OPTIMAL, true,     3530, test_str + "-10", GEQ);
     
    381381  checkMcf(mcf2, mcf2.run(param), neg1_gr, neg1_l1, neg1_u2, neg1_c, neg1_s,
    382382           mcf2.OPTIMAL, true,   -40000, test_str + "-14");
    383   mcf2.reset().lowerMap(neg1_l2).costMap(neg1_c).supplyMap(neg1_s);
     383  mcf2.resetParams().lowerMap(neg1_l2).costMap(neg1_c).supplyMap(neg1_s);
    384384  checkMcf(mcf2, mcf2.run(param), neg1_gr, neg1_l2, neg1_u1, neg1_c, neg1_s,
    385385           mcf2.UNBOUNDED, false,     0, test_str + "-15");
Note: See TracChangeset for help on using the changeset viewer.