test/max_cardinality_search_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 02 Apr 2015 13:36:16 +0200
changeset 1328 d51126dc39fa
parent 1088 7f6eeffe3cd1
permissions -rw-r--r--
Resolve MSVC conversion warnings C4244 (#519)
thoneyvazul@1020
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
thoneyvazul@1020
     2
 *
thoneyvazul@1020
     3
 * This file is a part of LEMON, a generic C++ optimization library.
thoneyvazul@1020
     4
 *
alpar@1270
     5
 * Copyright (C) 2003-2013
thoneyvazul@1020
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
thoneyvazul@1020
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
thoneyvazul@1020
     8
 *
thoneyvazul@1020
     9
 * Permission to use, modify and distribute this software is granted
thoneyvazul@1020
    10
 * provided that this copyright notice appears in all copies. For
thoneyvazul@1020
    11
 * precise terms see the accompanying LICENSE file.
thoneyvazul@1020
    12
 *
thoneyvazul@1020
    13
 * This software is provided "AS IS" with no warranty of any kind,
thoneyvazul@1020
    14
 * express or implied, and with no claim as to its suitability for any
thoneyvazul@1020
    15
 * purpose.
thoneyvazul@1020
    16
 *
thoneyvazul@1020
    17
 */
thoneyvazul@1020
    18
thoneyvazul@1020
    19
#include <iostream>
thoneyvazul@1020
    20
thoneyvazul@1020
    21
#include "test_tools.h"
thoneyvazul@1020
    22
#include <lemon/smart_graph.h>
thoneyvazul@1020
    23
#include <lemon/max_cardinality_search.h>
thoneyvazul@1020
    24
#include <lemon/concepts/digraph.h>
thoneyvazul@1020
    25
#include <lemon/concepts/maps.h>
thoneyvazul@1020
    26
#include <lemon/concepts/heap.h>
thoneyvazul@1020
    27
#include <lemon/lgf_reader.h>
thoneyvazul@1020
    28
thoneyvazul@1020
    29
using namespace lemon;
thoneyvazul@1020
    30
using namespace std;
thoneyvazul@1020
    31
thoneyvazul@1020
    32
char test_lgf[] =
thoneyvazul@1020
    33
  "@nodes\n"
thoneyvazul@1020
    34
  "label\n"
thoneyvazul@1020
    35
  "0\n"
thoneyvazul@1020
    36
  "1\n"
thoneyvazul@1020
    37
  "2\n"
thoneyvazul@1020
    38
  "3\n"
thoneyvazul@1020
    39
  "@arcs\n"
thoneyvazul@1020
    40
  "    label capacity\n"
thoneyvazul@1020
    41
  "0 1 0     2\n"
thoneyvazul@1020
    42
  "1 0 1     2\n"
thoneyvazul@1020
    43
  "2 1 2     1\n"
thoneyvazul@1020
    44
  "2 3 3     3\n"
thoneyvazul@1020
    45
  "3 2 4     3\n"
thoneyvazul@1020
    46
  "3 1 5     5\n"
thoneyvazul@1020
    47
  "@attributes\n"
thoneyvazul@1020
    48
  "s 0\n"
thoneyvazul@1020
    49
  "x 1\n"
thoneyvazul@1020
    50
  "y 2\n"
thoneyvazul@1020
    51
  "z 3\n";
thoneyvazul@1020
    52
thoneyvazul@1020
    53
void checkMaxCardSearchCompile() {
thoneyvazul@1020
    54
thoneyvazul@1020
    55
  typedef concepts::Digraph Digraph;
thoneyvazul@1020
    56
  typedef int Value;
thoneyvazul@1020
    57
  typedef Digraph::Node Node;
thoneyvazul@1020
    58
  typedef Digraph::Arc Arc;
thoneyvazul@1020
    59
  typedef concepts::ReadMap<Arc,Value> CapMap;
thoneyvazul@1020
    60
  typedef concepts::ReadWriteMap<Node,Value> CardMap;
thoneyvazul@1020
    61
  typedef concepts::ReadWriteMap<Node,bool> ProcMap;
thoneyvazul@1020
    62
  typedef Digraph::NodeMap<int> HeapCrossRef;
thoneyvazul@1020
    63
thoneyvazul@1020
    64
  Digraph g;
thoneyvazul@1020
    65
  Node n,s;
thoneyvazul@1020
    66
  CapMap cap;
thoneyvazul@1020
    67
  CardMap card;
thoneyvazul@1020
    68
  ProcMap proc;
thoneyvazul@1020
    69
  HeapCrossRef crossref(g);
alpar@1270
    70
thoneyvazul@1020
    71
  typedef MaxCardinalitySearch<Digraph,CapMap>
thoneyvazul@1020
    72
    ::SetCapacityMap<CapMap>
thoneyvazul@1020
    73
    ::SetCardinalityMap<CardMap>
thoneyvazul@1020
    74
    ::SetProcessedMap<ProcMap>
thoneyvazul@1020
    75
    ::SetStandardHeap<BinHeap<Value,HeapCrossRef> >
thoneyvazul@1020
    76
    ::Create MaxCardType;
thoneyvazul@1020
    77
thoneyvazul@1020
    78
  MaxCardType maxcard(g,cap);
thoneyvazul@1020
    79
  const MaxCardType& const_maxcard = maxcard;
thoneyvazul@1020
    80
thoneyvazul@1020
    81
  const MaxCardType::Heap& heap_const = const_maxcard.heap();
thoneyvazul@1020
    82
  MaxCardType::Heap& heap = const_cast<MaxCardType::Heap&>(heap_const);
thoneyvazul@1020
    83
  maxcard.heap(heap,crossref);
alpar@1270
    84
thoneyvazul@1020
    85
  maxcard.capacityMap(cap).cardinalityMap(card).processedMap(proc);
thoneyvazul@1020
    86
thoneyvazul@1020
    87
  maxcard.init();
thoneyvazul@1020
    88
  maxcard.addSource(s);
thoneyvazul@1020
    89
  n = maxcard.nextNode();
thoneyvazul@1020
    90
   maxcard.processNextNode();
thoneyvazul@1020
    91
   maxcard.start();
thoneyvazul@1020
    92
   maxcard.run(s);
thoneyvazul@1020
    93
   maxcard.run();
thoneyvazul@1020
    94
 }
thoneyvazul@1020
    95
thoneyvazul@1020
    96
 void checkWithIntMap( std::istringstream& input)
thoneyvazul@1020
    97
 {
thoneyvazul@1020
    98
   typedef SmartDigraph Digraph;
thoneyvazul@1020
    99
   typedef Digraph::Node Node;
thoneyvazul@1020
   100
   typedef Digraph::ArcMap<int> CapMap;
thoneyvazul@1020
   101
thoneyvazul@1020
   102
   Digraph g;
thoneyvazul@1020
   103
   Node s,x,y,z,a;
thoneyvazul@1020
   104
   CapMap cap(g);
thoneyvazul@1020
   105
thoneyvazul@1020
   106
   DigraphReader<Digraph>(g,input).
thoneyvazul@1020
   107
     arcMap("capacity", cap).
thoneyvazul@1020
   108
     node("s",s).
thoneyvazul@1020
   109
     node("x",x).
thoneyvazul@1020
   110
     node("y",y).
thoneyvazul@1020
   111
     node("z",z).
thoneyvazul@1020
   112
     run();
thoneyvazul@1020
   113
thoneyvazul@1020
   114
   MaxCardinalitySearch<Digraph,CapMap> maxcard(g,cap);
thoneyvazul@1020
   115
thoneyvazul@1020
   116
   maxcard.init();
thoneyvazul@1020
   117
   maxcard.addSource(s);
thoneyvazul@1020
   118
   maxcard.start(x);
thoneyvazul@1020
   119
kpeter@1088
   120
   check(maxcard.processed(s) && !maxcard.processed(x) &&
thoneyvazul@1020
   121
         !maxcard.processed(y), "Wrong processed()!");
thoneyvazul@1020
   122
thoneyvazul@1020
   123
   a=maxcard.nextNode();
thoneyvazul@1020
   124
   check(maxcard.processNextNode()==a,
thoneyvazul@1020
   125
         "Wrong nextNode() or processNextNode() return value!");
thoneyvazul@1020
   126
thoneyvazul@1020
   127
   check(maxcard.processed(a), "Wrong processNextNode()!");
thoneyvazul@1020
   128
thoneyvazul@1020
   129
   maxcard.start();
kpeter@1088
   130
   check(maxcard.cardinality(x)==2 && maxcard.cardinality(y)>=4,
thoneyvazul@1020
   131
         "Wrong cardinalities!");
thoneyvazul@1020
   132
 }
thoneyvazul@1020
   133
thoneyvazul@1020
   134
 void checkWithConst1Map(std::istringstream &input) {
thoneyvazul@1020
   135
   typedef SmartDigraph Digraph;
thoneyvazul@1020
   136
   typedef Digraph::Node Node;
thoneyvazul@1020
   137
thoneyvazul@1020
   138
   Digraph g;
thoneyvazul@1020
   139
   Node s,x,y,z;
thoneyvazul@1020
   140
thoneyvazul@1020
   141
  DigraphReader<Digraph>(g,input).
thoneyvazul@1020
   142
    node("s",s).
thoneyvazul@1020
   143
    node("x",x).
thoneyvazul@1020
   144
    node("y",y).
thoneyvazul@1020
   145
    node("z",z).
thoneyvazul@1020
   146
    run();
thoneyvazul@1020
   147
thoneyvazul@1020
   148
  MaxCardinalitySearch<Digraph> maxcard(g);
thoneyvazul@1020
   149
  maxcard.run(s);
thoneyvazul@1020
   150
  check(maxcard.cardinality(x)==1 &&
thoneyvazul@1020
   151
        maxcard.cardinality(y)+maxcard.cardinality(z)==3,
thoneyvazul@1020
   152
        "Wrong cardinalities!");
thoneyvazul@1020
   153
}
thoneyvazul@1020
   154
thoneyvazul@1020
   155
int main() {
thoneyvazul@1020
   156
thoneyvazul@1020
   157
  std::istringstream input1(test_lgf);
thoneyvazul@1020
   158
  checkWithIntMap(input1);
thoneyvazul@1020
   159
thoneyvazul@1020
   160
  std::istringstream input2(test_lgf);
thoneyvazul@1020
   161
  checkWithConst1Map(input2);
thoneyvazul@1020
   162
}