transport.cpp
Go to the documentation of this file.
1/*
2  * GAMS - General Algebraic Modeling System C++ API
3  *
4  * Copyright (c) 2017-2022 GAMS Software GmbH <support@gams.com>
5  * Copyright (c) 2017-2022 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 
30 using namespace gams;
31 using namespace std;
32 
35 Transport::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 
60 void 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 
75 string 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 }
GAMSJob addJobFromString(const std::string &gamsSource, const std::string &jobName="")
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
GAMSDatabase addDatabase(const std::string &databaseName="", const std::string &inModelName="")
Wrapper class definition for GAMS trnsport model.
GAMSOptions addOptions()