1 /*** Copyright 1989 Norbert Schlenker. All rights reserved.
3 *** This software is distributed subject to the following provisions:
4 *** - this notice may not be removed;
5 *** - you may modify the source code, as long as redistributed
6 *** versions have their modifications clearly marked;
7 *** - no charge, other than a nominal copying fee, may be made
8 *** when providing copies of this source code to others;
9 *** - if this source code is used as part of a product which is
10 *** distributed only as a binary, a copy of this source code
11 *** must be included in the distribution.
13 *** Unlike the GNU GPL, use of this code does not obligate you to
14 *** disclose your own proprietary source code.
16 *** The author of this software provides no warranty, express or
17 *** implied, as to its utility or correctness. That said, reports
18 *** of bugs or compatibility problems will be gladly received by
19 *** nfs@princeton.edu, and fixes will be attempted.
23 /*** netgen - C version of the standard NETGEN network generator
24 *** This program is a functional equivalent of the
25 *** standard network generator NETGEN described in:
26 *** Klingman, D., A. Napier, and J. Stutz, "NETGEN: A Program
27 *** for Generating Large Scale Capacitated Assignment,
28 *** Transportation, and Minimum Cost Flow Network Problems",
29 *** Management Science 20, 5, 814-821 (1974)
31 *** This software provides a number of interfaces for use by
32 *** network solvers. Standard call interfaces are supplied for
33 *** use by (Unix) C and Fortran solvers, with generation parameters
34 *** passed into the generator and the flow network passed back to
35 *** the solver via large external (COMMON in Fortran) arrays.
36 *** For the DIMACS challenge, this code will produce output files
37 *** in the appropriate format for later reading by solvers.
38 *** Undefine the symbol DIMACS when using the call interface.
40 *** The generator produces exact duplicates of the networks
41 *** made by the Fortran code (even though that means bugs
42 *** are being perpetuated). It is faster by orders of magnitude
43 *** in generating large networks, primarily by imposing the
44 *** notion of the abstract data type INDEX_LIST and implementing
45 *** the type operations in a reasonably efficient fashion.
48 /*** Generates transportation problems if:
49 *** SOURCES+SINKS == NODES && TSOURCES == TSINKS == 0
51 *** Generates assignment problems if above conditions are satisfied, and:
52 *** SOURCES == SINKS && SUPPLY == SOURCES
54 *** Generates maximum flow problems if not an assignment problem and:
55 *** MINCOST == MAXCOST == 1
57 *** Implementation notes:
59 *** This file contains both a Fortran and a C interface. The
60 *** Fortran interface is suffixed with an underscore to make it
61 *** callable in the normal fashion from Fortran (a Unix convention).
63 *** Because Fortran has no facility for pointers, the common arrays
64 *** are statically allocated. Static allocation has nothing to recommend
65 *** it except for the need for a Fortran interface.
67 *** This software expects input parameters to be long integers
68 *** (in the sense of C); that means no INTEGER*2 from Fortran callers.
70 *** Compiling with -DDIMACS produces a program that reads problem
71 *** parameters, generates the appropriate problem, and prints it.
73 *** Compiling with -DDEBUG produces code with externally visible
74 *** procedure names, useful for debugging and profiling.
78 /*** System interfaces */
83 /*** Public interfaces */
85 #define ALLOCATE_NETWORK
90 /*** Private interfaces */
95 #define PRIVATE static
99 PRIVATE void create_supply(NODE, CAPACITY); /* create supply nodes */
100 PRIVATE void create_assignment(long*); /* create assignment problem */
101 PRIVATE void sort_skeleton(int); /* sorts skeleton chains */
102 PRIVATE void pick_head(long*, int, NODE); /* choose destination nodes for rubbish arcs */
103 PRIVATE void error_exit(long); /* print error message and exit */
105 PRIVATE void create_supply(); /* create supply nodes */
106 PRIVATE void create_assignment(); /* create assignment problem */
107 PRIVATE void sort_skeleton(); /* sorts skeleton chains */
108 PRIVATE void pick_head(); /* chooses destination nodes for rubbish arcs */
109 PRIVATE void error_exit(); /* print error message and exit */
112 /*** Private variables */
114 static NODE nodes_left;
115 static ARC arc_count;
116 static NODE pred[MAXARCS];
117 static NODE head[MAXARCS];
118 static NODE tail[MAXARCS];
123 #define MIN(x, y) ((x) < (y) ? (x) : (y))
124 #define MAX(x, y) ((x) > (y) ? (x) : (y))
125 #define SAVE_ARC(tail, head, cost, capacity) /* records an arc where our caller can get it */ \
127 FROM[arc_count] = tail; \
128 TO [arc_count] = head; \
129 C [arc_count] = cost; \
130 U [arc_count] = capacity; \
135 /*** Fortran callable interface routine */
137 void netgen_(seed, parms, generated_nodes, generated_arcs)
138 long* seed; /* pointer to random seed */
139 long parms[PROBLEM_PARMS]; /* problem parameters */
140 long* generated_nodes; /* number of generated nodes */
141 long* generated_arcs; /* number of generated arcs */
143 *generated_nodes = NODES;
144 if ((*generated_arcs = netgen(*seed, parms)) < 0)
145 error_exit(*generated_arcs);
149 /*** C callable interface routine */
151 ARC netgen(seed, parms)
152 long seed; /* random seed */
153 long parms[]; /* problem parameters */
158 NODE sinks_per_source;
170 /*** Perform sanity checks on the input */
174 if (NODES > MAXNODES || DENSITY > MAXARCS)
180 (SOURCES + SINKS > NODES) ||
181 (MINCOST > MAXCOST) ||
182 (SUPPLY < SOURCES) ||
183 (TSOURCES > SOURCES) ||
185 (HICOST < 0 || HICOST > 100) ||
186 (CAPACITATED < 0 || CAPACITATED > 100) ||
191 /*** Do a little bit of setting up. */
196 nodes_left = NODES - SINKS + TSINKS;
198 if ((SOURCES-TSOURCES)+(SINKS-TSINKS) == NODES &&
199 (SOURCES-TSOURCES) == (SINKS-TSINKS) &&
201 create_assignment(parms);
205 (void)memset((void *)B, 0, sizeof(B));/* set supplies and demands to zero */
207 create_supply((NODE)SOURCES, (CAPACITY)SUPPLY);
210 /*** Form most of the network skeleton. First, 60% of the transshipment
211 *** nodes are divided evenly among the various sources; the remainder
212 *** are chained onto the end of the chains belonging to random sources.
215 for (i = 1; i <= SOURCES; i++) /* point SOURCES at themselves */
217 handle = make_index_list((INDEX)(SOURCES + 1), (INDEX)(NODES - SINKS));
219 for (i = NODES-SOURCES-SINKS; i > (4*(NODES-SOURCES-SINKS)+9)/10; i--) {
220 node = choose_index(handle, (INDEX)ng_random(1L, (long)index_size(handle)));
221 pred[node] = pred[source];
223 if (++source > SOURCES)
226 for ( ; i > 0; --i) {
227 node = choose_index(handle, (INDEX)ng_random(1L, (long)index_size(handle)));
228 source = ng_random(1L, SOURCES);
229 pred[node] = pred[source];
232 free_index_list(handle);
235 /*** For each source chain, hook it to an "appropriate" number of sinks,
236 *** place capacities and costs on the skeleton edges, and then call
237 *** pick_head to add a bunch of rubbish edges at each node on the chain.
240 for (source = 1; source <= SOURCES; source++) {
243 while (node != source) {
245 head[sort_count] = node;
246 node = tail[sort_count] = pred[node];
248 if ((NODES-SOURCES-SINKS) == 0)
249 sinks_per_source = SINKS/SOURCES + 1;
251 /* changing to handle overflows with large n; Mar 18 -- jc */
252 sinks_per_source = ((double) 2*sort_count*SINKS) / ((double) NODES-SOURCES-SINKS);
253 sinks_per_source = MAX(2, MIN(sinks_per_source, SINKS));
254 sinks = (NODE*) malloc(sinks_per_source * sizeof(NODE));
255 handle = make_index_list((INDEX)(NODES - SINKS), (INDEX)(NODES - 1));
256 for (i = 0; i < sinks_per_source; i++) {
257 sinks[i] = choose_index(handle, (INDEX)ng_random(1L, (long)index_size(handle)));
259 if (source == SOURCES && index_size(handle) > 0) {
260 sinks = (NODE*) realloc((void *)sinks, (sinks_per_source + index_size(handle)) * sizeof(NODE));
261 while (index_size(handle) > 0) {
262 j = choose_index(handle, 1);
264 sinks[sinks_per_source++] = j;
267 free_index_list(handle);
269 chain_length = sort_count;
270 supply_per_sink = B[source-1] / sinks_per_source;
272 for (i = 0; i < sinks_per_source; i++) {
274 partial_supply = ng_random(1L, (long)supply_per_sink);
275 j = ng_random(0L, (long)sinks_per_source - 1);
276 tail[sort_count] = k;
277 head[sort_count] = sinks[i] + 1;
278 B[sinks[i]] -= partial_supply;
279 B[sinks[j]] -= (supply_per_sink - partial_supply);
281 for (j = ng_random(1L, (long)chain_length); j > 0; j--)
284 B[sinks[0]] -= (B[source-1] % sinks_per_source);
287 sort_skeleton(sort_count);
288 tail[sort_count+1] = 0;
289 for (i = 1; i <= sort_count; ) {
290 handle = make_index_list((INDEX)(SOURCES - TSOURCES + 1), (INDEX)NODES);
291 remove_index(handle, (INDEX)tail[i]);
293 while (it == tail[i]) {
294 remove_index(handle, (INDEX)head[i]);
296 if (ng_random(1L, 100L) <= CAPACITATED)
297 cap = MAX(B[source-1], MINCAP);
299 if (ng_random(1L, 100L) > HICOST)
300 cost = ng_random(MINCOST, MAXCOST);
301 SAVE_ARC(it,head[i],cost,cap);
304 pick_head(parms, handle, it);
305 free_index_list(handle);
310 /*** Add more rubbish edges out of the transshipment sinks. */
312 for (i = NODES - SINKS + 1; i <= NODES - SINKS + TSINKS; i++) {
313 handle = make_index_list((INDEX)(SOURCES - TSOURCES + 1), (INDEX)NODES);
314 remove_index(handle, (INDEX)i);
315 pick_head(parms, handle, i);
316 free_index_list(handle);
323 PRIVATE void create_supply(sources, supply)
327 CAPACITY supply_per_source = supply / sources;
328 CAPACITY partial_supply;
331 for (i = 0; i < sources; i++) {
332 B[i] += (partial_supply = ng_random(1L, (long)supply_per_source));
333 B[ng_random(0L, (long)(sources - 1))] += supply_per_source - partial_supply;
335 B[ng_random(0L, (long)(sources - 1))] += supply % sources;
339 PRIVATE void create_assignment(parms)
342 INDEX_LIST skeleton, handle;
346 for (source = 0; source < NODES/2; source++)
348 for ( ; source < NODES; source++)
351 skeleton = make_index_list((INDEX)(SOURCES + 1), (INDEX)NODES);
352 for (source = 1; source <= NODES/2; source++) {
353 index = choose_index(skeleton, (INDEX)ng_random(1L, (long)index_size(skeleton)));
354 SAVE_ARC(source, index, ng_random(MINCOST, MAXCOST), 1);
355 handle = make_index_list((INDEX)(SOURCES + 1), (INDEX)NODES);
356 remove_index(handle, index);
357 pick_head(parms, handle, source);
358 free_index_list(handle);
360 free_index_list(skeleton);
364 PRIVATE void sort_skeleton(sort_count) /* Shell sort */
371 while ((m /= 2) != 0) {
373 for (j = 1; j <= k; j++) {
375 while (i >= 1 && tail[i] > tail[i+m]) {
389 PRIVATE void pick_head(parms, handle, desired_tail)
394 NODE non_sources = NODES - SOURCES + TSOURCES;
396 /* changing Aug 29 -- jc
397 ARC remaining_arcs = DENSITY - arc_count;
399 int remaining_arcs = (int) DENSITY - (int) arc_count;
406 /* changing Aug 29 -- jc
409 if ((2 * (int) nodes_left) >= (int) remaining_arcs)
412 if ((remaining_arcs + non_sources - pseudo_size(handle) - 1) / (nodes_left + 1) >= non_sources - 1) {
415 upper_bound = 2 * (remaining_arcs / (nodes_left + 1) - 1);
417 limit = ng_random(1L, upper_bound);
419 limit = remaining_arcs;
420 /* changing to handle overflows with large n; Mar 18 -- jc */
421 } while ( ((double) nodes_left * (non_sources - 1)) < ((double) remaining_arcs - limit));
424 for ( ; limit > 0; limit--) {
425 index = choose_index(handle, (INDEX)ng_random(1L, (long)pseudo_size(handle)));
427 if (ng_random(1L, 100L) <= CAPACITATED)
428 cap = ng_random(MINCAP, MAXCAP);
430 /* adding Aug 29 -- jc */
431 if ((1 <= index) && (index <= NODES)) {
432 SAVE_ARC(desired_tail, index, ng_random(MINCOST, MAXCOST), cap);
439 /*** Print an appropriate error message and then exit with a nonzero code. */
441 PRIVATE void error_exit(rc)
446 fprintf(stderr, "NETGEN requires a positive random seed\n");
449 fprintf(stderr, "Problem too large for generator\n");
452 fprintf(stderr, "Inconsistent parameter settings - check the input\n");
454 case ALLOCATION_FAILURE:
455 fprintf(stderr, "Memory allocation failure\n");
458 fprintf(stderr, "Internal error\n");
461 exit(1000 - (int)rc);
464 #ifdef DIMACS /* generates network on standard output */
466 #define READ(v) /* read one variable using scanf */ \
467 switch( scanf("%ld", &v) ) { \
474 int orig_main(long seed,long problem,long *parms)
479 /*** Read problem parameters and generate networks */
481 printf("c NETGEN flow network generator (C version)\n");
482 printf("c Problem %2ld input parameters\n", problem);
483 printf("c ---------------------------\n");
484 printf("c Random seed: %10ld\n", seed);
485 printf("c Number of nodes: %10ld\n", NODES);
486 printf("c Source nodes: %10ld\n", SOURCES);
487 printf("c Sink nodes: %10ld\n", SINKS);
488 printf("c Number of arcs: %10ld\n", DENSITY);
489 printf("c Minimum arc cost: %10ld\n", MINCOST);
490 printf("c Maximum arc cost: %10ld\n", MAXCOST);
491 printf("c Total supply: %10ld\n", SUPPLY);
492 printf("c Transshipment -\n");
493 printf("c Sources: %10ld\n", TSOURCES);
494 printf("c Sinks: %10ld\n", TSINKS);
495 printf("c Skeleton arcs -\n");
496 printf("c With max cost: %10ld%%\n", HICOST);
497 printf("c Capacitated: %10ld%%\n", CAPACITATED);
498 printf("c Minimum arc capacity: %10ld\n", MINCAP);
499 printf("c Maximum arc capacity: %10ld\n", MAXCAP);
501 if ((arcs = netgen(seed, parms)) < 0)
503 if ((SOURCES-TSOURCES)+(SINKS-TSINKS) == NODES &&
504 (SOURCES-TSOURCES) == (SINKS-TSINKS) &&
507 printf("c *** Assignment ***\n");
509 printf("p asn %ld %ld\n", NODES, arcs);
510 for (i = 0; i < NODES; i++) {
512 printf("n %ld\n", i + 1);
514 for (i = 0; i < arcs; i++) {
515 printf("a %ld %ld %ld\n", FROM[i], TO[i], C[i]);
518 if (MINCOST == 1 && MAXCOST == 1) {
520 printf("c *** Maximum flow ***\n");
522 printf("p max %ld %ld\n", NODES, arcs);
523 for (i = 0; i < NODES; i++) {
525 printf("n %ld s\n", i + 1);
528 printf("n %ld t\n", i + 1);
530 for (i = 0; i < arcs; i++) {
531 printf("a %ld %ld %ld\n", FROM[i], TO[i], U[i]);
535 printf("c *** Minimum cost flow ***\n");
537 printf("p min %ld %ld\n", NODES, arcs);
538 for (i = 0; i < NODES; i++) {
540 printf("n %ld %ld\n", i + 1, B[i]);
542 for (i = 0; i < arcs; i++) {
543 printf("a %ld %ld %ld %ld %ld\n", FROM[i], TO[i], 0, U[i], C[i]);