Loading...
Searching...
No Matches
transport4.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#include <vector>
30#include <map>
31
32using namespace gams;
33using namespace std;
34
37{
38 return "Sets \n"
39 " i canning plants \n"
40 " j markets \n"
41 " \n"
42 "Parameters \n"
43 " a(i) capacity of plant i in cases \n"
44 " b(j) demand at market j in cases \n"
45 " d(i,j) distance in thousands of miles \n"
46 "Scalar f freight in dollars per case per thousand miles; \n"
47 " \n"
48 "$if not set gdxincname $abort 'no include file name for data file provided'\n"
49 "$gdxin %gdxincname% \n"
50 "$load i j a b d f \n"
51 "$gdxin \n"
52 " \n"
53 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
54 " \n"
55 " c(i,j) = f * d(i,j) / 1000 ; \n"
56 " \n"
57 " Variables \n"
58 " x(i,j) shipment quantities in cases \n"
59 " z total transportation costs in thousands of dollars ; \n"
60 " \n"
61 " Positive Variable x ; \n"
62 " \n"
63 " Equations \n"
64 " \n"
65 " cost define objective function \n"
66 " supply(i) observe supply limit at plant i \n"
67 " demand(j) satisfy demand at market j ; \n"
68 " \n"
69 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
70 " \n"
71 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
72 " \n"
73 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
74 " \n"
75 " Model transport /all/ ; \n"
76 " \n"
77 " Solve transport using lp minimizing z ; \n"
78 " \n"
79 "Display x.l, x.m ; \n";
80}
81
88int main(int argc, char* argv[])
89{
90 cout << "---------- Transport 4 --------------" << endl;
91
92 try {
93 GAMSWorkspaceInfo wsInfo;
94 if (argc > 1)
95 wsInfo.setSystemDirectory(argv[1]);
96 GAMSWorkspace ws(wsInfo);
97
98 // define some data by using C++ data structures
99 vector<string> plants = {
100 "Seattle", "San-Diego"
101 };
102 vector<string> markets = {
103 "New-York", "Chicago", "Topeka"
104 };
105 map<string, double> capacity = {
106 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
107 };
108 map<string, double> demand = {
109 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
110 };
111 map<tuple<string, string>, double> distance = {
112 { make_tuple("Seattle", "New-York"), 2.5 },
113 { make_tuple("Seattle", "Chicago"), 1.7 },
114 { make_tuple("Seattle", "Topeka"), 1.8 },
115 { make_tuple("San-Diego", "New-York"), 2.5 },
116 { make_tuple("San-Diego", "Chicago"), 1.8 },
117 { make_tuple("San-Diego", "Topeka"), 1.4 }
118 };
119
120 // prepare a GAMSDatabase with data from the C++ data structures
121 GAMSDatabase db = ws.addDatabase();
122
123 GAMSSet i = db.addSet("i", 1, "canning plants");
124 for (string p: plants)
125 i.addRecord(p);
126
127 GAMSSet j = db.addSet("j", 1, "markets");
128 for (string m: markets)
129 j.addRecord(m);
130
131 GAMSParameter a = db.addParameter("a", "capacity of plant i in cases", i);
132 for (string p: plants)
133 a.addRecord(p).setValue(capacity[p]);
134
135 GAMSParameter b = db.addParameter("b", "demand at market j in cases", j);
136 for (string m: markets)
137 b.addRecord(m).setValue(demand[m]);
138
139 GAMSParameter d = db.addParameter("d", "distance in thousands of miles", i, j);
140 for (auto t : distance)
141 d.addRecord(get<0>(t.first), get<1>(t.first)).setValue(t.second);
142
143 GAMSParameter f = db.addParameter("f", "freight in dollars per case per thousand miles");
144 f.addRecord().setValue(90);
145
146 // run a job using data from the created GAMSDatabase
147 GAMSJob t4 = ws.addJobFromString(getModelText());
148 GAMSOptions opt = ws.addOptions();
149 opt.setDefine("gdxincname", db.name());
150 opt.setAllModelTypes("xpress");
151 t4.run(opt, db);
152
153 for (GAMSVariableRecord rec : t4.outDB().getVariable("x"))
154 cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal="
155 << rec.marginal() << endl;
156
157 } catch (GAMSException &ex) {
158 cout << "GAMSException occured: " << ex.what() << endl;
159 } catch (exception &ex) {
160 cout << ex.what() << endl;
161 }
162
163 return 0;
164}
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
std::string name()
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
void setAllModelTypes(const std::string &solver)
void setDefine(const std::string &key, const std::string &value)
void setValue(const double val)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
void setSystemDirectory(std::string systemDir)
string getModelText()
Get model as string.
Definition: transport4.cpp:36