18 #define VERY_FAR 100000000
20 #define DOUBLE_CYCLE 0
27 #define NODE( x, y ) (x*Y + y + 1)
32 void free_arc(void *);
33 void help(struct vty *);
34 void print_arc(struct vty *, struct list *, long, long, long);
35 void hhelp(struct vty *);
36 void usage(struct vty *);
38 const char *graph_type[] = {
48 long X, /* horizontal size of grid */
49 Y; /* vertical size of grid */
54 dl, dx, xn, yyn, count,
81 /* initialized by default values */
83 /* variables for generating one layer */
85 /* variables for generating spanning graph */
86 int c_f = 0, cw_f = 0, cm_f = 0, cl_f = 0;
88 int cw = DOUBLE_CYCLE; /* type of spanning graph */
89 long cm = 0, /* lower bound of the interval */
90 cl = 100; /* upper bound of the interval */
92 /* variables for generating additional arcs */
93 int a_f = 0, ax_f = 0, am_f = 0, al_f = 0;
95 long ax = 0, /* number of additional arcs */
96 am = 0, /* lower bound of the interval */
97 al = 100; /* upper bound of the interval */
99 /* variables for inter-layer arcs */
100 int i_f = 0, ip_f = 0, ix_f = 0, ih_f = 0,
101 im_f = 0, il_f = 0, in_f = 0, is_f = 0;
103 int ip = NO; /* to mess or not to mess */
104 long ix = 1, /* number of interlayered arcs in a NODE */
105 ih = 1, /* step between two layeres */
106 il = 10000, /* upper bound of the interval */
107 im = 1000; /* lower bound of the interval */
108 double in = 1, /* l *= in * |x1-x2| */
109 is = 0; /* l *= is * |x1-x2|^2 */
111 /* variables for artifical source */
112 int s_f = 0, sl_f = 0, sm_f = 0;
113 long sl = VERY_FAR, /* upper bound of artifical arc */
114 sm, /* lower bound of artifical arc */
117 /* variables for potentials */
118 int p_f = 0, pl_f = 0, pm_f = 0, pn_f = 0, ps_f = 0;
120 long pl, /* upper bound of the interval */
121 pm; /* lower bound of the interval */
122 double pn = 0, /* p += ln * (x+1) */
123 ps = 0; /* p += ls * (x+1)^2 */
125 int np; /* number of parameter parsing now */
129 free_arc (void *val) {
134 print_arc (struct vty *vty, struct list *topology, long i, long j, long length)
139 if ( p_f ) l += ( p[i] - p[j] );
140 // vty_out (vty,"a %8ld %8ld %12ld%s", i, j, l ,VTY_NEWLINE);
141 myarc = malloc (sizeof(struct arc));
142 myarc->from_node = i;
145 topology->del = free_arc;
146 listnode_add (topology, myarc);
151 help (struct vty *vty) {
152 // if ( args[2] == 'h') hhelp (vty);
153 vty_out (vty,"grid network generator for shortest paths problem.%s",VTY_NEWLINE);
154 vty_out (vty,"Generates problems in extended DIMACS format.%s",VTY_NEWLINE);
155 vty_out (vty,"X Y seed [ -cl#i -cm#i -c{c|d|p} -ip -il#i -im#i -p -pl#i -pm#i... ]%s",VTY_NEWLINE);
156 vty_out (vty,"#i - integer number%s",VTY_NEWLINE);
157 vty_out (vty,"-cl#i - #i is the upper bound on layer arc lengths (default 100)%s",VTY_NEWLINE);
158 vty_out (vty,"-cm#i - #i is the lower bound on layer arc lengths (default 0)%s",VTY_NEWLINE);
159 vty_out (vty,"-c#t - #t is the type of connecting graph: { c | d | p }%s",VTY_NEWLINE);
160 vty_out (vty," c - cycle, d - double cycle, p - path (default d)%s",VTY_NEWLINE);
161 vty_out (vty,"-ip - shuffle inter-layer arcs (default NO)%s",VTY_NEWLINE);
162 vty_out (vty,"-il#i - #i is the upper bound on inter-layer arc lengths (default 10000)%s",VTY_NEWLINE);
163 vty_out (vty,"-im#i - #i is the lower bound on inter-layer arc lengths (default 1000)%s",VTY_NEWLINE);
164 vty_out (vty,"-p - generate potentials%s",VTY_NEWLINE);
165 vty_out (vty,"-pl#i - #i is the upper bound on potentials (default il)%s",VTY_NEWLINE);
166 vty_out (vty,"-pm#i - #i is the lower bound on potentials (default im)%s",VTY_NEWLINE);
167 vty_out (vty,"%s",VTY_NEWLINE);
168 vty_out (vty,"-hh - extended help%s",VTY_NEWLINE);
171 /* --------- sophisticated help ------------ */
173 hhelp (struct vty *vty) {
176 "\n'%s' - grid network generator for shortest paths problem.\n\
177 Generates problems in extended DIMACS format.\n\
179 %s X Y seed [ -cl#i -cm#i -c{c|d|p}\n\
181 -ip -il#i -im#i -in#i -is#i -ix#i -ih#i\n\
182 -p -pl#i -pm#i -pn#f -ps#f\n\
187 #i - integer number #f - real number\n\
189 Parameters of connecting arcs within one layer:\n\
190 -cl#i - #i is the upper bound on arc lengths (default 100)\n\
191 -cm#i - #i is the lower bound on arc lengths (default 0)\n\
192 -c#t - #t is the type of connecting graph: { c | d | p }\n\
193 c - cycle, d - double cycle, p - path (default d)\n\
195 Parameters of additional arcs within one layer:\n\
196 -ax#i - #i is the number of additional arcs (default 0)\n\
197 -al#i - #i is the upper bound on arc lengths (default 100)\n\
198 -am#i - #i is the lower bound on arc lengths (default 0)\n\
200 Interlayerd arc parameters:\n\
201 -ip - shuffle inter-layer arcs (default NO)\n\
202 -il#i - #i is the upper bound on arc lengths (default 10000)\n\
203 -im#i - #i is the lower bound on arc lengths (default 1000)\n\
204 -in#f - multiply l(i, j) by #f * x(j)-x(i) (default 1)\n\
205 if #f=0 - don't multiply\n\
206 -is#f - multiply l(i, j) by #f * (x(j)-x(i))^2 (default NO)\n\
207 -ix#i - #i - is the number of arcs from a node (default 1)\n\
208 -ih#i - #i - is the step between connected layers (default 1)\n\
210 Potential parameters:\n\
211 -p - generate potentials \n\
212 -pl#i - #i is the upper bound on potentials (default ll)\n\
213 -pm#i - #i is the lower bound on potentials (default lm)\n\
214 -pn#f - multiply p(i) by #f * x(i) (default NO)\n\
215 -ps#f - multiply p(i) by #f * x(i)^2 (default NO)\n\
218 " Artificial source parameters:\n\
219 -s - generate artificial source with default connecting arc lengths\n\
220 -sl#i - #i is the upper bound on art. arc lengths (default 100000000)\n\
221 -sm#i - #i is the lower bound on art. arc lengths (default sl)\n\"
225 /* ----- wrong usage ----- */
227 usage (struct vty *vty) {
228 vty_out (vty,"usage: X Y seed [-ll#i -lm#i -cl#i -p -pl#i -pm#i ...]%s",VTY_NEWLINE);
229 vty_out (vty,"help: -h or -hh%s",VTY_NEWLINE);
232 zlog_err ("error in parameter # %d\n\n", np );
236 /* parsing parameters */
237 /* checks the validity of incoming parameters */
239 spgrid_check_params ( struct vty *vty, int argc, const char **argv)
241 /* initialized by default values */
244 /* variables for generating one layer */
246 /* variables for generating spanning graph */
252 cw = PATH; /* type of spanning graph */
253 cm = 0; /* lower bound of the interval */
254 cl = 63; /* upper bound of the interval */
256 /* variables for generating additional arcs */
262 ax = 0; /* number of additional arcs */
263 am = 0; /* lower bound of the interval */
264 al = 63; /* upper bound of the interval */
266 /* variables for inter-layer arcs */
276 ip = NO; /* to mess or not to mess */
277 ix = 1; /* number of interlayered arcs in a NODE */
278 ih = 1; /* step between two layeres */
279 il = 63; //was 10000; /* upper bound of the interval */
280 im = 0; //was 1000; /* lower bound of the interval */
281 in = 1; /* l *= in * |x1-x2| */
282 is = 0; /* l *= is * |x1-x2|^2 */
284 /* variables for artifical source */
288 sl = VERY_FAR; /* upper bound of artifical arc */
290 /* variables for potentials */
297 pn = 0; /* p += ln * (x+1) */
298 ps = 0; /* p += ls * (x+1)^2 */
308 strcpy ( args, argv[0] );
310 if ((args[0] == DASH) && (args[1] == 'h'))
318 /* first parameter - horizontal size */
320 if ( ( X = atoi ( argv[0] ) ) < 1 ) {
325 /* second parameter - vertical size */
327 if ( ( Y = atoi ( argv[1] ) ) < 1 ) {
332 /* third parameter - seed */
334 if ( ( seed = atoi ( argv[2] ) ) <= 0 ) {
339 /* other parameters */
340 for ( np = 3; np < argc; np ++ ) {
341 strcpy ( args, argv[np] );
342 if ( args[0] != DASH ) {
348 case 'c' : /* spanning graph in one layer */
351 case 'l': /* upper bound of the interval */
353 cl = atol ( &args[3] );
355 case 'm': /* lower bound */
357 cm = atol ( &args[3] );
359 case 'c': /* type - cycle */
363 case 'd': /* type - double cycle */
367 case 'p': /* type - path */
372 default: /* unknown switch value */
378 case 'a' : /* additional arcs in one layer */
382 case 'l': /* upper bound of the interval */
384 al = atol ( &args[3] );
386 case 'm': /* lower bound */
388 am = atol ( &args[3] );
390 case 'x': /* number of additional arcs */
392 ax = atol ( &args[3] );
400 default: /* unknown switch value */
409 case 'i' : /* interlayered arcs */
414 case 'l': /* upper bound */
416 il = atol ( &args[3] );
418 case 'm': /* lower bound */
420 im = atol ( &args[3] );
422 case 'n': /* additional length: l *= in*|i1-i2| */
424 in = atof ( &args[3] );
426 case 's': /* additional length: l *= is*|i1-i2|^2 */
428 is = atof ( &args[3] );
430 case 'p': /* mess interlayered arcs */
434 case 'x': /* number of interlayered arcs */
436 ix = atof ( &args[3] );
442 case 'h': /* step between two layeres */
444 ih = atof ( &args[3] );
450 default: /* unknown switch value */
456 case 's' : /* additional source */
458 if ( strlen ( args ) > 2 )
462 case 'l': /* upper bound of art. arc */
464 sl = atol ( &args[3] );
466 case 'm': /* lower bound of art. arc */
468 sm = atol ( &args[3] );
470 default: /* unknown switch value */
477 case 'p' : /* potentials */
479 if ( strlen ( args ) > 2 )
483 case 'l': /* upper bound */
485 pl = atol ( &args[3] );
487 case 'm': /* lower bound */
489 pm = atol ( &args[3] );
491 case 'n': /* additional: p *= pn*(x+1) */
493 pn = atof ( &args[3] );
495 case 's': /* additional: p = ps* (x+1)^2 */
497 ps = atof ( &args[3] );
499 default: /* unknown switch value */
506 default: /* unknoun case */
517 /* generator of layered networks for the shortest paths problem;
518 extended DIMACS format for output */
520 gen_spgrid_topology (struct vty *vty, struct list *topology)
522 /* ----- ajusting parameters ----- */
525 if ( cl < cm ) { lx = cl; cl = cm; cm = lx; }
527 /* additional arcs */
528 if ( al < am ) { lx = al; al = am; am = lx; }
530 /* interlayered arcs */
531 if ( il < im ) { lx = il; il = im; im = lx; }
533 /* potential parameters */
536 if ( ! pl_f ) pl = il;
537 if ( ! pm_f ) pm = im;
538 if ( pl < pm ) { lx = pl; pl = pm; pm = lx; }
541 /* number of nodes and arcs */
543 n = (double)X *(double)Y + 1;
545 m = (double)Y; /* arcs from source */
559 m += (double)X * (double)mc; /* spanning arcs */
560 m += (double)X * (double)ax; /* additional arcs */
562 /* interlayered arcs */
563 for ( x = 0; x < X; x ++ )
565 dl = ( ( X - x - 1 ) + ( ih - 1 ) ) / ih;
566 if ( dl > ix ) dl = ix;
567 m += (double)Y * (double)dl;
570 /* artifical source parameters */
573 if ( ! sm_f ) sm = sl;
574 if ( sl < sm ) { lx = sl; sl = sm; sm = lx; }
577 if ( n >= (double)LONG_MAX || m >= (double)LONG_MAX )
579 zlog_err ("Too large problem. It can't be generated\n");
584 n0 = (long)n; m0 = (long)m;
588 mess = (long*) calloc ( Y, sizeof ( long ) );
591 zlog_info ("Generating topology for ISIS");
593 source = ( s_f ) ? n0-1 : n0;
595 if ( p_f ) /* generating potentials */ {
596 p = (long*) calloc ( n0+1, sizeof (long) );
601 for ( x = 0; x < X; x ++ ) {
602 for ( y = 0; y < Y; y ++ ) {
603 p_t = pm + nrand ( pl );
604 if ( pn_f ) p_t *= (long) ( (1 + x) * pn );
605 if ( ps_f ) p_t *= (long) ( (1 + x) * ( (1 + x) * ps ));
607 p[ NODE ( x, y ) ] = p_t;
611 if ( s_f ) p[n0-1] = 0;
614 if ( s_f ) /* additional arcs from artifical source */
620 for ( x = X - 1; x >= 0; x -- )
621 for ( y = Y - 1; y >= 0; y -- )
624 s = sm + nrand ( sl );
625 print_arc (vty, topology, n0, i, s );
628 print_arc (vty, topology, n0, n0-1, 0 );
632 /* ----- generating arcs within layers ----- */
638 for ( x = 0; x < X; x ++ )
640 /* generating arcs within one layer */
641 for ( y = 0; y < Y-1; y ++ )
643 /* generating spanning graph */
646 l = cm + nrand ( cl );
647 print_arc (vty, topology, i, j, l );
649 if ( cw == DOUBLE_CYCLE )
651 l = cm + nrand ( cl );
652 print_arc (vty, topology, j, i, l );
660 l = cm + nrand ( cl );
661 print_arc (vty, topology, i, j, l );
663 if ( cw == DOUBLE_CYCLE )
665 l = cm + nrand ( cl );
666 print_arc (vty, topology, j, i, l );
670 /* generating additional arcs */
672 for ( k = ax; k > 0; k -- )
677 while ( yy2 == yy1 );
680 l = am + nrand ( al );
681 print_arc (vty, topology, i, j, l );
685 /* ----- generating interlayered arcs ------ */
689 /* arcs from the source */
691 for ( y = 0; y < Y; y ++ )
693 l = im + nrand ( il );
695 print_arc (vty, topology, source, i, l );
698 for ( x = 0; x < X-1; x ++ )
700 /* generating arcs from one layer */
701 for ( count = 0, xn = x + 1;
702 count < ix && xn < X;
706 for ( y = 0; y < Y; y ++ )
709 for ( y = 0; y < Y; y ++ )
717 mess[ yyp ] = mess[ Y - y - 1 ];
721 j = NODE ( xn, yyn );
722 l = im + nrand ( il );
724 l *= (long) ( in * dx );
726 l *= (long) ( ( is * dx ) * dx );
727 print_arc (vty, topology, i, j, l );