alpar@2389
|
1 |
/* -*- C++ -*-
|
alpar@2389
|
2 |
*
|
alpar@2389
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@2389
|
4 |
*
|
alpar@2553
|
5 |
* Copyright (C) 2003-2008
|
alpar@2389
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@2389
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@2389
|
8 |
*
|
alpar@2389
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@2389
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@2389
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@2389
|
12 |
*
|
alpar@2389
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@2389
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@2389
|
15 |
* purpose.
|
alpar@2389
|
16 |
*
|
alpar@2389
|
17 |
*/
|
alpar@2389
|
18 |
|
alpar@2389
|
19 |
#ifndef LEMON_RANDOM_H
|
alpar@2389
|
20 |
#define LEMON_RANDOM_H
|
alpar@2389
|
21 |
|
alpar@2389
|
22 |
#include<iostream>
|
alpar@2389
|
23 |
#include<fstream>
|
alpar@2389
|
24 |
#include<string>
|
alpar@2389
|
25 |
|
alpar@2389
|
26 |
#include <lemon/dim2.h>
|
alpar@2389
|
27 |
|
alpar@2389
|
28 |
///\ingroup misc
|
alpar@2389
|
29 |
///\file
|
alpar@2389
|
30 |
///\brief Measure a Distribution
|
alpar@2389
|
31 |
///
|
alpar@2389
|
32 |
///\todo Needs lot more docs
|
alpar@2389
|
33 |
///
|
alpar@2389
|
34 |
|
alpar@2389
|
35 |
|
alpar@2389
|
36 |
namespace lemon {
|
alpar@2389
|
37 |
|
alpar@2389
|
38 |
///Measure a distribution
|
alpar@2389
|
39 |
class DistLog
|
alpar@2389
|
40 |
{
|
alpar@2389
|
41 |
std::vector<int> _dist;
|
alpar@2389
|
42 |
double _lo,_up;
|
alpar@2389
|
43 |
int _count;
|
alpar@2389
|
44 |
bool _cut;
|
alpar@2389
|
45 |
public:
|
alpar@2389
|
46 |
///\e
|
alpar@2389
|
47 |
Dist(double lo,double up,int gr,bool cut=true)
|
alpar@2389
|
48 |
: _dist(gr,0),_lo(lo),_up(up),_count(0),_cut(cut) {}
|
alpar@2389
|
49 |
///\e
|
alpar@2389
|
50 |
void operator()(double v)
|
alpar@2389
|
51 |
{
|
alpar@2389
|
52 |
if(_cut) {
|
alpar@2389
|
53 |
if(_lo<=v && v<_up)
|
alpar@2389
|
54 |
_dist[int((v-_lo)/(_up-_lo)*_dist.size())]++;
|
alpar@2389
|
55 |
}
|
alpar@2389
|
56 |
else {
|
alpar@2389
|
57 |
_dist[std::max(0,std::min(int(_dist.size())-1,
|
alpar@2389
|
58 |
int((v-_lo)/(_up-_lo)*_dist.size())
|
alpar@2389
|
59 |
))]++;
|
alpar@2389
|
60 |
}
|
alpar@2389
|
61 |
_count++;
|
alpar@2389
|
62 |
}
|
alpar@2389
|
63 |
///\e
|
alpar@2389
|
64 |
void dump(std::ostream& os=std::cout)
|
alpar@2389
|
65 |
{
|
alpar@2389
|
66 |
for(int i=0;i<_dist.size();i++)
|
alpar@2389
|
67 |
os << _lo+(i+0.5)*(_up-_lo)/_dist.size() << ' '
|
alpar@2389
|
68 |
<< double(_dist[i])/_count << std::endl;
|
alpar@2389
|
69 |
}
|
alpar@2389
|
70 |
///\e
|
alpar@2389
|
71 |
void dump(const std::string& file_name)
|
alpar@2389
|
72 |
{
|
alpar@2389
|
73 |
dump(std::ofstream(file_name.c_str()));
|
alpar@2389
|
74 |
}
|
alpar@2389
|
75 |
};
|
alpar@2389
|
76 |
|
alpar@2389
|
77 |
///Measure a two dimensional distribution
|
alpar@2389
|
78 |
class DistLog2
|
alpar@2389
|
79 |
{
|
alpar@2389
|
80 |
public:
|
alpar@2389
|
81 |
typedef dim2::Point<double> Point;
|
alpar@2389
|
82 |
private:
|
alpar@2389
|
83 |
std::vector<int> _dist;
|
alpar@2389
|
84 |
int _gr;
|
alpar@2389
|
85 |
Point _lo,_up;
|
alpar@2389
|
86 |
int _count;
|
alpar@2389
|
87 |
bool _cut;
|
alpar@2389
|
88 |
public:
|
alpar@2389
|
89 |
///\e
|
alpar@2389
|
90 |
Dist2(Point a,Point b,int gr,bool cut=true) :
|
alpar@2389
|
91 |
_dist(gr*gr,0),_gr(gr),
|
alpar@2389
|
92 |
_lo(a),_up(b),_count(0),_cut(cut) {}
|
alpar@2389
|
93 |
///\e
|
alpar@2389
|
94 |
Dist2(double lox,double upx,double loy,double upy,int gr,bool cut=true) :
|
alpar@2389
|
95 |
_dist(gr*gr,0),_gr(gr),
|
alpar@2389
|
96 |
_lo(Point(lox,loy)),_up(Point(upx,upy)),_count(0),_cut(cut) {}
|
alpar@2389
|
97 |
///\e
|
alpar@2389
|
98 |
void operator()(Point v)
|
alpar@2389
|
99 |
{
|
alpar@2389
|
100 |
if(_cut)
|
alpar@2389
|
101 |
{
|
alpar@2389
|
102 |
if(v.x>=_lo.x && v.x<_up.x && v.y>=_lo.y && v.y<_up.y)
|
alpar@2389
|
103 |
_dist[int((v.x-_lo.x)/(_up.x-_lo.x)*_gr)*_gr+
|
alpar@2389
|
104 |
int((v.y-_lo.y)/(_up.y-_lo.y)*_gr)]++;
|
alpar@2389
|
105 |
}
|
alpar@2389
|
106 |
else {
|
alpar@2389
|
107 |
_dist[std::max(0,std::min(_gr-1,
|
alpar@2389
|
108 |
int((v.x-_lo.x)/(_up.x-_lo.x)*_gr)
|
alpar@2389
|
109 |
))*_gr+
|
alpar@2389
|
110 |
std::max(0,std::min(_gr-1,
|
alpar@2389
|
111 |
int((v.y-_lo.y)/(_up.y-_lo.y)*_gr)
|
alpar@2389
|
112 |
))
|
alpar@2389
|
113 |
]++;
|
alpar@2389
|
114 |
}
|
alpar@2389
|
115 |
_count++;
|
alpar@2389
|
116 |
}
|
alpar@2389
|
117 |
///\e
|
alpar@2389
|
118 |
void dump(std::ostream& os=std::cout)
|
alpar@2389
|
119 |
{
|
alpar@2389
|
120 |
for(int i=0;i<_gr;i++)
|
alpar@2389
|
121 |
{
|
alpar@2389
|
122 |
for(int j=0;j<_gr;j++)
|
alpar@2389
|
123 |
os << _lo.x+(i+0.5)*(_up.x-_lo.x)/_gr << ' '
|
alpar@2389
|
124 |
<< _lo.y+(j+0.5)*(_up.y-_lo.y)/_gr << ' '
|
alpar@2389
|
125 |
<< double(_dist[i*_gr+j])/_count << std::endl;
|
alpar@2389
|
126 |
os << std::endl;
|
alpar@2389
|
127 |
}
|
alpar@2389
|
128 |
}
|
alpar@2389
|
129 |
///\e
|
alpar@2389
|
130 |
void dump(const std::string& file_name)
|
alpar@2389
|
131 |
{
|
alpar@2389
|
132 |
dump(std::ofstream(file_name.c_str()));
|
alpar@2389
|
133 |
}
|
alpar@2389
|
134 |
};
|
alpar@2389
|
135 |
}
|
alpar@2389
|
136 |
|
alpar@2389
|
137 |
#endif
|