transport4.cpp File Reference
This is the 4th model in a series of tutorial examples. More...
#include "gams.h"#include <iostream>#include <fstream>#include <vector>#include <map>Go to the source code of this file.
Functions | |
| string | getModelText () | 
| Get model as string.   | |
| int | main (int argc, char *argv[]) | 
Detailed Description
This is the 4th model in a series of tutorial examples.
Here we show:
- How to define data using C++ data structures
 - How to prepare a GAMSDatabase from C++ data structures
 
Definition in file transport4.cpp.
Function Documentation
◆ getModelText()
| string getModelText | ( | ) | 
Get model as string.
Definition at line 35 of file transport4.cpp.
   36{
   37    return "Sets                                                                       \n"
   38           "      i   canning plants                                                   \n"
   39           "      j   markets                                                          \n"
   40           "                                                                           \n"
   41           "Parameters                                                                 \n"
   42           "      a(i)   capacity of plant i in cases                                  \n"
   43           "      b(j)   demand at market j in cases                                   \n"
   44           "      d(i,j) distance in thousands of miles                                \n"
   45           "Scalar f  freight in dollars per case per thousand miles;                  \n"
   46           "                                                                           \n"
   47           "$if not set gdxincname $abort 'no include file name for data file provided'\n"
   48           "$gdxin %gdxincname%                                                        \n"
   49           "$load i j a b d f                                                          \n"
   50           "$gdxin                                                                     \n"
   51           "                                                                           \n"
   52           " Parameter c(i,j)  transport cost in thousands of dollars per case ;       \n"
   53           "                                                                           \n"
   54           "            c(i,j) = f * d(i,j) / 1000 ;                                   \n"
   55           "                                                                           \n"
   56           " Variables                                                                 \n"
   57           "       x(i,j)  shipment quantities in cases                                \n"
   58           "       z       total transportation costs in thousands of dollars ;        \n"
   59           "                                                                           \n"
   60           " Positive Variable x ;                                                     \n"
   61           "                                                                           \n"
   62           " Equations                                                                 \n"
   63           "                                                                           \n"
   64           "      cost        define objective function                                \n"
   65           "      supply(i)   observe supply limit at plant i                          \n"
   66           "       demand(j)   satisfy demand at market j ;                            \n"
   67           "                                                                           \n"
   68           "  cost ..        z  =e=  sum((i,j), c(i,j)*x(i,j)) ;                       \n"
   69           "                                                                           \n"
   70           "  supply(i) ..   sum(j, x(i,j))  =l=  a(i) ;                               \n"
   71           "                                                                           \n"
   72           "  demand(j) ..   sum(i, x(i,j))  =g=  b(j) ;                               \n"
   73           "                                                                           \n"
   74           " Model transport /all/ ;                                                   \n"
   75           "                                                                           \n"
   76           " Solve transport using lp minimizing z ;                                   \n"
   77           "                                                                           \n"
   78           "Display x.l, x.m ;                                                         \n";
   79}
Referenced by main().
◆ main()
| int main | ( | int | argc, | 
| char * | argv[] ) | 
Definition at line 87 of file transport4.cpp.
   88{
   89    cout << "---------- Transport 4 --------------" << endl;
   90 
   91    try {
   92        GAMSWorkspaceInfo wsInfo;
   93        if (argc > 1)
   94            wsInfo.setSystemDirectory(argv[1]);
   95        GAMSWorkspace ws(wsInfo);
   96 
   97        // define some data by using C++ data structures
   98        vector<string> plants = {
   99            "Seattle", "San-Diego"
  100        };
  101        vector<string> markets = {
  102            "New-York", "Chicago", "Topeka"
  103        };
  104        map<string, double> capacity = {
  105            { "Seattle", 350.0 }, { "San-Diego", 600.0 }
  106        };
  107        map<string, double> demand = {
  108            { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
  109        };
  110        map<tuple<string, string>, double> distance = {
  111            { make_tuple("Seattle", "New-York"), 2.5 },
  112            { make_tuple("Seattle", "Chicago"), 1.7 },
  113            { make_tuple("Seattle", "Topeka"), 1.8 },
  114            { make_tuple("San-Diego", "New-York"), 2.5 },
  115            { make_tuple("San-Diego", "Chicago"), 1.8 },
  116            { make_tuple("San-Diego", "Topeka"), 1.4 }
  117        };
  118 
  119        // prepare a GAMSDatabase with data from the C++ data structures
  120        GAMSDatabase db = ws.addDatabase();
  121 
  123        for (string p: plants)
  124            i.addRecord(p);
  125 
  127        for (string m: markets)
  128            j.addRecord(m);
  129 
  131        for (string p: plants)
  133 
  135        for (string m: markets)
  137 
  139        for (auto t : distance)
  141 
  144 
  145        // run a job using data from the created GAMSDatabase
  147        GAMSOptions opt = ws.addOptions();
  150        t4.run(opt, db);
  151 
  153            cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
  154                 << rec.marginal() << endl;
  155 
  157        cout << "GAMSException occured: " << ex.what() << endl;
  158    } catch (exception &ex) {
  159        cout << ex.what() << endl;
  160    }
  161 
  162    return 0;
  163}
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
std::string name()
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
void run()
void setAllModelTypes(const std::string &solver)
void setDefine(const std::string &key, const std::string &value)
void setValue(const double val)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
void setSystemDirectory(const std::string &systemDir)