Loading...
Searching...
No Matches
transportGDX.cpp
Go to the documentation of this file.
1/*
2 *
3 * GAMS - General Algebraic Modeling System C++ API
4 *
5 * Copyright (c) 2017-2023 GAMS Software GmbH <support@gams.com>
6 * Copyright (c) 2017-2023 GAMS Development Corp. <support@gams.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26#include "gams.h"
27#include <iostream>
28#include <fstream>
29#include <vector>
30#include <map>
31#include <cstdlib>
32
33using namespace gams;
34using namespace std;
35
36
43int main(int argc, char* argv[])
44{
45 cout << "---------- Transport GDX --------------" << endl;
46
47 try {
48 GAMSWorkspaceInfo wsInfo;
49 if (argc > 1)
50 wsInfo.setSystemDirectory(argv[1]);
51 GAMSWorkspace ws(wsInfo);
52
53 // define some data by using C++ data structures
54 vector<string> plants = {
55 "Seattle", "San-Diego"
56 };
57 vector<string> markets = {
58 "New-York", "Chicago", "Topeka"
59 };
60 map<string, double> capacity = {
61 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
62 };
63 map<string, double> demand = {
64 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
65 };
66 map<tuple<string, string>, double> distance = {
67 { make_tuple("Seattle", "New-York"), 2.5 },
68 { make_tuple("Seattle", "Chicago"), 1.7 },
69 { make_tuple("Seattle", "Topeka"), 1.8 },
70 { make_tuple("San-Diego", "New-York"), 2.5 },
71 { make_tuple("San-Diego", "Chicago"), 1.8 },
72 { make_tuple("San-Diego", "Topeka"), 1.4 }
73 };
74
75 // create new GAMSDatabase instance
76 GAMSDatabase db = ws.addDatabase();
77
78 // add 1-dimensional set 'i' with explanatory text 'canning plants' to the GAMSDatabase
79 GAMSSet i = db.addSet("i", 1, "canning plants");
80 for (string p: plants)
81 i.addRecord(p);
82
83 // add 1-dimensional set 'j' with explanatory text 'markets' to the GAMSDatabase
84 GAMSSet j = db.addSet("j", 1, "markets");
85 for (string m: markets)
86 j.addRecord(m);
87
88 // add parameter 'a' with domain 'i'
89 GAMSParameter a = db.addParameter("a", "capacity of plant i in cases", i);
90 for (string p: plants)
91 a.addRecord(p).setValue(capacity[p]);
92
93 // add parameter 'b' with domain 'j'
94 GAMSParameter b = db.addParameter("b", "demand at market j in cases", j);
95 for (string m: markets)
96 b.addRecord(m).setValue(demand[m]);
97
98 // add parameter 'd' with domains 'i' and 'j'
99 GAMSParameter d = db.addParameter("d", "distance in thousands of miles", i, j);
100 for (auto t : distance)
101 d.addRecord(get<0>(t.first), get<1>(t.first)).setValue(t.second);
102
103 // add scalar 'f'
104 GAMSParameter f = db.addParameter("f", "freight in dollars per case per thousand miles");
105 f.addRecord().setValue(90);
106
107 // export the GAMSDatabase to a GDX file with name 'data.gdx' located in the 'workingDirectory' of the GAMSWorkspace
108 db.doExport("data.gdx");
109
110 cout << "Content of GDX file 'data.gdx':";
111 string command = "gdxdump " + ws.workingDirectory() + cPathSep + "data.gdx";
112 int ret = system(command.c_str());
113 if (ret)
114 cerr << "system(" << command.c_str() << ") returned " << ret << endl;
115
116 // add a new GAMSDatabase and initialize it from the GDX file just created
117 GAMSDatabase db2 = ws.addDatabaseFromGDX("data.gdx");
118
119 // read data from symbols into C++ data structures
120 vector<string> iNew;
121 for(GAMSSetRecord rec : db2.getSet("i"))
122 iNew.push_back(rec.key(0));
123
124 vector<string> jNew;
125 for(GAMSSetRecord rec : db2.getSet("j"))
126 jNew.push_back(rec.key(0));
127
128 map<string, double> aNew;
129 for(GAMSParameterRecord rec : db2.getParameter("a"))
130 aNew[rec.key(0)] = rec.value();
131
132 map<string, double> bNew;
133 for(GAMSParameterRecord rec : db2.getParameter("b"))
134 bNew[rec.key(0)] = rec.value();
135
136 map<tuple<string, string>, double> dNew;
137 for(GAMSParameterRecord rec : db2.getParameter("d"))
138 dNew[make_tuple(rec.key(0), rec.key(1))] = rec.value();
139
140 double fNew = db2.getParameter("f").firstRecord().value();
141
142 cout << "i:" << endl;
143 for(string s : iNew)
144 cout << " " << s << endl;
145 cout << "j:" << endl;
146 for(string s : jNew)
147 cout << " " << s << endl;
148 cout << "a:" << endl;
149 for (auto rec : aNew)
150 cout << " " << rec.first << " : " << rec.second << endl;
151 cout << "b:" << endl;
152 for (auto rec : bNew)
153 cout << " " << rec.first << " : " << rec.second << endl;
154 cout << "d:" << endl;
155 for (auto rec : dNew)
156 cout << " " << get<0>(rec.first) << ", " << get<1>(rec.first) << " : " << rec.second << endl;
157 cout << "f:" << endl;
158 cout << " " << fNew;
159
160 } catch (GAMSException &ex) {
161 cout << "GAMSException occured: " << ex.what() << endl;
162 } catch (exception &ex) {
163 cout << ex.what() << endl;
164 }
165
166 return 0;
167}
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)