1 /* glpnet04.c (grid-like network problem generator) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * This code is a modified version of the program GRIDGEN, a grid-like
7 * network problem generator developed by Yusin Lee and Jim Orlin.
8 * The original code is publically available on the DIMACS ftp site at:
9 * <ftp://dimacs.rutgers.edu/pub/netflow/generators/network/gridgen>.
11 * All changes concern only the program interface, so this modified
12 * version produces exactly the same instances as the original version.
14 * Changes were made by Andrew Makhorin <mao@gnu.org>.
16 * GLPK is free software: you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * GLPK is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
28 ***********************************************************************/
32 /***********************************************************************
35 * glp_gridgen - grid-like network problem generator
39 * int glp_gridgen(glp_graph *G, int v_rhs, int a_cap, int a_cost,
40 * const int parm[1+14]);
44 * The routine glp_gridgen is a grid-like network problem generator
45 * developed by Yusin Lee and Jim Orlin.
47 * The parameter G specifies the graph object, to which the generated
48 * problem data have to be stored. Note that on entry the graph object
49 * is erased with the routine glp_erase_graph.
51 * The parameter v_rhs specifies an offset of the field of type double
52 * in the vertex data block, to which the routine stores the supply or
53 * demand value. If v_rhs < 0, the value is not stored.
55 * The parameter a_cap specifies an offset of the field of type double
56 * in the arc data block, to which the routine stores the arc capacity.
57 * If a_cap < 0, the capacity is not stored.
59 * The parameter a_cost specifies an offset of the field of type double
60 * in the arc data block, to which the routine stores the per-unit cost
61 * if the arc flow. If a_cost < 0, the cost is not stored.
63 * The array parm contains description of the network to be generated:
66 * parm[1] two-ways arcs indicator:
67 * 1 - if links in both direction should be generated
69 * parm[2] random number seed (a positive integer)
70 * parm[3] number of nodes (the number of nodes generated might be
71 * slightly different to make the network a grid)
73 * parm[5] number of sources
74 * parm[6] number of sinks
75 * parm[7] average degree
77 * parm[9] distribution of arc costs:
80 * parm[10] lower bound for arc cost (uniform)
81 * 100 * lambda (exponential)
82 * parm[11] upper bound for arc cost (uniform)
83 * not used (exponential)
84 * parm[12] distribution of arc capacities:
87 * parm[13] lower bound for arc capacity (uniform)
88 * 100 * lambda (exponential)
89 * parm[14] upper bound for arc capacity (uniform)
90 * not used (exponential)
94 * If the instance was successfully generated, the routine glp_gridgen
95 * returns zero; otherwise, if specified parameters are inconsistent,
96 * the routine returns a non-zero error code.
100 * This network generator generates a grid-like network plus a super
101 * node. In additional to the arcs connecting the nodes in the grid,
102 * there is an arc from each supply node to the super node and from the
103 * super node to each demand node to guarantee feasiblity. These arcs
104 * have very high costs and very big capacities.
106 * The idea of this network generator is as follows: First, a grid of
107 * n1 * n2 is generated. For example, 5 * 3. The nodes are numbered as
108 * 1 to 15, and the supernode is numbered as n1*n2+1. Then arcs between
109 * adjacent nodes are generated. For these arcs, the user is allowed to
110 * specify either to generate two-way arcs or one-way arcs. If two-way
111 * arcs are to be generated, two arcs, one in each direction, will be
112 * generated between each adjacent node pairs. Otherwise, only one arc
113 * will be generated. If this is the case, the arcs will be generated
114 * in alterntive directions as shown below.
116 * 1 ---> 2 ---> 3 ---> 4 ---> 5
120 * 6 <--- 7 <--- 8 <--- 9 <--- 10
124 * 11 --->12 --->13 --->14 ---> 15
126 * Then the arcs between the super node and the source/sink nodes are
127 * added as mentioned before. If the number of arcs still doesn't reach
128 * the requirement, additional arcs will be added by uniformly picking
129 * random node pairs. There is no checking to prevent multiple arcs
130 * between any pair of nodes. However, there will be no self-arcs (arcs
131 * that poins back to its tail node) in the network.
133 * The source and sink nodes are selected uniformly in the network, and
134 * the imbalances of each source/sink node are also assigned by uniform
138 { /* structure for statistical distributions */
140 /* the distribution: */
141 #define UNIFORM 1 /* uniform distribution */
142 #define EXPONENTIAL 2 /* exponential distribution */
144 /* the parameters of the distribution */
149 /* the FROM node of that arc */
151 /* the TO node of that arc */
153 /* original cost of that arc */
155 /* capacity of the arc */
162 /* Supply of that node */
166 { /* common storage area */
168 int v_rhs, a_cap, a_cost;
170 /* random number seed */
172 /* the original seed from input */
174 /* 0: generate arcs in both direction for the basic grid, except
175 for the arcs to/from the super node. 1: o/w */
177 /* total number of nodes in the network, numbered 1 to n_node,
178 including the super node, which is the last one */
180 /* total number of arcs in the network, counting EVERY arc. */
182 /* number of arcs in the basic grid, including the arcs to/from
184 int n_source, n_sink;
185 /* number of source and sink nodes */
187 /* average degree, arcs to and from the super node are counted */
189 /* total supply in the network */
191 /* the two edges of the network grid. n1 >= n2 */
192 struct imbalance *source_list, *sink_list;
193 /* head of the array of source/sink nodes */
194 struct stat_para arc_costs;
195 /* the distribution of arc costs */
196 struct stat_para capacities;
197 /* distribution of the capacities of the arcs */
198 struct arcs *arc_list;
199 /* head of the arc list array. Arcs in this array are in the
200 order of grid_arcs, arcs to/from super node, and other arcs */
204 #define v_rhs (csa->v_rhs)
205 #define a_cap (csa->a_cap)
206 #define a_cost (csa->a_cost)
207 #define seed (csa->seed)
208 #define seed_original (csa->seed_original)
209 #define two_way (csa->two_way)
210 #define n_node (csa->n_node)
211 #define n_arc (csa->n_arc)
212 #define n_grid_arc (csa->n_grid_arc)
213 #define n_source (csa->n_source)
214 #define n_sink (csa->n_sink)
215 #define avg_degree (csa->avg_degree)
216 #define t_supply (csa->t_supply)
219 #define source_list (csa->source_list)
220 #define sink_list (csa->sink_list)
221 #define arc_costs (csa->arc_costs)
222 #define capacities (csa->capacities)
223 #define arc_list (csa->arc_list)
225 static void assign_capacities(struct csa *csa);
226 static void assign_costs(struct csa *csa);
227 static void assign_imbalance(struct csa *csa);
228 static int exponential(struct csa *csa, double lambda[1]);
229 static struct arcs *gen_additional_arcs(struct csa *csa, struct arcs
231 static struct arcs *gen_basic_grid(struct csa *csa, struct arcs
233 static void gen_more_arcs(struct csa *csa, struct arcs *arc_ptr);
234 static void generate(struct csa *csa);
235 static void output(struct csa *csa);
236 static double randy(struct csa *csa);
237 static void select_source_sinks(struct csa *csa);
238 static int uniform(struct csa *csa, double a[2]);
240 int glp_gridgen(glp_graph *G_, int _v_rhs, int _a_cap, int _a_cost,
241 const int parm[1+14])
242 { struct csa _csa, *csa = &_csa;
249 { if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
250 xerror("glp_gridgen: v_rhs = %d; invalid offset\n", v_rhs);
251 if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
252 xerror("glp_gridgen: a_cap = %d; invalid offset\n", a_cap);
253 if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
254 xerror("glp_gridgen: a_cost = %d; invalid offset\n", a_cost)
257 /* Check the parameters for consistency. */
258 if (!(parm[1] == 0 || parm[1] == 1))
266 if (!(10 <= parm[3] && parm[3] <= 40000))
270 if (!(1 <= parm[4] && parm[4] <= 40000))
274 if (!(parm[5] >= 0 && parm[6] >= 0 && parm[5] + parm[6] <=
279 if (!(1 <= parm[7] && parm[7] <= parm[3]))
287 if (!(parm[9] == 1 || parm[9] == 2))
291 if (parm[9] == 1 && parm[10] > parm[11] ||
292 parm[9] == 2 && parm[10] < 1)
296 if (!(parm[12] == 1 || parm[12] == 2))
300 if (parm[12] == 1 && !(0 <= parm[13] && parm[13] <= parm[14]) ||
301 parm[12] == 2 && parm[13] < 1)
305 /* Initialize the graph object. */
307 { glp_erase_graph(G, G->v_size, G->a_size);
308 glp_set_graph_name(G, "GRIDGEN");
310 /* Copy the generator parameters. */
312 seed_original = seed = parm[2];
317 avg_degree = parm[7];
319 arc_costs.distribution = parm[9];
321 { arc_costs.parameter[0] = parm[10];
322 arc_costs.parameter[1] = parm[11];
325 { arc_costs.parameter[0] = (double)parm[10] / 100.0;
326 arc_costs.parameter[1] = 0.0;
328 capacities.distribution = parm[12];
330 { capacities.parameter[0] = parm[13];
331 capacities.parameter[1] = parm[14];
334 { capacities.parameter[0] = (double)parm[13] / 100.0;
335 capacities.parameter[1] = 0.0;
337 /* Calculate the edge lengths of the grid according to the
341 n2 = (int)((double)n_node / (double)n + 0.5);
345 n1 = (int)((double)n_node / (double)n + 0.5);
347 /* Recalculate the total number of nodes and plus 1 for the super
349 n_node = n1 * n2 + 1;
350 n_arc = n_node * avg_degree;
351 n_grid_arc = (two_way + 1) * ((n1 - 1) * n2 + (n2 - 1) * n1) +
353 if (n_grid_arc > n_arc) n_arc = n_grid_arc;
354 arc_list = xcalloc(n_arc, sizeof(struct arcs));
355 source_list = xcalloc(n_source, sizeof(struct imbalance));
356 sink_list = xcalloc(n_sink, sizeof(struct imbalance));
357 /* Generate a random network. */
359 /* Output the network. */
361 /* Free all allocated memory. */
365 /* The instance has been successfully generated. */
372 static void assign_capacities(struct csa *csa)
373 { /* Assign a capacity to each arc. */
374 struct arcs *arc_ptr = arc_list;
375 int (*random)(struct csa *csa, double *);
377 /* Determine the random number generator to use. */
378 switch (arc_costs.distribution)
383 random = exponential;
388 /* Assign capacities to grid arcs. */
389 for (i = n_source + n_sink; i < n_grid_arc; i++, arc_ptr++)
390 arc_ptr->u = random(csa, capacities.parameter);
391 i = i - n_source - n_sink;
392 /* Assign capacities to arcs to/from supernode. */
393 for (; i < n_grid_arc; i++, arc_ptr++)
394 arc_ptr->u = t_supply;
395 /* Assign capacities to all other arcs. */
396 for (; i < n_arc; i++, arc_ptr++)
397 arc_ptr->u = random(csa, capacities.parameter);
401 static void assign_costs(struct csa *csa)
402 { /* Assign a cost to each arc. */
403 struct arcs *arc_ptr = arc_list;
404 int (*random)(struct csa *csa, double *);
406 /* A high cost assigned to arcs to/from the supernode. */
408 /* The maximum cost assigned to arcs in the base grid. */
410 /* Determine the random number generator to use. */
411 switch (arc_costs.distribution)
416 random = exponential;
421 /* Assign costs to arcs in the base grid. */
422 for (i = n_source + n_sink; i < n_grid_arc; i++, arc_ptr++)
423 { arc_ptr->cost = random(csa, arc_costs.parameter);
424 if (max_cost < arc_ptr->cost) max_cost = arc_ptr->cost;
426 i = i - n_source - n_sink;
427 /* Assign costs to arcs to/from the super node. */
428 high_cost = max_cost * 2;
429 for (; i < n_grid_arc; i++, arc_ptr++)
430 arc_ptr->cost = high_cost;
431 /* Assign costs to all other arcs. */
432 for (; i < n_arc; i++, arc_ptr++)
433 arc_ptr->cost = random(csa, arc_costs.parameter);
437 static void assign_imbalance(struct csa *csa)
438 { /* Assign an imbalance to each node. */
441 struct imbalance *ptr;
442 /* assign the supply nodes */
443 avg = 2.0 * t_supply / n_source;
445 { for (i = 1, total = t_supply, ptr = source_list + 1;
446 i < n_source; i++, ptr++)
447 { ptr->supply = (int)(randy(csa) * avg + 0.5);
448 total -= ptr->supply;
450 source_list->supply = total;
452 /* redo all if the assignment "overshooted" */
454 /* assign the demand nodes */
455 avg = -2.0 * t_supply / n_sink;
457 { for (i = 1, total = t_supply, ptr = sink_list + 1;
458 i < n_sink; i++, ptr++)
459 { ptr->supply = (int)(randy(csa) * avg - 0.5);
460 total += ptr->supply;
462 sink_list->supply = - total;
468 static int exponential(struct csa *csa, double lambda[1])
469 { /* Returns an "exponentially distributed" integer with parameter
471 return ((int)(- lambda[0] * log((double)randy(csa)) + 0.5));
474 static struct arcs *gen_additional_arcs(struct csa *csa, struct arcs
476 { /* Generate an arc from each source to the supernode and from
477 supernode to each sink. */
479 for (i = 0; i < n_source; i++, arc_ptr++)
480 { arc_ptr->from = source_list[i].node;
481 arc_ptr->to = n_node;
483 for (i = 0; i < n_sink; i++, arc_ptr++)
484 { arc_ptr->to = sink_list[i].node;
485 arc_ptr->from = n_node;
490 static struct arcs *gen_basic_grid(struct csa *csa, struct arcs
492 { /* Generate the basic grid. */
493 int direction = 1, i, j, k;
495 { /* Generate an arc in each direction. */
496 for (i = 1; i < n_node; i += n1)
497 { for (j = i, k = j + n1 - 1; j < k; j++)
501 arc_ptr->from = j + 1;
506 for (i = 1; i <= n1; i++)
507 { for (j = i + n1; j < n_node; j += n1)
509 arc_ptr->to = j - n1;
511 arc_ptr->from = j - n1;
518 { /* Generate one arc in each direction. */
519 for (i = 1; i < n_node; i += n1)
520 { if (direction == 1)
524 for (k = j + n1 - 1; j < k; j++)
526 arc_ptr->to = j + direction;
529 direction = - direction;
531 for (i = 1; i <= n1; i++)
534 { for (; j < n_node; j += n1)
535 { arc_ptr->from = j - n1;
541 { for (; j < n_node; j += n1)
542 { arc_ptr->from = j - n1;
547 direction = - direction;
553 static void gen_more_arcs(struct csa *csa, struct arcs *arc_ptr)
554 { /* Generate random arcs to meet the specified density. */
558 ab[1] = n_node - 0.99; /* upper limit is n_node-1 because the
559 supernode cannot be selected */
560 for (i = n_grid_arc; i < n_arc; i++, arc_ptr++)
561 { arc_ptr->from = uniform(csa, ab);
562 arc_ptr->to = uniform(csa, ab);
563 if (arc_ptr->from == arc_ptr->to)
571 static void generate(struct csa *csa)
572 { /* Generate a random network. */
573 struct arcs *arc_ptr = arc_list;
574 arc_ptr = gen_basic_grid(csa, arc_ptr);
575 select_source_sinks(csa);
576 arc_ptr = gen_additional_arcs(csa, arc_ptr);
577 gen_more_arcs(csa, arc_ptr);
579 assign_capacities(csa);
580 assign_imbalance(csa);
584 static void output(struct csa *csa)
585 { /* Output the network in DIMACS format. */
586 struct arcs *arc_ptr;
587 struct imbalance *imb_ptr;
589 if (G != NULL) goto skip;
590 /* Output "c", "p" records. */
591 xprintf("c generated by GRIDGEN\n");
592 xprintf("c seed %d\n", seed_original);
593 xprintf("c nodes %d\n", n_node);
594 xprintf("c grid size %d X %d\n", n1, n2);
595 xprintf("c sources %d sinks %d\n", n_source, n_sink);
596 xprintf("c avg. degree %d\n", avg_degree);
597 xprintf("c supply %d\n", t_supply);
598 switch (arc_costs.distribution)
600 xprintf("c arc costs: UNIFORM distr. min %d max %d\n",
601 (int)arc_costs.parameter[0],
602 (int)arc_costs.parameter[1]);
605 xprintf("c arc costs: EXPONENTIAL distr. lambda %d\n",
606 (int)arc_costs.parameter[0]);
611 switch (capacities.distribution)
613 xprintf("c arc caps : UNIFORM distr. min %d max %d\n",
614 (int)capacities.parameter[0],
615 (int)capacities.parameter[1]);
618 xprintf("c arc caps : EXPONENTIAL distr. %d lambda %d\n",
619 (int)capacities.parameter[0]);
625 xprintf("p min %d %d\n", n_node, n_arc);
627 { glp_add_vertices(G, n_node);
630 for (i = 1; i <= n_node; i++)
631 { glp_vertex *v = G->v[i];
632 memcpy((char *)v->data + v_rhs, &zero, sizeof(double));
636 /* Output "n node supply". */
637 for (i = 0, imb_ptr = source_list; i < n_source; i++, imb_ptr++)
639 xprintf("n %d %d\n", imb_ptr->node, imb_ptr->supply);
642 { double temp = (double)imb_ptr->supply;
643 glp_vertex *v = G->v[imb_ptr->node];
644 memcpy((char *)v->data + v_rhs, &temp, sizeof(double));
648 for (i = 0, imb_ptr = sink_list; i < n_sink; i++, imb_ptr++)
650 xprintf("n %d %d\n", imb_ptr->node, imb_ptr->supply);
653 { double temp = (double)imb_ptr->supply;
654 glp_vertex *v = G->v[imb_ptr->node];
655 memcpy((char *)v->data + v_rhs, &temp, sizeof(double));
659 /* Output "a from to lowcap=0 hicap cost". */
660 for (i = 0, arc_ptr = arc_list; i < n_arc; i++, arc_ptr++)
662 xprintf("a %d %d 0 %d %d\n", arc_ptr->from, arc_ptr->to,
663 arc_ptr->u, arc_ptr->cost);
665 { glp_arc *a = glp_add_arc(G, arc_ptr->from, arc_ptr->to);
667 { double temp = (double)arc_ptr->u;
668 memcpy((char *)a->data + a_cap, &temp, sizeof(double));
671 { double temp = (double)arc_ptr->cost;
672 memcpy((char *)a->data + a_cost, &temp, sizeof(double));
679 static double randy(struct csa *csa)
680 { /* Returns a random number between 0.0 and 1.0.
681 See Ward Cheney & David Kincaid, "Numerical Mathematics and
682 Computing," 2Ed, pp. 335. */
683 seed = 16807 * seed % 2147483647;
684 if (seed < 0) seed = - seed;
685 return seed * 4.6566128752459e-10;
688 static void select_source_sinks(struct csa *csa)
689 { /* Randomly select the source nodes and sink nodes. */
691 int *temp_list; /* a temporary list of nodes */
692 struct imbalance *ptr;
693 double ab[2]; /* parameter for random number generator */
695 ab[1] = n_node - 0.99; /* upper limit is n_node-1 because the
696 supernode cannot be selected */
697 temp_list = xcalloc(n_node, sizeof(int));
698 for (i = 0, int_ptr = temp_list; i < n_node; i++, int_ptr++)
700 /* Select the source nodes. */
701 for (i = 0, ptr = source_list; i < n_source; i++, ptr++)
702 { ptr->node = uniform(csa, ab);
703 if (temp_list[ptr->node] == 1) /* check for duplicates */
708 temp_list[ptr->node] = 1;
710 /* Select the sink nodes. */
711 for (i = 0, ptr = sink_list; i < n_sink; i++, ptr++)
712 { ptr->node = uniform(csa, ab);
713 if (temp_list[ptr->node] == 1)
718 temp_list[ptr->node] = 1;
724 int uniform(struct csa *csa, double a[2])
725 { /* Generates an integer uniformly selected from [a[0],a[1]]. */
726 return (int)((a[1] - a[0]) * randy(csa) + a[0] + 0.5);
729 /**********************************************************************/
735 scanf("%d", &parm[1]);
736 scanf("%d", &parm[2]);
737 scanf("%d", &parm[3]);
738 scanf("%d", &parm[4]);
739 scanf("%d", &parm[5]);
740 scanf("%d", &parm[6]);
741 scanf("%d", &parm[7]);
742 scanf("%d", &parm[8]);
743 scanf("%d", &parm[9]);
745 { scanf("%d", &parm[10]);
746 scanf("%d", &parm[11]);
749 { scanf("%le", &temp);
750 parm[10] = (int)(100.0 * temp + .5);
753 scanf("%d", &parm[12]);
755 { scanf("%d", &parm[13]);
756 scanf("%d", &parm[14]);
759 { scanf("%le", &temp);
760 parm[13] = (int)(100.0 * temp + .5);
763 glp_gridgen(NULL, 0, 0, 0, parm);