transportGDX.cpp File Reference
This example shows the use of the GAMSDatabase class for reading and writing GDX files. More...
#include "gams.h"#include <iostream>#include <fstream>#include <vector>#include <map>#include <cstdlib>Go to the source code of this file.
Functions | |
| int | main (int argc, char *argv[]) | 
Detailed Description
This example shows the use of the GAMSDatabase class for reading and writing GDX files.
Here we show:
- How to fill a GAMSDatabase from C++ data structures and export it to a GDX file
 - How to import a GDX file as a GAMSDatabase
 
Definition in file transportGDX.cpp.
Function Documentation
◆ main()
| int main | ( | int | argc, | 
| char * | argv[] ) | 
Definition at line 42 of file transportGDX.cpp.
   43{
   44    cout << "---------- Transport GDX --------------" << endl;
   45 
   46    try {
   47        GAMSWorkspaceInfo wsInfo;
   48        if (argc > 1)
   49            wsInfo.setSystemDirectory(argv[1]);
   50        GAMSWorkspace ws(wsInfo);
   51 
   52        // define some data by using C++ data structures
   53        vector<string> plants = {
   54            "Seattle", "San-Diego"
   55        };
   56        vector<string> markets = {
   57            "New-York", "Chicago", "Topeka"
   58        };
   59        map<string, double> capacity = {
   60            { "Seattle", 350.0 }, { "San-Diego", 600.0 }
   61        };
   62        map<string, double> demand = {
   63            { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
   64        };
   65        map<tuple<string, string>, double> distance = {
   66            { make_tuple("Seattle", "New-York"), 2.5 },
   67            { make_tuple("Seattle", "Chicago"), 1.7 },
   68            { make_tuple("Seattle", "Topeka"), 1.8 },
   69            { make_tuple("San-Diego", "New-York"), 2.5 },
   70            { make_tuple("San-Diego", "Chicago"), 1.8 },
   71            { make_tuple("San-Diego", "Topeka"), 1.4 }
   72        };
   73 
   74        // create new GAMSDatabase instance
   75        GAMSDatabase db = ws.addDatabase();
   76 
   77        // add 1-dimensional set 'i' with explanatory text 'canning plants' to the GAMSDatabase
   79        for (string p: plants)
   80            i.addRecord(p);
   81 
   82        // add 1-dimensional set 'j' with explanatory text 'markets' to the GAMSDatabase
   84        for (string m: markets)
   85            j.addRecord(m);
   86 
   87        // add parameter 'a' with domain 'i'
   89        for (string p: plants)
   91 
   92        // add parameter 'b' with domain 'j'
   94        for (string m: markets)
   96 
   97        // add parameter 'd' with domains 'i' and 'j'
   99        for (auto t : distance)
  101 
  102        // add scalar 'f'
  105 
  106        // export the GAMSDatabase to a GDX file with name 'data.gdx' located in the 'workingDirectory' of the GAMSWorkspace
  108 
  109        cout << "Content of GDX file 'data.gdx':";
  110        string command = "gdxdump " + ws.workingDirectory() + cPathSep + "data.gdx";
  111        int ret = system(command.c_str());
  112        if (ret)
  113            cerr << "system(" << command.c_str() << ") returned " << ret << endl;
  114 
  115        // add a new GAMSDatabase and initialize it from the GDX file just created
  117 
  118        // read data from symbols into C++ data structures
  119        vector<string> iNew;
  121            iNew.push_back(rec.key(0));
  122 
  123        vector<string> jNew;
  125            jNew.push_back(rec.key(0));
  126 
  127        map<string, double> aNew;
  129            aNew[rec.key(0)] = rec.value();
  130 
  131        map<string, double> bNew;
  133            bNew[rec.key(0)] = rec.value();
  134 
  135        map<tuple<string, string>, double> dNew;
  137            dNew[make_tuple(rec.key(0), rec.key(1))] = rec.value();
  138 
  140 
  141        cout << "i:" << endl;
  142        for(string s : iNew)
  143            cout << "  " << s << endl;
  144        cout  << "j:" << endl;
  145        for(string s : jNew)
  146            cout << "  " << s << endl;
  147        cout << "a:" << endl;
  148        for (auto rec : aNew)
  149            cout << "  " << rec.first << " : " << rec.second << endl;
  150        cout << "b:" << endl;
  151        for (auto rec : bNew)
  152            cout << "  " << rec.first << " : " << rec.second << endl;
  153        cout << "d:" << endl;
  154        for (auto rec : dNew)
  155            cout << "  " << get<0>(rec.first) << ", " << get<1>(rec.first) << " : " << rec.second << endl;
  156        cout << "f:" << endl;
  157        cout << "  " << fNew;
  158 
  160        cout << "GAMSException occured: " << ex.what() << endl;
  161    } catch (exception &ex) {
  162        cout << ex.what() << endl;
  163    }
  164 
  165    return 0;
  166}
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="")
void doExport(const std::string &filePath="")
GAMSSet getSet(const std::string &name)
GAMSParameter getParameter(const std::string &name)
double value()
void setValue(const double val)
GAMSParameterRecord firstRecord(const std::vector< std::string > &slice)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
void setSystemDirectory(const std::string &systemDir)