11 #define VERY_FAR 100000000
13 /* generator of random networks for the shortest paths problem;
14 extended DIMACS format for output */
51 /* variables for lengths generating */
52 /* initialized by default values */
53 int l_f = 0, ll_f = 0, lm_f = 0, ln_f = 0, ls_f = 0;
54 long ll = 10000, /* length of the interval */
55 lm = 0; /* minimal bound of the interval */
56 double ln = 0, /* l += ln * |i-j| */
57 ls = 0; /* l += ls * |i-j|^2 */
59 /* variables for connecting cycle(s) */
60 int c_f = 0, cl_f = 0, ch_f = 0, c_random = 1;
61 long cl = 1; /* length of cycle arc */
62 long ch; /* number of arcs in the cycle
65 /* variables for artifical source */
66 int s_f = 0, sl_f = 0, sm_f = 0;
67 long sl = VERY_FAR, /* upper bound of artifical arc */
68 sm, /* lower bound of artifical arc */
71 /* variables for potentials */
72 int p_f = 0, pl_f = 0, pm_f = 0, pn_f = 0, ps_f = 0,
73 pa_f = 0, pap_f = 0, pac_f = 0;
74 long pl, /* length of the interval */
75 pm; /* minimal bound of the interval */
76 double pn = 0, /* l += ln * |i-j| */
77 ps = 0, /* l += ls * |i-j|^2 */
78 pap = 0, /* part of nodes with alternative dustribution */
79 pac = -1; /* multiplier for alternative distribution */
81 int np; /* number of parameter parsing now */
83 #define PRINT_ARC( i, j, length )\
86 if ( p_f ) l += ( p[i] - p[j] );\
87 printf ("a %8ld %8ld %12ld\n", i, j, l );\
90 /* parsing parameters */
92 if ( argc < 2 ) goto usage;
96 strcpy ( args, argv[1] );
98 if ( ( args[0] == DASH ) && ( args[1] == 'h')
102 if ( argc < 4 ) goto usage;
104 /* first parameter - number of nodes */
106 if ( ( n = atoi ( argv[1] ) ) < 2 ) goto usage;
108 /* second parameter - number of arcs */
110 if ( ( m = atoi ( argv[2] ) ) < n ) goto usage;
112 /* third parameter - seed */
114 if ( ( seed = atoi ( argv[3] ) ) <= 0 ) goto usage;
116 /* other parameters */
118 for ( np = 4; np < argc; np ++ )
120 strcpy ( args, argv[np] );
121 if ( args[0] != DASH ) goto usage;
126 case 'l' : /* an interval for arc length */
130 case 'l': /* length of the interval */
132 ll = (long) atof ( &args[3] );
134 case 'm': /* minimal bound */
136 lm = (long ) atof ( &args[3] );
138 case 'n': /* additional length: l*|i-j| */
140 ln = atof ( &args[3] );
142 case 's': /* additional length: l*|i-j|^2 */
144 ls = atof ( &args[3] );
146 default: /* unknown switch value */
151 case 'c' : /* connecting cycle(s) */
158 cl = (long) atof ( &args[3] );
159 if ( cl < 0 ) goto usage;
163 ch = (long) atof ( &args[3] );
164 if ( ch < 2 || ch > n ) goto usage;
166 default: /* unknown switch value */
171 case 's' : /* additional source */
173 if ( strlen ( args ) > 2 )
177 case 'l': /* upper bound of art. arc */
179 sl = (long) atof ( &args[3] );
181 case 'm': /* lower bound of art. arc */
183 sm = (long) atof ( &args[3] );
185 default: /* unknown switch value */
191 case 'p' : /* potentials */
193 if ( strlen ( args ) > 2 )
197 case 'l': /* length of the interval */
199 pl = (long) atof ( &args[3] );
201 case 'm': /* minimal bound */
203 pm = (long ) atof ( &args[3] );
205 case 'n': /* additional length: l*|i-j| */
207 pn = atof ( &args[3] );
209 case 's': /* additional length: l*|i-j|^2 */
211 ps = atof ( &args[3] );
213 case 'a': /* bipolar distribution */
217 case 'p': /* % of alternative potentials */
219 pap = atof ( &args[4] );
220 if ( pap < 0 ) pap = 0;
221 if ( pap > 100 ) pap = 100;
224 case 'c': /* multiplier */
226 pac = atof ( &args[4] );
228 default: /* unknown switch value */
232 default: /* unknown switch value */
238 default : /* unknoun case */
244 /* ----- ajusting parameters ----- */
248 /* length parameters */
249 if ( ll < lm ) { lx = ll; ll = lm; lm = lx; }
251 /* potential parameters */
254 if ( ! pl_f ) pl = ll;
255 if ( ! pm_f ) pm = lm;
256 if ( pl < pm ) { lx = pl; pl = pm; pm = lx; }
259 /* path(s) parameters */
260 if ( ! ch_f ) ch = n;
262 mc = n + (n-2) / (ch-1);
265 "Error: not enough arcs for generating connecting cycle(s)\n" );
269 /* artifical source parameters */
272 if ( ! sm_f ) sm = sl;
273 if ( sl < sm ) { lx = sl; sl = sm; sm = lx; }
277 printf ("c random network for shortest paths problem\n");
278 printf ("c extended DIMACS format\nc\n" );
280 /* name of the problem */
281 printf ("t rd_%ld_%ld_%ld_", n, m, seed );
292 /* printing additional information */
294 printf ("c length -> min: %ld max: %ld k1: %.2f k2: %.2f\n",
299 printf ("c cycle -> number of arcs: %ld arc length: random\n", ch);
301 printf ("c cycle -> number of arcs: %ld arc length: %ld\n",
305 printf ("c length of arcs from artifical source -> min: %ld max: %ld\n",
309 printf ("c potentials -> min: %ld max: %ld k1: %.2f k2: %.2f\n",
312 printf ("c potentials -> part of alternative distribution: %.2f k: %.2f\n",
318 printf ("p sp %8ld %8ld\nc\n", n0, m0 );
320 source = ( s_f ) ? n0 : 1;
321 printf ("n %8ld\nc\n", source );
323 if ( p_f ) /* generating potentials */
325 p = (long*) calloc ( n+2, sizeof (long) );
330 for ( i = 0; i <= n; i ++ )
332 p_t = pm + nrand ( pl );
333 if ( pn_f ) p_t += (long) ( i * pn );
334 if ( ps_f ) p_t += (long) ( i * ( i * ps ));
336 if ( rand01() < pap )
337 p_t = (long) ( p_t * pac );
344 if ( s_f ) /* additional arcs from artifical source */
350 for ( i = n; i > 1; i -- )
352 s = sm + nrand ( sl );
353 PRINT_ARC ( n0, i, s )
356 PRINT_ARC ( n0, 1, 0 )
359 /* initialize random number generator */
363 /* generating connecting cycle(s) */
365 cl = lm + nrand ( ll );
366 PRINT_ARC ( 1, 2, cl )
368 cl = lm + nrand ( ll );
369 PRINT_ARC ( n, 1, cl )
371 for ( i = 2; i < n; i ++ )
374 cl = lm + nrand ( ll );
376 if ( ( (i-1) % (ch-1) ) != 0 )
377 PRINT_ARC ( i, i+1, cl )
379 { PRINT_ARC ( i, 1, cl )
381 cl = lm + nrand ( ll );
382 PRINT_ARC ( 1, i+1, cl )
386 /* generating random arcs */
388 for ( k = 1; k <= m - mc; k ++ )
396 dij = ( i > j ) ? ( i - j ) : ( j - i );
397 l = lm + nrand ( ll );
398 if ( ln_f ) l += (long) ( dij * ln );
399 if ( ls_f ) l += (long) ( dij * ( dij * ls ) );
400 PRINT_ARC ( i, j, l );
406 /* ----- wrong usage ----- */
410 "\nusage: %s n m seed [ -ll#i -lm#i -cl#i -p -pl#i -pm#i ... ]\n\
411 help: %s -h\n\n", argv[0], argv[0] );
414 fprintf ( stderr, "error in parameter # %d\n\n", np );
421 if ( args[2] == 'h') goto hhelp;
424 "\n'%s' - random network generator for shortest paths problem.\n\
425 Generates problems in extended DIMACS format.\n\
427 %s n m seed [ -ll#i -lm#i -cl#i -p -pl#i -pm#i ... ]\n\
430 #i - integer number #f - real number\n\
432 -ll#i - #i is the upper bound on arc lengths (default 10000)\n\
433 -lm#i - #i is the lower bound on arc lengths (default 0)\n\
434 -cl#i - #i is length of arcs in connecting cycle(s) (default random)\n\
435 -p - generate potentials \n\
436 -pl#i - #i is the upper bound on potentials (default ll)\n\
437 -pm#i - #i is the lower bound on potentials (default lm)\n\
439 -hh - extended help \n\n",
440 argv[0], argv[0], argv[0] );
444 /* --------- sophisticated help ------------ */
450 fout = fopen ( argv[2], "w" );
453 { fprintf ( stderr, "\nCan't open file '%s' for writing help\n\n", argv[2] );
458 "\n'%s' - random network generator for shortest paths problem.\n\
459 Generates problems in extended DIMACS format.\n\
461 %s n m seed [ -ll#i -lm#i -ln#f -ls#f\n\
462 -p -pl#i -pm#i -pn#f -ps#f -pap#i -pac#f\n\
468 #i - integer number #f - real number\n\
470 Arc length parameters:\n\
471 -ll#i - #i is the upper bound on arc lengths (default 10000)\n\
472 -lm#i - #i is the lower bound on arc lengths (default 0)\n\
473 -ln#f - multipliy l(i, j) by #f * |i-j| (default 0)\n\
474 -ls#f - multipliy l(i, j) by #f * |i-j|^2 (default 0)\n\
476 Potential parameters:\n\
477 -p - generate potentials \n\
478 -pl#i - #i is the upper bound on potentials (default ll)\n\
479 -pm#i - #i is the lower bound on potentials (default lm)\n\
480 -pn#f - multiply p(i) by #f * i (default 0)\n\
481 -ps#f - multiply p(i) by #f * i^2 (default 0)\n\
482 -pap#i - percentage of alternative potential nodes (default 0)\n\
483 -pac#f - if i is alternative, multiply p(i) by #f (default -1)\n\
485 Connecting cycle(s) parameters:\n\
486 -cl#i - #i is length of arcs in connecting cycle(s) (default random)\n\
487 -ch#i - #i is length of connecting cycles (default n)\n\
489 Artificial source parameters:\n\
490 -s - generate artificial source with default connecting arc lengths\n\
491 -sl#i - #i is the upper bound on art. arc lengths (default 100000000)\n\
492 -sm#i - #i is the lower bound on art. arc lengths (default sl)\n\
494 -hh file_name - save this help in the file 'file_name'\n\n",
495 argv[0], argv[0], argv[0] );