transport2.cpp File Reference
This is the 2nd model in a series of tutorial examples. More...
#include "gams.h"#include <iostream>#include <fstream>Go to the source code of this file.
Functions | |
| string | getDataText () | 
| Get data as string.   | |
| string | getModelText () | 
| Get model as string.   | |
| int | main (int argc, char *argv[]) | 
Detailed Description
This is the 2nd model in a series of tutorial examples.
Here we show:
- How to use include files to separate model and data
 
Definition in file transport2.cpp.
Function Documentation
◆ getDataText()
| string getDataText | ( | ) | 
Get data as string.
Definition at line 33 of file transport2.cpp.
   34{
   35    return "Sets                                                           \n"
   36           "  i   canning plants   / seattle, san-diego /                  \n"
   37           "  j   markets          / new-york, chicago, topeka / ;         \n"
   38           "Parameters                                                     \n"
   39           "                                                               \n"
   40           "  a(i)  capacity of plant i in cases                           \n"
   41           "                     /    seattle     350                      \n"
   42           "                          san-diego   600  /                   \n"
   43           "                                                               \n"
   44           "  b(j)  demand at market j in cases                            \n"
   45           "                     /    new-york    325                      \n"
   46           "                          chicago     300                      \n"
   47           "                          topeka      275  / ;                 \n"
   48           "                                                               \n"
   49           "Table d(i,j)  distance in thousands of miles                   \n"
   50           "               new-york       chicago      topeka              \n"
   51           "  seattle          2.5           1.7          1.8              \n"
   52           "  san-diego        2.5           1.8          1.4  ;           \n"
   53           "                                                               \n"
   54           "Scalar f  freight in dollars per case per thousand miles  /90/;\n";
   55}
Referenced by main().
◆ getModelText()
| string getModelText | ( | ) | 
Get model as string.
Definition at line 58 of file transport2.cpp.
   59{
   60    return "Sets                                                                    \n"
   61           "      i   canning plants                                                \n"
   62           "      j   markets                                                       \n"
   63           "                                                                        \n"
   64           "Parameters                                                              \n"
   65           "      a(i)   capacity of plant i in cases                               \n"
   66           "      b(j)   demand at market j in cases                                \n"
   67           "      d(i,j) distance in thousands of miles                             \n"
   68           "Scalar f  freight in dollars per case per thousand miles;               \n"
   69           "                                                                        \n"
   70           "$if not set incname $abort 'no include file name for data file provided'\n"
   71           "$include %incname%                                                      \n"
   72           "                                                                        \n"
   73           " Parameter c(i,j)  transport cost in thousands of dollars per case ;    \n"
   74           "                                                                        \n"
   75           "            c(i,j) = f * d(i,j) / 1000 ;                                \n"
   76           "                                                                        \n"
   77           " Variables                                                              \n"
   78           "       x(i,j)  shipment quantities in cases                             \n"
   79           "       z       total transportation costs in thousands of dollars ;     \n"
   80           "                                                                        \n"
   81           " Positive Variable x ;                                                  \n"
   82           "                                                                        \n"
   83           " Equations                                                              \n"
   84           "                                                                        \n"
   85           "      cost        define objective function                             \n"
   86           "      supply(i)   observe supply limit at plant i                       \n"
   87           "       demand(j)   satisfy demand at market j ;                         \n"
   88           "                                                                        \n"
   89           "  cost ..        z  =e=  sum((i,j), c(i,j)*x(i,j)) ;                    \n"
   90           "                                                                        \n"
   91           "  supply(i) ..   sum(j, x(i,j))  =l=  a(i) ;                            \n"
   92           "                                                                        \n"
   93           "  demand(j) ..   sum(i, x(i,j))  =g=  b(j) ;                            \n"
   94           "                                                                        \n"
   95           " Model transport /all/ ;                                                \n"
   96           "                                                                        \n"
   97           " Solve transport using lp minimizing z ;                                \n"
   98           "                                                                        \n"
   99           "Display x.l, x.m ;                                                      \n";
  100}
Referenced by main().
◆ main()
| int main | ( | int | argc, | 
| char * | argv[] ) | 
Definition at line 107 of file transport2.cpp.
  108{
  109    cout << "---------- Transport 2 --------------" << endl;
  110 
  111    try {
  112        GAMSWorkspaceInfo wsInfo;
  113        if (argc > 1)
  114            wsInfo.setSystemDirectory(argv[1]);
  115        GAMSWorkspace ws(wsInfo);
  116 
  117        // write an include file containing data with GAMS syntax
  118        ofstream tdata(ws.workingDirectory() + cPathSep + "tdata.gms");
  119        tdata << getDataText();
  120        tdata.close();
  121 
  122        // run a job using an instance of GAMSOptions that defines the data include file
  123        GAMSOptions opt = ws.addOptions();
  126        t2.run(opt);
  128            cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
  129                 << rec.marginal() << endl;
  130 
  132        cout << "GAMSException occured: " << ex.what() << endl;
  133    } catch (exception &ex) {
  134        cout << ex.what() << endl;
  135    }
  136 
  137    return 0;
  138}
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
void run()
void setDefine(const std::string &key, const std::string &value)
void setSystemDirectory(const std::string &systemDir)