COIN-OR::LEMON - Graph Library

Ticket #168: bp_matching_test_751a90ada614.patch

File bp_matching_test_751a90ada614.patch, 4.0 KB (added by Mohammed El-Kebir, 11 years ago)

Test cases for Maximum Weight Bipartite Matching (751a90ada614)

  • new file test/bp_matching_test.cc

    exporting patch:
    # HG changeset patch
    # User Mohammed El-Kebir <m.elkebir@gmail.com>
    # Date 1291391613 -3600
    # Node ID 751a90ada614a84aaaaccd7abd4c082aa1ca2b2a
    # Parent  b08198bdd6138ae9e8905579f81756961247a839
    Added test cases for maximum weight bipartite matching
    
    diff --git a/test/bp_matching_test.cc b/test/bp_matching_test.cc
    new file mode 100644
    - +  
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2 *
     3 * This file is a part of LEMON, a generic C++ optimization library.
     4 *
     5 * Copyright (C) 2003-2010
     6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8 *
     9 * Permission to use, modify and distribute this software is granted
     10 * provided that this copyright notice appears in all copies. For
     11 * precise terms see the accompanying LICENSE file.
     12 *
     13 * This software is provided "AS IS" with no warranty of any kind,
     14 * express or implied, and with no claim as to its suitability for any
     15 * purpose.
     16 *
     17 */
     18
     19#include <lemon/concepts/bpgraph.h>
     20#include <lemon/list_graph.h>
     21#include <lemon/smart_graph.h>
     22#include <lemon/full_graph.h>
     23#include <lemon/bp_matching.h>
     24
     25#include "test_tools.h"
     26
     27using namespace lemon;
     28
     29static const unsigned int seed = 123456;
     30
     31bool prob(double p)
     32{
     33  return rand() / static_cast<double>(RAND_MAX) < p;
     34}
     35
     36template<typename G>
     37int generateBpGraph(G& g,
     38                    typename G::template EdgeMap<int>& edgeWeight,
     39                    int nX, int nY, double p)
     40{
     41  int nEdges = 0;
     42 
     43  g.reserveNode(nX + nY);
     44  g.reserveEdge(static_cast<int>(nX * nY * p));
     45
     46  for (int x = 0; x < nX; x++)
     47    g.addRedNode();
     48
     49  for (int y = 0; y < nY; y++)
     50    g.addBlueNode();
     51
     52  for (typename G::RedIt x(g); x != INVALID; ++x)
     53  {
     54    for (typename G::BlueIt y(g); y != INVALID; ++y)
     55    {
     56      if (prob(p))
     57      {
     58        const typename G::Edge e = g.addEdge(x, y);
     59        edgeWeight[e] = (rand() % 100) + 1;
     60        nEdges++;
     61      }
     62    }
     63  }
     64
     65  return nEdges;
     66}
     67
     68template<>
     69int generateBpGraph<FullBpGraph>(FullBpGraph& g,
     70                                 FullBpGraph::EdgeMap<int>& edgeWeight,
     71                                 int nX, int nY, double)
     72{
     73  for (FullBpGraph::RedIt x(g); x != INVALID; ++x)
     74  {
     75    for (FullBpGraph::BlueIt y(g); y != INVALID; ++y)
     76    {
     77      const FullBpGraph::Edge e = g.edge(x, y);
     78      edgeWeight[e] = (rand() % 100) + 1;
     79    }
     80  }
     81
     82  return nX * nY;
     83}
     84
     85template<typename G>
     86int runMWBM(G& graph, int nX, int nY, double p)
     87{
     88  srand(seed);
     89
     90  typename G::template EdgeMap<int> weight(graph);
     91  generateBpGraph(graph, weight, nX, nY, p);
     92
     93  MaxWeightedBipartiteMatching<G> mwbm(graph, weight);
     94  mwbm.run();
     95
     96  MaxWeightedDenseBipartiteMatching<G> mwbm_d(graph, weight);
     97  mwbm_d.run();
     98
     99  check(mwbm.checkOptimality(),
     100        "MaxWeightedBipartiteMatching returned a sub-optimal matching");
     101
     102  check(mwbm.checkOptimality(),
     103        "MaxWeightedDenseBipartiteMatching returned a sub-optimal matching");
     104
     105  check(mwbm.matchingWeight() == mwbm_d.matchingWeight(),
     106      "The two methods returned different matchings");
     107
     108  return mwbm.matchingWeight();
     109}
     110
     111int main()
     112{
     113  static const int card_count = 3;
     114  static const int prob_count = 3;
     115
     116  const double prob[prob_count] = {0.01, 1};
     117  const int card[card_count] = {100, 500, 1000};
     118
     119  for (int i = 0; i < card_count; i++)
     120  {
     121    for (int j = 0; j < card_count; j++)
     122    {
     123      for (int k = 0; k < prob_count; k++)
     124      {
     125        int nX = card[i];
     126        int nY = card[j];
     127        double p = prob[k];
     128
     129        if (nY < nX)
     130          std::swap(nX, nY);
     131
     132        ListBpGraph list_G;
     133        SmartBpGraph smart_G;
     134        // FullBpGraph does not compile
     135
     136        int list_matching = runMWBM(list_G, nX, nY, p);
     137        int smart_matching = runMWBM(smart_G, nX, nY, p);
     138
     139        check(list_matching == smart_matching,
     140          "Different solutions for identical graphs (MWBM)");
     141      }
     142    }
     143  }
     144
     145  return 0;
     146}