9 #define VERY_FAR 100000000
11 /* generator of random networks for the shortest paths problem;
12 extended DIMACS format for output */
49 /* variables for lengths generating */
50 /* initialized by default values */
51 int l_f
= 0, ll_f
= 0, lm_f
= 0, ln_f
= 0, ls_f
= 0;
52 long ll
= 10000, /* length of the interval */
53 lm
= 0; /* minimal bound of the interval */
54 double ln
= 0, /* l += ln * |i-j| */
55 ls
= 0; /* l += ls * |i-j|^2 */
57 /* variables for connecting cycle(s) */
58 int c_f
= 0, cl_f
= 0, ch_f
= 0, c_random
= 1;
59 long cl
= 1; /* length of cycle arc */
60 long ch
; /* number of arcs in the cycle
63 /* variables for artifical source */
64 int s_f
= 0, sl_f
= 0, sm_f
= 0;
65 long sl
= VERY_FAR
, /* upper bound of artifical arc */
66 sm
, /* lower bound of artifical arc */
69 /* variables for potentials */
70 int p_f
= 0, pl_f
= 0, pm_f
= 0, pn_f
= 0, ps_f
= 0,
71 pa_f
= 0, pap_f
= 0, pac_f
= 0;
72 long pl
, /* length of the interval */
73 pm
; /* minimal bound of the interval */
74 double pn
= 0, /* l += ln * |i-j| */
75 ps
= 0, /* l += ls * |i-j|^2 */
76 pap
= 0, /* part of nodes with alternative dustribution */
77 pac
= -1; /* multiplier for alternative distribution */
79 int np
; /* number of parameter parsing now */
81 #define PRINT_ARC( i, j, length )\
84 if ( p_f ) l += ( p[i] - p[j] );\
85 printf ("a %8ld %8ld %12ld\n", i, j, l );\
88 /* parsing parameters */
90 if ( argc
< 2 ) goto usage
;
94 strcpy ( args
, argv
[1] );
96 if ( ( args
[0] == DASH
) && ( args
[1] == 'h')
100 if ( argc
< 4 ) goto usage
;
102 /* first parameter - number of nodes */
104 if ( ( n
= atoi ( argv
[1] ) ) < 2 ) goto usage
;
106 /* second parameter - number of arcs */
108 if ( ( m
= atoi ( argv
[2] ) ) < n
) goto usage
;
110 /* third parameter - seed */
112 if ( ( seed
= atoi ( argv
[3] ) ) <= 0 ) goto usage
;
114 /* other parameters */
116 for ( np
= 4; np
< argc
; np
++ )
118 strcpy ( args
, argv
[np
] );
119 if ( args
[0] != DASH
) goto usage
;
124 case 'l' : /* an interval for arc length */
128 case 'l': /* length of the interval */
130 ll
= (long) atof ( &args
[3] );
132 case 'm': /* minimal bound */
134 lm
= (long ) atof ( &args
[3] );
136 case 'n': /* additional length: l*|i-j| */
138 ln
= atof ( &args
[3] );
140 case 's': /* additional length: l*|i-j|^2 */
142 ls
= atof ( &args
[3] );
144 default: /* unknown switch value */
149 case 'c' : /* connecting cycle(s) */
156 cl
= (long) atof ( &args
[3] );
157 if ( cl
< 0 ) goto usage
;
161 ch
= (long) atof ( &args
[3] );
162 if ( ch
< 2 || ch
> n
) goto usage
;
164 default: /* unknown switch value */
169 case 's' : /* additional source */
171 if ( strlen ( args
) > 2 )
175 case 'l': /* upper bound of art. arc */
177 sl
= (long) atof ( &args
[3] );
179 case 'm': /* lower bound of art. arc */
181 sm
= (long) atof ( &args
[3] );
183 default: /* unknown switch value */
189 case 'p' : /* potentials */
191 if ( strlen ( args
) > 2 )
195 case 'l': /* length of the interval */
197 pl
= (long) atof ( &args
[3] );
199 case 'm': /* minimal bound */
201 pm
= (long ) atof ( &args
[3] );
203 case 'n': /* additional length: l*|i-j| */
205 pn
= atof ( &args
[3] );
207 case 's': /* additional length: l*|i-j|^2 */
209 ps
= atof ( &args
[3] );
211 case 'a': /* bipolar distribution */
215 case 'p': /* % of alternative potentials */
217 pap
= atof ( &args
[4] );
218 if ( pap
< 0 ) pap
= 0;
219 if ( pap
> 100 ) pap
= 100;
222 case 'c': /* multiplier */
224 pac
= atof ( &args
[4] );
226 default: /* unknown switch value */
230 default: /* unknown switch value */
236 default : /* unknoun case */
242 /* ----- ajusting parameters ----- */
246 /* length parameters */
247 if ( ll
< lm
) { lx
= ll
; ll
= lm
; lm
= lx
; }
249 /* potential parameters */
252 if ( ! pl_f
) pl
= ll
;
253 if ( ! pm_f
) pm
= lm
;
254 if ( pl
< pm
) { lx
= pl
; pl
= pm
; pm
= lx
; }
257 /* path(s) parameters */
258 if ( ! ch_f
) ch
= n
;
260 mc
= n
+ (n
-2) / (ch
-1);
263 "Error: not enough arcs for generating connecting cycle(s)\n" );
267 /* artifical source parameters */
270 if ( ! sm_f
) sm
= sl
;
271 if ( sl
< sm
) { lx
= sl
; sl
= sm
; sm
= lx
; }
275 printf ("c random network for shortest paths problem\n");
276 printf ("c extended DIMACS format\nc\n" );
278 /* name of the problem */
279 printf ("t rd_%ld_%ld_%ld_", n
, m
, seed
);
290 /* printing additional information */
292 printf ("c length -> min: %ld max: %ld k1: %.2f k2: %.2f\n",
297 printf ("c cycle -> number of arcs: %ld arc length: random\n", ch
);
299 printf ("c cycle -> number of arcs: %ld arc length: %ld\n",
303 printf ("c length of arcs from artifical source -> min: %ld max: %ld\n",
307 printf ("c potentials -> min: %ld max: %ld k1: %.2f k2: %.2f\n",
310 printf ("c potentials -> part of alternative distribution: %.2f k: %.2f\n",
316 printf ("p sp %8ld %8ld\nc\n", n0
, m0
);
318 source
= ( s_f
) ? n0
: 1;
319 printf ("n %8ld\nc\n", source
);
321 if ( p_f
) /* generating potentials */
323 p
= (long*) calloc ( n
+2, sizeof (long) );
328 for ( i
= 0; i
<= n
; i
++ )
330 p_t
= pm
+ nrand ( pl
);
331 if ( pn_f
) p_t
+= (long) ( i
* pn
);
332 if ( ps_f
) p_t
+= (long) ( i
* ( i
* ps
));
334 if ( rand01() < pap
)
335 p_t
= (long) ( p_t
* pac
);
342 if ( s_f
) /* additional arcs from artifical source */
348 for ( i
= n
; i
> 1; i
-- )
350 s
= sm
+ nrand ( sl
);
351 PRINT_ARC ( n0
, i
, s
)
354 PRINT_ARC ( n0
, 1, 0 )
357 /* initialize random number generator */
361 /* generating connecting cycle(s) */
363 cl
= lm
+ nrand ( ll
);
364 PRINT_ARC ( 1, 2, cl
)
366 cl
= lm
+ nrand ( ll
);
367 PRINT_ARC ( n
, 1, cl
)
369 for ( i
= 2; i
< n
; i
++ )
372 cl
= lm
+ nrand ( ll
);
374 if ( ( (i
-1) % (ch
-1) ) != 0 )
375 PRINT_ARC ( i
, i
+1, cl
)
377 { PRINT_ARC ( i
, 1, cl
)
379 cl
= lm
+ nrand ( ll
);
380 PRINT_ARC ( 1, i
+1, cl
)
384 /* generating random arcs */
386 for ( k
= 1; k
<= m
- mc
; k
++ )
394 dij
= ( i
> j
) ? ( i
- j
) : ( j
- i
);
395 l
= lm
+ nrand ( ll
);
396 if ( ln_f
) l
+= (long) ( dij
* ln
);
397 if ( ls_f
) l
+= (long) ( dij
* ( dij
* ls
) );
398 PRINT_ARC ( i
, j
, l
);
404 /* ----- wrong usage ----- */
408 "\nusage: %s n m seed [ -ll#i -lm#i -cl#i -p -pl#i -pm#i ... ]\n\
409 help: %s -h\n\n", argv
[0], argv
[0] );
412 fprintf ( stderr
, "error in parameter # %d\n\n", np
);
419 if ( args
[2] == 'h') goto hhelp
;
422 "\n'%s' - random network generator for shortest paths problem.\n\
423 Generates problems in extended DIMACS format.\n\
425 %s n m seed [ -ll#i -lm#i -cl#i -p -pl#i -pm#i ... ]\n\
428 #i - integer number #f - real number\n\
430 -ll#i - #i is the upper bound on arc lengths (default 10000)\n\
431 -lm#i - #i is the lower bound on arc lengths (default 0)\n\
432 -cl#i - #i is length of arcs in connecting cycle(s) (default random)\n\
433 -p - generate potentials \n\
434 -pl#i - #i is the upper bound on potentials (default ll)\n\
435 -pm#i - #i is the lower bound on potentials (default lm)\n\
437 -hh - extended help \n\n",
438 argv
[0], argv
[0], argv
[0] );
442 /* --------- sophisticated help ------------ */
448 fout
= fopen ( argv
[2], "w" );
451 { fprintf ( stderr
, "\nCan't open file '%s' for writing help\n\n", argv
[2] );
456 "\n'%s' - random network generator for shortest paths problem.\n\
457 Generates problems in extended DIMACS format.\n\
459 %s n m seed [ -ll#i -lm#i -ln#f -ls#f\n\
460 -p -pl#i -pm#i -pn#f -ps#f -pap#i -pac#f\n\
466 #i - integer number #f - real number\n\
468 Arc length parameters:\n\
469 -ll#i - #i is the upper bound on arc lengths (default 10000)\n\
470 -lm#i - #i is the lower bound on arc lengths (default 0)\n\
471 -ln#f - multipliy l(i, j) by #f * |i-j| (default 0)\n\
472 -ls#f - multipliy l(i, j) by #f * |i-j|^2 (default 0)\n\
474 Potential parameters:\n\
475 -p - generate potentials \n\
476 -pl#i - #i is the upper bound on potentials (default ll)\n\
477 -pm#i - #i is the lower bound on potentials (default lm)\n\
478 -pn#f - multiply p(i) by #f * i (default 0)\n\
479 -ps#f - multiply p(i) by #f * i^2 (default 0)\n\
480 -pap#i - percentage of alternative potential nodes (default 0)\n\
481 -pac#f - if i is alternative, multiply p(i) by #f (default -1)\n\
483 Connecting cycle(s) parameters:\n\
484 -cl#i - #i is length of arcs in connecting cycle(s) (default random)\n\
485 -ch#i - #i is length of connecting cycles (default n)\n\
487 Artificial source parameters:\n\
488 -s - generate artificial source with default connecting arc lengths\n\
489 -sl#i - #i is the upper bound on art. arc lengths (default 100000000)\n\
490 -sm#i - #i is the lower bound on art. arc lengths (default sl)\n\
492 -hh file_name - save this help in the file 'file_name'\n\n",
493 argv
[0], argv
[0], argv
[0] );