-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsontogdx.cpp
executable file
·348 lines (304 loc) · 10.8 KB
/
jsontogdx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// Created by andre on 13.09.17.
//
#include "jsontogdx.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include "utils.hpp"
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace json11;
using namespace gams;
// FIXME: Also handle 2-dimensional non-contiguous sets (e.g. not indices != 1..n) correctly
static map<string, vector<string>> setCache;
void jsontogdx::addSet(GAMSDatabase &db, const string &name, int from, int to, const string &description) {
auto s = db.addSet(name, description.empty() ? name : description);
vector<string> elems;
for(int j=from; j<=to; j++) {
string ename = name + to_string(j);
s.addRecord(ename);
elems.push_back(ename);
}
setCache.insert(make_pair(name, elems));
}
void jsontogdx::addSetsFromJson(GAMSDatabase &db, const Json &sets) {
auto add1DSetWithExplicitElements = [](GAMSSet &myset, const string &name, const Json::array &items) {
vector<string> elems;
for(const auto &entry : items) {
const string &ename = entry.string_value();
myset.addRecord(ename).setText("yes");
elems.push_back(ename);
}
setCache.insert(make_pair(name, elems));
};
for(const auto &s : sets.array_items()) {
// {"name":"i", "from": 1, "to": 10} => set i /i1*i10/
if(s["from"].is_number() && s["to"].is_number()) {
string descr = s["description"].is_string() ? s["description"].string_value() : "";
addSet(db, s["name"].string_value(), s["from"].int_value(), s["to"].int_value(), descr);
} else if(s["indices"].is_string()) {
string name = s["name"].string_value();
auto myset = db.addSet(name, 1);
add1DSetWithExplicitElements(myset, name, s["values"].array_items());
} else if(s["indices"].is_array()) {
if(s["indices"].array_items().size() == 2) {
auto myset = db.addSet(s["name"].string_value(), 2);
for(const auto &row : s["values"].array_items()) {
auto pair = row.array_items();
myset.addRecord(pair[0].string_value(), pair[1].string_value()).setText("yes");
}
}
} else if(s["elements"].is_array()) {
int dim = s["dim"].is_number() ? (int)s["dim"].number_value() : 1;
auto myset = db.addSet(s["name"].string_value(), dim);
switch(dim) {
case 1:
add1DSetWithExplicitElements(myset, s["name"].string_value(), s["elements"].array_items());
break;
case 2:
for(const auto &pairstr : s["elements"].array_items()) {
vector<string> parts;
boost::split(parts, pairstr.string_value(), boost::is_any_of(","));
myset.addRecord(parts[0], parts[1]).setText("yes");
}
break;
default:
break;
}
}
}
}
void jsontogdx::addParametersFromJson(gams::GAMSDatabase &db, const json11::Json ¶ms) {
auto add1DParameter = [](GAMSParameter &p, const string &index, const Json::array &items) {
int cctr = 0;
for(const auto &varrentry : items) {
string keyname = setCache.find(index) != setCache.end() ? setCache[index][cctr] : index + to_string(cctr+1);
p.addRecord(keyname).setValue(varrentry.number_value());
cctr++;
}
};
auto add2DParameter = [](GAMSParameter &p, const vector<string> &indices, const Json::array &items) {
int cctr = 0, rctr = 0;
for(const auto &row : items) {
cctr = 0;
for(const auto &entry : row.array_items()) {
p.addRecord(indices[0]+to_string(rctr+1), indices[1]+to_string(cctr+1)).setValue(entry.number_value());
cctr++;
}
rctr++;
}
};
for(const auto ¶m : params.array_items()) {
auto name = param["name"].string_value();
vector<string> indices;
if(param["indices"].is_string()) indices.push_back(param["indices"].string_value());
else if(param["indices"].is_array()) {
for(const auto &index : param["indices"].array_items()) {
indices.push_back(index.string_value());
}
} else continue;
auto dim = static_cast<int>(indices.size());
auto p = db.addParameter(name, dim);
switch(dim) {
default:
case 1:
add1DParameter(p, indices[0], param["values"].array_items());
break;
case 2:
add2DParameter(p, indices, param["values"].array_items());
break;
}
}
}
void jsontogdx::addScalarsFromJson(gams::GAMSDatabase &db, const json11::Json &scalars) {
for(const auto &scalar : scalars.array_items()) {
auto param = db.addParameter(scalar["name"].string_value(), 0);
param.addRecord().setValue(scalar["value"].number_value());
}
}
void jsontogdx::addDataFromJson(GAMSDatabase &db, const Json &obj) {
setCache.clear();
addSetsFromJson(db, obj["sets"]);
addParametersFromJson(db, obj["parameters"]);
addScalarsFromJson(db, obj["scalars"]);
}
// hidden
static string _sysDir, _workDir, _optionsJsonStr = "";
gams::GAMSWorkspace jsontogdx::writeJsonStrToGdxFile(const std::string &jss, const std::string &gdxFilename) {
Json obj = utils::loadJsonFromString(jss);
return writeJsonObjToGdxFile(obj, gdxFilename);
}
gams::GAMSWorkspace jsontogdx::writeJsonObjToGdxFile(const json11::Json &obj, const std::string &gdxFilename) {
gams::GAMSWorkspace ws(_workDir, _sysDir);
auto db = ws.addDatabase("MyDatabase");
jsontogdx::addDataFromJson(db, obj);
db.doExport(gdxFilename);
return ws;
}
static string fixComma(double v) {
return boost::replace_first_copy(to_string(v), ",", ".");
}
// FFI accessible
void writeJsonStrToGdxFile(const char *jsonStr, const char *gdxFilename) {
string jss(jsonStr), gdxfn(gdxFilename);
jsontogdx::writeJsonStrToGdxFile(jss, gdxfn);
}
void setGAMSDirectories(const char *systemDirectory, const char *workingDirectory) {
_sysDir = systemDirectory;
_workDir = workingDirectory;
}
void setGAMSOptions(const char *jsonStr) {
_optionsJsonStr = jsonStr;
}
json11::Json gamsVarToJsonObject(GAMSVariable &v) {
if(v.dim() == 0) {
return json11::Json::object { {"name", v.name() }, {"level", fixComma(v.firstRecord().level()) } };
} else if(v.dim() == 1) {
vector<json11::Json> levels;
for(auto rec : v) {
levels.push_back(Json::object { {rec.key(0), fixComma(rec.level()) } });
}
return json11::Json::object { {"name", v.name() }, {"levels", levels } };
} else if(v.dim() == 2) {
vector<json11::Json> levels;
for(auto rec : v) {
levels.push_back(Json::object { {rec.key(0)+","+rec.key(1), fixComma(rec.level()) } });
}
return json11::Json::object { {"name", v.name() }, {"levels", levels } };
}
return json11::Json::object {};
}
json11::Json extractResultsFromOutDatabase(GAMSDatabase &outDB) {
vector<json11::Json> jsvars;
for(auto sym : outDB) {
if(sym.type() == GAMSEnum::SymTypeVar) {
auto v = (GAMSVariable)sym;
jsvars.push_back(gamsVarToJsonObject(v));
}
}
json11::Json oobj = json11::Json::object{{"results", jsvars}};
return oobj;
}
void transferOptionsFromJson(GAMSOptions &options, const Json &obj) {
map<string, void (GAMSOptions::*)(double)> dblSetters = {
{"OptCR", &GAMSOptions::setOptCR},
{"OptCA", &GAMSOptions::setOptCA},
{"ResLim", &GAMSOptions::setResLim},
};
for(auto pair : dblSetters) {
if(obj[pair.first].is_number()) {
(options.*pair.second)(obj[pair.first].number_value());
}
}
if(obj["Solver"].is_array()) {
string modelType = obj["Solver"].array_items()[0].string_value();
string solverName = obj["Solver"].array_items()[1].string_value();
options.setSolver(modelType, solverName);
}
}
const char *solveModelWithDataJsonStr(const char *modelCode, const char *jsonStr) {
if(_sysDir.empty() || _workDir.empty()) return "";
const string GDX_FILENAME = "data";
string mc(modelCode), jss(jsonStr);
auto ws = jsontogdx::writeJsonStrToGdxFile(jss, GDX_FILENAME+".gdx");
auto job = ws.addJobFromString(mc);
auto options = ws.addOptions();
options.setDefine("gdxincname", GDX_FILENAME);
options.setOptCR(0.0);
if(!_optionsJsonStr.empty()) {
transferOptionsFromJson(options, utils::loadJsonFromString(_optionsJsonStr));
}
job.run(options);
auto outDB = job.outDB();
string ostr = extractResultsFromOutDatabase(outDB).dump();
return strdup(ostr.c_str());
}
Json gamsSetToJsonObject(GAMSSet &set) {
vector<Json> elems;
int dim = set.dim();
for (auto &rec : set) {
if(dim == 1) {
elems.push_back(rec.key(0));
} else if(dim == 2) {
vector<Json> keys;
for(auto skey : rec.keys()) {
keys.push_back(skey);
}
elems.push_back(keys);
}
}
return Json::object {
{"name", set.name()},
{"dim", dim},
{"elements", elems }
};
}
Json gamsParameterToJsonObject(GAMSParameter ¶m) {
int dim = param.dim();
if(dim == 0) {
return Json::object {
{"name", param.name()},
{"dim", dim},
{"value", param.firstRecord().value()}
};
}
vector<Json> recs;
for (auto &rec : param) {
vector<Json> kvs;
if(dim == 1) {
kvs = { rec.key(0), rec.value()};
recs.push_back(kvs);
} else if(dim == 2) {
kvs = { rec.key(0), rec.key(1), rec.value() };
recs.push_back(kvs);
}
}
return Json::object {
{"name", param.name()},
{"dim", dim},
{"records", recs}
};
}
json11::Json jsontogdx::readJsonObjFromGdxFile(const std::string &gdxFilename) {
gams::GAMSWorkspace ws(_workDir, _sysDir);
auto db = ws.addDatabaseFromGDX(gdxFilename);
GAMSSet myset;
GAMSParameter myparam;
GAMSVariable myvar;
vector<Json> sets, params, scalars, vars;
vector<Json> *oarr = nullptr;
for(auto sym : db) {
switch(sym.type()) {
case GAMSEnum::SymTypeSet:
myset = (GAMSSet)sym;
sets.push_back(gamsSetToJsonObject(myset));
break;
case GAMSEnum::SymTypePar:
myparam = (GAMSParameter)sym;
oarr = myparam.dim() == 0 ? &scalars : ¶ms;
oarr->push_back(gamsParameterToJsonObject(myparam));
break;
case GAMSEnum::SymTypeVar:
myvar = (GAMSVariable)sym;
vars.push_back(gamsVarToJsonObject(myvar));
break;
default:
break;
}
}
Json obj = Json::object { {"sets", sets}, {"parameters", params}, {"scalars", scalars}, {"variables", vars} };
return obj;
}
string jsontogdx::readJsonStrFromGdxFile(const string &gdxFilename) {
return readJsonObjFromGdxFile(gdxFilename).dump();
}
const char *readJsonStrFromGdxFile(const char *gdxFilename) {
string ostr = jsontogdx::readJsonStrFromGdxFile(gdxFilename);
return strdup(ostr.c_str());
}
const char *solveModelWithDataJsonStrAndOptions(const char *modelCode, const char *jsonStr, const char *optionsJsonStr) {
setGAMSOptions(optionsJsonStr);
return solveModelWithDataJsonStr(modelCode, jsonStr);
}