transport.cpp
Go to the documentation of this file.
1/*
2 * GAMS - General Algebraic Modeling System C++ API
3 *
4 * Copyright (c) 2017 GAMS Software GmbH <support@gams.com>
5 * Copyright (c) 2017 GAMS Development Corp. <support@gams.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
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 // TODO(AF) add enum to GAMSOptions
47 fopt.setSolveLink(GAMSOptions::ESolveLink::LoadLibrary);
48 fopt.setAllModelTypes("Cplex");
49 fopt.setDefine("dbOut1", "dbOut1");
50
51 fi = fDbIn1.addSet("i", "canning plants");
52 fj = fDbIn1.addSet("j", "markets");
53 fa = fDbIn1.addParameter("a", "capacity of plant i in cases", fi);
54 fb = fDbIn1.addParameter("b", "demand at market j in cases", fj);
55 fd = fDbIn1.addParameter("d", "distance in thousands of miles", fi, fj);
56 ff = fDbIn2.addParameter("f", "freight in dollars per case per thousand miles");
57
58 job = ws.addJobFromString(getModelSource());
59}
60
61void Transport::run(GAMSCheckpoint checkpoint, ostream &output)
62{
63 if (!fDbIn1.checkDomains())
64 throw GAMSException("Domain Errors in Database 1");
65 if (!fDbIn2.checkDomains())
66 throw GAMSException("Domain Errors in Database 2");
67
68 vector<GAMSDatabase> dbs {fDbIn1, fDbIn2};
69 job.run(fopt, checkpoint, output, false, dbs);
70
71 fDbOut1 = fws.addDatabaseFromGDX(fopt.getDefine("dbOut1") + ".gdx");
72 fx = fDbOut1.getVariable("x");
73 fz = fDbOut1.getVariable("z");
74}
75
76string Transport::getModelSource()
77{
78 return " Sets \n"
79 " i canning plants \n"
80 " j markets \n"
81 " \n"
82 " Parameters \n"
83 " a(i) capacity of plant i in cases \n"
84 " b(j) demand at market j in cases \n"
85 " d(i,j) distance in thousands of miles \n"
86 " Scalar f freight in dollars per case per thousand miles; \n"
87 " \n"
88 "$if not set dbIn1 $abort 'no file name for in-database 1 file provided' \n"
89 "$gdxin %dbIn1% \n"
90 "$load i j a b d \n"
91 " \n"
92 "$if not set dbIn2 $abort 'no file name for in-database 2 file provided' \n"
93 "$gdxin %dbIn2% \n"
94 "$load f \n"
95 "$gdxin \n"
96 " \n"
97 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
98 " \n"
99 " c(i,j) = f * d(i,j) / 1000 ; \n"
100 " \n"
101 " Variables \n"
102 " x(i,j) shipment quantities in cases \n"
103 " z total transportation costs in thousands of dollars ; \n"
104 " \n"
105 " Positive Variable x ; \n"
106 " \n"
107 " Equations \n"
108 " cost define objective function \n"
109 " supply(i) observe supply limit at plant i \n"
110 " demand(j) satisfy demand at market j ; \n"
111 " \n"
112 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
113 " \n"
114 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
115 " \n"
116 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
117 " \n"
118 " Model transport /all/ ; \n"
119 " \n"
120 " Solve transport using lp minimizing z ; \n"
121 " \n"
122 "$if not set dbOut1 $abort 'no file name for out-database 1 file provided' \n"
123 " execute_unload '%dbOut1%', x, z; \n";
124}
Wrapper class definition for GAMS trnsport model.
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="")
GAMSDatabase addDatabase(const std::string &databaseName="", const std::string &inModelName="")
GAMSOptions addOptions()
GAMSJob addJobFromString(const std::string &gamsSource, const std::string &jobName="")