Loading...
Searching...
No Matches
transport2.cpp
Go to the documentation of this file.
1/*
2 *
3 * GAMS - General Algebraic Modeling System C++ API
4 *
5 * Copyright (c) 2017-2023 GAMS Software GmbH <support@gams.com>
6 * Copyright (c) 2017-2023 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 incname $abort 'no include file name for data file provided'\n"
72 "$include %incname% \n"
73 " \n"
74 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
75 " \n"
76 " c(i,j) = f * d(i,j) / 1000 ; \n"
77 " \n"
78 " Variables \n"
79 " x(i,j) shipment quantities in cases \n"
80 " z total transportation costs in thousands of dollars ; \n"
81 " \n"
82 " Positive Variable x ; \n"
83 " \n"
84 " Equations \n"
85 " \n"
86 " cost define objective function \n"
87 " supply(i) observe supply limit at plant i \n"
88 " demand(j) satisfy demand at market j ; \n"
89 " \n"
90 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
91 " \n"
92 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
93 " \n"
94 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
95 " \n"
96 " Model transport /all/ ; \n"
97 " \n"
98 " Solve transport using lp minimizing z ; \n"
99 " \n"
100 "Display x.l, x.m ; \n";
101}
102
108int main(int argc, char* argv[])
109{
110 cout << "---------- Transport 2 --------------" << endl;
111
112 try {
113 GAMSWorkspaceInfo wsInfo;
114 if (argc > 1)
115 wsInfo.setSystemDirectory(argv[1]);
116 GAMSWorkspace ws(wsInfo);
117
118 // write an include file containing data with GAMS syntax
119 ofstream tdata(ws.workingDirectory() + cPathSep + "tdata.gms");
120 tdata << getDataText();
121 tdata.close();
122
123 // run a job using an instance of GAMSOptions that defines the data include file
124 GAMSOptions opt = ws.addOptions();
125 GAMSJob t2 = ws.addJobFromString(getModelText());
126 opt.setDefine("incname", "tdata");
127 t2.run(opt);
128 for (GAMSVariableRecord rec : t2.outDB().getVariable("x"))
129 cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
130 << rec.marginal() << endl;
131
132 } catch (GAMSException &ex) {
133 cout << "GAMSException occured: " << ex.what() << endl;
134 } catch (exception &ex) {
135 cout << ex.what() << endl;
136 }
137
138 return 0;
139}
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
void setDefine(const std::string &key, const std::string &value)
void setSystemDirectory(std::string systemDir)
string getDataText()
Get data as string.
Definition: transport2.cpp:34
string getModelText()
Get model as string.
Definition: transport2.cpp:59