lemon/dist_log.h
author alpar
Tue, 05 Jun 2007 14:48:20 +0000
changeset 2448 ab899ae3505f
parent 2389 df6a32249b46
child 2553 bfced05fa852
permissions -rw-r--r--
Bugfix and improvement in -tsp2 algorithm
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2007
     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 #ifndef LEMON_RANDOM_H
    20 #define LEMON_RANDOM_H
    21 
    22 #include<iostream>
    23 #include<fstream>
    24 #include<string>
    25 
    26 #include <lemon/dim2.h>
    27 
    28 ///\ingroup misc
    29 ///\file
    30 ///\brief Measure a Distribution
    31 ///
    32 ///\todo Needs lot more docs
    33 ///
    34 
    35 
    36 namespace lemon {
    37 
    38   ///Measure a distribution
    39   class DistLog
    40   {
    41     std::vector<int> _dist;
    42     double _lo,_up;
    43     int _count;
    44     bool _cut;
    45   public:
    46     ///\e
    47     Dist(double lo,double up,int gr,bool cut=true) 
    48       : _dist(gr,0),_lo(lo),_up(up),_count(0),_cut(cut) {}
    49     ///\e
    50     void operator()(double v)
    51     {
    52       if(_cut) {
    53 	if(_lo<=v && v<_up)
    54 	  _dist[int((v-_lo)/(_up-_lo)*_dist.size())]++;
    55       }
    56       else {
    57 	_dist[std::max(0,std::min(int(_dist.size())-1,
    58 				  int((v-_lo)/(_up-_lo)*_dist.size())
    59 				  ))]++;
    60       }
    61       _count++;
    62     }
    63     ///\e
    64     void dump(std::ostream& os=std::cout)
    65     {
    66       for(int i=0;i<_dist.size();i++)
    67 	os << _lo+(i+0.5)*(_up-_lo)/_dist.size() << ' '
    68 	   << double(_dist[i])/_count << std::endl;
    69     }
    70     ///\e
    71     void dump(const std::string& file_name)
    72     {
    73       dump(std::ofstream(file_name.c_str()));
    74     }
    75   };
    76   
    77   ///Measure a two dimensional distribution
    78   class DistLog2
    79   {
    80   public:
    81     typedef dim2::Point<double> Point;
    82   private:
    83     std::vector<int> _dist;
    84     int _gr;
    85     Point _lo,_up;
    86     int _count;
    87     bool _cut;
    88   public:  
    89     ///\e
    90     Dist2(Point a,Point b,int gr,bool cut=true) :
    91       _dist(gr*gr,0),_gr(gr),
    92       _lo(a),_up(b),_count(0),_cut(cut) {}
    93     ///\e
    94     Dist2(double lox,double upx,double loy,double upy,int gr,bool cut=true) :
    95       _dist(gr*gr,0),_gr(gr),
    96       _lo(Point(lox,loy)),_up(Point(upx,upy)),_count(0),_cut(cut) {}
    97     ///\e
    98     void operator()(Point v)
    99     {
   100       if(_cut)
   101 	{
   102 	  if(v.x>=_lo.x && v.x<_up.x && v.y>=_lo.y && v.y<_up.y)
   103 	    _dist[int((v.x-_lo.x)/(_up.x-_lo.x)*_gr)*_gr+
   104 		  int((v.y-_lo.y)/(_up.y-_lo.y)*_gr)]++;
   105 	}
   106       else {
   107 	_dist[std::max(0,std::min(_gr-1,
   108 				  int((v.x-_lo.x)/(_up.x-_lo.x)*_gr)
   109 				  ))*_gr+
   110 	      std::max(0,std::min(_gr-1,
   111 				  int((v.y-_lo.y)/(_up.y-_lo.y)*_gr)
   112 				  ))
   113 	      ]++;
   114       }
   115       _count++;
   116     }
   117     ///\e
   118     void dump(std::ostream& os=std::cout)
   119     {
   120       for(int i=0;i<_gr;i++)
   121 	{
   122 	  for(int j=0;j<_gr;j++)
   123 	    os << _lo.x+(i+0.5)*(_up.x-_lo.x)/_gr << ' '
   124 	       << _lo.y+(j+0.5)*(_up.y-_lo.y)/_gr << ' '
   125 	       << double(_dist[i*_gr+j])/_count << std::endl;
   126 	  os << std::endl;
   127 	}
   128     }
   129     ///\e
   130     void dump(const std::string& file_name)
   131     {
   132       dump(std::ofstream(file_name.c_str()));
   133     }
   134   };
   135 }
   136 
   137 #endif