tgridmix.gms : Grid Transportation Problem with Single Submit and Collect Loop

Description

This problem finds a least cost shipping schedule that meets
requirements at markets and supplies at factories.

The model demonstrates how to run multiple scenarios with different
demands in a parallel fashion using the GAMS asynchronous grid and
threads facility. This model submits and collects jobs in a single
loop. This allows to control the total number of active jobs during
the entire execution.


Small Model of Type : LP


Category : GAMS Model library


Main file : tgridmix.gms

$title Grid/MT Transportation Problem with Single Submit and Collect Loop (TGRIDMIX,SEQ=391)

$onText
This problem finds a least cost shipping schedule that meets
requirements at markets and supplies at factories.

The model demonstrates how to run multiple scenarios with different
demands in a parallel fashion using the GAMS asynchronous grid and
threads facility. This model submits and collects jobs in a single
loop. This allows to control the total number of active jobs during
the entire execution.


Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions.
Princeton University Press, Princeton, New Jersey, 1963.

Keywords: linear programming, transportation problem, scheduling, scenario analysis
$offText

Set
   i 'canning plants' / seattle,  san-diego /
   j 'markets'        / new-york, chicago, topeka /;

Parameter
   a(i) 'capacity of plant i in cases'
        / seattle    350
          san-diego  600 /

   b(j) 'demand at market j in cases'
        / new-york   325
          chicago    300
          topeka     275 /;

Table d(i,j) 'distance in thousands of miles'
              new-york  chicago  topeka
   seattle         2.5      1.7     1.8
   san-diego       2.5      1.8     1.4;

Scalar f 'freight in dollars per case per thousand miles' / 90 /;

Parameter c(i,j) 'transport cost in thousands of dollars per case';
c(i,j) = f*d(i,j)/1000;

Variable
   x(i,j) 'shipment quantities in cases'
   z      'total transportation costs in thousands of dollars';

Positive Variable x;

Equation
   cost      'define objective function'
   supply(i) 'observe supply limit at plant i'
   demand(j) 'satisfy demand at market j';

cost..      z =e= sum((i,j), c(i,j)*x(i,j));

supply(i).. sum(j, x(i,j)) =l= a(i);

demand(j).. sum(i, x(i,j)) =g= b(j);

Model transport / all /;

$eolCom //

transport.limCol    = 0;
transport.limRow    = 0;
transport.solPrint  = %solPrint.quiet%;
$if not set threads $set threads 4
option threadsAsync = %threads%;

Set
$if not set numscen $set numscen 10
   s         'scenarios' / 1*%numscen% /
   sl        'solvelink'
   submit(s) 'list of jobs to submit'
   done(s)   'list of completed jobs';

Parameter
$ifThen not set asynctype
   slnum(sl<)'solvelink number' / Threads %solveLink.asyncThreads%
                                  Grid    %solveLink.asyncGrid%    /
$else
   slnum(sl<)'solvelink number' / %asynctype% /
$endif
   dem(s,j)  'random demand'
   h(s)      'store the instance handle'
   repx      'solution report'
   repy      'summary report'
   maxS      'maximum number of active jobs' / %threads% /
   tooSlow   'did we hit the elapsed time limit?'
   tStart    'time stamp';

dem(s,j) = b(j)*uniform(.95,1.15); // create some random demands
loop(sl,
   tStart = jnow;
   repy(sl,s,'solvestat') = na;
   repy(sl,s,'modelstat') = na;
   done(s) = no;
   h(s)    =  0;
   transport.solveLink = slnum(sl);

   repeat
      submit(s) = no;
      loop(s$(not (done(s) or h(s))), submit(s) = yes$(card(submit) + card(h) < maxS));
      loop(submit(s),
         b(j) = dem(s,j);
$if set sleep display$sleep(%sleep%) 'sleep a little';
         solve transport using lp minimizing z;
         h(s) = transport.handle;
      );
      display$readyCollect(h) 'Waiting for next instance to complete';
      loop(s$handleCollect(h(s)),
         repx(sl,s,i,j) = x.l(i,j);
         repy(sl,s,'solvestat') = transport.solveStat;
         repy(sl,s,'modelstat') = transport.modelStat;
         repy(sl,s,'resusd'   ) = transport.resUsd;
         repy(sl,s,'objval')    = transport.objVal;
         display$handleDelete(h(s)) 'trouble deleting handles';
         done(s) = yes;
         h(s)    =   0;
      );
      tooSlow = [timeElapsed > 10];
   until card(done) = card(s) or tooSlow;  // wait until all model results are harvested or time limit
   abort.noError$tooSlow 'elapsed time limit exceeded: this job ran too slowly', h, repy;
   repy(sl,'time','elapsed') = (jnow - tStart)*3600*24;
   abort$sum(s$(repy(sl,s,'solvestat') = na or repy(sl,s,'solvestat') = %solveStat.licensingProblems%),1) 'Some jobs did not return or had licensing issues', h, repy;
);
display repx, repy;