|
graph_analysis
|
#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) |
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:
Solutions approaches vary from:
Edge:
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();
| typedef std::pair<Vertex::Ptr, size_t> graph_analysis::algorithms::MultiCommodityMinCostFlow::VertexCommodity |
| typedef std::vector<VertexCommodity> graph_analysis::algorithms::MultiCommodityMinCostFlow::VertexCommodityList |
| typedef std::set<VertexCommodity> graph_analysis::algorithms::MultiCommodityMinCostFlow::VertexCommoditySet |
| graph_analysis::algorithms::MultiCommodityMinCostFlow::MultiCommodityMinCostFlow | ( | const BaseGraph::Ptr & | graph, |
| uint32_t | commodities = 0, |
||
| LPSolver::Type | solverType = LPSolver::GLPK_SOLVER |
||
| ) |
| graph | that contains only (!) MultiCommodityVertices and MultiCommodityEdges (!) |
| commodities | number of commodities that need to be accounted for, if 0 is given then information is extracted from the edges |
| solverType | The type of LP solver to use |
|
inlinevirtual |
Default deconstructor
| 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
| std::string graph_analysis::algorithms::MultiCommodityMinCostFlow::createProblemCPLEX | ( | ) |
Create the problem in CPLEX format
| std::set< Edge::Ptr > graph_analysis::algorithms::MultiCommodityMinCostFlow::edgeConstraints | ( | ) | const |
|
static |
Create a MultiCommodityMinCostFlow problem by loading (problem graph) from file
| filename | The filename from which the problem should be read |
| format | The format of the problem |
| solverType | The type of LP solver to use |
| double graph_analysis::algorithms::MultiCommodityMinCostFlow::getCost | ( | ) | const |
Get the cost of the min cost flow
| void graph_analysis::algorithms::MultiCommodityMinCostFlow::save | ( | const std::string & | filename, |
| representation::Type | format = representation::GEXF |
||
| ) |
Save the MultiCommodityMinCostFlow problem to a file
| 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
| solutionFile | the filename to save the solution |
| useCaching | True to tell the underlying LPSolver shall cache results, False otherwise |
| void graph_analysis::algorithms::MultiCommodityMinCostFlow::storeResult | ( | ) |
Save the solution to a given file and format
| std::vector< ConstraintViolation > graph_analysis::algorithms::MultiCommodityMinCostFlow::validateInflow | ( | ) | const |
Validate the result and return the number of violated inflow constraints
| MultiCommodityMinCostFlow::VertexCommoditySet graph_analysis::algorithms::MultiCommodityMinCostFlow::vertexConstraints | ( | ) | const |
1.8.17