spacyc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #include <zebra.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <values.h>
  6. #include "random.c"
  7. #define DASH '-'
  8. #define VERY_FAR 100000000
  9. /* generator of acyclic random networks for the shortest paths problem;
  10. extended DIMACS format for output */
  11. main ( argc, argv )
  12. int argc;
  13. char* argv[];
  14. {
  15. char args[30];
  16. long n,
  17. n0,
  18. source,
  19. i,
  20. i0,
  21. j,
  22. dij;
  23. long m,
  24. m0,
  25. mc,
  26. k;
  27. long *p,
  28. p_t,
  29. l,
  30. lx;
  31. long seed,
  32. seed1,
  33. seed2;
  34. int ext=0;
  35. FILE *fout;
  36. /* variables for lengths generating */
  37. /* initialized by default values */
  38. int l_f = 0, ll_f = 0, lm_f = 0, ln_f = 0, ls_f = 0;
  39. long ll = 10000, /* upper bound of the interval */
  40. lm = 0; /* lower bound of the interval */
  41. double ln = 0, /* l += ln * |i-j| */
  42. ls = 0; /* l += ls * |i-j|^2 */
  43. /* variables for connecting path(s) */
  44. int c_f = 0, cl_f = 0, ch_f = 0, c_rand = 1;
  45. long cl = 1; /* length of path arc */
  46. long ch; /* number of arcs in the path
  47. n - by default */
  48. /* variables for artifical source */
  49. int s_f = 0, sl_f = 0, sm_f = 0;
  50. long sl = VERY_FAR, /* upper bound of artifical arc */
  51. sm, /* lower bound of artifical arc */
  52. s;
  53. /* variables for potentials */
  54. int p_f = 0, pl_f = 0, pm_f = 0, pn_f = 0, ps_f = 0,
  55. pa_f = 0, pap_f = 0, pac_f = 0;
  56. long pl, /* upper bound of the interval */
  57. pm; /* lower bound of the interval */
  58. double pn = 0, /* l += ln * |i-j| */
  59. ps = 0, /* l += ls * |i-j|^2 */
  60. pap = 0, /* part of nodes with alternative dustribution */
  61. pac = -1; /* multiplier for alternative distribution */
  62. int np; /* number of parameter parsing now */
  63. #define PRINT_ARC( i, j, length )\
  64. {\
  65. l = length;\
  66. if ( p_f ) l += ( p[i] - p[j] );\
  67. printf ("a %8ld %8ld %12ld\n", i, j, l );\
  68. }
  69. /* parsing parameters */
  70. if ( argc < 2 ) goto usage;
  71. np = 0;
  72. strcpy ( args, argv[1] );
  73. if ( ( args[0] == DASH ) && ( args[1] == 'h')
  74. )
  75. goto help;
  76. if ( argc < 4 ) goto usage;
  77. /* first parameter - number of nodes */
  78. np = 1;
  79. if ( ( n = atoi ( argv[1] ) ) < 2 ) goto usage;
  80. /* second parameter - number of arcs */
  81. np = 2;
  82. if ( ( m = atoi ( argv[2] ) ) < n ) goto usage;
  83. /* third parameter - seed */
  84. np=3;
  85. if ( ( seed = atoi ( argv[3] ) ) <= 0 ) goto usage;
  86. /* other parameters */
  87. for ( np = 4; np < argc; np ++ )
  88. {
  89. strcpy ( args, argv[np] );
  90. if ( args[0] != DASH ) goto usage;
  91. switch ( args[1] )
  92. {
  93. case 'l' : /* an interval for arc length */
  94. l_f = 1;
  95. switch ( args[2] )
  96. {
  97. case 'l': /* length of the interval */
  98. ll_f = 1;
  99. ll = (long) atof ( &args[3] );
  100. break;
  101. case 'm': /* minimal bound */
  102. lm_f = 1;
  103. lm = (long ) atof ( &args[3] );
  104. break;
  105. case 'n': /* additional length: l*|i-j| */
  106. ln_f = 1;
  107. ln = atof ( &args[3] );
  108. break;
  109. case 's': /* additional length: l*|i-j|^2 */
  110. ls_f = 1;
  111. ls = atof ( &args[3] );
  112. break;
  113. default: /* unknown switch value */
  114. goto usage;
  115. }
  116. break;
  117. case 'c' : /* connecting path(s) */
  118. c_f = 1;
  119. switch ( args[2] )
  120. {
  121. case 'l': /* length of path arc */
  122. c_rand = 0; /* fixed arc length */
  123. cl_f = 1;
  124. cl = (long) atof ( &args[3] );
  125. break;
  126. case 'h': /* number of arcs in connecting path */
  127. ch_f = 1;
  128. ch = (long) atof ( &args[3] );
  129. if ( ch < 1 || ch > n ) goto usage;
  130. break;
  131. default: /* unknown switch value */
  132. goto usage;
  133. }
  134. break;
  135. case 's' : /* additional source */
  136. s_f = 1;
  137. if ( strlen ( args ) > 2 )
  138. {
  139. switch ( args[2] )
  140. {
  141. case 'l': /* upper bound of art. arc */
  142. sl_f = 1;
  143. sl = (long) atof ( &args[3] );
  144. break;
  145. case 'm': /* lower bound of art. arc */
  146. sm_f = 1;
  147. sm = (long) atof ( &args[3] );
  148. break;
  149. default: /* unknown switch value */
  150. goto usage;
  151. }
  152. }
  153. break;
  154. case 'p' : /* potentials */
  155. p_f = 1;
  156. if ( strlen ( args ) > 2 )
  157. {
  158. switch ( args[2] )
  159. {
  160. case 'l': /* length of the interval */
  161. pl_f = 1;
  162. pl = (long) atof ( &args[3] );
  163. break;
  164. case 'm': /* minimal bound */
  165. pm_f = 1;
  166. pm = (long ) atof ( &args[3] );
  167. break;
  168. case 'n': /* additional length: l*|i-j| */
  169. pn_f = 1;
  170. pn = atof ( &args[3] );
  171. break;
  172. case 's': /* additional length: l*|i-j|^2 */
  173. ps_f = 1;
  174. ps = atof ( &args[3] );
  175. break;
  176. case 'a': /* bipolar distribution */
  177. pa_f = 1;
  178. switch ( args[3] )
  179. {
  180. case 'p': /* % of alternative potentials */
  181. pap_f = 1;
  182. pap = atof ( &args[4] );
  183. if ( pap < 0 ) pap = 0;
  184. if ( pap > 100 ) pap = 100;
  185. pap /= 100;
  186. break;
  187. case 'c': /* multiplier */
  188. pac_f = 1;
  189. pac = atof ( &args[4] );
  190. break;
  191. default: /* unknown switch value */
  192. goto usage;
  193. }
  194. break;
  195. default: /* unknown switch value */
  196. goto usage;
  197. }
  198. }
  199. break;
  200. default : /* unknoun case */
  201. goto usage;
  202. }
  203. }
  204. /* ----- ajusting parameters ----- */
  205. n0 = n; m0 = m;
  206. /* length parameters */
  207. if ( ll < lm ) { lx = ll; ll = lm; lm = lx; }
  208. /* potential parameters */
  209. if ( p_f )
  210. {
  211. if ( ! pl_f ) pl = ll;
  212. if ( ! pm_f ) pm = lm;
  213. if ( pl < pm ) { lx = pl; pl = pm; pm = lx; }
  214. }
  215. /* path(s) parameters */
  216. if ( ! ch_f ) ch = n - 1;
  217. mc = n - 1;
  218. /* artifical source parameters */
  219. if ( s_f )
  220. { m0 += n; n0 ++ ;
  221. if ( ! sm_f ) sm = sl;
  222. if ( sl < sm ) { lx = sl; sl = sm; sm = lx; }
  223. }
  224. /*----- printing title -----*/
  225. printf ("c acyclic network for shortest paths problem\n");
  226. printf ("c extended DIMACS format\nc\n" );
  227. /* name of the problem */
  228. printf ("t ac_%ld_%ld_%ld_", n, m, seed );
  229. if ( l_f )
  230. printf ("%c", 'l');
  231. if ( c_f )
  232. printf ("%c", 'c');
  233. if ( s_f )
  234. printf ("%c", 's');
  235. if ( p_f )
  236. printf ("%c", 'p');
  237. printf ("\nc\n");
  238. /* printing additional information */
  239. if ( l_f )
  240. printf ("c length -> min: %ld max: %ld k1: %.2f k2: %.2f\n",
  241. lm, ll, ln, ls );
  242. if ( c_f )
  243. printf ("c path(s) -> number of arcs: %ld arc length: %ld\n",
  244. ch, cl );
  245. if ( s_f )
  246. printf ("c length of arcs from artifical source -> min: %ld max: %ld\n",
  247. sm, sl );
  248. if ( p_f )
  249. {
  250. printf ("c potentials -> min: %ld max: %ld k1: %.2f k2: %.2f\n",
  251. pm, pl, pn, ps );
  252. if ( pa_f )
  253. printf ("c potentials -> part of alternative distribution: %.2f k: %.2f\n",
  254. pap, pac );
  255. }
  256. printf ("c\n" );
  257. printf ("p sp %8ld %8ld\nc\n", n0, m0 );
  258. source = ( s_f ) ? n0 : 1;
  259. printf ("n %8ld\nc\n", source );
  260. if ( p_f ) /* generating potentials */
  261. {
  262. seed1 = 2*seed + 1;
  263. p = (long*) calloc ( n+2, sizeof (long) );
  264. init_rand ( seed1);
  265. pl = pl - pm + 1;
  266. for ( i = 0; i <= n; i ++ )
  267. {
  268. p_t = pm + nrand ( pl );
  269. if ( pn_f ) p_t += (long) ( i * pn );
  270. if ( ps_f ) p_t += (long) ( i * ( i * ps ));
  271. if ( pap_f )
  272. if ( rand01() < pap )
  273. p_t = (long) ( p_t * pac );
  274. p[i] = p_t;
  275. }
  276. p[n+1] = 0;
  277. }
  278. if ( s_f ) /* additional arcs from artifical source */
  279. {
  280. seed2 = 3*seed + 1;
  281. init_rand ( seed2 );
  282. sl = sl - sm + 1;
  283. for ( i = n; i > 1; i -- )
  284. {
  285. s = sm + nrand ( sl );
  286. PRINT_ARC ( n0, i, s )
  287. }
  288. PRINT_ARC ( n0, 1, 0 )
  289. }
  290. /* initialize random number generator */
  291. init_rand ( seed );
  292. ll = ll - lm + 1;
  293. /* generating connecting path(s) */
  294. for ( i = 1; i < n; i ++ )
  295. {
  296. if ( ( (i-1) % ch ) != 0 )
  297. i0 = i;
  298. else
  299. i0 = 1;
  300. if (c_rand)
  301. cl = lm + nrand(ll);
  302. PRINT_ARC ( i0, i+1, cl )
  303. }
  304. /* generating random arcs */
  305. for ( k = 1; k <= m - mc; k ++ )
  306. {
  307. i = 1 + nrand ( n );
  308. do
  309. j = 1 + nrand ( n );
  310. while ( j == i );
  311. if ( i > j )
  312. { i0 = i; i = j; j = i0; }
  313. dij = j - i;
  314. l = lm + nrand ( ll );
  315. if ( ln_f ) l += (long) ( dij * ln );
  316. if ( ls_f ) l += (long) ( dij * ( dij * ls ) );
  317. PRINT_ARC ( i, j, l );
  318. }
  319. /* all is done */
  320. exit (ext);
  321. /* ----- wrong usage ----- */
  322. usage:
  323. fprintf ( stderr,
  324. "\nusage: %s n m seed [ -ll#i -lm#i -cl#i -p -pl#i -pm#i ... ]\n\
  325. help: %s -h\n\n", argv[0], argv[0] );
  326. if ( np > 0 )
  327. fprintf ( stderr, "error in parameter # %d\n\n", np );
  328. exit (4);
  329. /* ---- help ---- */
  330. help:
  331. if ( args[2] == 'h') goto hhelp;
  332. fprintf ( stderr,
  333. "\n'%s' - acyclic network generator for shortest paths problem.\n\
  334. Generates problems in extended DIMACS format.\n\
  335. \n\
  336. %s n m seed [ -ll#i -lm#i -cl#i -p -pl#i -pm#i ... ]\n\
  337. %s -hh\n\
  338. \n\
  339. #i - integer number #f - real number\n\
  340. \n\
  341. -ll#i - #i is the upper bound on arc lengths (default 10000)\n\
  342. -lm#i - #i is the lower bound on arc lengths (default 0)\n\
  343. -cl#i - #i is length of arcs in connecting path(s) (default random)\n\
  344. -p - generate potentials \n\
  345. -pl#i - #i is the upper bound on potentials (default ll)\n\
  346. -pm#i - #i is the lower bound on potentials (default lm)\n\
  347. \n\
  348. -hh - extended help \n\n",
  349. argv[0], argv[0], argv[0] );
  350. exit (0);
  351. /* --------- sophisticated help ------------ */
  352. hhelp:
  353. if ( argc < 3 )
  354. fout = stderr;
  355. else
  356. fout = fopen ( argv[2], "w" );
  357. if ( fout == NULL )
  358. { fprintf ( stderr, "\nCan't open file '%s' for writing help\n\n", argv[2] );
  359. exit ( 2 );
  360. }
  361. fprintf (fout,
  362. "\n'%s' - acyclic network generator for shortest paths problem.\n\
  363. Generates problems in extended DIMACS format.\n\
  364. \n\
  365. %s n m seed [ -ll#i -lm#i -ln#f -ls#f\n\
  366. -p -pl#i -pm#i -pn#f -ps#f -pap#i -pac#f\n\
  367. -cl#i -ch#i\n\
  368. -s -sl#i -sm#i\n\
  369. ]\n\
  370. %s -hh file_name\n\
  371. \n\
  372. #i - integer number #f - real number\n\
  373. \n\
  374. Arc length parameters:\n\
  375. -ll#i - #i is the upper bound on arc lengths (default 10000)\n\
  376. -lm#i - #i is the lower bound on arc lengths (default 0)\n\
  377. -ln#f - multipliy l(i, j) by #f * |i-j| (default 0)\n\
  378. -ls#f - multipliy l(i, j) by #f * |i-j|^2 (default 0)\n\
  379. \n\
  380. Potential parameters:\n\
  381. -p - generate potentials \n\
  382. -pl#i - #i is the upper bound on potentials (default ll)\n\
  383. -pm#i - #i is the lower bound on potentials (default lm)\n\
  384. -pn#f - multiply p(i) by #f * i (default 0)\n\
  385. -ps#f - multiply p(i) by #f * i^2 (default 0)\n\
  386. -pap#i - percentage of alternative potential nodes (default 0)\n\
  387. -pac#f - if i is alternative, multiply p(i) by #f (default -1)\n\
  388. \n\
  389. Connecting path(s) parameters:\n\
  390. -cl#i - #i is length of arcs in connecting path(s) (default random)\n\
  391. -ch#i - #i is length of connecting path(s) (default n-1)\n\
  392. \n\
  393. Artificial source parameters:\n\
  394. -s - generate artificial source with default connecting arc lengths\n\
  395. -sl#i - #i is the upper bound on art. arc lengths (default 100000000)\n\
  396. -sm#i - #i is the lower bound on art. arc lengths (default sl)\n\
  397. \n\
  398. -hh file_name - save this help in the file 'file_name'\n\n",
  399. argv[0], argv[0], argv[0] );
  400. exit (0);
  401. }