| | 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 | |
| | 27 | using namespace lemon; |
| | 28 | |
| | 29 | static const unsigned int seed = 123456; |
| | 30 | |
| | 31 | bool prob(double p) |
| | 32 | { |
| | 33 | return rand() / static_cast<double>(RAND_MAX) < p; |
| | 34 | } |
| | 35 | |
| | 36 | template<typename G> |
| | 37 | int 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 | |
| | 68 | template<> |
| | 69 | int 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 | |
| | 85 | template<typename G> |
| | 86 | int 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 | |
| | 111 | int 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 | } |