kpeter@663: /* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@663:  *
kpeter@663:  * This file is a part of LEMON, a generic C++ optimization library.
kpeter@663:  *
kpeter@663:  * Copyright (C) 2003-2009
kpeter@663:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@663:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@663:  *
kpeter@663:  * Permission to use, modify and distribute this software is granted
kpeter@663:  * provided that this copyright notice appears in all copies. For
kpeter@663:  * precise terms see the accompanying LICENSE file.
kpeter@663:  *
kpeter@663:  * This software is provided "AS IS" with no warranty of any kind,
kpeter@663:  * express or implied, and with no claim as to its suitability for any
kpeter@663:  * purpose.
kpeter@663:  *
kpeter@663:  */
kpeter@663: 
kpeter@663: namespace lemon {
kpeter@663: 
kpeter@663: /**
kpeter@663: \page min_cost_flow Minimum Cost Flow Problem
kpeter@663: 
kpeter@663: \section mcf_def Definition (GEQ form)
kpeter@663: 
kpeter@663: The \e minimum \e cost \e flow \e problem is to find a feasible flow of
kpeter@663: minimum total cost from a set of supply nodes to a set of demand nodes
kpeter@663: in a network with capacity constraints (lower and upper bounds)
kpeter@663: and arc costs.
kpeter@663: 
kpeter@663: Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$,
kpeter@663: \f$upper: A\rightarrow\mathbf{R}\cup\{+\infty\}\f$ denote the lower and
kpeter@663: upper bounds for the flow values on the arcs, for which
kpeter@663: \f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$,
kpeter@663: \f$cost: A\rightarrow\mathbf{R}\f$ denotes the cost per unit flow
kpeter@663: on the arcs and \f$sup: V\rightarrow\mathbf{R}\f$ denotes the
kpeter@663: signed supply values of the nodes.
kpeter@663: If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$
kpeter@663: supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with
kpeter@663: \f$-sup(u)\f$ demand.
kpeter@663: A minimum cost flow is an \f$f: A\rightarrow\mathbf{R}\f$ solution
kpeter@663: of the following optimization problem.
kpeter@663: 
kpeter@663: \f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
kpeter@663: \f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq
kpeter@663:     sup(u) \quad \forall u\in V \f]
kpeter@663: \f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f]
kpeter@663: 
kpeter@663: The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
kpeter@663: zero or negative in order to have a feasible solution (since the sum
kpeter@663: of the expressions on the left-hand side of the inequalities is zero).
kpeter@663: It means that the total demand must be greater or equal to the total
kpeter@663: supply and all the supplies have to be carried out from the supply nodes,
kpeter@663: but there could be demands that are not satisfied.
kpeter@663: If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
kpeter@663: constraints have to be satisfied with equality, i.e. all demands
kpeter@663: have to be satisfied and all supplies have to be used.
kpeter@663: 
kpeter@663: 
kpeter@663: \section mcf_algs Algorithms
kpeter@663: 
kpeter@663: LEMON contains several algorithms for solving this problem, for more
kpeter@663: information see \ref min_cost_flow_algs "Minimum Cost Flow Algorithms".
kpeter@663: 
kpeter@663: A feasible solution for this problem can be found using \ref Circulation.
kpeter@663: 
kpeter@663: 
kpeter@663: \section mcf_dual Dual Solution
kpeter@663: 
kpeter@663: The dual solution of the minimum cost flow problem is represented by
kpeter@663: node potentials \f$\pi: V\rightarrow\mathbf{R}\f$.
kpeter@663: An \f$f: A\rightarrow\mathbf{R}\f$ primal feasible solution is optimal
kpeter@663: if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ node potentials
kpeter@663: the following \e complementary \e slackness optimality conditions hold.
kpeter@663: 
kpeter@663:  - For all \f$uv\in A\f$ arcs:
kpeter@663:    - if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$;
kpeter@663:    - if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$;
kpeter@663:    - if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$.
kpeter@663:  - For all \f$u\in V\f$ nodes:
kpeter@663:    - \f$\pi(u)<=0\f$;
kpeter@663:    - if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$,
kpeter@663:      then \f$\pi(u)=0\f$.
kpeter@663:  
kpeter@663: Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc
kpeter@663: \f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e.
kpeter@663: \f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f]
kpeter@663: 
kpeter@663: All algorithms provide dual solution (node potentials), as well,
kpeter@663: if an optimal flow is found.
kpeter@663: 
kpeter@663: 
kpeter@663: \section mcf_eq Equality Form
kpeter@663: 
kpeter@663: The above \ref mcf_def "definition" is actually more general than the
kpeter@663: usual formulation of the minimum cost flow problem, in which strict
kpeter@663: equalities are required in the supply/demand contraints.
kpeter@663: 
kpeter@663: \f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
kpeter@663: \f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) =
kpeter@663:     sup(u) \quad \forall u\in V \f]
kpeter@663: \f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f]
kpeter@663: 
kpeter@663: However if the sum of the supply values is zero, then these two problems
kpeter@663: are equivalent.
kpeter@663: The \ref min_cost_flow_algs "algorithms" in LEMON support the general
kpeter@663: form, so if you need the equality form, you have to ensure this additional
kpeter@663: contraint manually.
kpeter@663: 
kpeter@663: 
kpeter@663: \section mcf_leq Opposite Inequalites (LEQ Form)
kpeter@663: 
kpeter@663: Another possible definition of the minimum cost flow problem is
kpeter@663: when there are <em>"less or equal"</em> (LEQ) supply/demand constraints,
kpeter@663: instead of the <em>"greater or equal"</em> (GEQ) constraints.
kpeter@663: 
kpeter@663: \f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
kpeter@663: \f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq
kpeter@663:     sup(u) \quad \forall u\in V \f]
kpeter@663: \f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f]
kpeter@663: 
kpeter@663: It means that the total demand must be less or equal to the 
kpeter@663: total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or
kpeter@663: positive) and all the demands have to be satisfied, but there
kpeter@663: could be supplies that are not carried out from the supply
kpeter@663: nodes.
kpeter@663: The equality form is also a special case of this form, of course.
kpeter@663: 
kpeter@663: You could easily transform this case to the \ref mcf_def "GEQ form"
kpeter@663: of the problem by reversing the direction of the arcs and taking the
kpeter@663: negative of the supply values (e.g. using \ref ReverseDigraph and
kpeter@663: \ref NegMap adaptors).
kpeter@663: However \ref NetworkSimplex algorithm also supports this form directly
kpeter@663: for the sake of convenience.
kpeter@663: 
kpeter@663: Note that the optimality conditions for this supply constraint type are
kpeter@663: slightly differ from the conditions that are discussed for the GEQ form,
kpeter@663: namely the potentials have to be non-negative instead of non-positive.
kpeter@663: An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem
kpeter@663: is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$
kpeter@663: node potentials the following conditions hold.
kpeter@663: 
kpeter@663:  - For all \f$uv\in A\f$ arcs:
kpeter@663:    - if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$;
kpeter@663:    - if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$;
kpeter@663:    - if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$.
kpeter@663:  - For all \f$u\in V\f$ nodes:
kpeter@663:    - \f$\pi(u)>=0\f$;
kpeter@663:    - if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$,
kpeter@663:      then \f$\pi(u)=0\f$.
kpeter@663: 
kpeter@663: */
kpeter@663: }