transport8.cpp File Reference
This is the 8th model in a series of tutorial examples. More...
#include "gams.h"#include <vector>#include <mutex>#include <thread>#include <iostream>Go to the source code of this file.
Functions | |
| void | scenSolve (GAMSWorkspace *ws, GAMSCheckpoint *cp, vector< double > *bmultVector, std::mutex *vectorMutex, std::mutex *ioMutex) | 
| Solve the model in different scenarios.   | |
| string | getModelText () | 
| Get model as string.   | |
| int | main (int argc, char *argv[]) | 
Detailed Description
This is the 8th model in a series of tutorial examples.
Here we show:
- How to use a vector to solve multiple GAMSModelInstances in parallel
 
Definition in file transport8.cpp.
Function Documentation
◆ getModelText()
| string getModelText | ( | ) | 
Get model as string.
Definition at line 74 of file transport8.cpp.
   75{
   76    return "Sets                                                                  \n"
   77           "   i   canning plants / seattle, san-diego /                          \n"
   78           "   j   markets / new-york, chicago, topeka / ;                        \n"
   79           "                                                                      \n"
   80           "Parameters                                                            \n"
   81           "                                                                      \n"
   82           "   a(i)  capacity of plant i in cases                                 \n"
   83           "   / seattle     350                                                  \n"
   84           "     san-diego   600 /                                                \n"
   85           "                                                                      \n"
   86           "   b(j)  demand at market j in cases                                  \n"
   87           "   / new-york    325                                                  \n"
   88           "     chicago     300                                                  \n"
   89           "     topeka      275 / ;                                              \n"
   90           "                                                                      \n"
   91           "Table d(i, j)  distance in thousands of miles                         \n"
   92           "                   new-york       chicago      topeka                 \n"
   93           "   seattle            2.5           1.7          1.8                  \n"
   94           "   san-diego          2.5           1.8          1.4;                 \n"
   95           "                                                                      \n"
   96           "Scalar f      freight in dollars per case per thousand miles / 90 / ; \n"
   97           "Scalar bmult  demand multiplier / 1 / ;                               \n"
   98           "                                                                      \n"
   99           "Parameter c(i, j)  transport cost in thousands of dollars per case;   \n"
  100           "                                                                      \n"
  101           "c(i, j) = f * d(i, j) / 1000;                                         \n"
  102           "                                                                      \n"
  103           "Variables                                                             \n"
  104           "   x(i, j)  shipment quantities in cases                              \n"
  105           "   z       total transportation costs in thousands of dollars;        \n"
  106           "                                                                      \n"
  107           "Positive Variable x;                                                  \n"
  108           "                                                                      \n"
  109           "Equations                                                             \n"
  110           "   cost        define objective function                              \n"
  111           "   supply(i)   observe supply limit at plant i                        \n"
  112           "   demand(j)   satisfy demand at market j;                            \n"
  113           "                                                                      \n"
  114           "cost..        z =e= sum((i, j), c(i, j)*x(i, j));                     \n"
  115           "                                                                      \n"
  116           "supply(i)..   sum(j, x(i, j)) =l= a(i);                               \n"
  117           "                                                                      \n"
  118           "demand(j)..   sum(i, x(i, j)) =g= bmult*b(j);                         \n"
  119           "                                                                      \n"
  120           "Model transport / all / ;                                             \n";
  121}
Referenced by main().
◆ main()
| int main | ( | int | argc, | 
| char * | argv[] ) | 
Definition at line 128 of file transport8.cpp.
  129{
  130    cout << "---------- Transport 8 --------------" << endl;
  131 
  132    try {
  133        GAMSWorkspaceInfo wsInfo;
  134        if (argc > 1)
  135            wsInfo.setSystemDirectory(argv[1]);
  136        GAMSWorkspace ws(wsInfo);
  137        GAMSCheckpoint cp = ws.addCheckpoint();
  138 
  139        // initialize a GAMSCheckpoint by running a GAMSJob
  140        ws.addJobFromString(getModelText()).run(cp);
  141 
  142        vector<double> bmultVector = { 1.3, 1.2, 1.1, 1.0, 0.9, 0.8, 0.7, 0.6 };
  143        int nrThreads = 2;
  144        // solve multiple model instances in parallel
  145        std::mutex vectorMutex;
  146        std::mutex ioMutex;
  147        vector<thread> v;
  148        for (int i = 0; i < nrThreads; i++)
  149            v.emplace_back([&ws, &cp, &bmultVector, &vectorMutex, &ioMutex] {scenSolve(&ws, &cp, &bmultVector, &vectorMutex, &ioMutex);});
  150        for (auto& t : v)
  151            t.join();
  152 
  154        cout << "GAMSException occured: " << ex.what() << endl;
  155    } catch (exception &ex) {
  156        cout << ex.what() << endl;
  157    }
  158 
  159    return 0;
  160}
void setSystemDirectory(const std::string &systemDir)
◆ scenSolve()
| void scenSolve | ( | GAMSWorkspace * | ws, | 
| GAMSCheckpoint * | cp, | ||
| vector< double > * | bmultVector, | ||
| std::mutex * | vectorMutex, | ||
| std::mutex * | ioMutex ) | 
Solve the model in different scenarios.
Definition at line 35 of file transport8.cpp.
   36{
   37    unique_lock<mutex> vectorLock(*vectorMutex);
   39    vectorLock.unlock();
   40 
   44    // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
   46 
   48 
   49    while (true)
   50    {
   51        double b;
   52 
   53        // dynamically get a bmult value from the vector instead of passing it to the different threads at creation time
   54        vectorLock.lock();
   55        if (bmultVector->empty())
   56            return;
   57        b = bmultVector->back();
   58        bmultVector->pop_back();
   59        vectorLock.unlock();
   61        mi.solve();
   62 
   63        // we need to make the output a critical section to avoid messed up report informations
   64        unique_lock<mutex> ioLock(*ioMutex);
   65        cout << "Scenario bmult=" << b << ":" << endl;
   69        ioLock.unlock();
   70    }
   71}
GAMSModelInstance addModelInstance(const std::string &modelInstanceName="")
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
GAMSVariable getVariable(const std::string &name)
void instantiate(const std::string &modelDefinition, const gams::GAMSOptions &options, const std::vector< gams::GAMSModifier > &modifiers={ })
GAMSEnum::SolveStat solveStatus()
GAMSEnum::ModelStat modelStatus()
void solve(GAMSEnum::SymbolUpdateType updateType, std::ostream &output, const GAMSModelInstanceOpt &miOpt)
GAMSDatabase syncDb()
void setAllModelTypes(const std::string &solver)
void setValue(const double val)
GAMSParameterRecord firstRecord(const std::vector< std::string > &slice)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
double level()
GAMSVariableRecord findRecord(const std::vector< std::string > &keys)
GAMSOptions addOptions()