transport12.cpp File Reference
This is the 12th model in a series of tutorial examples. More...
#include "gams.h"#include <algorithm>#include <iostream>Go to the source code of this file.
Functions | |
| string | getModelText () | 
| Get model as string.   | |
| string | toLower (const string &str) | 
| Convert string to lower case.   | |
| void | GUSSCall (GAMSSet dict, GAMSModelInstance mi, string solveStatement, gams::GAMSOptions *opt=nullptr, gams::GAMSModelInstanceOpt miOpt=gams::GAMSModelInstanceOpt(), std::ostream *output=nullptr) | 
| Using GUSS to solve the model in different scenarios.   | |
| int | main (int argc, char *argv[]) | 
Detailed Description
This is the 12th model in a series of tutorial examples.
Here we show:
- How to implement a GUSS approach using the GAMS API
 
Definition in file transport12.cpp.
Function Documentation
◆ getModelText()
| string getModelText | ( | ) | 
Get model as string.
Definition at line 34 of file transport12.cpp.
   35{
   36    return "  Sets                                                                     \n"
   37           "       i   canning plants   / seattle, san-diego /                         \n"
   38           "       j   markets          / new-york, chicago, topeka / ;                \n"
   39           "                                                                           \n"
   40           "  Parameters                                                               \n"
   41           "                                                                           \n"
   42           "       a(i)  capacity of plant i in cases                                  \n"
   43           "         /    seattle     350                                              \n"
   44           "              san-diego   600  /                                           \n"
   45           "                                                                           \n"
   46           "       b(j)  demand at market j in cases                                   \n"
   47           "         /    new-york    325                                              \n"
   48           "              chicago     300                                              \n"
   49           "              topeka      275  / ;                                         \n"
   50           "                                                                           \n"
   51           "  Table d(i,j)  distance in thousands of miles                             \n"
   52           "                    new-york       chicago      topeka                     \n"
   53           "      seattle          2.5           1.7          1.8                      \n"
   54           "      san-diego        2.5           1.8          1.4  ;                   \n"
   55           "                                                                           \n"
   56           "  Scalar f      freight in dollars per case per thousand miles  /90/ ;     \n"
   57           "  Scalar bmult  demand multiplier /1/;                                     \n"
   58           "                                                                           \n"
   59           "  Parameter c(i,j)  transport cost in thousands of dollars per case ;      \n"
   60           "                                                                           \n"
   61           "            c(i,j) = f * d(i,j) / 1000 ;                                   \n"
   62           "                                                                           \n"
   63           "  Variables                                                                \n"
   64           "       x(i,j)  shipment quantities in cases                                \n"
   65           "       z       total transportation costs in thousands of dollars ;        \n"
   66           "                                                                           \n"
   67           "  Positive Variable x ;                                                    \n"
   68           "                                                                           \n"
   69           "  Equations                                                                \n"
   70           "       cost        define objective function                               \n"
   71           "       supply(i)   observe supply limit at plant i                         \n"
   72           "       demand(j)   satisfy demand at market j ;                            \n"
   73           "                                                                           \n"
   74           "  cost ..        z  =e=  sum((i,j), c(i,j)*x(i,j)) ;                       \n"
   75           "                                                                           \n"
   76           "  supply(i) ..   sum(j, x(i,j))  =l=  a(i) ;                               \n"
   77           "                                                                           \n"
   78           "  demand(j) ..   sum(i, x(i,j))  =g=  bmult*b(j) ;                         \n"
   79           "                                                                           \n"
   80           "  Model transport /all/ ;                                                  \n";
   81}
Referenced by main().
◆ GUSSCall()
| void GUSSCall | ( | GAMSSet | dict, | 
| GAMSModelInstance | mi, | ||
| string | solveStatement, | ||
| gams::GAMSOptions * | opt = nullptr, | ||
| gams::GAMSModelInstanceOpt | miOpt = gams::GAMSModelInstanceOpt(), | ||
| std::ostream * | output = nullptr ) | 
Using GUSS to solve the model in different scenarios.
Definition at line 92 of file transport12.cpp.
   96{
   97    vector< tuple<GAMSModifier, GAMSParameter> > modifierList;
   98 
  101 
  102    vector<string> strs { " ", "scenario", " " };
  105 
  106 
  109            continue;
  112            if (modifierDim < 0)
  115                                                 dict.database().getParameter(rec.key(2)));
  116            modifierList.push_back(tuple);
  117 
  118        } else if ((rec.key(1) == "lower") || (toLower(rec.key(1)) == "upper") || (toLower(rec.key(1)) == "fixed")) {
  120            if (modifierDim < 0)
  122 
  123            GAMSVariable modifierVar;
  124            try {
  126            } catch (...) {
  128            }
  129 
  133                                        dict.database().getParameter(rec.key(2)));
  134                modifierList.push_back(tuple);
  135 
  139                                        dict.database().getParameter(rec.key(2)));
  140                modifierList.push_back(tuple);
  141 
  142            } else {  // fixed
  145                                        dict.database().getParameter(rec.key(2)));
  146                modifierList.push_back(tuple);
  147            }
  149            // Check that parameter exists in GAMSDatabase, will throw an exception if not
  150            dict.database().getParameter(rec.key(2));
  151        } else {
  153        }
  154    }
  155 
  156    vector<GAMSModifier> mL;
  157    for (auto tuple : modifierList)
  158        mL.push_back(get<0>(tuple));
  159    if (opt)
  160        mi.instantiate(solveStatement, *opt, mL);
  161    else
  162        mi.instantiate(solveStatement, mL);
  163 
  164    vector<tuple<GAMSSymbol, GAMSParameter, string>> outList;
  165 
  167    {
  168        for (auto tup : modifierList)
  169        {
  170            GAMSParameter p;
  171            GAMSParameter pscen = get<1>(tup);
  172 
  173            if (!get<0>(tup).dataSymbol().isValid())
  174                p = get<0>(tup).gamsSymbol();
  175            else
  176                p = get<0>(tup).dataSymbol();
  177 
  178            // Implemented SymbolUpdateType=BaseCase
  179            p.clear();
  180 
  181            GAMSParameterRecord rec;
  182            vector<string> filter(pscen.dim());
  183            for (int i = 0; i < scenSymbol.dim(); i++)
  184                filter[i] = s.key(i);
  186                filter[i] = " ";
  187 
  188            try {
  189                rec = pscen.firstRecord(filter);
  191                continue;
  192            }
  193 
  194            do {
  195                vector<string> myKeys(p.dim());
  197                    myKeys[i] = rec.key(scenSymbol.dim()+i);
  200        }
  201 
  202        mi.solve(GAMSEnum::SymbolUpdateType::BaseCase, *output, miOpt);
  203 
  204        if (outList.size() == 0)
  210                    outList.push_back(tuple);
  211                }
  212 
  213        for (tuple<GAMSSymbol, GAMSParameter, string> tup : outList) {
  214            GAMSSymbol mySym = get<0>(tup);
  215            vector<string> myKeys;
  216            for (int i = 0; i < scenSymbol.dim(); i++)
  217                myKeys.push_back(s.key(i));
  218 
  222                        myKeys[scenSymbol.dim() + i] = s.key(i);
  223                    get<1>(tup).addRecord(myKeys).setValue(rec.level());
  224                }
  228                        myKeys[scenSymbol.dim() + i] = s.key(i);
  229                    get<1>(tup).addRecord(myKeys).setValue(rec.level());
  230                }
  234                        myKeys[scenSymbol.dim() + i] = s.key(i);
  235                    get<1>(tup).addRecord(myKeys).setValue(rec.marginal());
  236                }
  240                        myKeys[scenSymbol.dim() + i] = s.key(i);
  241                    get<1>(tup).addRecord(myKeys).setValue(rec.marginal());
  242                }
  243            }
  244        }
  245    }
  246}
GAMSVariable addVariable(const std::string &name, const int dimension, const GAMSEnum::VarType varType, const std::string &explanatoryText="")
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
GAMSSymbol getSymbol(const std::string &name)
GAMSSet getSet(const std::string &name)
GAMSParameter getParameter(const std::string &name)
GAMSVariable getVariable(const std::string &name)
SymTypeVar
SymTypeEqu
void instantiate(const std::string &modelDefinition, const gams::GAMSOptions &options, const std::vector< gams::GAMSModifier > &modifiers={ })
void solve(GAMSEnum::SymbolUpdateType updateType, std::ostream &output, const GAMSModelInstanceOpt &miOpt)
GAMSDatabase syncDb()
double value()
void setValue(const double val)
GAMSParameterRecord firstRecord(const std::vector< std::string > &slice)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord firstRecord(const std::vector< std::string > &slice)
std::string key(int index)
bool moveNext()
std::vector< std::string > keys()
int dim() const
gams::GAMSDatabase & database() const
GAMSEnum::SymbolType type() const
bool clear()
Referenced by main().
◆ main()
| int main | ( | int | argc, | 
| char * | argv[] ) | 
Definition at line 253 of file transport12.cpp.
  254{
  255    cout << "---------- Transport 12 --------------" << endl;
  256 
  257    try {
  258        GAMSWorkspaceInfo wsInfo;
  259        if (argc > 1)
  260            wsInfo.setSystemDirectory(argv[1]);
  261        GAMSWorkspace ws(wsInfo);
  262        GAMSCheckpoint cp = ws.addCheckpoint();
  263 
  264        // initialize a GAMSCheckpoint by running a GAMSJob
  266        t12.run(cp);
  267 
  268        // create a GAMSModelInstance and solve it multiple times with different scalar bmult
  270 
  271        double bmultlist[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
  272 
  273        GAMSDatabase db = ws.addDatabase();
  274 
  278 
  279        int i = 0;
  280        for (double b : bmultlist) {
  283        }
  284 
  289 
  290 
  292 
  294            cout << rec.key(0) << " obj: " << rec.value() << endl;
  295 
  297        GAMSDatabase db2 = ws.addDatabase();
  298 
  302 
  303        for (int j = 0; j < 4; j++) {
  308        }
  309 
  314 
  316 
  318            cout << rec.key(0) << " obj: " << rec.value() << endl;
  319 
  321        cout << "GAMSException occured: " << ex.what() << endl;
  322    } catch (exception &ex) {
  323        cout << ex.what() << endl;
  324    }
  325 
  326    return 0;
  327}
GAMSModelInstance addModelInstance(const std::string &modelInstanceName="")
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
GAMSDatabase outDB()
void run()
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
std::string & name() const
void setSystemDirectory(const std::string &systemDir)
void GUSSCall(GAMSSet dict, GAMSModelInstance mi, string solveStatement, gams::GAMSOptions *opt=nullptr, gams::GAMSModelInstanceOpt miOpt=gams::GAMSModelInstanceOpt(), std::ostream *output=nullptr)
Using GUSS to solve the model in different scenarios.
Definition transport12.cpp:92
◆ toLower()
| string toLower | ( | const string & | str | ) | 
Convert string to lower case.
Definition at line 84 of file transport12.cpp.
   85{
   86    string lstr(str);
   87    transform(lstr.begin(), lstr.end(), lstr.begin(), ::tolower);
   88    return lstr;
   89}
Referenced by GUSSCall().