/* -*- 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>
// Check the interface of an MCF algorithm
template <typename GR, typename Value>
checkConcept<concepts::Digraph, GR>();
const typename MCF::FlowMap &fm = mcf.flowMap();
const typename MCF::PotentialMap &pm = mcf.potentialMap();
double x = mcf.template totalCost<double>();
ignore_unused_variable_warning(fm);
ignore_unused_variable_warning(pm);
ignore_unused_variable_warning(x);
typedef typename GR::Node Node;
typedef typename GR::Arc Arc;
typedef concepts::ReadMap<Node, Value> NM;
typedef concepts::ReadMap<Arc, Value> AM;
typename MCF::FlowMap &flow;
typename MCF::PotentialMap &pot;
// 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)
if (sum != supply[n]) return false;
// 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 FM, typename PM >
bool checkPotential( const GR& gr, const LM& lower, const UM& upper,
const CM& cost, const FM& flow, const PM& pi )
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]);
// 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, bool mcf_result,
const GR& gr, const LM& lower, const UM& upper,
const CM& cost, const SM& supply,
bool result, typename CM::Value total,
const std::string &test_id = "" )
check(mcf_result == result, "Wrong result " + test_id);
check(checkFlow(gr, lower, upper, supply, mcf.flowMap()),
"The flow is not feasible " + test_id);
check(mcf.totalCost() == total, "The flow is not optimal " + test_id);
check(checkPotential(gr, lower, upper, cost, mcf.flowMap(),
"Wrong potentials " + test_id);
// TODO: This typedef should be enabled if the standard maps are
// reference maps in the graph concepts (See #190).
//typedef concepts::Digraph GR;
checkConcept< McfClassConcept<GR, Value>,
NetworkSimplex<GR, Value> >();
typedef ListDigraph Digraph;
DIGRAPH_TYPEDEFS(ListDigraph);
Digraph::ArcMap<int> c(gr), l1(gr), l2(gr), u(gr);
Digraph::NodeMap<int> s1(gr), s2(gr), s3(gr);
ConstMap<Arc, int> cc(1), cu(std::numeric_limits<int>::max());
std::istringstream input(test_lgf);
DigraphReader<Digraph>(gr, input)
// A. Test NetworkSimplex with the default pivot rule
NetworkSimplex<Digraph> mcf(gr);
mcf.upperMap(u).costMap(c);
checkMcf(mcf, mcf.supplyMap(s1).run(),
gr, l1, u, c, s1, true, 5240, "#A1");
checkMcf(mcf, mcf.stSupply(v, w, 27).run(),
gr, l1, u, c, s2, true, 7620, "#A2");
checkMcf(mcf, mcf.supplyMap(s1).run(),
gr, l2, u, c, s1, true, 5970, "#A3");
checkMcf(mcf, mcf.stSupply(v, w, 27).run(),
gr, l2, u, c, s2, true, 8010, "#A4");
checkMcf(mcf, mcf.supplyMap(s1).run(),
gr, l1, cu, cc, s1, true, 74, "#A5");
checkMcf(mcf, mcf.lowerMap(l2).stSupply(v, w, 27).run(),
gr, l2, cu, cc, s2, true, 94, "#A6");
gr, l1, cu, cc, s3, true, 0, "#A7");
checkMcf(mcf, mcf.boundMaps(l2, u).run(),
gr, l2, u, cc, s3, false, 0, "#A8");
// B. Test NetworkSimplex with each pivot rule
NetworkSimplex<Digraph> mcf(gr);
mcf.supplyMap(s1).costMap(c).capacityMap(u).lowerMap(l2);
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::FIRST_ELIGIBLE),
gr, l2, u, c, s1, true, 5970, "#B1");
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::BEST_ELIGIBLE),
gr, l2, u, c, s1, true, 5970, "#B2");
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::BLOCK_SEARCH),
gr, l2, u, c, s1, true, 5970, "#B3");
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::CANDIDATE_LIST),
gr, l2, u, c, s1, true, 5970, "#B4");
checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::ALTERING_LIST),
gr, l2, u, c, s1, true, 5970, "#B5");