benchmark/bench_tools.h
author kpeter
Thu, 13 Nov 2008 16:17:50 +0000
changeset 2630 d239741cfd44
parent 2391 14a343be7a5a
permissions -rw-r--r--
Various improvements in NetworkSimplex.

- Faster variant of "Altering Candidate List" pivot rule using make_heap
instead of partial_sort.
- Doc improvements.
- Removing unecessary inline keywords.
alpar@1956
     1
/* -*- C++ -*-
alpar@1956
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1956
     8
 *
alpar@1956
     9
 * Permission to use, modify and distribute this software is granted
alpar@1956
    10
 * provided that this copyright notice appears in all copies. For
alpar@1956
    11
 * precise terms see the accompanying LICENSE file.
alpar@1956
    12
 *
alpar@1956
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1956
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1956
    15
 * purpose.
alpar@1956
    16
 *
alpar@1956
    17
 */
alpar@1956
    18
alpar@921
    19
#ifndef LEMON_BENCH_TEST_H
alpar@921
    20
#define LEMON_BENCH_TEST_H
alpar@711
    21
alpar@711
    22
#include<vector>
alpar@718
    23
#include<iostream>
alpar@718
    24
alpar@1756
    25
#include<lemon/graph_utils.h>
alpar@921
    26
#include<lemon/time_measure.h>
alpar@711
    27
alpar@711
    28
///A primitive primtest
alpar@718
    29
alpar@718
    30
///\bug 2 is not a prime according to this function!
klao@979
    31
///
klao@979
    32
///\bug This function should go out of header file. I'm making it
klao@979
    33
/// inline for now.
klao@979
    34
inline bool isPrim(int n)
alpar@711
    35
{
alpar@711
    36
  if(n%2) {
alpar@711
    37
    for(int k=3;n/k>=k;k+=2)
alpar@711
    38
      if(!(n%k)) return false;
alpar@711
    39
    return true;
alpar@711
    40
  }
alpar@711
    41
  return false;
alpar@711
    42
}
alpar@711
    43
alpar@711
    44
///Finds the smallest prime not less then \c n.
klao@979
    45
klao@979
    46
///\bug This function should go out of header file. I'm making it
klao@979
    47
/// inline for now.
klao@979
    48
inline int nextPrim(int n)
alpar@711
    49
{
alpar@711
    50
  for(n+=!(n%2);!isPrim(n);n+=2) ;
alpar@711
    51
  return n;
alpar@711
    52
}
alpar@711
    53
alpar@711
    54
alpar@711
    55
/// Class to generate consecutive primes
alpar@711
    56
class Primes 
alpar@711
    57
{
alpar@711
    58
  std::vector<int> primes;
alpar@711
    59
  int n;
alpar@711
    60
  
alpar@711
    61
  bool isPrime(int m) 
alpar@711
    62
  {
alpar@711
    63
    for(int i=0;m<primes[i]*primes[i];i++) if(!(m%primes[i])) return false;
alpar@711
    64
    return true;
alpar@711
    65
  }
alpar@711
    66
public:
alpar@711
    67
  Primes() : n(1) {}
alpar@711
    68
  
alpar@711
    69
  int operator() ()
alpar@711
    70
    {
alpar@711
    71
      if(primes.size()==0) {
alpar@711
    72
	primes.push_back(2);
alpar@711
    73
	return 2;
alpar@711
    74
      }
alpar@711
    75
      else {
alpar@711
    76
	do n+=2; while(!isPrime(n));
alpar@711
    77
	primes.push_back(n);
alpar@711
    78
	return n;
alpar@711
    79
      }
alpar@711
    80
    }
alpar@711
    81
};
alpar@711
    82
deba@2386
    83
inline void PrintTime(const char *ID, lemon::Timer &T) 
alpar@718
    84
{
alpar@921
    85
  lemon::TimeStamp S(T);
alpar@1689
    86
  std::cout << ID << ' ' << S.userTime() << ' '
alpar@1689
    87
	    << S.systemTime() << ' ' << S.realTime() << std::endl;
alpar@718
    88
}
alpar@718
    89
alpar@742
    90
alpar@742
    91
alpar@742
    92
///
alpar@742
    93
template<class Graph>
alpar@1689
    94
void addHyperCube(Graph &G,int dim,std::vector<typename Graph::Node> &nodes)
alpar@742
    95
{
alpar@1756
    96
  GRAPH_TYPEDEFS(typename Graph);
alpar@718
    97
  
alpar@742
    98
  std::vector<int> bits(dim+1);
alpar@742
    99
  bits[0]=1;
alpar@742
   100
  for(int i=1;i<=dim;i++) bits[i]=2*bits[i-1];
alpar@742
   101
  
alpar@742
   102
  for(int i=0;i<bits[dim];i++) {
alpar@742
   103
    nodes.push_back(G.addNode());
alpar@742
   104
    for(int j=0;j<dim;j++) if(i&bits[j]) G.addEdge(nodes[i-bits[j]],nodes[i]);
alpar@742
   105
  }
alpar@742
   106
}
alpar@742
   107
alpar@742
   108
///
alpar@742
   109
template<class Graph>
alpar@1689
   110
void addBiDirHyperCube(Graph &G,int dim,std::vector<typename Graph::Node>&nodes)
alpar@742
   111
{
alpar@1756
   112
  GRAPH_TYPEDEFS(typename Graph);
alpar@742
   113
  
alpar@742
   114
  std::vector<int> bits(dim+1);
alpar@742
   115
  bits[0]=1;
alpar@742
   116
  for(int i=1;i<=dim;i++) bits[i]=2*bits[i-1];
alpar@742
   117
  
alpar@742
   118
  for(int i=0;i<bits[dim];i++) {
alpar@742
   119
    nodes.push_back(G.addNode());
alpar@742
   120
    for(int j=0;j<dim;j++) if(i&bits[j]) {
alpar@742
   121
      G.addEdge(nodes[i-bits[j]],nodes[i]);
alpar@742
   122
      G.addEdge(nodes[i],nodes[i-bits[j]]);
alpar@742
   123
    }
alpar@742
   124
    
alpar@742
   125
  }
alpar@742
   126
}  
alpar@711
   127
alpar@711
   128
#endif