alpar@1: /* glpnet04.c (grid-like network problem generator) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * This code is a modified version of the program GRIDGEN, a grid-like alpar@1: * network problem generator developed by Yusin Lee and Jim Orlin. alpar@1: * The original code is publically available on the DIMACS ftp site at: alpar@1: * . alpar@1: * alpar@1: * All changes concern only the program interface, so this modified alpar@1: * version produces exactly the same instances as the original version. alpar@1: * alpar@1: * Changes were made by Andrew Makhorin . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpapi.h" alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_gridgen - grid-like network problem generator alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_gridgen(glp_graph *G, int v_rhs, int a_cap, int a_cost, alpar@1: * const int parm[1+14]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_gridgen is a grid-like network problem generator alpar@1: * developed by Yusin Lee and Jim Orlin. alpar@1: * alpar@1: * The parameter G specifies the graph object, to which the generated alpar@1: * problem data have to be stored. Note that on entry the graph object alpar@1: * is erased with the routine glp_erase_graph. alpar@1: * alpar@1: * The parameter v_rhs specifies an offset of the field of type double alpar@1: * in the vertex data block, to which the routine stores the supply or alpar@1: * demand value. If v_rhs < 0, the value is not stored. alpar@1: * alpar@1: * The parameter a_cap specifies an offset of the field of type double alpar@1: * in the arc data block, to which the routine stores the arc capacity. alpar@1: * If a_cap < 0, the capacity is not stored. alpar@1: * alpar@1: * The parameter a_cost specifies an offset of the field of type double alpar@1: * in the arc data block, to which the routine stores the per-unit cost alpar@1: * if the arc flow. If a_cost < 0, the cost is not stored. alpar@1: * alpar@1: * The array parm contains description of the network to be generated: alpar@1: * alpar@1: * parm[0] not used alpar@1: * parm[1] two-ways arcs indicator: alpar@1: * 1 - if links in both direction should be generated alpar@1: * 0 - otherwise alpar@1: * parm[2] random number seed (a positive integer) alpar@1: * parm[3] number of nodes (the number of nodes generated might be alpar@1: * slightly different to make the network a grid) alpar@1: * parm[4] grid width alpar@1: * parm[5] number of sources alpar@1: * parm[6] number of sinks alpar@1: * parm[7] average degree alpar@1: * parm[8] total flow alpar@1: * parm[9] distribution of arc costs: alpar@1: * 1 - uniform alpar@1: * 2 - exponential alpar@1: * parm[10] lower bound for arc cost (uniform) alpar@1: * 100 * lambda (exponential) alpar@1: * parm[11] upper bound for arc cost (uniform) alpar@1: * not used (exponential) alpar@1: * parm[12] distribution of arc capacities: alpar@1: * 1 - uniform alpar@1: * 2 - exponential alpar@1: * parm[13] lower bound for arc capacity (uniform) alpar@1: * 100 * lambda (exponential) alpar@1: * parm[14] upper bound for arc capacity (uniform) alpar@1: * not used (exponential) alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the instance was successfully generated, the routine glp_gridgen alpar@1: * returns zero; otherwise, if specified parameters are inconsistent, alpar@1: * the routine returns a non-zero error code. alpar@1: * alpar@1: * COMMENTS alpar@1: * alpar@1: * This network generator generates a grid-like network plus a super alpar@1: * node. In additional to the arcs connecting the nodes in the grid, alpar@1: * there is an arc from each supply node to the super node and from the alpar@1: * super node to each demand node to guarantee feasiblity. These arcs alpar@1: * have very high costs and very big capacities. alpar@1: * alpar@1: * The idea of this network generator is as follows: First, a grid of alpar@1: * n1 * n2 is generated. For example, 5 * 3. The nodes are numbered as alpar@1: * 1 to 15, and the supernode is numbered as n1*n2+1. Then arcs between alpar@1: * adjacent nodes are generated. For these arcs, the user is allowed to alpar@1: * specify either to generate two-way arcs or one-way arcs. If two-way alpar@1: * arcs are to be generated, two arcs, one in each direction, will be alpar@1: * generated between each adjacent node pairs. Otherwise, only one arc alpar@1: * will be generated. If this is the case, the arcs will be generated alpar@1: * in alterntive directions as shown below. alpar@1: * alpar@1: * 1 ---> 2 ---> 3 ---> 4 ---> 5 alpar@1: * | ^ | ^ | alpar@1: * | | | | | alpar@1: * V | V | V alpar@1: * 6 <--- 7 <--- 8 <--- 9 <--- 10 alpar@1: * | ^ | ^ | alpar@1: * | | | | | alpar@1: * V | V | V alpar@1: * 11 --->12 --->13 --->14 ---> 15 alpar@1: * alpar@1: * Then the arcs between the super node and the source/sink nodes are alpar@1: * added as mentioned before. If the number of arcs still doesn't reach alpar@1: * the requirement, additional arcs will be added by uniformly picking alpar@1: * random node pairs. There is no checking to prevent multiple arcs alpar@1: * between any pair of nodes. However, there will be no self-arcs (arcs alpar@1: * that poins back to its tail node) in the network. alpar@1: * alpar@1: * The source and sink nodes are selected uniformly in the network, and alpar@1: * the imbalances of each source/sink node are also assigned by uniform alpar@1: * distribution. */ alpar@1: alpar@1: struct stat_para alpar@1: { /* structure for statistical distributions */ alpar@1: int distribution; alpar@1: /* the distribution: */ alpar@1: #define UNIFORM 1 /* uniform distribution */ alpar@1: #define EXPONENTIAL 2 /* exponential distribution */ alpar@1: double parameter[5]; alpar@1: /* the parameters of the distribution */ alpar@1: }; alpar@1: alpar@1: struct arcs alpar@1: { int from; alpar@1: /* the FROM node of that arc */ alpar@1: int to; alpar@1: /* the TO node of that arc */ alpar@1: int cost; alpar@1: /* original cost of that arc */ alpar@1: int u; alpar@1: /* capacity of the arc */ alpar@1: }; alpar@1: alpar@1: struct imbalance alpar@1: { int node; alpar@1: /* Node ID */ alpar@1: int supply; alpar@1: /* Supply of that node */ alpar@1: }; alpar@1: alpar@1: struct csa alpar@1: { /* common storage area */ alpar@1: glp_graph *G; alpar@1: int v_rhs, a_cap, a_cost; alpar@1: int seed; alpar@1: /* random number seed */ alpar@1: int seed_original; alpar@1: /* the original seed from input */ alpar@1: int two_way; alpar@1: /* 0: generate arcs in both direction for the basic grid, except alpar@1: for the arcs to/from the super node. 1: o/w */ alpar@1: int n_node; alpar@1: /* total number of nodes in the network, numbered 1 to n_node, alpar@1: including the super node, which is the last one */ alpar@1: int n_arc; alpar@1: /* total number of arcs in the network, counting EVERY arc. */ alpar@1: int n_grid_arc; alpar@1: /* number of arcs in the basic grid, including the arcs to/from alpar@1: the super node */ alpar@1: int n_source, n_sink; alpar@1: /* number of source and sink nodes */ alpar@1: int avg_degree; alpar@1: /* average degree, arcs to and from the super node are counted */ alpar@1: int t_supply; alpar@1: /* total supply in the network */ alpar@1: int n1, n2; alpar@1: /* the two edges of the network grid. n1 >= n2 */ alpar@1: struct imbalance *source_list, *sink_list; alpar@1: /* head of the array of source/sink nodes */ alpar@1: struct stat_para arc_costs; alpar@1: /* the distribution of arc costs */ alpar@1: struct stat_para capacities; alpar@1: /* distribution of the capacities of the arcs */ alpar@1: struct arcs *arc_list; alpar@1: /* head of the arc list array. Arcs in this array are in the alpar@1: order of grid_arcs, arcs to/from super node, and other arcs */ alpar@1: }; alpar@1: alpar@1: #define G (csa->G) alpar@1: #define v_rhs (csa->v_rhs) alpar@1: #define a_cap (csa->a_cap) alpar@1: #define a_cost (csa->a_cost) alpar@1: #define seed (csa->seed) alpar@1: #define seed_original (csa->seed_original) alpar@1: #define two_way (csa->two_way) alpar@1: #define n_node (csa->n_node) alpar@1: #define n_arc (csa->n_arc) alpar@1: #define n_grid_arc (csa->n_grid_arc) alpar@1: #define n_source (csa->n_source) alpar@1: #define n_sink (csa->n_sink) alpar@1: #define avg_degree (csa->avg_degree) alpar@1: #define t_supply (csa->t_supply) alpar@1: #define n1 (csa->n1) alpar@1: #define n2 (csa->n2) alpar@1: #define source_list (csa->source_list) alpar@1: #define sink_list (csa->sink_list) alpar@1: #define arc_costs (csa->arc_costs) alpar@1: #define capacities (csa->capacities) alpar@1: #define arc_list (csa->arc_list) alpar@1: alpar@1: static void assign_capacities(struct csa *csa); alpar@1: static void assign_costs(struct csa *csa); alpar@1: static void assign_imbalance(struct csa *csa); alpar@1: static int exponential(struct csa *csa, double lambda[1]); alpar@1: static struct arcs *gen_additional_arcs(struct csa *csa, struct arcs alpar@1: *arc_ptr); alpar@1: static struct arcs *gen_basic_grid(struct csa *csa, struct arcs alpar@1: *arc_ptr); alpar@1: static void gen_more_arcs(struct csa *csa, struct arcs *arc_ptr); alpar@1: static void generate(struct csa *csa); alpar@1: static void output(struct csa *csa); alpar@1: static double randy(struct csa *csa); alpar@1: static void select_source_sinks(struct csa *csa); alpar@1: static int uniform(struct csa *csa, double a[2]); alpar@1: alpar@1: int glp_gridgen(glp_graph *G_, int _v_rhs, int _a_cap, int _a_cost, alpar@1: const int parm[1+14]) alpar@1: { struct csa _csa, *csa = &_csa; alpar@1: int n, ret; alpar@1: G = G_; alpar@1: v_rhs = _v_rhs; alpar@1: a_cap = _a_cap; alpar@1: a_cost = _a_cost; alpar@1: if (G != NULL) alpar@1: { if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double)) alpar@1: xerror("glp_gridgen: v_rhs = %d; invalid offset\n", v_rhs); alpar@1: if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_gridgen: a_cap = %d; invalid offset\n", a_cap); alpar@1: if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double)) alpar@1: xerror("glp_gridgen: a_cost = %d; invalid offset\n", a_cost) alpar@1: ; alpar@1: } alpar@1: /* Check the parameters for consistency. */ alpar@1: if (!(parm[1] == 0 || parm[1] == 1)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: if (parm[2] < 1) alpar@1: { ret = 2; alpar@1: goto done; alpar@1: } alpar@1: if (!(10 <= parm[3] && parm[3] <= 40000)) alpar@1: { ret = 3; alpar@1: goto done; alpar@1: } alpar@1: if (!(1 <= parm[4] && parm[4] <= 40000)) alpar@1: { ret = 4; alpar@1: goto done; alpar@1: } alpar@1: if (!(parm[5] >= 0 && parm[6] >= 0 && parm[5] + parm[6] <= alpar@1: parm[3])) alpar@1: { ret = 5; alpar@1: goto done; alpar@1: } alpar@1: if (!(1 <= parm[7] && parm[7] <= parm[3])) alpar@1: { ret = 6; alpar@1: goto done; alpar@1: } alpar@1: if (parm[8] < 0) alpar@1: { ret = 7; alpar@1: goto done; alpar@1: } alpar@1: if (!(parm[9] == 1 || parm[9] == 2)) alpar@1: { ret = 8; alpar@1: goto done; alpar@1: } alpar@1: if (parm[9] == 1 && parm[10] > parm[11] || alpar@1: parm[9] == 2 && parm[10] < 1) alpar@1: { ret = 9; alpar@1: goto done; alpar@1: } alpar@1: if (!(parm[12] == 1 || parm[12] == 2)) alpar@1: { ret = 10; alpar@1: goto done; alpar@1: } alpar@1: if (parm[12] == 1 && !(0 <= parm[13] && parm[13] <= parm[14]) || alpar@1: parm[12] == 2 && parm[13] < 1) alpar@1: { ret = 11; alpar@1: goto done; alpar@1: } alpar@1: /* Initialize the graph object. */ alpar@1: if (G != NULL) alpar@1: { glp_erase_graph(G, G->v_size, G->a_size); alpar@1: glp_set_graph_name(G, "GRIDGEN"); alpar@1: } alpar@1: /* Copy the generator parameters. */ alpar@1: two_way = parm[1]; alpar@1: seed_original = seed = parm[2]; alpar@1: n_node = parm[3]; alpar@1: n = parm[4]; alpar@1: n_source = parm[5]; alpar@1: n_sink = parm[6]; alpar@1: avg_degree = parm[7]; alpar@1: t_supply = parm[8]; alpar@1: arc_costs.distribution = parm[9]; alpar@1: if (parm[9] == 1) alpar@1: { arc_costs.parameter[0] = parm[10]; alpar@1: arc_costs.parameter[1] = parm[11]; alpar@1: } alpar@1: else alpar@1: { arc_costs.parameter[0] = (double)parm[10] / 100.0; alpar@1: arc_costs.parameter[1] = 0.0; alpar@1: } alpar@1: capacities.distribution = parm[12]; alpar@1: if (parm[12] == 1) alpar@1: { capacities.parameter[0] = parm[13]; alpar@1: capacities.parameter[1] = parm[14]; alpar@1: } alpar@1: else alpar@1: { capacities.parameter[0] = (double)parm[13] / 100.0; alpar@1: capacities.parameter[1] = 0.0; alpar@1: } alpar@1: /* Calculate the edge lengths of the grid according to the alpar@1: input. */ alpar@1: if (n * n >= n_node) alpar@1: { n1 = n; alpar@1: n2 = (int)((double)n_node / (double)n + 0.5); alpar@1: } alpar@1: else alpar@1: { n2 = n; alpar@1: n1 = (int)((double)n_node / (double)n + 0.5); alpar@1: } alpar@1: /* Recalculate the total number of nodes and plus 1 for the super alpar@1: node. */ alpar@1: n_node = n1 * n2 + 1; alpar@1: n_arc = n_node * avg_degree; alpar@1: n_grid_arc = (two_way + 1) * ((n1 - 1) * n2 + (n2 - 1) * n1) + alpar@1: n_source + n_sink; alpar@1: if (n_grid_arc > n_arc) n_arc = n_grid_arc; alpar@1: arc_list = xcalloc(n_arc, sizeof(struct arcs)); alpar@1: source_list = xcalloc(n_source, sizeof(struct imbalance)); alpar@1: sink_list = xcalloc(n_sink, sizeof(struct imbalance)); alpar@1: /* Generate a random network. */ alpar@1: generate(csa); alpar@1: /* Output the network. */ alpar@1: output(csa); alpar@1: /* Free all allocated memory. */ alpar@1: xfree(arc_list); alpar@1: xfree(source_list); alpar@1: xfree(sink_list); alpar@1: /* The instance has been successfully generated. */ alpar@1: ret = 0; alpar@1: done: return ret; alpar@1: } alpar@1: alpar@1: #undef random alpar@1: alpar@1: static void assign_capacities(struct csa *csa) alpar@1: { /* Assign a capacity to each arc. */ alpar@1: struct arcs *arc_ptr = arc_list; alpar@1: int (*random)(struct csa *csa, double *); alpar@1: int i; alpar@1: /* Determine the random number generator to use. */ alpar@1: switch (arc_costs.distribution) alpar@1: { case UNIFORM: alpar@1: random = uniform; alpar@1: break; alpar@1: case EXPONENTIAL: alpar@1: random = exponential; alpar@1: break; alpar@1: default: alpar@1: xassert(csa != csa); alpar@1: } alpar@1: /* Assign capacities to grid arcs. */ alpar@1: for (i = n_source + n_sink; i < n_grid_arc; i++, arc_ptr++) alpar@1: arc_ptr->u = random(csa, capacities.parameter); alpar@1: i = i - n_source - n_sink; alpar@1: /* Assign capacities to arcs to/from supernode. */ alpar@1: for (; i < n_grid_arc; i++, arc_ptr++) alpar@1: arc_ptr->u = t_supply; alpar@1: /* Assign capacities to all other arcs. */ alpar@1: for (; i < n_arc; i++, arc_ptr++) alpar@1: arc_ptr->u = random(csa, capacities.parameter); alpar@1: return; alpar@1: } alpar@1: alpar@1: static void assign_costs(struct csa *csa) alpar@1: { /* Assign a cost to each arc. */ alpar@1: struct arcs *arc_ptr = arc_list; alpar@1: int (*random)(struct csa *csa, double *); alpar@1: int i; alpar@1: /* A high cost assigned to arcs to/from the supernode. */ alpar@1: int high_cost; alpar@1: /* The maximum cost assigned to arcs in the base grid. */ alpar@1: int max_cost = 0; alpar@1: /* Determine the random number generator to use. */ alpar@1: switch (arc_costs.distribution) alpar@1: { case UNIFORM: alpar@1: random = uniform; alpar@1: break; alpar@1: case EXPONENTIAL: alpar@1: random = exponential; alpar@1: break; alpar@1: default: alpar@1: xassert(csa != csa); alpar@1: } alpar@1: /* Assign costs to arcs in the base grid. */ alpar@1: for (i = n_source + n_sink; i < n_grid_arc; i++, arc_ptr++) alpar@1: { arc_ptr->cost = random(csa, arc_costs.parameter); alpar@1: if (max_cost < arc_ptr->cost) max_cost = arc_ptr->cost; alpar@1: } alpar@1: i = i - n_source - n_sink; alpar@1: /* Assign costs to arcs to/from the super node. */ alpar@1: high_cost = max_cost * 2; alpar@1: for (; i < n_grid_arc; i++, arc_ptr++) alpar@1: arc_ptr->cost = high_cost; alpar@1: /* Assign costs to all other arcs. */ alpar@1: for (; i < n_arc; i++, arc_ptr++) alpar@1: arc_ptr->cost = random(csa, arc_costs.parameter); alpar@1: return; alpar@1: } alpar@1: alpar@1: static void assign_imbalance(struct csa *csa) alpar@1: { /* Assign an imbalance to each node. */ alpar@1: int total, i; alpar@1: double avg; alpar@1: struct imbalance *ptr; alpar@1: /* assign the supply nodes */ alpar@1: avg = 2.0 * t_supply / n_source; alpar@1: do alpar@1: { for (i = 1, total = t_supply, ptr = source_list + 1; alpar@1: i < n_source; i++, ptr++) alpar@1: { ptr->supply = (int)(randy(csa) * avg + 0.5); alpar@1: total -= ptr->supply; alpar@1: } alpar@1: source_list->supply = total; alpar@1: } alpar@1: /* redo all if the assignment "overshooted" */ alpar@1: while (total <= 0); alpar@1: /* assign the demand nodes */ alpar@1: avg = -2.0 * t_supply / n_sink; alpar@1: do alpar@1: { for (i = 1, total = t_supply, ptr = sink_list + 1; alpar@1: i < n_sink; i++, ptr++) alpar@1: { ptr->supply = (int)(randy(csa) * avg - 0.5); alpar@1: total += ptr->supply; alpar@1: } alpar@1: sink_list->supply = - total; alpar@1: } alpar@1: while (total <= 0); alpar@1: return; alpar@1: } alpar@1: alpar@1: static int exponential(struct csa *csa, double lambda[1]) alpar@1: { /* Returns an "exponentially distributed" integer with parameter alpar@1: lambda. */ alpar@1: return ((int)(- lambda[0] * log((double)randy(csa)) + 0.5)); alpar@1: } alpar@1: alpar@1: static struct arcs *gen_additional_arcs(struct csa *csa, struct arcs alpar@1: *arc_ptr) alpar@1: { /* Generate an arc from each source to the supernode and from alpar@1: supernode to each sink. */ alpar@1: int i; alpar@1: for (i = 0; i < n_source; i++, arc_ptr++) alpar@1: { arc_ptr->from = source_list[i].node; alpar@1: arc_ptr->to = n_node; alpar@1: } alpar@1: for (i = 0; i < n_sink; i++, arc_ptr++) alpar@1: { arc_ptr->to = sink_list[i].node; alpar@1: arc_ptr->from = n_node; alpar@1: } alpar@1: return arc_ptr; alpar@1: } alpar@1: alpar@1: static struct arcs *gen_basic_grid(struct csa *csa, struct arcs alpar@1: *arc_ptr) alpar@1: { /* Generate the basic grid. */ alpar@1: int direction = 1, i, j, k; alpar@1: if (two_way) alpar@1: { /* Generate an arc in each direction. */ alpar@1: for (i = 1; i < n_node; i += n1) alpar@1: { for (j = i, k = j + n1 - 1; j < k; j++) alpar@1: { arc_ptr->from = j; alpar@1: arc_ptr->to = j + 1; alpar@1: arc_ptr++; alpar@1: arc_ptr->from = j + 1; alpar@1: arc_ptr->to = j; alpar@1: arc_ptr++; alpar@1: } alpar@1: } alpar@1: for (i = 1; i <= n1; i++) alpar@1: { for (j = i + n1; j < n_node; j += n1) alpar@1: { arc_ptr->from = j; alpar@1: arc_ptr->to = j - n1; alpar@1: arc_ptr++; alpar@1: arc_ptr->from = j - n1; alpar@1: arc_ptr->to = j; alpar@1: arc_ptr++; alpar@1: } alpar@1: } alpar@1: } alpar@1: else alpar@1: { /* Generate one arc in each direction. */ alpar@1: for (i = 1; i < n_node; i += n1) alpar@1: { if (direction == 1) alpar@1: j = i; alpar@1: else alpar@1: j = i + 1; alpar@1: for (k = j + n1 - 1; j < k; j++) alpar@1: { arc_ptr->from = j; alpar@1: arc_ptr->to = j + direction; alpar@1: arc_ptr++; alpar@1: } alpar@1: direction = - direction; alpar@1: } alpar@1: for (i = 1; i <= n1; i++) alpar@1: { j = i + n1; alpar@1: if (direction == 1) alpar@1: { for (; j < n_node; j += n1) alpar@1: { arc_ptr->from = j - n1; alpar@1: arc_ptr->to = j; alpar@1: arc_ptr++; alpar@1: } alpar@1: } alpar@1: else alpar@1: { for (; j < n_node; j += n1) alpar@1: { arc_ptr->from = j - n1; alpar@1: arc_ptr->to = j; alpar@1: arc_ptr++; alpar@1: } alpar@1: } alpar@1: direction = - direction; alpar@1: } alpar@1: } alpar@1: return arc_ptr; alpar@1: } alpar@1: alpar@1: static void gen_more_arcs(struct csa *csa, struct arcs *arc_ptr) alpar@1: { /* Generate random arcs to meet the specified density. */ alpar@1: int i; alpar@1: double ab[2]; alpar@1: ab[0] = 0.9; alpar@1: ab[1] = n_node - 0.99; /* upper limit is n_node-1 because the alpar@1: supernode cannot be selected */ alpar@1: for (i = n_grid_arc; i < n_arc; i++, arc_ptr++) alpar@1: { arc_ptr->from = uniform(csa, ab); alpar@1: arc_ptr->to = uniform(csa, ab); alpar@1: if (arc_ptr->from == arc_ptr->to) alpar@1: { arc_ptr--; alpar@1: i--; alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: static void generate(struct csa *csa) alpar@1: { /* Generate a random network. */ alpar@1: struct arcs *arc_ptr = arc_list; alpar@1: arc_ptr = gen_basic_grid(csa, arc_ptr); alpar@1: select_source_sinks(csa); alpar@1: arc_ptr = gen_additional_arcs(csa, arc_ptr); alpar@1: gen_more_arcs(csa, arc_ptr); alpar@1: assign_costs(csa); alpar@1: assign_capacities(csa); alpar@1: assign_imbalance(csa); alpar@1: return; alpar@1: } alpar@1: alpar@1: static void output(struct csa *csa) alpar@1: { /* Output the network in DIMACS format. */ alpar@1: struct arcs *arc_ptr; alpar@1: struct imbalance *imb_ptr; alpar@1: int i; alpar@1: if (G != NULL) goto skip; alpar@1: /* Output "c", "p" records. */ alpar@1: xprintf("c generated by GRIDGEN\n"); alpar@1: xprintf("c seed %d\n", seed_original); alpar@1: xprintf("c nodes %d\n", n_node); alpar@1: xprintf("c grid size %d X %d\n", n1, n2); alpar@1: xprintf("c sources %d sinks %d\n", n_source, n_sink); alpar@1: xprintf("c avg. degree %d\n", avg_degree); alpar@1: xprintf("c supply %d\n", t_supply); alpar@1: switch (arc_costs.distribution) alpar@1: { case UNIFORM: alpar@1: xprintf("c arc costs: UNIFORM distr. min %d max %d\n", alpar@1: (int)arc_costs.parameter[0], alpar@1: (int)arc_costs.parameter[1]); alpar@1: break; alpar@1: case EXPONENTIAL: alpar@1: xprintf("c arc costs: EXPONENTIAL distr. lambda %d\n", alpar@1: (int)arc_costs.parameter[0]); alpar@1: break; alpar@1: default: alpar@1: xassert(csa != csa); alpar@1: } alpar@1: switch (capacities.distribution) alpar@1: { case UNIFORM: alpar@1: xprintf("c arc caps : UNIFORM distr. min %d max %d\n", alpar@1: (int)capacities.parameter[0], alpar@1: (int)capacities.parameter[1]); alpar@1: break; alpar@1: case EXPONENTIAL: alpar@1: xprintf("c arc caps : EXPONENTIAL distr. %d lambda %d\n", alpar@1: (int)capacities.parameter[0]); alpar@1: break; alpar@1: default: alpar@1: xassert(csa != csa); alpar@1: } alpar@1: skip: if (G == NULL) alpar@1: xprintf("p min %d %d\n", n_node, n_arc); alpar@1: else alpar@1: { glp_add_vertices(G, n_node); alpar@1: if (v_rhs >= 0) alpar@1: { double zero = 0.0; alpar@1: for (i = 1; i <= n_node; i++) alpar@1: { glp_vertex *v = G->v[i]; alpar@1: memcpy((char *)v->data + v_rhs, &zero, sizeof(double)); alpar@1: } alpar@1: } alpar@1: } alpar@1: /* Output "n node supply". */ alpar@1: for (i = 0, imb_ptr = source_list; i < n_source; i++, imb_ptr++) alpar@1: { if (G == NULL) alpar@1: xprintf("n %d %d\n", imb_ptr->node, imb_ptr->supply); alpar@1: else alpar@1: { if (v_rhs >= 0) alpar@1: { double temp = (double)imb_ptr->supply; alpar@1: glp_vertex *v = G->v[imb_ptr->node]; alpar@1: memcpy((char *)v->data + v_rhs, &temp, sizeof(double)); alpar@1: } alpar@1: } alpar@1: } alpar@1: for (i = 0, imb_ptr = sink_list; i < n_sink; i++, imb_ptr++) alpar@1: { if (G == NULL) alpar@1: xprintf("n %d %d\n", imb_ptr->node, imb_ptr->supply); alpar@1: else alpar@1: { if (v_rhs >= 0) alpar@1: { double temp = (double)imb_ptr->supply; alpar@1: glp_vertex *v = G->v[imb_ptr->node]; alpar@1: memcpy((char *)v->data + v_rhs, &temp, sizeof(double)); alpar@1: } alpar@1: } alpar@1: } alpar@1: /* Output "a from to lowcap=0 hicap cost". */ alpar@1: for (i = 0, arc_ptr = arc_list; i < n_arc; i++, arc_ptr++) alpar@1: { if (G == NULL) alpar@1: xprintf("a %d %d 0 %d %d\n", arc_ptr->from, arc_ptr->to, alpar@1: arc_ptr->u, arc_ptr->cost); alpar@1: else alpar@1: { glp_arc *a = glp_add_arc(G, arc_ptr->from, arc_ptr->to); alpar@1: if (a_cap >= 0) alpar@1: { double temp = (double)arc_ptr->u; alpar@1: memcpy((char *)a->data + a_cap, &temp, sizeof(double)); alpar@1: } alpar@1: if (a_cost >= 0) alpar@1: { double temp = (double)arc_ptr->cost; alpar@1: memcpy((char *)a->data + a_cost, &temp, sizeof(double)); alpar@1: } alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: static double randy(struct csa *csa) alpar@1: { /* Returns a random number between 0.0 and 1.0. alpar@1: See Ward Cheney & David Kincaid, "Numerical Mathematics and alpar@1: Computing," 2Ed, pp. 335. */ alpar@1: seed = 16807 * seed % 2147483647; alpar@1: if (seed < 0) seed = - seed; alpar@1: return seed * 4.6566128752459e-10; alpar@1: } alpar@1: alpar@1: static void select_source_sinks(struct csa *csa) alpar@1: { /* Randomly select the source nodes and sink nodes. */ alpar@1: int i, *int_ptr; alpar@1: int *temp_list; /* a temporary list of nodes */ alpar@1: struct imbalance *ptr; alpar@1: double ab[2]; /* parameter for random number generator */ alpar@1: ab[0] = 0.9; alpar@1: ab[1] = n_node - 0.99; /* upper limit is n_node-1 because the alpar@1: supernode cannot be selected */ alpar@1: temp_list = xcalloc(n_node, sizeof(int)); alpar@1: for (i = 0, int_ptr = temp_list; i < n_node; i++, int_ptr++) alpar@1: *int_ptr = 0; alpar@1: /* Select the source nodes. */ alpar@1: for (i = 0, ptr = source_list; i < n_source; i++, ptr++) alpar@1: { ptr->node = uniform(csa, ab); alpar@1: if (temp_list[ptr->node] == 1) /* check for duplicates */ alpar@1: { ptr--; alpar@1: i--; alpar@1: } alpar@1: else alpar@1: temp_list[ptr->node] = 1; alpar@1: } alpar@1: /* Select the sink nodes. */ alpar@1: for (i = 0, ptr = sink_list; i < n_sink; i++, ptr++) alpar@1: { ptr->node = uniform(csa, ab); alpar@1: if (temp_list[ptr->node] == 1) alpar@1: { ptr--; alpar@1: i--; alpar@1: } alpar@1: else alpar@1: temp_list[ptr->node] = 1; alpar@1: } alpar@1: xfree(temp_list); alpar@1: return; alpar@1: } alpar@1: alpar@1: int uniform(struct csa *csa, double a[2]) alpar@1: { /* Generates an integer uniformly selected from [a[0],a[1]]. */ alpar@1: return (int)((a[1] - a[0]) * randy(csa) + a[0] + 0.5); alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: #if 0 alpar@1: int main(void) alpar@1: { int parm[1+14]; alpar@1: double temp; alpar@1: scanf("%d", &parm[1]); alpar@1: scanf("%d", &parm[2]); alpar@1: scanf("%d", &parm[3]); alpar@1: scanf("%d", &parm[4]); alpar@1: scanf("%d", &parm[5]); alpar@1: scanf("%d", &parm[6]); alpar@1: scanf("%d", &parm[7]); alpar@1: scanf("%d", &parm[8]); alpar@1: scanf("%d", &parm[9]); alpar@1: if (parm[9] == 1) alpar@1: { scanf("%d", &parm[10]); alpar@1: scanf("%d", &parm[11]); alpar@1: } alpar@1: else alpar@1: { scanf("%le", &temp); alpar@1: parm[10] = (int)(100.0 * temp + .5); alpar@1: parm[11] = 0; alpar@1: } alpar@1: scanf("%d", &parm[12]); alpar@1: if (parm[12] == 1) alpar@1: { scanf("%d", &parm[13]); alpar@1: scanf("%d", &parm[14]); alpar@1: } alpar@1: else alpar@1: { scanf("%le", &temp); alpar@1: parm[13] = (int)(100.0 * temp + .5); alpar@1: parm[14] = 0; alpar@1: } alpar@1: glp_gridgen(NULL, 0, 0, 0, parm); alpar@1: return 0; alpar@1: } alpar@1: #endif alpar@1: alpar@1: /* eof */