Loading...
Searching...
No Matches
transport.cpp
Go to the documentation of this file.
1
26#include "transport.h"
27
28#include <ostream>
29
30using namespace gams;
31using namespace std;
32
35Transport::Transport(GAMSWorkspace ws)
36 : fws(ws),
37 job(fws.addJobFromString(getModelSource())),
38 fopt(fws.addOptions())
39{
40 fws = ws;
41 fopt = ws.addOptions();
42
43 fDbIn1 = ws.addDatabase("", "dbIn1");
44 fDbIn2 = ws.addDatabase("", "dbIn2");
45
46 fopt.setSolveLink(GAMSOptions::ESolveLink::LoadLibrary);
47 fopt.setAllModelTypes("Cplex");
48 fopt.setDefine("dbOut1", "dbOut1");
49
50 fi = fDbIn1.addSet("i", "canning plants");
51 fj = fDbIn1.addSet("j", "markets");
52 fa = fDbIn1.addParameter("a", "capacity of plant i in cases", fi);
53 fb = fDbIn1.addParameter("b", "demand at market j in cases", fj);
54 fd = fDbIn1.addParameter("d", "distance in thousands of miles", fi, fj);
55 ff = fDbIn2.addParameter("f", "freight in dollars per case per thousand miles");
56
57 job = ws.addJobFromString(getModelSource());
58}
59
60void Transport::run(GAMSCheckpoint checkpoint, ostream &output)
61{
62 if (!fDbIn1.checkDomains())
63 throw GAMSException("Domain Errors in Database 1");
64 if (!fDbIn2.checkDomains())
65 throw GAMSException("Domain Errors in Database 2");
66
67 vector<GAMSDatabase> dbs {fDbIn1, fDbIn2};
68 job.run(fopt, checkpoint, output, false, dbs);
69
70 fDbOut1 = fws.addDatabaseFromGDX(fopt.getDefine("dbOut1") + ".gdx");
71 fx = fDbOut1.getVariable("x");
72 fz = fDbOut1.getVariable("z");
73}
74
75string Transport::getModelSource()
76{
77 return " Sets \n"
78 " i canning plants \n"
79 " j markets \n"
80 " \n"
81 " Parameters \n"
82 " a(i) capacity of plant i in cases \n"
83 " b(j) demand at market j in cases \n"
84 " d(i,j) distance in thousands of miles \n"
85 " Scalar f freight in dollars per case per thousand miles; \n"
86 " \n"
87 "$if not set dbIn1 $abort 'no file name for in-database 1 file provided' \n"
88 "$gdxin %dbIn1% \n"
89 "$load i j a b d \n"
90 " \n"
91 "$if not set dbIn2 $abort 'no file name for in-database 2 file provided' \n"
92 "$gdxin %dbIn2% \n"
93 "$load f \n"
94 "$gdxin \n"
95 " \n"
96 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
97 " \n"
98 " c(i,j) = f * d(i,j) / 1000 ; \n"
99 " \n"
100 " Variables \n"
101 " x(i,j) shipment quantities in cases \n"
102 " z total transportation costs in thousands of dollars ; \n"
103 " \n"
104 " Positive Variable x ; \n"
105 " \n"
106 " Equations \n"
107 " cost define objective function \n"
108 " supply(i) observe supply limit at plant i \n"
109 " demand(j) satisfy demand at market j ; \n"
110 " \n"
111 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
112 " \n"
113 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
114 " \n"
115 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
116 " \n"
117 " Model transport /all/ ; \n"
118 " \n"
119 " Solve transport using lp minimizing z ; \n"
120 " \n"
121 "$if not set dbOut1 $abort 'no file name for out-database 1 file provided' \n"
122 " execute_unload '%dbOut1%', x, z; \n";
123}
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
GAMSOptions addOptions()
GAMSJob addJobFromString(const std::string &gamsSource, const std::string &jobName="")
GAMSDatabase addDatabase(const std::string &databaseName="", const std::string &inModelName="")
Wrapper class definition for GAMS trnsport model.