alpar@9: /* glpnet03.c (Klingman's network problem generator) */ alpar@9: alpar@9: /*********************************************************************** alpar@9: * This code is part of GLPK (GNU Linear Programming Kit). alpar@9: * alpar@9: * This code is the result of translation of the Fortran program NETGEN alpar@9: * developed by Dr. Darwin Klingman, which is publically available from alpar@9: * NETLIB at . alpar@9: * alpar@9: * The translation was made by Andrew Makhorin . alpar@9: * alpar@9: * GLPK is free software: you can redistribute it and/or modify it alpar@9: * under the terms of the GNU General Public License as published by alpar@9: * the Free Software Foundation, either version 3 of the License, or alpar@9: * (at your option) any later version. alpar@9: * alpar@9: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@9: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@9: * License for more details. alpar@9: * alpar@9: * You should have received a copy of the GNU General Public License alpar@9: * along with GLPK. If not, see . alpar@9: ***********************************************************************/ alpar@9: alpar@9: #include "glpapi.h" alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_netgen - Klingman's network problem generator alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_netgen(glp_graph *G, int v_rhs, int a_cap, int a_cost, alpar@9: * const int parm[1+15]); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_netgen is a network problem generator developed by alpar@9: * Dr. Darwin Klingman. It can create capacitated and uncapacitated alpar@9: * minimum cost flow (or transshipment), transportation, and assignment alpar@9: * problems. alpar@9: * alpar@9: * The parameter G specifies the graph object, to which the generated alpar@9: * problem data have to be stored. Note that on entry the graph object alpar@9: * is erased with the routine glp_erase_graph. alpar@9: * alpar@9: * The parameter v_rhs specifies an offset of the field of type double alpar@9: * in the vertex data block, to which the routine stores the supply or alpar@9: * demand value. If v_rhs < 0, the value is not stored. alpar@9: * alpar@9: * The parameter a_cap specifies an offset of the field of type double alpar@9: * in the arc data block, to which the routine stores the arc capacity. alpar@9: * If a_cap < 0, the capacity is not stored. alpar@9: * alpar@9: * The parameter a_cost specifies an offset of the field of type double alpar@9: * in the arc data block, to which the routine stores the per-unit cost alpar@9: * if the arc flow. If a_cost < 0, the cost is not stored. alpar@9: * alpar@9: * The array parm contains description of the network to be generated: alpar@9: * alpar@9: * parm[0] not used alpar@9: * parm[1] (iseed) 8-digit positive random number seed alpar@9: * parm[2] (nprob) 8-digit problem id number alpar@9: * parm[3] (nodes) total number of nodes alpar@9: * parm[4] (nsorc) total number of source nodes (including alpar@9: * transshipment nodes) alpar@9: * parm[5] (nsink) total number of sink nodes (including alpar@9: * transshipment nodes) alpar@9: * parm[6] (iarcs) number of arcs alpar@9: * parm[7] (mincst) minimum cost for arcs alpar@9: * parm[8] (maxcst) maximum cost for arcs alpar@9: * parm[9] (itsup) total supply alpar@9: * parm[10] (ntsorc) number of transshipment source nodes alpar@9: * parm[11] (ntsink) number of transshipment sink nodes alpar@9: * parm[12] (iphic) percentage of skeleton arcs to be given alpar@9: * the maximum cost alpar@9: * parm[13] (ipcap) percentage of arcs to be capacitated alpar@9: * parm[14] (mincap) minimum upper bound for capacitated arcs alpar@9: * parm[15] (maxcap) maximum upper bound for capacitated arcs alpar@9: * alpar@9: * The routine generates a transportation problem if: alpar@9: * alpar@9: * nsorc + nsink = nodes, ntsorc = 0, and ntsink = 0. alpar@9: * alpar@9: * The routine generates an assignment problem if the requirements for alpar@9: * a transportation problem are met and: alpar@9: * alpar@9: * nsorc = nsink and itsup = nsorc. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * If the instance was successfully generated, the routine glp_netgen alpar@9: * returns zero; otherwise, if specified parameters are inconsistent, alpar@9: * the routine returns a non-zero error code. alpar@9: * alpar@9: * REFERENCES alpar@9: * alpar@9: * D.Klingman, A.Napier, and J.Stutz. NETGEN: A program for generating alpar@9: * large scale capacitated assignment, transportation, and minimum cost alpar@9: * flow networks. Management Science 20 (1974), 814-20. */ alpar@9: alpar@9: struct csa alpar@9: { /* common storage area */ alpar@9: glp_graph *G; alpar@9: int v_rhs, a_cap, a_cost; alpar@9: int nodes, iarcs, mincst, maxcst, itsup, nsorc, nsink, nonsor, alpar@9: nfsink, narcs, nsort, nftsor, ipcap, mincap, maxcap, ktl, alpar@9: nodlft, *ipred, *ihead, *itail, *iflag, *isup, *lsinks, mult, alpar@9: modul, i15, i16, jran; alpar@9: }; alpar@9: alpar@9: #define G (csa->G) alpar@9: #define v_rhs (csa->v_rhs) alpar@9: #define a_cap (csa->a_cap) alpar@9: #define a_cost (csa->a_cost) alpar@9: #define nodes (csa->nodes) alpar@9: #define iarcs (csa->iarcs) alpar@9: #define mincst (csa->mincst) alpar@9: #define maxcst (csa->maxcst) alpar@9: #define itsup (csa->itsup) alpar@9: #define nsorc (csa->nsorc) alpar@9: #define nsink (csa->nsink) alpar@9: #define nonsor (csa->nonsor) alpar@9: #define nfsink (csa->nfsink) alpar@9: #define narcs (csa->narcs) alpar@9: #define nsort (csa->nsort) alpar@9: #define nftsor (csa->nftsor) alpar@9: #define ipcap (csa->ipcap) alpar@9: #define mincap (csa->mincap) alpar@9: #define maxcap (csa->maxcap) alpar@9: #define ktl (csa->ktl) alpar@9: #define nodlft (csa->nodlft) alpar@9: #if 0 alpar@9: /* spent a day to find out this bug */ alpar@9: #define ist (csa->ist) alpar@9: #else alpar@9: #define ist (ipred[0]) alpar@9: #endif alpar@9: #define ipred (csa->ipred) alpar@9: #define ihead (csa->ihead) alpar@9: #define itail (csa->itail) alpar@9: #define iflag (csa->iflag) alpar@9: #define isup (csa->isup) alpar@9: #define lsinks (csa->lsinks) alpar@9: #define mult (csa->mult) alpar@9: #define modul (csa->modul) alpar@9: #define i15 (csa->i15) alpar@9: #define i16 (csa->i16) alpar@9: #define jran (csa->jran) alpar@9: alpar@9: static void cresup(struct csa *csa); alpar@9: static void chain(struct csa *csa, int lpick, int lsorc); alpar@9: static void chnarc(struct csa *csa, int lsorc); alpar@9: static void sort(struct csa *csa); alpar@9: static void pickj(struct csa *csa, int it); alpar@9: static void assign(struct csa *csa); alpar@9: static void setran(struct csa *csa, int iseed); alpar@9: static int iran(struct csa *csa, int ilow, int ihigh); alpar@9: alpar@9: int glp_netgen(glp_graph *G_, int _v_rhs, int _a_cap, int _a_cost, alpar@9: const int parm[1+15]) alpar@9: { struct csa _csa, *csa = &_csa; alpar@9: int iseed, nprob, ntsorc, ntsink, iphic, i, nskel, nltr, ltsink, alpar@9: ntrans, npsink, nftr, npsorc, ntravl, ntrrem, lsorc, lpick, alpar@9: nsksr, nsrchn, j, item, l, ks, k, ksp, li, n, ii, it, ih, icap, alpar@9: jcap, icost, jcost, ret; alpar@9: G = G_; alpar@9: v_rhs = _v_rhs; alpar@9: a_cap = _a_cap; alpar@9: a_cost = _a_cost; alpar@9: if (G != NULL) alpar@9: { if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double)) alpar@9: xerror("glp_netgen: v_rhs = %d; invalid offset\n", v_rhs); alpar@9: if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double)) alpar@9: xerror("glp_netgen: a_cap = %d; invalid offset\n", a_cap); alpar@9: if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double)) alpar@9: xerror("glp_netgen: a_cost = %d; invalid offset\n", a_cost); alpar@9: } alpar@9: /* Input the user's random number seed and fix it if alpar@9: non-positive. */ alpar@9: iseed = parm[1]; alpar@9: nprob = parm[2]; alpar@9: if (iseed <= 0) iseed = 13502460; alpar@9: setran(csa, iseed); alpar@9: /* Input the user's problem characteristics. */ alpar@9: nodes = parm[3]; alpar@9: nsorc = parm[4]; alpar@9: nsink = parm[5]; alpar@9: iarcs = parm[6]; alpar@9: mincst = parm[7]; alpar@9: maxcst = parm[8]; alpar@9: itsup = parm[9]; alpar@9: ntsorc = parm[10]; alpar@9: ntsink = parm[11]; alpar@9: iphic = parm[12]; alpar@9: ipcap = parm[13]; alpar@9: mincap = parm[14]; alpar@9: maxcap = parm[15]; alpar@9: /* Check the size of the problem. */ alpar@9: if (!(10 <= nodes && nodes <= 100000)) alpar@9: { ret = 1; alpar@9: goto done; alpar@9: } alpar@9: /* Check user supplied parameters for consistency. */ alpar@9: if (!(nsorc >= 0 && nsink >= 0 && nsorc + nsink <= nodes)) alpar@9: { ret = 2; alpar@9: goto done; alpar@9: } alpar@9: if (iarcs < 0) alpar@9: { ret = 3; alpar@9: goto done; alpar@9: } alpar@9: if (mincst > maxcst) alpar@9: { ret = 4; alpar@9: goto done; alpar@9: } alpar@9: if (itsup < 0) alpar@9: { ret = 5; alpar@9: goto done; alpar@9: } alpar@9: if (!(0 <= ntsorc && ntsorc <= nsorc)) alpar@9: { ret = 6; alpar@9: goto done; alpar@9: } alpar@9: if (!(0 <= ntsink && ntsink <= nsink)) alpar@9: { ret = 7; alpar@9: goto done; alpar@9: } alpar@9: if (!(0 <= iphic && iphic <= 100)) alpar@9: { ret = 8; alpar@9: goto done; alpar@9: } alpar@9: if (!(0 <= ipcap && ipcap <= 100)) alpar@9: { ret = 9; alpar@9: goto done; alpar@9: } alpar@9: if (mincap > maxcap) alpar@9: { ret = 10; alpar@9: goto done; alpar@9: } alpar@9: /* Initailize the graph object. */ alpar@9: if (G != NULL) alpar@9: { glp_erase_graph(G, G->v_size, G->a_size); alpar@9: glp_add_vertices(G, nodes); alpar@9: if (v_rhs >= 0) alpar@9: { double zero = 0.0; alpar@9: for (i = 1; i <= nodes; i++) alpar@9: { glp_vertex *v = G->v[i]; alpar@9: memcpy((char *)v->data + v_rhs, &zero, sizeof(double)); alpar@9: } alpar@9: } alpar@9: } alpar@9: /* Allocate working arrays. */ alpar@9: ipred = xcalloc(1+nodes, sizeof(int)); alpar@9: ihead = xcalloc(1+nodes, sizeof(int)); alpar@9: itail = xcalloc(1+nodes, sizeof(int)); alpar@9: iflag = xcalloc(1+nodes, sizeof(int)); alpar@9: isup = xcalloc(1+nodes, sizeof(int)); alpar@9: lsinks = xcalloc(1+nodes, sizeof(int)); alpar@9: /* Print the problem documentation records. */ alpar@9: if (G == NULL) alpar@9: { xprintf("BEGIN\n"); alpar@9: xprintf("NETGEN PROBLEM%8d%10s%10d NODES AND%10d ARCS\n", alpar@9: nprob, "", nodes, iarcs); alpar@9: xprintf("USER:%11d%11d%11d%11d%11d%11d\nDATA:%11d%11d%11d%11d%" alpar@9: "11d%11d\n", iseed, nsorc, nsink, mincst, alpar@9: maxcst, itsup, ntsorc, ntsink, iphic, ipcap, alpar@9: mincap, maxcap); alpar@9: } alpar@9: else alpar@9: glp_set_graph_name(G, "NETGEN"); alpar@9: /* Set various constants used in the program. */ alpar@9: narcs = 0; alpar@9: nskel = 0; alpar@9: nltr = nodes - nsink; alpar@9: ltsink = nltr + ntsink; alpar@9: ntrans = nltr - nsorc; alpar@9: nfsink = nltr + 1; alpar@9: nonsor = nodes - nsorc + ntsorc; alpar@9: npsink = nsink - ntsink; alpar@9: nodlft = nodes - nsink + ntsink; alpar@9: nftr = nsorc + 1; alpar@9: nftsor = nsorc - ntsorc + 1; alpar@9: npsorc = nsorc - ntsorc; alpar@9: /* Randomly distribute the supply among the source nodes. */ alpar@9: if (npsorc + npsink == nodes && npsorc == npsink && alpar@9: itsup == nsorc) alpar@9: { assign(csa); alpar@9: nskel = nsorc; alpar@9: goto L390; alpar@9: } alpar@9: cresup(csa); alpar@9: /* Print the supply records. */ alpar@9: if (G == NULL) alpar@9: { xprintf("SUPPLY\n"); alpar@9: for (i = 1; i <= nsorc; i++) alpar@9: xprintf("%6s%6d%18s%10d\n", "", i, "", isup[i]); alpar@9: xprintf("ARCS\n"); alpar@9: } alpar@9: else alpar@9: { if (v_rhs >= 0) alpar@9: { for (i = 1; i <= nsorc; i++) alpar@9: { double temp = (double)isup[i]; alpar@9: glp_vertex *v = G->v[i]; alpar@9: memcpy((char *)v->data + v_rhs, &temp, sizeof(double)); alpar@9: } alpar@9: } alpar@9: } alpar@9: /* Make the sources point to themselves in ipred array. */ alpar@9: for (i = 1; i <= nsorc; i++) alpar@9: ipred[i] = i; alpar@9: if (ntrans == 0) goto L170; alpar@9: /* Chain the transshipment nodes together in the ipred array. */ alpar@9: ist = nftr; alpar@9: ipred[nltr] = 0; alpar@9: for (i = nftr; i < nltr; i++) alpar@9: ipred[i] = i+1; alpar@9: /* Form even length chains for 60 percent of the transshipments.*/ alpar@9: ntravl = 6 * ntrans / 10; alpar@9: ntrrem = ntrans - ntravl; alpar@9: L140: lsorc = 1; alpar@9: while (ntravl != 0) alpar@9: { lpick = iran(csa, 1, ntravl + ntrrem); alpar@9: ntravl--; alpar@9: chain(csa, lpick, lsorc); alpar@9: if (lsorc == nsorc) goto L140; alpar@9: lsorc++; alpar@9: } alpar@9: /* Add the remaining transshipments to the chains. */ alpar@9: while (ntrrem != 0) alpar@9: { alpar@9: lpick = iran(csa, 1, ntrrem); alpar@9: ntrrem--; alpar@9: lsorc = iran(csa, 1, nsorc); alpar@9: chain(csa, lpick, lsorc); alpar@9: } alpar@9: L170: /* Set all demands equal to zero. */ alpar@9: for (i = nfsink; i <= nodes; i++) alpar@9: ipred[i] = 0; alpar@9: /* The following loop takes one chain at a time (through the use alpar@9: of logic contained in the loop and calls to other routines) and alpar@9: creates the remaining network arcs. */ alpar@9: for (lsorc = 1; lsorc <= nsorc; lsorc++) alpar@9: { chnarc(csa, lsorc); alpar@9: for (i = nfsink; i <= nodes; i++) alpar@9: iflag[i] = 0; alpar@9: /* Choose the number of sinks to be hooked up to the current alpar@9: chain. */ alpar@9: if (ntrans != 0) alpar@9: nsksr = (nsort * 2 * nsink) / ntrans; alpar@9: else alpar@9: nsksr = nsink / nsorc + 1; alpar@9: if (nsksr < 2) nsksr = 2; alpar@9: if (nsksr > nsink) nsksr = nsink; alpar@9: nsrchn = nsort; alpar@9: /* Randomly pick nsksr sinks and put their names in lsinks. */ alpar@9: ktl = nsink; alpar@9: for (j = 1; j <= nsksr; j++) alpar@9: { item = iran(csa, 1, ktl); alpar@9: ktl--; alpar@9: for (l = nfsink; l <= nodes; l++) alpar@9: { if (iflag[l] != 1) alpar@9: { item--; alpar@9: if (item == 0) goto L230; alpar@9: } alpar@9: } alpar@9: break; alpar@9: L230: lsinks[j] = l; alpar@9: iflag[l] = 1; alpar@9: } alpar@9: /* If last source chain, add all sinks with zero demand to alpar@9: lsinks list. */ alpar@9: if (lsorc == nsorc) alpar@9: { for (j = nfsink; j <= nodes; j++) alpar@9: { if (ipred[j] == 0 && iflag[j] != 1) alpar@9: { nsksr++; alpar@9: lsinks[nsksr] = j; alpar@9: iflag[j] = 1; alpar@9: } alpar@9: } alpar@9: } alpar@9: /* Create demands for group of sinks in lsinks. */ alpar@9: ks = isup[lsorc] / nsksr; alpar@9: k = ipred[lsorc]; alpar@9: for (i = 1; i <= nsksr; i++) alpar@9: { nsort++; alpar@9: ksp = iran(csa, 1, ks); alpar@9: j = iran(csa, 1, nsksr); alpar@9: itail[nsort] = k; alpar@9: li = lsinks[i]; alpar@9: ihead[nsort] = li; alpar@9: ipred[li] += ksp; alpar@9: li = lsinks[j]; alpar@9: ipred[li] += ks - ksp; alpar@9: n = iran(csa, 1, nsrchn); alpar@9: k = lsorc; alpar@9: for (ii = 1; ii <= n; ii++) alpar@9: k = ipred[k]; alpar@9: } alpar@9: li = lsinks[1]; alpar@9: ipred[li] += isup[lsorc] - ks * nsksr; alpar@9: nskel += nsort; alpar@9: /* Sort the arcs in the chain from source lsorc using itail as alpar@9: sort key. */ alpar@9: sort(csa); alpar@9: /* Print this part of skeleton and create the arcs for these alpar@9: nodes. */ alpar@9: i = 1; alpar@9: itail[nsort+1] = 0; alpar@9: L300: for (j = nftsor; j <= nodes; j++) alpar@9: iflag[j] = 0; alpar@9: ktl = nonsor - 1; alpar@9: it = itail[i]; alpar@9: iflag[it] = 1; alpar@9: L320: ih = ihead[i]; alpar@9: iflag[ih] = 1; alpar@9: narcs++; alpar@9: ktl--; alpar@9: /* Determine if this skeleton arc should be capacitated. */ alpar@9: icap = itsup; alpar@9: jcap = iran(csa, 1, 100); alpar@9: if (jcap <= ipcap) alpar@9: { icap = isup[lsorc]; alpar@9: if (mincap > icap) icap = mincap; alpar@9: } alpar@9: /* Determine if this skeleton arc should have the maximum alpar@9: cost. */ alpar@9: icost = maxcst; alpar@9: jcost = iran(csa, 1, 100); alpar@9: if (jcost > iphic) alpar@9: icost = iran(csa, mincst, maxcst); alpar@9: if (G == NULL) alpar@9: xprintf("%6s%6d%6d%2s%10d%10d\n", "", it, ih, "", icost, alpar@9: icap); alpar@9: else alpar@9: { glp_arc *a = glp_add_arc(G, it, ih); alpar@9: if (a_cap >= 0) alpar@9: { double temp = (double)icap; alpar@9: memcpy((char *)a->data + a_cap, &temp, sizeof(double)); alpar@9: } alpar@9: if (a_cost >= 0) alpar@9: { double temp = (double)icost; alpar@9: memcpy((char *)a->data + a_cost, &temp, sizeof(double)); alpar@9: } alpar@9: } alpar@9: i++; alpar@9: if (itail[i] == it) goto L320; alpar@9: pickj(csa, it); alpar@9: if (i <= nsort) goto L300; alpar@9: } alpar@9: /* Create arcs from the transshipment sinks. */ alpar@9: if (ntsink != 0) alpar@9: { for (i = nfsink; i <= ltsink; i++) alpar@9: { for (j = nftsor; j <= nodes; j++) alpar@9: iflag[j] = 0; alpar@9: ktl = nonsor - 1; alpar@9: iflag[i] = 1; alpar@9: pickj(csa, i); alpar@9: } alpar@9: } alpar@9: L390: /* Print the demand records and end record. */ alpar@9: if (G == NULL) alpar@9: { xprintf("DEMAND\n"); alpar@9: for (i = nfsink; i <= nodes; i++) alpar@9: xprintf("%6s%6d%18s%10d\n", "", i, "", ipred[i]); alpar@9: xprintf("END\n"); alpar@9: } alpar@9: else alpar@9: { if (v_rhs >= 0) alpar@9: { for (i = nfsink; i <= nodes; i++) alpar@9: { double temp = - (double)ipred[i]; alpar@9: glp_vertex *v = G->v[i]; alpar@9: memcpy((char *)v->data + v_rhs, &temp, sizeof(double)); alpar@9: } alpar@9: } alpar@9: } alpar@9: /* Free working arrays. */ alpar@9: xfree(ipred); alpar@9: xfree(ihead); alpar@9: xfree(itail); alpar@9: xfree(iflag); alpar@9: xfree(isup); alpar@9: xfree(lsinks); alpar@9: /* The instance has been successfully generated. */ alpar@9: ret = 0; alpar@9: done: return ret; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * The routine cresup randomly distributes the total supply among the alpar@9: * source nodes. */ alpar@9: alpar@9: static void cresup(struct csa *csa) alpar@9: { int i, j, ks, ksp; alpar@9: xassert(itsup > nsorc); alpar@9: ks = itsup / nsorc; alpar@9: for (i = 1; i <= nsorc; i++) alpar@9: isup[i] = 0; alpar@9: for (i = 1; i <= nsorc; i++) alpar@9: { ksp = iran(csa, 1, ks); alpar@9: j = iran(csa, 1, nsorc); alpar@9: isup[i] += ksp; alpar@9: isup[j] += ks - ksp; alpar@9: } alpar@9: j = iran(csa, 1, nsorc); alpar@9: isup[j] += itsup - ks * nsorc; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * The routine chain adds node lpick to the end of the chain with source alpar@9: * node lsorc. */ alpar@9: alpar@9: static void chain(struct csa *csa, int lpick, int lsorc) alpar@9: { int i, j, k, l, m; alpar@9: k = 0; alpar@9: m = ist; alpar@9: for (i = 1; i <= lpick; i++) alpar@9: { l = k; alpar@9: k = m; alpar@9: m = ipred[k]; alpar@9: } alpar@9: ipred[l] = m; alpar@9: j = ipred[lsorc]; alpar@9: ipred[k] = j; alpar@9: ipred[lsorc] = k; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * The routine chnarc puts the arcs in the chain from source lsorc into alpar@9: * the ihead and itail arrays for sorting. */ alpar@9: alpar@9: static void chnarc(struct csa *csa, int lsorc) alpar@9: { int ito, ifrom; alpar@9: nsort = 0; alpar@9: ito = ipred[lsorc]; alpar@9: L10: if (ito == lsorc) return; alpar@9: nsort++; alpar@9: ifrom = ipred[ito]; alpar@9: ihead[nsort] = ito; alpar@9: itail[nsort] = ifrom; alpar@9: ito = ifrom; alpar@9: goto L10; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * The routine sort sorts the nsort arcs in the ihead and itail arrays. alpar@9: * ihead is used as the sort key (i.e. forward star sort order). */ alpar@9: alpar@9: static void sort(struct csa *csa) alpar@9: { int i, j, k, l, m, n, it; alpar@9: n = nsort; alpar@9: m = n; alpar@9: L10: m /= 2; alpar@9: if (m == 0) return; alpar@9: k = n - m; alpar@9: j = 1; alpar@9: L20: i = j; alpar@9: L30: l = i + m; alpar@9: if (itail[i] <= itail[l]) goto L40; alpar@9: it = itail[i]; alpar@9: itail[i] = itail[l]; alpar@9: itail[l] = it; alpar@9: it = ihead[i]; alpar@9: ihead[i] = ihead[l]; alpar@9: ihead[l] = it; alpar@9: i -= m; alpar@9: if (i >= 1) goto L30; alpar@9: L40: j++; alpar@9: if (j <= k) goto L20; alpar@9: goto L10; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * The routine pickj creates a random number of arcs out of node 'it'. alpar@9: * Various parameters are dynamically adjusted in an attempt to ensure alpar@9: * that the generated network has the correct number of arcs. */ alpar@9: alpar@9: static void pickj(struct csa *csa, int it) alpar@9: { int j, k, l, nn, nupbnd, icap, jcap, icost; alpar@9: if ((nodlft - 1) * 2 > iarcs - narcs - 1) alpar@9: { nodlft--; alpar@9: return; alpar@9: } alpar@9: if ((iarcs - narcs + nonsor - ktl - 1) / nodlft - nonsor + 1 >= 0) alpar@9: k = nonsor; alpar@9: else alpar@9: { nupbnd = (iarcs - narcs - nodlft) / nodlft * 2; alpar@9: L40: k = iran(csa, 1, nupbnd); alpar@9: if (nodlft == 1) k = iarcs - narcs; alpar@9: if ((nodlft - 1) * (nonsor - 1) < iarcs - narcs - k) goto L40; alpar@9: } alpar@9: nodlft--; alpar@9: for (j = 1; j <= k; j++) alpar@9: { nn = iran(csa, 1, ktl); alpar@9: ktl--; alpar@9: for (l = nftsor; l <= nodes; l++) alpar@9: { if (iflag[l] != 1) alpar@9: { nn--; alpar@9: if (nn == 0) goto L70; alpar@9: } alpar@9: } alpar@9: return; alpar@9: L70: iflag[l] = 1; alpar@9: icap = itsup; alpar@9: jcap = iran(csa, 1, 100); alpar@9: if (jcap <= ipcap) alpar@9: icap = iran(csa, mincap, maxcap); alpar@9: icost = iran(csa, mincst, maxcst); alpar@9: if (G == NULL) alpar@9: xprintf("%6s%6d%6d%2s%10d%10d\n", "", it, l, "", icost, alpar@9: icap); alpar@9: else alpar@9: { glp_arc *a = glp_add_arc(G, it, l); alpar@9: if (a_cap >= 0) alpar@9: { double temp = (double)icap; alpar@9: memcpy((char *)a->data + a_cap, &temp, sizeof(double)); alpar@9: } alpar@9: if (a_cost >= 0) alpar@9: { double temp = (double)icost; alpar@9: memcpy((char *)a->data + a_cost, &temp, sizeof(double)); alpar@9: } alpar@9: } alpar@9: narcs++; alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * The routine assign generate assignment problems. It defines the unit alpar@9: * supplies, builds a skeleton, then calls pickj to create the arcs. */ alpar@9: alpar@9: static void assign(struct csa *csa) alpar@9: { int i, it, nn, l, ll, icost; alpar@9: if (G == NULL) alpar@9: xprintf("SUPPLY\n"); alpar@9: for (i = 1; i <= nsorc; i++) alpar@9: { isup[i] = 1; alpar@9: iflag[i] = 0; alpar@9: if (G == NULL) alpar@9: xprintf("%6s%6d%18s%10d\n", "", i, "", isup[i]); alpar@9: else alpar@9: { if (v_rhs >= 0) alpar@9: { double temp = (double)isup[i]; alpar@9: glp_vertex *v = G->v[i]; alpar@9: memcpy((char *)v->data + v_rhs, &temp, sizeof(double)); alpar@9: } alpar@9: } alpar@9: } alpar@9: if (G == NULL) alpar@9: xprintf("ARCS\n"); alpar@9: for (i = nfsink; i <= nodes; i++) alpar@9: ipred[i] = 1; alpar@9: for (it = 1; it <= nsorc; it++) alpar@9: { for (i = nfsink; i <= nodes; i++) alpar@9: iflag[i] = 0; alpar@9: ktl = nsink - 1; alpar@9: nn = iran(csa, 1, nsink - it + 1); alpar@9: for (l = 1; l <= nsorc; l++) alpar@9: { if (iflag[l] != 1) alpar@9: { nn--; alpar@9: if (nn == 0) break; alpar@9: } alpar@9: } alpar@9: narcs++; alpar@9: ll = nsorc + l; alpar@9: icost = iran(csa, mincst, maxcst); alpar@9: if (G == NULL) alpar@9: xprintf("%6s%6d%6d%2s%10d%10d\n", "", it, ll, "", icost, alpar@9: isup[1]); alpar@9: else alpar@9: { glp_arc *a = glp_add_arc(G, it, ll); alpar@9: if (a_cap >= 0) alpar@9: { double temp = (double)isup[1]; alpar@9: memcpy((char *)a->data + a_cap, &temp, sizeof(double)); alpar@9: } alpar@9: if (a_cost >= 0) alpar@9: { double temp = (double)icost; alpar@9: memcpy((char *)a->data + a_cost, &temp, sizeof(double)); alpar@9: } alpar@9: } alpar@9: iflag[l] = 1; alpar@9: iflag[ll] = 1; alpar@9: pickj(csa, it); alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * Portable congruential (uniform) random number generator: alpar@9: * alpar@9: * next_value = ((7**5) * previous_value) modulo ((2**31)-1) alpar@9: * alpar@9: * This generator consists of three routines: alpar@9: * alpar@9: * (1) setran - initializes constants and seed alpar@9: * (2) iran - generates an integer random number alpar@9: * (3) rran - generates a real random number alpar@9: * alpar@9: * The generator requires a machine with at least 32 bits of precision. alpar@9: * The seed (iseed) must be in the range [1,(2**31)-1]. */ alpar@9: alpar@9: static void setran(struct csa *csa, int iseed) alpar@9: { xassert(iseed >= 1); alpar@9: mult = 16807; alpar@9: modul = 2147483647; alpar@9: i15 = 1 << 15; alpar@9: i16 = 1 << 16; alpar@9: jran = iseed; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * The routine iran generates an integer random number between ilow and alpar@9: * ihigh. If ilow > ihigh then iran returns ihigh. */ alpar@9: alpar@9: static int iran(struct csa *csa, int ilow, int ihigh) alpar@9: { int ixhi, ixlo, ixalo, leftlo, ixahi, ifulhi, irtlo, iover, alpar@9: irthi, j; alpar@9: ixhi = jran / i16; alpar@9: ixlo = jran - ixhi * i16; alpar@9: ixalo = ixlo * mult; alpar@9: leftlo = ixalo / i16; alpar@9: ixahi = ixhi * mult; alpar@9: ifulhi = ixahi + leftlo; alpar@9: irtlo = ixalo - leftlo * i16; alpar@9: iover = ifulhi / i15; alpar@9: irthi = ifulhi - iover * i15; alpar@9: jran = ((irtlo - modul) + irthi * i16) + iover; alpar@9: if (jran < 0) jran += modul; alpar@9: j = ihigh - ilow + 1; alpar@9: if (j > 0) alpar@9: return jran % j + ilow; alpar@9: else alpar@9: return ihigh; alpar@9: } alpar@9: alpar@9: /**********************************************************************/ alpar@9: alpar@9: #if 0 alpar@9: static int scan(char card[80+1], int pos, int len) alpar@9: { char buf[10+1]; alpar@9: memcpy(buf, &card[pos-1], len); alpar@9: buf[len] = '\0'; alpar@9: return atoi(buf); alpar@9: } alpar@9: alpar@9: int main(void) alpar@9: { int parm[1+15]; alpar@9: char card[80+1]; alpar@9: xassert(fgets(card, sizeof(card), stdin) == card); alpar@9: parm[1] = scan(card, 1, 8); alpar@9: parm[2] = scan(card, 9, 8); alpar@9: xassert(fgets(card, sizeof(card), stdin) == card); alpar@9: parm[3] = scan(card, 1, 5); alpar@9: parm[4] = scan(card, 6, 5); alpar@9: parm[5] = scan(card, 11, 5); alpar@9: parm[6] = scan(card, 16, 5); alpar@9: parm[7] = scan(card, 21, 5); alpar@9: parm[8] = scan(card, 26, 5); alpar@9: parm[9] = scan(card, 31, 10); alpar@9: parm[10] = scan(card, 41, 5); alpar@9: parm[11] = scan(card, 46, 5); alpar@9: parm[12] = scan(card, 51, 5); alpar@9: parm[13] = scan(card, 56, 5); alpar@9: parm[14] = scan(card, 61, 10); alpar@9: parm[15] = scan(card, 71, 10); alpar@9: glp_netgen(NULL, 0, 0, 0, parm); alpar@9: return 0; alpar@9: } alpar@9: #endif alpar@9: alpar@9: /* eof */