User Tools

Site Tools


env:spawning_gams_from_php

Spawning GAMS from PHP

A minimal example

index.html

<html>
  <body>
    <form action="calling_gams.php" method="post">
      Solve transport with f = <select name="f">
          <option>70</option>
          <option>80</option>
          <option>90</option>
          <option>100</option>
        </select>
      <input type="submit" value="call GAMS"/>
    </form>
  </body>
</html>

calling_gams.php

<?php
 
$f = $_POST['f'];
 
//some model data
$modelfile = 'trnsport_php.gms';
$city   = array('new-york','chicago', 'topeka');
$demand = array(325.0, 300.0, 275.0);
 
//write demand.inc
$fh = fopen('./demand.inc', 'w+');
for($i=0; $i<count($city); $i++){
  fwrite($fh, $city[$i]." ".$demand[$i]."\n");
}
fclose($fh);
 
//write f.inc
$fh = fopen('./f.inc', 'w+');
fwrite($fh, $f);
fclose($fh);
 
//call gams
system('</path/to/gams>/gams '.$modelfile.' lo=2');
 
//read solutions
$fh = fopen('./results.txt', 'r');
echo '<p><b>result of '.$modelfile.' (f='.$f.') :</b></p>';
while (!feof($fh)){
   $line = fgets($fh);
   echo '<p>'.$line.'</p>';
}
fclose($fh);
 
?>

trnsport_php.gms

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

  Parameters

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

       b(j)  demand at market j in cases
         /
$include demand.inc
         / ;


  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  /
$include f.inc
         / ;

  Parameter c(i,j)  transport cost in thousands of dollars per case ;

            c(i,j) = f * d(i,j) / 1000 ;

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

  Positive Variable x ;

  Equations
       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/ ;

  Solve transport using lp minimizing z ;

  Display x.l, x.m ;

  file fout /results.txt/;
  put fout;
  loop((i,j),
    put i.tl:0 '->' j.tl:0 ':  ' x.l(i,j)/;
  );
  put 'cost: 'z.l/;
  putclose;
env/spawning_gams_from_php.txt · Last modified: 2011/01/24 03:53 by interfaces