Loading...
Searching...
No Matches
transport4.cpp
Go to the documentation of this file.
1
25#include "gams.h"
26#include <iostream>
27#include <fstream>
28#include <vector>
29#include <map>
30
31using namespace gams;
32using namespace std;
33
36{
37 return "Sets \n"
38 " i canning plants \n"
39 " j markets \n"
40 " \n"
41 "Parameters \n"
42 " a(i) capacity of plant i in cases \n"
43 " b(j) demand at market j in cases \n"
44 " d(i,j) distance in thousands of miles \n"
45 "Scalar f freight in dollars per case per thousand miles; \n"
46 " \n"
47 "$if not set gdxincname $abort 'no include file name for data file provided'\n"
48 "$gdxin %gdxincname% \n"
49 "$load i j a b d f \n"
50 "$gdxin \n"
51 " \n"
52 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
53 " \n"
54 " c(i,j) = f * d(i,j) / 1000 ; \n"
55 " \n"
56 " Variables \n"
57 " x(i,j) shipment quantities in cases \n"
58 " z total transportation costs in thousands of dollars ; \n"
59 " \n"
60 " Positive Variable x ; \n"
61 " \n"
62 " Equations \n"
63 " \n"
64 " cost define objective function \n"
65 " supply(i) observe supply limit at plant i \n"
66 " demand(j) satisfy demand at market j ; \n"
67 " \n"
68 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
69 " \n"
70 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
71 " \n"
72 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
73 " \n"
74 " Model transport /all/ ; \n"
75 " \n"
76 " Solve transport using lp minimizing z ; \n"
77 " \n"
78 "Display x.l, x.m ; \n";
79}
80
87int main(int argc, char* argv[])
88{
89 cout << "---------- Transport 4 --------------" << endl;
90
91 try {
92 GAMSWorkspaceInfo wsInfo;
93 if (argc > 1)
94 wsInfo.setSystemDirectory(argv[1]);
95 GAMSWorkspace ws(wsInfo);
96
97 // define some data by using C++ data structures
98 vector<string> plants = {
99 "Seattle", "San-Diego"
100 };
101 vector<string> markets = {
102 "New-York", "Chicago", "Topeka"
103 };
104 map<string, double> capacity = {
105 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
106 };
107 map<string, double> demand = {
108 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
109 };
110 map<tuple<string, string>, double> distance = {
111 { make_tuple("Seattle", "New-York"), 2.5 },
112 { make_tuple("Seattle", "Chicago"), 1.7 },
113 { make_tuple("Seattle", "Topeka"), 1.8 },
114 { make_tuple("San-Diego", "New-York"), 2.5 },
115 { make_tuple("San-Diego", "Chicago"), 1.8 },
116 { make_tuple("San-Diego", "Topeka"), 1.4 }
117 };
118
119 // prepare a GAMSDatabase with data from the C++ data structures
120 GAMSDatabase db = ws.addDatabase();
121
122 GAMSSet i = db.addSet("i", 1, "canning plants");
123 for (string p: plants)
124 i.addRecord(p);
125
126 GAMSSet j = db.addSet("j", 1, "markets");
127 for (string m: markets)
128 j.addRecord(m);
129
130 GAMSParameter a = db.addParameter("a", "capacity of plant i in cases", i);
131 for (string p: plants)
132 a.addRecord(p).setValue(capacity[p]);
133
134 GAMSParameter b = db.addParameter("b", "demand at market j in cases", j);
135 for (string m: markets)
136 b.addRecord(m).setValue(demand[m]);
137
138 GAMSParameter d = db.addParameter("d", "distance in thousands of miles", i, j);
139 for (auto t : distance)
140 d.addRecord(get<0>(t.first), get<1>(t.first)).setValue(t.second);
141
142 GAMSParameter f = db.addParameter("f", "freight in dollars per case per thousand miles");
143 f.addRecord().setValue(90);
144
145 // run a job using data from the created GAMSDatabase
146 GAMSJob t4 = ws.addJobFromString(getModelText());
147 GAMSOptions opt = ws.addOptions();
148 opt.setDefine("gdxincname", db.name());
149 opt.setAllModelTypes("xpress");
150 t4.run(opt, db);
151
152 for (GAMSVariableRecord rec : t4.outDB().getVariable("x"))
153 cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
154 << rec.marginal() << endl;
155
156 } catch (GAMSException &ex) {
157 cout << "GAMSException occured: " << ex.what() << endl;
158 } catch (exception &ex) {
159 cout << ex.what() << endl;
160 }
161
162 return 0;
163}
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="")
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 setValue(const double val)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
void setSystemDirectory(std::string systemDir)
string getModelText()
Get model as string.
Definition: transport4.cpp:35