-
Notifications
You must be signed in to change notification settings - Fork 0
/
unhomogeneus_crude1_csv.c
412 lines (303 loc) · 8.26 KB
/
unhomogeneus_crude1_csv.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#define U genrand_real1() \
\
/*1:*/
#line 120 "./crude1.w"
/*2:*/
#line 140 "./crude1.w"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include"mt19937ar.c"
/*:2*/
#line 122 "./crude1.w"
;
/*4:*/
#line 162 "./crude1.w"
typedef struct link {
int adj;
double rlb;
int smp;
} lnk, *pt_lnk;
typedef struct network {
int s;
int t;
int numnodes;
int numlinks;
struct link**I;
} net, *pt_net;
int**list;
int*visited;
int connected;
int seed;
double breadth = 2.0;
double l;
double beta;
double reliability;
int indicator;
double tempL = 0.0;
/*:4*/
#line 123 "./crude1.w"
;
/*5:*/
#line 211 "./crude1.w"
pt_net Initialize(char*filename);
int X(int node1, int node2, pt_net nt);
void Fail(pt_net nt);
void DFS(int node, pt_net nt);
int Phi(pt_net nt);
/*:5*/
#line 124 "./crude1.w"
;
int main(int argc, char*argv[]) {
/*6:*/
#line 235 "./crude1.w"
/*Start of actual implementation*/
int i, j, k, size, X;
double V, Q, t, S;
pt_net n;
clock_t ti;
/*:6*/
#line 127 "./crude1.w"
;
/*7:*/
#line 254 "./crude1.w"
if (argc < 4) {
printf("\n Some input data is missing! Run as:\n");
printf("\n ./executable <File> <Size> <Seed>\n\n");
exit(1);
}
if (argc > 4) {
printf("\n You've entered more data than necessary! Run as:\n\n");
printf("\n ./executable <File> <Size> <Seed>\n\n");
exit(1);
}
if ((size = atoi(argv[2])) < 1) {
printf("\n The number of trials can not be less than 1! Run as:\n");
printf("\n ./executable <File> <Size> <Seed>\n\n");
exit(1);
}
if ((seed = atoi(argv[3])) < 0) {
printf("\n The seed can not be negative! Run as:\n");
printf("\n ./executable <File> <Size> <Seed>\n\n");
exit(1);
}
/*:7*/
#line 128 "./crude1.w"
;
/*8:*/
#line 289 "./crude1.w"
n = Initialize(argv[1]);
/*minimum cut set in relation to nodes in network -> how critical is the min cut set in relation to overall network size?*/
ti = clock();
beta = 0.0;
FILE*fp = fopen("results_try.csv", "w+");
for (reliability = 0.7; reliability <= 0.999; reliability += 0.0001) {
S = 0.0;
X = 0.0;
V = 0.0;
beta = 0.5; // best case for wheatbridge
double a;
/* start of monte carlo logic*/
for (k = 0; k < size; k++) {
/* reset l for current monte carlo trial*/
l = 1.0;
/* set link random fails and builds the adjacency lists after that fail */
Fail(n);
/* Phi: if node t is reached Phi ( ) returns 1, otherwise it returns 0 */
/* Deviation metric*/
X = (1 - Phi(n));
if (X == 1) {
S += l;
indicator++;
V += l*l;
}
//V+= X*X;
}
/* compare successes to size as number of monte carlo trials*/
/* sum/N*/
Q = (double) S / size;
/*estimator of the variance*/
V = (V / size - Q * Q) / (size - 1);
t = (double) (clock() - ti) / CLOCKS_PER_SEC;
/*:8*/
#line 129 "./crude1.w"
;
/*9:*/
#line 319 "./crude1.w"
printf("\n Network: %s Replications: %d ExecTime=%f", argv[1], size, t);
printf("\n ******************* CRUDE MONTE CARLO (1) ******************");
printf("\n reliability b = %f", reliability);
printf("\n Unreliability Q = %1.16f = %1.2e", Q, Q);
printf("\n Success S = %d", indicator);
printf("\n Variance V = %1.16f = %1.2e", V, V);
printf("\n Std. Dev. SD = %1.16f = %1.2e", sqrt(V), sqrt(V));
printf("\n Relative Error RE = %1.16f = %1.2f%%", sqrt(V) / Q,
100 * sqrt(V) / Q);
printf("\n ************************************************************\n\n");
//printing to the file
fprintf(fp, "\n%f, %6.16f, %6.16f,%6.16f", reliability, Q, V,sqrt(V));
}
fclose(fp);
/*:9*/
#line 130 "./crude1.w"
;
return 0;
}
/*10:*/
#line 336 "./crude1.w"
/*11:*/
#line 345 "./crude1.w"
pt_net Initialize(char*filename) {
pt_net pt_n;
FILE*fp;
int i, j, node1, node2, num;
double reliability;
if ((pt_n = (pt_net) calloc(1, sizeof (net))) == NULL) {
printf("\n Fail attempting to allocate memory...\n");
exit(1);
}
if ((fp = fopen(filename, "r")) == NULL) {
printf("\n Fail attempting to open a disk file...\n");
exit(1);
}
fscanf(fp, "%d", &pt_n->s);
fscanf(fp, "%d", &pt_n->t);
fscanf(fp, "%d", &pt_n->numnodes);
fscanf(fp, "%d", &pt_n->numlinks);
if ((pt_n->I = (pt_lnk*) calloc(pt_n->numnodes + 1, sizeof (struct link*))) == NULL) {
printf("\n Fail attempting to allocate memory...\n");
exit(1);
}
for (i = 0; i <= pt_n->numnodes; i++) {
if ((pt_n->I[i] = (pt_lnk) calloc(pt_n->numnodes + 1, sizeof (struct link))) == NULL) {
printf("\n Fail attempting to allocate memory...\n");
exit(1);
}
}
/* relation of min cut set to network size */
//beta = breadth/pt_n->numnodes;
for (i = 1; i <= pt_n->numnodes; i++)
for (j = 1; j <= pt_n->numnodes; j++) {
pt_n->I[i][j].adj = 0;
pt_n->I[i][j].rlb = 0.0;
pt_n->I[i][j].smp = 0;
}
for (i = 1; i <= pt_n->numnodes; i++) {
fscanf(fp, "%d%d", &node1, &num);
for (j = 1; j <= num; j++) {
fscanf(fp, "%d%lf", &node2, &reliability);
pt_n->I[node1][node2].adj = 1;
pt_n->I[node1][node2].rlb = reliability;
}
}
fclose(fp);
init_genrand(seed);
if ((list = (int**) calloc(pt_n->numnodes + 1, sizeof (int*))) == NULL) {
printf("\n Fail attempting to allocate memory...\n");
exit(1);
}
for (i = 0; i <= pt_n->numnodes; i++) {
if ((list[i] = (int*) calloc(pt_n->numnodes, sizeof (int))) == NULL) {
printf("\n Fail attempting to allocate memory...\n");
exit(1);
}
}
for (i = 1; i <= pt_n->numnodes; i++)
for (j = 1; j <= pt_n->numnodes; j++) {
list[i][j] = 0;
}
if ((visited = (int*) calloc(pt_n->numnodes, sizeof (int))) == NULL) {
printf("\n Fail attempting to allocate memory...\n");
exit(1);
}
for (i = 1; i <= pt_n->numnodes; i++)
visited[i] = 0;
connected = 0;
return pt_n;
}
/*:11*/
#line 337 "./crude1.w"
;
/*12:*/
#line 456 "./crude1.w"
int X(int node1, int node2, pt_net nt) {
/* returns 1 if link node1 –node2 is operative, 0 otherwise */
double epsilon;
double rel;
if(nt->I[node1][node2].rlb == 0.666)
rel = reliability;
else
rel = nt->I[node1][node2].rlb;
epsilon = 1 - rel;
if (U < beta) {
tempL = (1 - epsilon) / (1 - beta);
l *= tempL;
return 1;
} else {
tempL = epsilon / beta;
l *= tempL;
return 0;
}
}
void Fail(pt_net nt) {
int i, j, k;
for (i = 1; i <= nt->numnodes; i++)
for (j = i + 1; j <= nt->numnodes; j++)
if (nt->I[i][j].adj) {
nt->I[i][j].smp = (nt->I[i][j].adj && X(i, j, nt));
nt->I[j][i].smp = nt->I[i][j].smp;
}
for (i = 1; i <= nt->numnodes; i++) {
k = 1;
while (list[i][k] > 0) {
list[i][k++] = 0;
}
}
for (i = 1; i <= nt->numnodes; i++) {
k = 1;
for (j = 1; j <= nt->numnodes; j++) {
if (nt->I[i][j].smp) {
list[i][k++] = j;
}
}
}
return;
}
/*:12*/
#line 338 "./crude1.w"
;
/*13:*/
#line 502 "./crude1.w"
int Phi(pt_net nt) {
int k;
connected = 0;
for (k = 1; k <= nt->numnodes; k++)
visited[k] = 0;
DFS(nt->s, nt);
return connected;
}
void DFS(int node, pt_net nt) {
int k;
if (connected)
return;
if (node == nt->t) {
connected = 1;
return;
}
visited[node] = 1;
k = 1;
while (list[node][k] > 0) {
if (!visited[list[node][k]])
DFS(list[node][k], nt);
k++;
}
return;
}
/*:13*/
#line 339 "./crude1.w"
;
/*:10*/
#line 133 "./crude1.w"
;
/*:1*/