/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
#include <lemon/list_graph.h>
#include <lemon/lgf_reader.h>
#include <lemon/network_simplex.h>
#include <lemon/concepts/digraph.h>
#include <lemon/concept_check.h>
"label sup1 sup2 sup3 sup4 sup5 sup6\n"
" 12 -20 -27 0 -30 -30 -20\n"
" cost cap low1 low2 low3\n"
// Check the interface of an MCF algorithm
template <typename GR, typename Value, typename Cost>
checkConcept<concepts::Digraph, GR>();
const MCF& const_mcf = mcf;
c = const_mcf.totalCost();
x = const_mcf.template totalCost<double>();
c = const_mcf.potential(n);
const_mcf.potentialMap(pm);
typedef typename GR::Node Node;
typedef typename GR::Arc Arc;
typedef concepts::ReadMap<Node, Value> NM;
typedef concepts::ReadMap<Arc, Value> VAM;
typedef concepts::ReadMap<Arc, Cost> CAM;
typedef concepts::WriteMap<Arc, Value> FlowMap;
typedef concepts::WriteMap<Node, Cost> PotMap;
// Check the feasibility of the given flow (primal soluiton)
template < typename GR, typename LM, typename UM,
typename SM, typename FM >
bool checkFlow( const GR& gr, const LM& lower, const UM& upper,
const SM& supply, const FM& flow,
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
for (ArcIt e(gr); e != INVALID; ++e) {
if (flow[e] < lower[e] || flow[e] > upper[e]) return false;
for (NodeIt n(gr); n != INVALID; ++n) {
typename SM::Value sum = 0;
for (OutArcIt e(gr, n); e != INVALID; ++e)
for (InArcIt e(gr, n); e != INVALID; ++e)
bool b = (type == EQ && sum == supply[n]) ||
(type == GEQ && sum >= supply[n]) ||
(type == LEQ && sum <= supply[n]);
// Check the feasibility of the given potentials (dual soluiton)
// using the "Complementary Slackness" optimality condition
template < typename GR, typename LM, typename UM,
typename CM, typename SM, typename FM, typename PM >
bool checkPotential( const GR& gr, const LM& lower, const UM& upper,
const CM& cost, const SM& supply, const FM& flow,
const PM& pi, SupplyType type )
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
for (ArcIt e(gr); opt && e != INVALID; ++e) {
typename CM::Value red_cost =
cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
(red_cost > 0 && flow[e] == lower[e]) ||
(red_cost < 0 && flow[e] == upper[e]);
for (NodeIt n(gr); opt && n != INVALID; ++n) {
typename SM::Value sum = 0;
for (OutArcIt e(gr, n); e != INVALID; ++e)
for (InArcIt e(gr, n); e != INVALID; ++e)
opt = (pi[n] <= 0) && (sum == supply[n] || pi[n] == 0);
opt = (pi[n] >= 0) && (sum == supply[n] || pi[n] == 0);
// Check whether the dual cost is equal to the primal cost
template < typename GR, typename LM, typename UM,
typename CM, typename SM, typename PM >
bool checkDualCost( const GR& gr, const LM& lower, const UM& upper,
const CM& cost, const SM& supply, const PM& pi,
typename CM::Value total )
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
typename CM::Value dual_cost = 0;
for (NodeIt n(gr); n != INVALID; ++n) {
red_supply[n] = supply[n];
for (ArcIt a(gr); a != INVALID; ++a) {
dual_cost += lower[a] * cost[a];
red_supply[gr.source(a)] -= lower[a];
red_supply[gr.target(a)] += lower[a];
for (NodeIt n(gr); n != INVALID; ++n) {
dual_cost -= red_supply[n] * pi[n];
for (ArcIt a(gr); a != INVALID; ++a) {
typename CM::Value red_cost =
cost[a] + pi[gr.source(a)] - pi[gr.target(a)];
dual_cost -= (upper[a] - lower[a]) * std::max(-red_cost, 0);
return dual_cost == total;
// Run a minimum cost flow algorithm and check the results
template < typename MCF, typename GR,
typename LM, typename UM,
typename CM, typename SM,
void checkMcf( const MCF& mcf, PT mcf_result,
const GR& gr, const LM& lower, const UM& upper,
const CM& cost, const SM& supply,
PT result, bool optimal, typename CM::Value total,
const std::string &test_id = "",
check(mcf_result == result, "Wrong result " + test_id);
typename GR::template ArcMap<typename SM::Value> flow(gr);
typename GR::template NodeMap<typename CM::Value> pi(gr);
check(checkFlow(gr, lower, upper, supply, flow, type),
"The flow is not feasible " + test_id);
check(mcf.totalCost() == total, "The flow is not optimal " + test_id);
check(checkPotential(gr, lower, upper, cost, supply, flow, pi, type),
"Wrong potentials " + test_id);
check(checkDualCost(gr, lower, upper, cost, supply, pi, total),
"Wrong dual cost " + test_id);
typedef concepts::Digraph GR;
checkConcept< McfClassConcept<GR, int, int>,
checkConcept< McfClassConcept<GR, double, double>,
NetworkSimplex<GR, double> >();
checkConcept< McfClassConcept<GR, int, double>,
NetworkSimplex<GR, int, double> >();
typedef ListDigraph Digraph;
DIGRAPH_TYPEDEFS(ListDigraph);
Digraph::ArcMap<int> c(gr), l1(gr), l2(gr), l3(gr), u(gr);
Digraph::NodeMap<int> s1(gr), s2(gr), s3(gr), s4(gr), s5(gr), s6(gr);
ConstMap<Arc, int> cc(1), cu(std::numeric_limits<int>::max());
std::istringstream input(test_lgf);
DigraphReader<Digraph>(gr, input)
// Build test digraphs with negative costs
Node n1 = neg_gr.addNode();
Node n2 = neg_gr.addNode();
Node n3 = neg_gr.addNode();
Node n4 = neg_gr.addNode();
Node n5 = neg_gr.addNode();
Node n6 = neg_gr.addNode();
Node n7 = neg_gr.addNode();
Arc a1 = neg_gr.addArc(n1, n2);
Arc a2 = neg_gr.addArc(n1, n3);
Arc a3 = neg_gr.addArc(n2, n4);
Arc a4 = neg_gr.addArc(n3, n4);
Arc a5 = neg_gr.addArc(n3, n2);
Arc a6 = neg_gr.addArc(n5, n3);
Arc a7 = neg_gr.addArc(n5, n6);
Arc a8 = neg_gr.addArc(n6, n7);
Arc a9 = neg_gr.addArc(n7, n5);
Digraph::ArcMap<int> neg_c(neg_gr), neg_l1(neg_gr, 0), neg_l2(neg_gr, 0);
ConstMap<Arc, int> neg_u1(std::numeric_limits<int>::max()), neg_u2(5000);
Digraph::NodeMap<int> neg_s(neg_gr, 0);
Digraph::NodeMap<int> negs_s(negs_gr);
Digraph::ArcMap<int> negs_c(negs_gr);
ConstMap<Arc, int> negs_l(0), negs_u(1000);
negs_c[negs_gr.addArc(n1, n2)] = -1;
// A. Test NetworkSimplex with the default pivot rule
NetworkSimplex<Digraph> mcf(gr);
// Check the equality form
mcf.upperMap(u).costMap(c);
checkMcf(mcf, mcf.supplyMap(s1).run(),
gr, l1, u, c, s1, mcf.OPTIMAL, true, 5240, "#A1");
checkMcf(mcf, mcf.stSupply(v, w, 27).run(),
gr, l1, u, c, s2, mcf.OPTIMAL, true, 7620, "#A2");
checkMcf(mcf, mcf.supplyMap(s1).run(),
gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#A3");
checkMcf(mcf, mcf.stSupply(v, w, 27).run(),
gr, l2, u, c, s2, mcf.OPTIMAL, true, 8010, "#A4");
checkMcf(mcf, mcf.supplyMap(s1).run(),
gr, l1, cu, cc, s1, mcf.OPTIMAL, true, 74, "#A5");
checkMcf(mcf, mcf.lowerMap(l2).stSupply(v, w, 27).run(),
gr, l2, cu, cc, s2, mcf.OPTIMAL, true, 94, "#A6");
gr, l1, cu, cc, s3, mcf.OPTIMAL, true, 0, "#A7");
checkMcf(mcf, mcf.lowerMap(l2).upperMap(u).run(),
gr, l2, u, cc, s3, mcf.INFEASIBLE, false, 0, "#A8");
mcf.reset().lowerMap(l3).upperMap(u).costMap(c).supplyMap(s4);
gr, l3, u, c, s4, mcf.OPTIMAL, true, 6360, "#A9");
mcf.reset().upperMap(u).costMap(c).supplyMap(s5);
gr, l1, u, c, s5, mcf.OPTIMAL, true, 3530, "#A10", GEQ);
checkMcf(mcf, mcf.lowerMap(l2).run(),
gr, l2, u, c, s5, mcf.OPTIMAL, true, 4540, "#A11", GEQ);
gr, l2, u, c, s6, mcf.INFEASIBLE, false, 0, "#A12", GEQ);
mcf.reset().supplyType(mcf.LEQ);
mcf.upperMap(u).costMap(c).supplyMap(s6);
gr, l1, u, c, s6, mcf.OPTIMAL, true, 5080, "#A13", LEQ);
checkMcf(mcf, mcf.lowerMap(l2).run(),
gr, l2, u, c, s6, mcf.OPTIMAL, true, 5930, "#A14", LEQ);
gr, l2, u, c, s5, mcf.INFEASIBLE, false, 0, "#A15", LEQ);
NetworkSimplex<Digraph> neg_mcf(neg_gr);
neg_mcf.lowerMap(neg_l1).costMap(neg_c).supplyMap(neg_s);
checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l1, neg_u1,
neg_c, neg_s, neg_mcf.UNBOUNDED, false, 0, "#A16");
neg_mcf.upperMap(neg_u2);
checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l1, neg_u2,
neg_c, neg_s, neg_mcf.OPTIMAL, true, -40000, "#A17");
neg_mcf.reset().lowerMap(neg_l2).costMap(neg_c).supplyMap(neg_s);
checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l2, neg_u1,
neg_c, neg_s, neg_mcf.UNBOUNDED, false, 0, "#A18");
NetworkSimplex<Digraph> negs_mcf(negs_gr);
negs_mcf.costMap(negs_c).supplyMap(negs_s);
checkMcf(negs_mcf, negs_mcf.run(), negs_gr, negs_l, negs_u,
negs_c, negs_s, negs_mcf.OPTIMAL, true, -300, "#A19", GEQ);
// B. Test NetworkSimplex with each pivot rule
NetworkSimplex<Digraph> mcf(gr);
mcf.supplyMap(s1).costMap(c).upperMap(u).lowerMap(l2);
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::FIRST_ELIGIBLE),
gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B1");
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::BEST_ELIGIBLE),
gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B2");
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::BLOCK_SEARCH),
gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B3");
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::CANDIDATE_LIST),
gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B4");
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::ALTERING_LIST),
gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B5");