transport3.cpp
Go to the documentation of this file.
1/*
2 *
3 * GAMS - General Algebraic Modeling System C++ API
4 *
5 * Copyright (c) 2017 GAMS Software GmbH <support@gams.com>
6 * Copyright (c) 2017 GAMS Development Corp. <support@gams.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26#include "gams.h"
27#include <iostream>
28#include <fstream>
29
30using namespace gams;
31using namespace std;
32
35{
36 return "Sets \n"
37 " i canning plants / seattle, san-diego / \n"
38 " j markets / new-york, chicago, topeka / ; \n"
39 "Parameters \n"
40 " \n"
41 " a(i) capacity of plant i in cases \n"
42 " / seattle 350 \n"
43 " san-diego 600 / \n"
44 " \n"
45 " b(j) demand at market j in cases \n"
46 " / new-york 325 \n"
47 " chicago 300 \n"
48 " topeka 275 / ; \n"
49 " \n"
50 "Table d(i,j) distance in thousands of miles \n"
51 " new-york chicago topeka \n"
52 " seattle 2.5 1.7 1.8 \n"
53 " san-diego 2.5 1.8 1.4 ; \n"
54 " \n"
55 "Scalar f freight in dollars per case per thousand miles /90/;\n";
56}
57
60{
61 return "Sets \n"
62 " i canning plants \n"
63 " j markets \n"
64 " \n"
65 "Parameters \n"
66 " a(i) capacity of plant i in cases \n"
67 " b(j) demand at market j in cases \n"
68 " d(i,j) distance in thousands of miles \n"
69 "Scalar f freight in dollars per case per thousand miles; \n"
70 " \n"
71 "$if not set gdxincname $abort 'no include file name for data file provided'\n"
72 "$gdxin %gdxincname% \n"
73 "$load i j a b d f \n"
74 "$gdxin \n"
75 " \n"
76 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
77 " \n"
78 " c(i,j) = f * d(i,j) / 1000 ; \n"
79 " \n"
80 " Variables \n"
81 " x(i,j) shipment quantities in cases \n"
82 " z total transportation costs in thousands of dollars ; \n"
83 " \n"
84 " Positive Variable x ; \n"
85 " \n"
86 " Equations \n"
87 " \n"
88 " cost define objective function \n"
89 " supply(i) observe supply limit at plant i \n"
90 " demand(j) satisfy demand at market j ; \n"
91 " \n"
92 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
93 " \n"
94 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
95 " \n"
96 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
97 " \n"
98 " Model transport /all/ ; \n"
99 " \n"
100 " Solve transport using lp minimizing z ; \n"
101 " \n"
102 "Display x.l, x.m ; \n";
103}
104
112int main(int argc, char* argv[])
113{
114 cout << "---------- Transport 3 --------------" << endl;
115
116 try {
117 GAMSWorkspaceInfo wsInfo;
118 if (argc > 1)
119 wsInfo.setSystemDirectory(argv[1]);
120 GAMSWorkspace ws(wsInfo);
121
122 // data from a string with GAMS syntax with explicit export to GDX file
124 t3.run();
125 //TODO: change doExport to export?
126 t3.outDB().doExport(ws.workingDirectory() + cPathSep + "tdata.gdx");
127
128 // run a job using an instance of GAMSOptions that defines the data include file
130 GAMSOptions opt = ws.addOptions();
131 opt.setDefine("gdxincname", "tdata");
132 opt.setAllModelTypes("xpress");
133 t3.run(opt);
134 for (GAMSVariableRecord rec : t3.outDB().getVariable("x"))
135 cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
136 << rec.marginal() << endl;
137
138 // same but with implicit database communication
141 t3a.run();
142 opt.setDefine("gdxincname", t3a.outDB().name());
143
144 t3b.run(opt, t3a.outDB());
145 for (GAMSVariableRecord rec : t3.outDB().getVariable("x"))
146 cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
147 << rec.marginal() << endl;
148
149 } catch (GAMSException &ex) {
150 cout << "GAMSException occured: " << ex.what() << endl;
151 } catch (exception &ex) {
152 cout << ex.what() << endl;
153 }
154
155 return 0;
156}
string getModelText()
Get model as string.
Definition: transport3.cpp:59
void setAllModelTypes(const std::string &solver)
string getDataText()
Get data as string.
Definition: transport3.cpp:34
void setSystemDirectory(std::string systemDir)
std::string name()
GAMSDatabase outDB()
void setDefine(const std::string &key, const std::string &value)
std::string workingDirectory() const
GAMSOptions addOptions()
GAMSVariable getVariable(const std::string &name)
void doExport(const std::string &filePath="")
GAMSJob addJobFromString(const std::string &gamsSource, const std::string &jobName="")