-
Notifications
You must be signed in to change notification settings - Fork 1
/
simcoverage.cpp
144 lines (127 loc) · 3.48 KB
/
simcoverage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* File: simcoverage.cpp
* Author: masoud
*
* Created on July 6, 2012, 5:31 PM
*/
#include <cstdlib>
#include <iostream>
#include <ctime>
#include "getopt_pp.h"
#include "utils.h"
using namespace std;
using namespace GetOpt;
/*
*
*/
unsigned int CouponCollector( unsigned int N,unsigned int d){
int *a=new int[N]; for(unsigned int i=0;i<N;i++) a[i]=i;
bool *V=new bool[N]; for(unsigned int i=0;i<N;i++) V[i]=false;
unsigned int nv=0,k=0;
while (nv<N){
Durstenfeld_shuffle(a,N,d);
//a[N-1]=RandInteger(N);
for(unsigned int j=1;j<=d;j++){
unsigned int t=a[N-j];//RandInteger(N);
if (!V[t]){
V[t]=true;
nv++;
}
}
k++;
//for(unsigned int i=0;i<N;i++) a[i]=i;
}
delete[] a;delete[] V;
return k;
}
unsigned int wallrandomwalk( unsigned int N,unsigned int d){
int *a=new int[N]; for(unsigned int i=0;i<N;i++) a[i]=i;
int *C=new int[N]; for(unsigned int i=0;i<N;i++) C[i]=1;
unsigned int k=0;
while (true){
unsigned int b1=RandInteger(N);
unsigned int b2=RandInteger(N-1);
if (b1==b2)
b2=N-1;
if(a[b1]!=a[b2]){
C[a[b1]]--;
C[a[b2]]++;
a[b1]=a[b2];
}
if(C[a[b2]]==N)
break;
k++;
}
delete[] a;delete[] C;
return k;
}
unsigned int PonderThisJuly12( unsigned int N,unsigned int M){
int *a=new int[N]; for(unsigned int i=0;i<N;i++) a[i]=i;
int *C=new int[N]; for(unsigned int i=0;i<N;i++) C[i]=1;
bool *V=new bool[M]; for(unsigned int i=0;i<M;i++) V[i]=false;
unsigned int mv=0,k=0;
while (true){
unsigned int b1=RandInteger(N);
unsigned int b2=RandInteger(N-1);
if (b1==b2)
b2=N-1;
if(a[b1]!=a[b2]){
C[a[b1]]--;
C[a[b2]]++;
a[b1]=a[b2];
}
if(C[a[b2]]==N)
break;
unsigned int t=RandInteger(M);
if (!V[t]){
V[t]=true;
mv++;
}
if(mv==M)
break;
k++;
}
delete[] a;delete[] V;delete[] C;
return mv==M?1:0;
}
int main(int argc, char** argv) {
unsigned long int rseed = static_cast<unsigned long int>(std::time(0));
unsigned int N=10,M=20,d=2,it=1000;
GetOpt_pp ops(argc, argv);
try {
ops.exceptions(std::ios::failbit);
ops
>> Option('n', N)
>> Option('m', M)
>> Option('d', d)
>> Option('i', it)
>> Option('x', "seed", rseed, rseed);
} catch (const GetOptEx& e) {
std::cerr << e.what();
std::cerr << "Invalid options or format\n";
//usage();
}
setrandseed(rseed);
//vector <unsigned int> a;
/*int *a=new int[N]; for(unsigned int i=0;i<N;i++) a[i]=i+1;
for(unsigned int i=0;i<it;i++){
//Durstenfeld_shuffle(a,d);
Durstenfeld_shuffle(a,N,d);
for(unsigned int j=1;j<=d;j++)
cout<<a[N-j]<<" ";
cout<<endl;
}
delete[] a;
*/
unsigned int s=0;
for(unsigned int i=0;i<it;i++){
//cout<<CouponCollector(N,d)<<endl;
//cout<<PonderThisJuly12(N,M)<<endl;
cout<<wallrandomwalk(N,d)<<endl;
//s+=PonderThisJuly12(N,M);
}
return 0;
cout<<"N="<<N<<", M="<<M<<endl;
cout<<(double)s/it<<endl;
return 0;
}