/
ensemble.c
64 lines (51 loc) · 1.51 KB
/
ensemble.c
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
/* Agent-based opinion dynamics program.
Exactly one interaction per time step.
Fully connected homogeneous agents.
Makes an ensemble of runs. For each run, prints the absorption time and absorbing state.
Compile with:
gcc ensemble.c libopdyn.c -std=gnu99 -Wall -lm -lgsl -lgslcblas -o ensemble
Usage:
ensemble <number of agents> <number of opinions> <confidence bound> <number of runs> [biastype]
Output: Absorption time and final popularity vector of opinions (absorbing state) for each run.
*/
#include <stdio.h>
#include <stdlib.h>
#include "opdyn.h"
int main( int argc, char *argv[])
{
double bias = 0.5;
int biastype = 0;
int c; // counter
if(argc<5){
fprintf(stderr,
"Usage: ensemble N Q tol ensemblesize [biastype]\n"
"biastype 1: p = 0.5 + 0.5*|n-n'|/(n+n')\n"
"biastype 2: p = 0.5 + 0.5*|n-n'|/N\n");
exit(1);
}
int N = atoi(argv[1]);
int Q = atoi(argv[2]);
int tol = atoi(argv[3]);
int enssize = atoi(argv[4]);
if(argc>=6)
biastype = atoi(argv[5]);
opdyn_workspace *w = opdyn_workspace_alloc(N, Q, tol);
int x[N+1];
int endtime;
for (int k=1; k<=enssize; k++){
/* distribute opinions uniformly on agents */
for(c=1; c<=N; c++)
x[c] = (c % Q) + 1;
// Initialize
if(biastype==0)
opdyn_workspace_init(w, x, bias, Unbiased);
if(biastype==1)
opdyn_workspace_init(w, x, bias, BiasToMajority_pair);
if(biastype==2)
opdyn_workspace_init(w, x, bias, BiasToMajority_global);
endtime = run(w, 0, 0);
printf("%d ", endtime);
disp_nops(stdout, w);
}
return 0;
}