37 " i canning plants / seattle, san-diego / \n"
38 " j markets / new-york, chicago, topeka / ; \n"
42 " a(i) capacity of plant i in cases \n"
46 " b(j) demand at market j in cases \n"
51 " Table d(i,j) distance in thousands of miles \n"
52 " new-york chicago topeka \n"
53 " seattle 2.5 1.7 1.8 \n"
54 " san-diego 2.5 1.8 1.4 ; \n"
56 " Scalar f freight in dollars per case per thousand miles /90/ ; \n"
57 " Scalar bmult demand multiplier /1/; \n"
59 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
61 " c(i,j) = f * d(i,j) / 1000 ; \n"
64 " x(i,j) shipment quantities in cases \n"
65 " z total transportation costs in thousands of dollars ; \n"
67 " Positive Variable x ; \n"
70 " cost define objective function \n"
71 " supply(i) observe supply limit at plant i \n"
72 " demand(j) satisfy demand at market j ; \n"
74 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
76 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
78 " demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; \n"
80 " Model transport /all/ ; \n";
87 transform(lstr.begin(), lstr.end(), lstr.begin(), ::tolower);
95 , std::ostream* output =
nullptr)
97 vector< tuple<GAMSModifier, GAMSParameter> > modifierList;
102 vector<string> strs {
" ",
"scenario",
" " };
108 if (
toLower(rec.key(1)) ==
"scenario")
110 if (
toLower(rec.key(1)) ==
"param") {
113 throw GAMSException(
"Dimension of " + rec.key(2) +
" too small");
115 dict.database().getParameter(rec.key(2)));
116 modifierList.push_back(tuple);
118 }
else if ((rec.key(1) ==
"lower") || (
toLower(rec.key(1)) ==
"upper") || (
toLower(rec.key(1)) ==
"fixed")) {
119 int modifierDim = dict.database().getParameter(rec.key(2)).dim() - scenSymbol.
dim();
121 throw GAMSException(
"Dimension of " + rec.key(2) +
" too small");
130 if (
toLower(rec.key(1)) ==
"lower") {
133 dict.database().getParameter(rec.key(2)));
134 modifierList.push_back(tuple);
136 }
else if (
toLower(rec.key(1)) ==
"upper") {
139 dict.database().getParameter(rec.key(2)));
140 modifierList.push_back(tuple);
145 dict.database().getParameter(rec.key(2)));
146 modifierList.push_back(tuple);
148 }
else if ((
toLower(rec.key(1)) ==
"level") || (
toLower(rec.key(1)) ==
"marginal")) {
150 dict.database().getParameter(rec.key(2));
152 throw GAMSException(
"Cannot handle UpdateAction " + rec.key(1));
156 vector<GAMSModifier> mL;
157 for (
auto tuple : modifierList)
158 mL.push_back(get<0>(tuple));
164 vector<tuple<GAMSSymbol, GAMSParameter, string>> outList;
168 for (
auto tup : modifierList)
173 if (!get<0>(tup).dataSymbol().isValid())
174 p = get<0>(tup).gamsSymbol();
176 p = get<0>(tup).dataSymbol();
182 vector<string> filter(pscen.
dim());
183 for (
int i = 0; i < scenSymbol.
dim(); i++)
184 filter[i] = s.key(i);
185 for (
int i = scenSymbol.
dim(); i < pscen.
dim(); i++)
195 vector<string> myKeys(p.
dim());
196 for (
int i = 0; i < p.
dim(); i++)
197 myKeys[i] = rec.
key(scenSymbol.
dim()+i);
202 mi.
solve(GAMSEnum::SymbolUpdateType::BaseCase, *output, miOpt);
204 if (outList.size() == 0)
206 if ((
toLower(rec.key(1)) ==
"level") || (
toLower(rec.key(1)) ==
"marginal")) {
210 outList.push_back(tuple);
213 for (tuple<GAMSSymbol, GAMSParameter, string> tup : outList) {
215 vector<string> myKeys;
216 for (
int i = 0; i < scenSymbol.
dim(); i++)
217 myKeys.push_back(s.key(i));
221 for (
int i = 0; i < static_cast<int>(rec.keys().size()); i++)
222 myKeys[scenSymbol.
dim() + i] = s.key(i);
223 get<1>(tup).addRecord(myKeys).setValue(rec.level());
227 for (
int i = 0; i < static_cast<int>(rec.keys().size()); i++)
228 myKeys[scenSymbol.
dim() + i] = s.key(i);
229 get<1>(tup).addRecord(myKeys).setValue(rec.level());
233 for (
int i = 0; i < static_cast<int>(rec.keys().size()); i++)
234 myKeys[scenSymbol.
dim() + i] = s.key(i);
235 get<1>(tup).addRecord(myKeys).setValue(rec.marginal());
239 for (
int i = 0; i < static_cast<int>(rec.keys().size()); i++)
240 myKeys[scenSymbol.
dim() + i] = s.key(i);
241 get<1>(tup).addRecord(myKeys).setValue(rec.marginal());
253int main(
int argc,
char* argv[])
255 cout <<
"---------- Transport 12 --------------" << endl;
271 double bmultlist[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
280 for (
double b : bmultlist) {
291 GUSSCall(dict, mi,
"transport use lp min z");
294 cout << rec.key(0) <<
" obj: " << rec.value() << endl;
303 for (
int j = 0; j < 4; j++) {
315 GUSSCall(dict2, mi2,
"transport use lp min z");
318 cout << rec.key(0) <<
" obj: " << rec.value() << endl;
321 cout <<
"GAMSException occured: " << ex.what() << endl;
322 }
catch (exception &ex) {
323 cout << ex.what() << endl;
GAMSModelInstance addModelInstance(const std::string &modelInstanceName="")
GAMSVariable addVariable(const std::string &name, const int dimension, const GAMSEnum::VarType varType, const std::string &explanatoryText="")
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="")
GAMSSymbol getSymbol(const std::string &name)
GAMSSet getSet(const std::string &name)
GAMSParameter getParameter(const std::string &name)
GAMSVariable getVariable(const std::string &name)
void solve(GAMSEnum::SymbolUpdateType updateType, std::ostream &output, GAMSModelInstanceOpt miOpt)
void instantiate(const std::string &modelDefinition, const gams::GAMSOptions &options, const std::vector< gams::GAMSModifier > &modifiers={ })
void setValue(const double val)
GAMSParameterRecord firstRecord(const std::vector< std::string > &slice)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord firstRecord(const std::vector< std::string > &slice)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
std::string key(int index)
std::string & name() const
gams::GAMSDatabase & database() const
GAMSEnum::SymbolType type() const
void setSystemDirectory(std::string systemDir)
void GUSSCall(GAMSSet dict, GAMSModelInstance mi, string solveStatement, gams::GAMSOptions *opt=nullptr, gams::GAMSModelInstanceOpt miOpt=gams::GAMSModelInstanceOpt(), std::ostream *output=nullptr)
Using GUSS to solve the model in different scenarios.
string getModelText()
Get model as string.
string toLower(const string &str)
Convert string to lower case.