summaryrefslogtreecommitdiffstats
path: root/isisd/topology/sprand.c
blob: 28b58b30e7f192bd7618a49ab12a307c875e405a (plain)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <values.h>

#include "random.c"

#define DASH '-'
#define VERY_FAR 100000000

/* generator of random networks for the shortest paths problem;
   extended DIMACS format for output */

main ( argc, argv )

int argc;
char* argv[];

{

char   args[30];

long   n,
       n0,
       source,
       i,
       i0,
       j,
       dij;

long   m,
       m0,
       mc,
       k;

long   *p,
       p_t,
       l,
       lx;

long   seed,
       seed1,
       seed2;

int    ext=0;

FILE   *fout;

/* variables for lengths generating */
/* initialized by default values */
int    l_f = 0, ll_f = 0, lm_f = 0, ln_f = 0, ls_f = 0;
long   ll = 10000,    /* length of the interval */
       lm = 0;        /* minimal bound of the interval */
double ln = 0,        /* l += ln * |i-j| */ 
       ls = 0;        /* l += ls * |i-j|^2 */

/* variables for connecting cycle(s) */
int    c_f = 0, cl_f = 0, ch_f = 0, c_random = 1;
long   cl = 1;        /* length of cycle arc */
long   ch;            /* number of arcs in the cycle 
                         n - by default */

/* variables for artifical source */
int    s_f = 0, sl_f = 0, sm_f = 0;
long   sl   = VERY_FAR, /* upper bound of artifical arc */
       sm,              /* lower bound of artifical arc */
       s;  

/* variables for potentials */
int    p_f = 0, pl_f = 0, pm_f = 0, pn_f = 0, ps_f = 0,
       pa_f = 0, pap_f = 0, pac_f = 0;
long   pl,            /* length of the interval */
       pm;            /* minimal bound of the interval */
double pn = 0,        /* l += ln * |i-j| */ 
       ps = 0,        /* l += ls * |i-j|^2 */
       pap = 0,       /* part of nodes with alternative dustribution */
       pac = -1;      /* multiplier for alternative distribution */

int np;               /* number of parameter parsing now */

#define PRINT_ARC( i, j, length )\
{\
l = length;\
if ( p_f ) l += ( p[i] - p[j] );\
printf ("a %8ld %8ld %12ld\n", i, j, l );\
}

  /* parsing  parameters */

if ( argc < 2 ) goto usage;

np = 0;

strcpy ( args, argv[1] );

  if ( ( args[0] == DASH ) && ( args[1] == 'h')
     )
      goto help;

if ( argc < 4 ) goto usage;

/* first parameter - number of nodes */
np = 1;
if ( ( n = atoi ( argv[1] ) )  <  2  )  goto usage;

/* second parameter - number of arcs */
np = 2;
if ( ( m = atoi ( argv[2] ) )  <  n  )  goto usage;

/* third parameter - seed */
np=3;
if ( ( seed = atoi ( argv[3] ) )  <=  0  )  goto usage;

/* other parameters */

for ( np = 4; np < argc; np ++ )
  {
    strcpy ( args, argv[np] );
    if ( args[0] != DASH ) goto usage;

    switch ( args[1] )
      {

      case 'l' : /* an interval for arc length */
	l_f = 1;
	switch ( args[2] )
	  { 
	  case 'l': /* length of the interval */
	    ll_f = 1;
	    ll  =  (long) atof ( &args[3] );
	    break;
	  case 'm': /* minimal bound */
	    lm_f = 1;
	    lm  = (long ) atof ( &args[3] );
	    break;
	  case 'n': /* additional length: l*|i-j| */
	    ln_f = 1;
	    ln  = atof ( &args[3] );
	    break;
	  case 's': /* additional length: l*|i-j|^2 */
	    ls_f = 1;
	    ls  = atof ( &args[3] );
	    break;
	  default:  /* unknown switch  value */
	    goto usage;
	  }
	break;

      case 'c' : /* connecting cycle(s) */
        c_f = 1;
	switch ( args[2] )
	  { 
	  case 'l':
            c_random = 0;
	    cl_f = 1;
	    cl  =  (long) atof ( &args[3] );
            if ( cl < 0 ) goto usage;
	    break;
	  case 'h':
	    ch_f = 1;
	    ch  =  (long) atof ( &args[3] );
            if ( ch < 2 || ch > n ) goto usage;
	    break;
	  default:  /* unknown switch  value */
	    goto usage;
          }
	break;

      case 's' : /* additional source */
        s_f = 1;
	if ( strlen ( args ) > 2 )
	{  
	switch ( args[2] )
	  { 
	  case 'l': /* upper bound of art. arc */
	    sl_f = 1;
	    sl  =  (long) atof ( &args[3] );
            break;
	  case 'm': /* lower bound of art. arc */
	    sm_f = 1;
	    sm  =  (long) atof ( &args[3] );
            break;
	  default:  /* unknown switch  value */
	    goto usage;
          }
         }
	break;

      case 'p' : /* potentials */
	p_f = 1;
	if ( strlen ( args ) > 2 )
	{  
	switch ( args[2] )
	  { 
	  case 'l': /* length of the interval */
	    pl_f = 1;
	    pl  =  (long) atof ( &args[3] );
	    break;
	  case 'm': /* minimal bound */
	    pm_f = 1;
	    pm  = (long ) atof ( &args[3] );
	    break;
	  case 'n': /* additional length: l*|i-j| */
	    pn_f = 1;
	    pn  = atof ( &args[3] );
	    break;
	  case 's': /* additional length: l*|i-j|^2 */
	    ps_f = 1;
	    ps  = atof ( &args[3] );
	    break;
	  case 'a': /* bipolar distribution */
	    pa_f = 1;
	    switch ( args[3] )
	      {
	      case 'p': /* % of alternative potentials */
                pap_f = 1;
                pap  = atof ( &args[4] );
		if ( pap < 0   ) pap = 0;
		if ( pap > 100 ) pap = 100;
                pap /= 100;
		break;
	      case 'c': /* multiplier */
		pac_f = 1;
		pac = atof ( &args[4] );
		break;
	      default: /* unknown switch value */
		goto usage;
	      }
	    break;
	  default:  /* unknown switch  value */
	    goto usage;
	  }
      }
	break;

      default  : /* unknoun case */
	goto usage;
      }
  }
   

/* ----- ajusting parameters ----- */

n0 = n; m0 = m;

/* length parameters */
if ( ll < lm ) { lx = ll; ll = lm; lm = lx; }

/* potential parameters */
if ( p_f )
  {
   if ( ! pl_f ) pl = ll;
   if ( ! pm_f ) pm = lm;
   if ( pl < pm ) { lx = pl; pl = pm; pm = lx; }
  }

/* path(s) parameters */
if ( ! ch_f ) ch = n;

mc = n + (n-2) / (ch-1);
if ( mc > m ) 
  { fprintf ( stderr,
              "Error: not enough arcs for generating connecting cycle(s)\n" );
    exit (4);
  }

 /* artifical source parameters */
if ( s_f )          
   { m0 += n; n0 ++ ; 
     if ( ! sm_f ) sm = sl;
     if ( sl < sm ) { lx = sl; sl = sm; sm = lx; }
   }

/* printing title */
printf ("c random network for shortest paths problem\n");
printf ("c extended DIMACS format\nc\n" );

/* name of the problem */
printf ("t rd_%ld_%ld_%ld_", n, m, seed );
if ( l_f )
  printf ("%c", 'l');
if ( c_f )
  printf ("%c", 'c');
if ( s_f )
  printf ("%c", 's');
if ( p_f )
  printf ("%c", 'p');
printf ("\nc\n");

/* printing additional information  */
if ( l_f )
  printf ("c length -> min: %ld max: %ld k1: %.2f k2: %.2f\n",
           lm, ll, ln, ls );
if ( c_f )
  {
    if ( c_random )
      printf ("c cycle -> number of arcs: %ld  arc length: random\n", ch);
    else
      printf ("c cycle -> number of arcs: %ld  arc length: %ld\n",
	       ch, cl );
  }
if ( s_f )
  printf ("c length of arcs from artifical source -> min: %ld max: %ld\n",
           sm, sl );
if ( p_f )
  {
  printf ("c potentials -> min: %ld max: %ld k1: %.2f k2: %.2f\n",
	  pm, pl, pn, ps );
  if ( pa_f )
  printf ("c potentials -> part of alternative distribution: %.2f k: %.2f\n",
          pap, pac );
  }
printf ("c\n" );


printf ("p sp %8ld %8ld\nc\n", n0, m0 );

source = ( s_f ) ? n0 : 1;
printf ("n %8ld\nc\n", source );

if ( p_f ) /* generating potentials */
  {
    p = (long*) calloc ( n+2, sizeof (long) );
    seed1 = 2*seed + 1;
    init_rand ( seed1);
    pl = pl - pm + 1;

    for ( i = 0; i <= n; i ++ )
      {
	p_t = pm + nrand ( pl );
	if ( pn_f ) p_t += (long) ( i * pn );
	if ( ps_f ) p_t += (long) ( i * ( i * ps ));
	if ( pap_f )
	    if ( rand01() < pap )
		p_t = (long) ( p_t * pac );
        p[i] = p_t;
      }
    p[n+1] = 0;
  }


if ( s_f ) /* additional arcs from artifical source */
  {
    seed2 = 3*seed + 1;
    init_rand ( seed2 );
    sl = sl - sm + 1;

    for ( i = n; i > 1; i -- )
      {
	s = sm + nrand ( sl );
	PRINT_ARC ( n0, i, s ) 
      }

    PRINT_ARC ( n0, 1, 0 )
  }

/* initialize random number generator */
init_rand ( seed );
ll = ll - lm + 1;

/* generating connecting cycle(s) */
if (c_random)
  cl = lm + nrand ( ll );
PRINT_ARC ( 1, 2, cl )
if (c_random)
  cl = lm + nrand ( ll );
PRINT_ARC ( n, 1, cl )

for ( i = 2; i < n; i ++ )
  {
    if (c_random)
      cl = lm + nrand ( ll );

    if ( ( (i-1) % (ch-1) ) != 0 )
        PRINT_ARC ( i, i+1, cl )
    else
      { PRINT_ARC ( i,   1, cl )
        if (c_random)
          cl = lm + nrand ( ll );
	PRINT_ARC ( 1, i+1, cl )
      }
  }

/* generating random arcs */

for ( k = 1; k <= m - mc; k ++ )
  {
    i = 1 + nrand ( n );

    do
    j = 1 + nrand ( n );
    while ( j == i );

    dij = ( i > j ) ? ( i - j ) : ( j - i );
    l = lm + nrand ( ll );
    if ( ln_f ) l += (long) ( dij * ln );
    if ( ls_f ) l += (long) ( dij * ( dij * ls ) );
    PRINT_ARC ( i, j, l );
  }

/* all is done */
exit (ext);

/* ----- wrong usage ----- */

 usage:
fprintf ( stderr,
"\nusage: %s  n  m  seed  [ -ll#i -lm#i -cl#i -p -pl#i -pm#i ... ]\n\
help:  %s -h\n\n", argv[0], argv[0] );

if ( np > 0 )
  fprintf ( stderr, "error in parameter # %d\n\n", np );   
exit (4);

/* ---- help ---- */

 help:

if ( args[2] == 'h') goto hhelp;

fprintf ( stderr, 
"\n'%s' - random network generator for shortest paths problem.\n\
Generates problems in extended DIMACS format.\n\
\n\
   %s  n m seed [ -ll#i -lm#i -cl#i -p -pl#i -pm#i ... ]\n\
   %s -hh\n\
\n\
                        #i - integer number   #f - real number\n\
\n\
-ll#i  - #i is the upper bound on arc lengths          (default 10000)\n\
-lm#i  - #i is the lower bound on arc lengths          (default 0)\n\
-cl#i  - #i is length of arcs in connecting cycle(s)   (default random)\n\
-p     - generate potentials \n\
-pl#i  - #i is the upper bound on potentials           (default ll)\n\
-pm#i  - #i is the lower bound on potentials           (default lm)\n\
\n\
-hh    - extended help \n\n",
argv[0], argv[0], argv[0] );

exit (0);

/* --------- sophisticated help ------------ */
 hhelp:

if ( argc < 3 )
     fout = stderr;
else
     fout = fopen ( argv[2], "w" );

if ( fout == NULL )
{ fprintf ( stderr, "\nCan't open file  '%s' for writing help\n\n", argv[2] );
  exit ( 2 );
}

fprintf (fout, 
"\n'%s' - random network generator for shortest paths problem.\n\
Generates problems in extended DIMACS format.\n\
\n\
   %s  n m seed [ -ll#i -lm#i -ln#f -ls#f\n\
                      -p  -pl#i -pm#i -pn#f -ps#f -pap#i -pac#f\n\
                      -cl#i -ch#i\n\
                      -s -sl#i -sm#i\n\
                    ]\n\
   %s -hh file_name\n\
\n\
                        #i - integer number   #f - real number\n\
\n\
      Arc length parameters:\n\
-ll#i  - #i is the upper bound on arc lengths          (default 10000)\n\
-lm#i  - #i is the lower bound on arc lengths          (default 0)\n\
-ln#f  - multipliy l(i, j) by #f * |i-j|               (default 0)\n\
-ls#f  - multipliy l(i, j) by #f * |i-j|^2             (default 0)\n\
\n\
      Potential parameters:\n\
-p     - generate potentials \n\
-pl#i  - #i is the upper bound on potentials           (default ll)\n\
-pm#i  - #i is the lower bound on potentials           (default lm)\n\
-pn#f  - multiply p(i) by #f * i                       (default 0)\n\
-ps#f  - multiply p(i) by #f * i^2                     (default 0)\n\
-pap#i - percentage of alternative potential nodes     (default 0)\n\
-pac#f - if i is alternative, multiply  p(i) by #f     (default -1)\n\
\n\
      Connecting cycle(s) parameters:\n\
-cl#i  - #i is length of arcs in connecting cycle(s)   (default random)\n\
-ch#i  - #i is length of connecting cycles             (default n)\n\
\n\
      Artificial source parameters:\n\
-s     - generate artificial source with default connecting arc lengths\n\
-sl#i  - #i is the upper bound on art. arc lengths    (default 100000000)\n\
-sm#i  - #i is the lower bound on art. arc lengths    (default sl)\n\
\n\
-hh file_name  - save this help in the file 'file_name'\n\n",
argv[0], argv[0], argv[0] );

exit (0);
}