Loading...
Searching...
No Matches
transport3.cpp
Go to the documentation of this file.
1
25#include "gams.h"
26#include <iostream>
27#include <fstream>
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 "Parameters \n"
39 " \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}
56
59{
60 return "Sets \n"
61 " i canning plants \n"
62 " j markets \n"
63 " \n"
64 "Parameters \n"
65 " a(i) capacity of plant i in cases \n"
66 " b(j) demand at market j in cases \n"
67 " d(i,j) distance in thousands of miles \n"
68 "Scalar f freight in dollars per case per thousand miles; \n"
69 " \n"
70 "$if not set gdxincname $abort 'no include file name for data file provided'\n"
71 "$gdxin %gdxincname% \n"
72 "$load i j a b d f \n"
73 "$gdxin \n"
74 " \n"
75 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
76 " \n"
77 " c(i,j) = f * d(i,j) / 1000 ; \n"
78 " \n"
79 " Variables \n"
80 " x(i,j) shipment quantities in cases \n"
81 " z total transportation costs in thousands of dollars ; \n"
82 " \n"
83 " Positive Variable x ; \n"
84 " \n"
85 " Equations \n"
86 " \n"
87 " cost define objective function \n"
88 " supply(i) observe supply limit at plant i \n"
89 " demand(j) satisfy demand at market j ; \n"
90 " \n"
91 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
92 " \n"
93 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
94 " \n"
95 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
96 " \n"
97 " Model transport /all/ ; \n"
98 " \n"
99 " Solve transport using lp minimizing z ; \n"
100 " \n"
101 "Display x.l, x.m ; \n";
102}
103
111int main(int argc, char* argv[])
112{
113 cout << "---------- Transport 3 --------------" << endl;
114
115 try {
116 GAMSWorkspaceInfo wsInfo;
117 if (argc > 1)
118 wsInfo.setSystemDirectory(argv[1]);
119 GAMSWorkspace ws(wsInfo);
120
121 // data from a string with GAMS syntax with explicit export to GDX file
122 GAMSJob t3 = ws.addJobFromString(getDataText());
123 t3.run();
124 //TODO: change doExport to export?
125 t3.outDB().doExport(ws.workingDirectory() + cPathSep + "tdata.gdx");
126
127 // run a job using an instance of GAMSOptions that defines the data include file
128 t3 = ws.addJobFromString(getModelText());
129 GAMSOptions opt = ws.addOptions();
130 opt.setDefine("gdxincname", "tdata");
131 opt.setAllModelTypes("xpress");
132 t3.run(opt);
133 for (GAMSVariableRecord rec : t3.outDB().getVariable("x"))
134 cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
135 << rec.marginal() << endl;
136
137 // same but with implicit database communication
138 GAMSJob t3a = ws.addJobFromString(getDataText());
139 GAMSJob t3b = ws.addJobFromString(getModelText());
140 t3a.run();
141 opt.setDefine("gdxincname", t3a.outDB().name());
142
143 t3b.run(opt, t3a.outDB());
144 for (GAMSVariableRecord rec : t3.outDB().getVariable("x"))
145 cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
146 << rec.marginal() << endl;
147
148 } catch (GAMSException &ex) {
149 cout << "GAMSException occured: " << ex.what() << endl;
150 } catch (exception &ex) {
151 cout << ex.what() << endl;
152 }
153
154 return 0;
155}
void doExport(const std::string &filePath="")
std::string name()
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
void setAllModelTypes(const std::string &solver)
void setDefine(const std::string &key, const std::string &value)
void setSystemDirectory(std::string systemDir)
string getDataText()
Get data as string.
Definition: transport3.cpp:33
string getModelText()
Get model as string.
Definition: transport3.cpp:58