Loading...
Searching...
No Matches
transportGDX.cpp
Go to the documentation of this file.
1
25#include "gams.h"
26#include <iostream>
27#include <fstream>
28#include <vector>
29#include <map>
30#include <cstdlib>
31
32using namespace gams;
33using namespace std;
34
35
42int main(int argc, char* argv[])
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
78 GAMSSet i = db.addSet("i", 1, "canning plants");
79 for (string p: plants)
80 i.addRecord(p);
81
82 // add 1-dimensional set 'j' with explanatory text 'markets' to the GAMSDatabase
83 GAMSSet j = db.addSet("j", 1, "markets");
84 for (string m: markets)
85 j.addRecord(m);
86
87 // add parameter 'a' with domain 'i'
88 GAMSParameter a = db.addParameter("a", "capacity of plant i in cases", i);
89 for (string p: plants)
90 a.addRecord(p).setValue(capacity[p]);
91
92 // add parameter 'b' with domain 'j'
93 GAMSParameter b = db.addParameter("b", "demand at market j in cases", j);
94 for (string m: markets)
95 b.addRecord(m).setValue(demand[m]);
96
97 // add parameter 'd' with domains 'i' and 'j'
98 GAMSParameter d = db.addParameter("d", "distance in thousands of miles", i, j);
99 for (auto t : distance)
100 d.addRecord(get<0>(t.first), get<1>(t.first)).setValue(t.second);
101
102 // add scalar 'f'
103 GAMSParameter f = db.addParameter("f", "freight in dollars per case per thousand miles");
104 f.addRecord().setValue(90);
105
106 // export the GAMSDatabase to a GDX file with name 'data.gdx' located in the 'workingDirectory' of the GAMSWorkspace
107 db.doExport("data.gdx");
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
116 GAMSDatabase db2 = ws.addDatabaseFromGDX("data.gdx");
117
118 // read data from symbols into C++ data structures
119 vector<string> iNew;
120 for(GAMSSetRecord rec : db2.getSet("i"))
121 iNew.push_back(rec.key(0));
122
123 vector<string> jNew;
124 for(GAMSSetRecord rec : db2.getSet("j"))
125 jNew.push_back(rec.key(0));
126
127 map<string, double> aNew;
128 for(GAMSParameterRecord rec : db2.getParameter("a"))
129 aNew[rec.key(0)] = rec.value();
130
131 map<string, double> bNew;
132 for(GAMSParameterRecord rec : db2.getParameter("b"))
133 bNew[rec.key(0)] = rec.value();
134
135 map<tuple<string, string>, double> dNew;
136 for(GAMSParameterRecord rec : db2.getParameter("d"))
137 dNew[make_tuple(rec.key(0), rec.key(1))] = rec.value();
138
139 double fNew = db2.getParameter("f").firstRecord().value();
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
159 } catch (GAMSException &ex) {
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)
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(std::string systemDir)