Loading...
Searching...
No Matches
transport7.cpp
Go to the documentation of this file.
1
25#include "gams.h"
26#include <vector>
27#include <iostream>
28
29using namespace gams;
30using namespace std;
31
34{
35 return " Sets \n"
36 " i canning plants / seattle, san-diego / \n"
37 " j markets / new-york, chicago, topeka / ; \n"
38 " \n"
39 "Parameters \n"
40 " a(i) capacity of plant i in cases \n"
41 " / seattle 350 \n"
42 " san-diego 600 / \n"
43 " \n"
44 " b(j) demand at market j in cases \n"
45 " / new-york 325 \n"
46 " chicago 300 \n"
47 " topeka 275 / ; \n"
48 " \n"
49 "Table d(i,j) distance in thousands of miles \n"
50 " new-york chicago topeka \n"
51 " seattle 2.5 1.7 1.8 \n"
52 " san-diego 2.5 1.8 1.4 ; \n"
53 " \n"
54 "Scalar f freight in dollars per case per thousand miles /90/ ;\n"
55 "Scalar bmult demand multiplier /1/; \n"
56 " \n"
57 "Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
58 " \n"
59 " c(i,j) = f * d(i,j) / 1000 ; \n"
60 " \n"
61 "Variables \n"
62 " x(i,j) shipment quantities in cases \n"
63 " z total transportation costs in thousands of dollars ; \n"
64 " \n"
65 "Positive Variable x ; \n"
66 " \n"
67 "Equations \n"
68 " cost define objective function \n"
69 " supply(i) observe supply limit at plant i \n"
70 " demand(j) satisfy demand at market j ; \n"
71 " \n"
72 "cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
73 " \n"
74 "supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
75 " \n"
76 "demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; \n"
77 " \n"
78 "Model transport /all/ ; \n";
79}
80
88int main(int argc, char* argv[])
89{
90 cout << "---------- Transport 7 --------------" << endl;
91
92 try {
93 GAMSWorkspaceInfo wsInfo;
94 if (argc > 1)
95 wsInfo.setSystemDirectory(argv[1]);
96 GAMSWorkspace ws(wsInfo);
97 GAMSCheckpoint cp = ws.addCheckpoint();
98
99 // initialize a GAMSCheckpoint by running a GAMSJob
100 GAMSJob t7 = ws.addJobFromString(getModelText());
101 t7.run(cp);
102
103 // create a GAMSModelInstance and solve it multiple times with different scalar bmult
105 GAMSParameter bmult = mi.syncDb().addParameter("bmult", 0, "demand multiplier");
106 GAMSOptions opt = ws.addOptions();
107 opt.setAllModelTypes("cplex");
108
109 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
110 mi.instantiate("transport use lp min z", opt, GAMSModifier(bmult));
111
112 bmult.addRecord().setValue(1.0);
113 vector<double> bmultlist = { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
114
115 for (double b : bmultlist) {
116 bmult.firstRecord().setValue(b);
117 mi.solve();
118 cout << "Scenario bmult=" << b << ":" << endl;
119 cout << " Modelstatus: " << mi.modelStatusAsString() << endl;
120 cout << " Solvestatus: " << mi.solveStatusAsString() << endl;
121 cout << " Obj: " << mi.syncDb().getVariable("z").findRecord().level() << endl;
122 }
123
124 // create a GAMSModelInstance and solve it with single links in the network blocked
125 mi = cp.addModelInstance();
126
127 GAMSVariable x = mi.syncDb().addVariable("x", 2, GAMSEnum::VarType::Positive, "");
128 GAMSParameter xup = mi.syncDb().addParameter("xup", 2, "upper bound on x");
129
130 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare upper bound of X mutable
131 mi.instantiate("transport use lp min z", GAMSModifier (x, GAMSEnum::SymbolUpdateAction::Upper, xup));
132
133 for (GAMSSetRecord i : t7.outDB().getSet("i")) {
134 for (GAMSSetRecord j : t7.outDB().getSet("j")) {
135 xup.clear();
136 xup.addRecord(i.key(0), j.key(0)).setValue(0);
137 mi.solve();
138 cout << "Scenario link blocked=" << i.key(0) << "-" << j.key(0) << endl;
139 cout << " Modelstatus: " << mi.modelStatusAsString() << endl;
140 cout << " Solvestatus: " << mi.solveStatusAsString() << endl;
141 cout << " Obj: " << mi.syncDb().getVariable("z").findRecord().level() << endl;
142 }
143 }
144
145 } catch (GAMSException &ex) {
146 cout << "GAMSException occured: " << ex.what() << endl;
147 } catch (exception &ex) {
148 cout << ex.what() << endl;
149 }
150
151 return 0;
152}
GAMSModelInstance addModelInstance(const std::string &modelInstanceName="")
GAMSVariable addVariable(const std::string &name, const int dimension, const GAMSEnum::VarType varType, const std::string &explanatoryText="")
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
GAMSSet getSet(const std::string &name)
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
std::string solveStatusAsString()
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={ })
std::string modelStatusAsString()
GAMSDatabase syncDb()
void setAllModelTypes(const std::string &solver)
void setValue(const double val)
GAMSParameterRecord firstRecord(const std::vector< std::string > &slice)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSVariableRecord findRecord(const std::vector< std::string > &keys)
void setSystemDirectory(std::string systemDir)
string getModelText()
Get model as string.
Definition: transport7.cpp:33