graph_analysis
Public Types | Public Member Functions | Static Public Member Functions | List of all members
graph_analysis::algorithms::MultiCommodityMinCostFlow Class Reference

#include <MultiCommodityMinCostFlow.hpp>

Public Types

typedef MultiCommodityEdge edge_t
 
typedef MultiCommodityVertex vertex_t
 
typedef std::pair< Vertex::Ptr, size_t > VertexCommodity
 
typedef std::set< VertexCommodity > VertexCommoditySet
 
typedef std::vector< VertexCommodity > VertexCommodityList
 

Public Member Functions

void save (const std::string &filename, representation::Type format=representation::GEXF)
 
 MultiCommodityMinCostFlow (const BaseGraph::Ptr &graph, uint32_t commodities=0, LPSolver::Type solverType=LPSolver::GLPK_SOLVER)
 
virtual ~MultiCommodityMinCostFlow ()
 
std::string createProblem (LPSolver::ProblemFormat format=LPSolver::CPLEX)
 
std::string createProblemCPLEX ()
 
LPSolver::Status solve (const std::string &solutionFile="", bool useCaching=true)
 
double getCost () const
 
void storeResult ()
 
VertexCommoditySet vertexConstraints () const
 
std::set< Edge::Ptr > edgeConstraints () const
 
std::vector< ConstraintViolation > validateInflow () const
 

Static Public Member Functions

static MultiCommodityMinCostFlow fromFile (const std::string &filename, representation::Type format=representation::UNKNOWN, LPSolver::Type solverType=LPSolver::GLPK_SOLVER)
 

Detailed Description

The multi-commodity min cost flow problem (MMCF) (Kennington, 1978) handles the routing of commodities (different types of resources) through a network which is subject to constraints:

\[ \min \sum_{k,m} c_{m}^{k} x_{m}^{k} \]

\[ \text{, where} \]

\begin{eqnarray*} G &=& (V,E) \\ K &=& \text{ number of commodities} \\ k &=& \text{ 1 \dots K } \\ M &=& \text{ size of G, i.e. number of edges } |E(G)|\\ m &=& \text{ 1 \dots M }\\ e_{m} &=& \text{ egde between node i and node j, i.e. (i,j) } \\ x_{m}^{k} &=& \text{ flow for commodity k in arc } e_{m} \\ c_{m}^{k} &=& \text{ unit cost for commodity k in arc } e_{m} \\ u_{m}^{k} &=& \text{ upper bound for flow of commodity k through edge m} \\ l_{m}^{k} &=& \text{ lower bound for flow of commodity k through edge m} \\ s_{k} &=& \text{ source of commodity k, } s_{k} \in V \\ t_{k} &=& \text{ target of commodity k, } t_{k} \in V \\ S_{k}^{+} &=& \text{ supply of source node } s_{k} \\ S_{k}^{-} &=& \text{ demand of target node } t_{k} \\ \end{eqnarray*}

\[ subject\text{ }to \]

\[ \sum_{e_{m} \in A_{n}} x_{m}^{k} - \sum_{e_{m} \in B_{n}} x_{m}^{k} = \begin{cases} S_{k}^{+} & \text{if } n=s_{k} \\ -S_{k}^{-} & \text{if } n=t_{k}\text{~~~~~~~~~~, }\forall\text{ }n,k \\ 0 & \text{otherwise} \end{cases} \]

\[ \text{, where} \]

\begin{eqnarray*} B_{n} &=& \{ e = (i,j) | j = n\text{ }i,j,n \in V\text{ }\forall e \in B_{n} \}\\ A_{n} &=& \{ e = (n,j) | i = n\text{ }i,j,n \in V\text{ }\forall e \in A_{n} \}\\ \end{eqnarray*}

\[ \sum_{k} x_{m}^{k} \leq b_{b}\text{, } \forall m \]

\[ 0 \leq x_{m}^{k} \leq u_{m}^{k}\text{, } \forall m,k \]

Solutions approaches vary from:

Edge:

Vertex:

Remarks:

We first solve this using a direct linear programming approach (see recommendation by Held, Wolfe, Crowder 1974), since we assume a small set of commodities ( <= 25) and transshipment nodes (< 50)

Edge needs to contain:

Usage example:

       BaseGraph::Ptr graph = BaseGraph::getInstance();
       uint32_t commodities = 3;
       uint32_t edgeCapacityUpperBound = 60;

       ...
       MultiCommodityMinCostFlow::vertex_t::Ptr v0( new MultiCommodityMinCostFlow::vertex_t(commodities));
       MultiCommodityMinCostFlow::vertex_t::Ptr v1( new MultiCommodityMinCostFlow::vertex_t(commodities));

       for(size_t i = 0; i < commodities; ++i)
       {
           // Source node
           v0->setCommoditySupply(i, 50);
           // Target node
           v1->setCommoditySupply(i, -50);
       }
       graph->addVertex(v0);
       graph->addVertex(v1);

       // Edges
       MultiCommodityMinCostFlow::edge_t::Ptr e0(new MultiCommodityMinCostFlow::edge_t(commodities));
       e0->setSourceVertex(v0);
       e0->setTargetVertex(v1);
       e0->setCapacityUpperBound(edgeCapacityUpperBound);
       for(size_t i = 0; i < commodities; ++i)
       {
           e0->setCommodityCapacityUpperBound(i, 20);
       }
       graph->addEdge(e0);


       MultiCommodityMinCostFlow minCostFlow(graph, commodities);
       LPSolver::Status status = minCostFlow.solve();
       double cost = minCostFlow.getCost();

       std::string file("/tmp/algorithm-multicommodity-mincostflow-2.");

       minCostFlow.storeResult();
       EdgeIterator::Ptr edgeIt = graph->getEdgeIterator();
       while(edgeIt->next())
       {
           MultiCommodityMinCostFlow::edge_t::Ptr edge = dynamic_pointer_cast<MultiCommodityMinCostFlow::edge_t>( edgeIt->current() );
           if(edge)
           {
               std::cout << "Flows on: " << edge->toString() << ": id " << graph->getEdgeId(edge) << std::endl;

               for(size_t i = 0; i < commodities; ++i)
               {
                   std::cout << "    commodity #" << i << ": " << edge->getCommodityFlow(i) << std::endl;
               }
           }
       }
       std::cout << "Final objective value: " << minCostFlow.getObjectiveValue();

Member Typedef Documentation

◆ edge_t

◆ vertex_t

◆ VertexCommodity

◆ VertexCommodityList

◆ VertexCommoditySet

Constructor & Destructor Documentation

◆ MultiCommodityMinCostFlow()

graph_analysis::algorithms::MultiCommodityMinCostFlow::MultiCommodityMinCostFlow ( const BaseGraph::Ptr &  graph,
uint32_t  commodities = 0,
LPSolver::Type  solverType = LPSolver::GLPK_SOLVER 
)
Parameters
graphthat contains only (!) MultiCommodityVertices and MultiCommodityEdges (!)
commoditiesnumber of commodities that need to be accounted for, if 0 is given then information is extracted from the edges
solverTypeThe type of LP solver to use

◆ ~MultiCommodityMinCostFlow()

virtual graph_analysis::algorithms::MultiCommodityMinCostFlow::~MultiCommodityMinCostFlow ( )
inlinevirtual

Default deconstructor

Member Function Documentation

◆ createProblem()

std::string graph_analysis::algorithms::MultiCommodityMinCostFlow::createProblem ( LPSolver::ProblemFormat  format = LPSolver::CPLEX)

Creates the problem instance and return the temporary file in which the problem is saved

◆ createProblemCPLEX()

std::string graph_analysis::algorithms::MultiCommodityMinCostFlow::createProblemCPLEX ( )

Create the problem in CPLEX format

◆ edgeConstraints()

std::set< Edge::Ptr > graph_analysis::algorithms::MultiCommodityMinCostFlow::edgeConstraints ( ) const

◆ fromFile()

MultiCommodityMinCostFlow graph_analysis::algorithms::MultiCommodityMinCostFlow::fromFile ( const std::string &  filename,
representation::Type  format = representation::UNKNOWN,
LPSolver::Type  solverType = LPSolver::GLPK_SOLVER 
)
static

Create a MultiCommodityMinCostFlow problem by loading (problem graph) from file

Parameters
filenameThe filename from which the problem should be read
formatThe format of the problem
solverTypeThe type of LP solver to use
Returns
min cost flow object

◆ getCost()

double graph_analysis::algorithms::MultiCommodityMinCostFlow::getCost ( ) const

Get the cost of the min cost flow

◆ save()

void graph_analysis::algorithms::MultiCommodityMinCostFlow::save ( const std::string &  filename,
representation::Type  format = representation::GEXF 
)

Save the MultiCommodityMinCostFlow problem to a file

◆ solve()

LPSolver::Status graph_analysis::algorithms::MultiCommodityMinCostFlow::solve ( const std::string &  solutionFile = "",
bool  useCaching = true 
)

Solve the multicommodity problem with the given LP solver save the solution to the given filename (or a temporary file), and store the flow status into the base graph with with which the problem has been initalized

Parameters
solutionFilethe filename to save the solution
useCachingTrue to tell the underlying LPSolver shall cache results, False otherwise

◆ storeResult()

void graph_analysis::algorithms::MultiCommodityMinCostFlow::storeResult ( )

Save the solution to a given file and format

◆ validateInflow()

std::vector< ConstraintViolation > graph_analysis::algorithms::MultiCommodityMinCostFlow::validateInflow ( ) const

Validate the result and return the number of violated inflow constraints

Returns
identified constraint violation of a provided solution

◆ vertexConstraints()

MultiCommodityMinCostFlow::VertexCommoditySet graph_analysis::algorithms::MultiCommodityMinCostFlow::vertexConstraints ( ) const

The documentation for this class was generated from the following files: