Loading...
Searching...
No Matches
transport7.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 <vector>
28#include <iostream>
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 " \n"
40 "Parameters \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 "Scalar bmult demand multiplier /1/; \n"
57 " \n"
58 "Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
59 " \n"
60 " c(i,j) = f * d(i,j) / 1000 ; \n"
61 " \n"
62 "Variables \n"
63 " x(i,j) shipment quantities in cases \n"
64 " z total transportation costs in thousands of dollars ; \n"
65 " \n"
66 "Positive Variable x ; \n"
67 " \n"
68 "Equations \n"
69 " cost define objective function \n"
70 " supply(i) observe supply limit at plant i \n"
71 " demand(j) satisfy demand at market j ; \n"
72 " \n"
73 "cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
74 " \n"
75 "supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
76 " \n"
77 "demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; \n"
78 " \n"
79 "Model transport /all/ ; \n";
80}
81
89int main(int argc, char* argv[])
90{
91 cout << "---------- Transport 7 --------------" << endl;
92
93 try {
94 GAMSWorkspaceInfo wsInfo;
95 if (argc > 1)
96 wsInfo.setSystemDirectory(argv[1]);
97 GAMSWorkspace ws(wsInfo);
98 GAMSCheckpoint cp = ws.addCheckpoint();
99
100 // initialize a GAMSCheckpoint by running a GAMSJob
101 GAMSJob t7 = ws.addJobFromString(getModelText());
102 t7.run(cp);
103
104 // create a GAMSModelInstance and solve it multiple times with different scalar bmult
106 GAMSParameter bmult = mi.syncDb().addParameter("bmult", 0, "demand multiplier");
107 GAMSOptions opt = ws.addOptions();
108 opt.setAllModelTypes("cplex");
109
110 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
111 mi.instantiate("transport use lp min z", opt, GAMSModifier(bmult));
112
113 bmult.addRecord().setValue(1.0);
114 vector<double> bmultlist = { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
115
116 for (double b : bmultlist) {
117 bmult.firstRecord().setValue(b);
118 mi.solve();
119 cout << "Scenario bmult=" << b << ":" << endl;
120 cout << " Modelstatus: " << mi.modelStatusAsString() << endl;
121 cout << " Solvestatus: " << mi.solveStatusAsString() << endl;
122 cout << " Obj: " << mi.syncDb().getVariable("z").findRecord().level() << endl;
123 }
124
125 // create a GAMSModelInstance and solve it with single links in the network blocked
126 mi = cp.addModelInstance();
127
128 GAMSVariable x = mi.syncDb().addVariable("x", 2, GAMSEnum::VarType::Positive, "");
129 GAMSParameter xup = mi.syncDb().addParameter("xup", 2, "upper bound on x");
130
131 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare upper bound of X mutable
132 mi.instantiate("transport use lp min z", GAMSModifier (x, GAMSEnum::SymbolUpdateAction::Upper, xup));
133
134 for (GAMSSetRecord i : t7.outDB().getSet("i")) {
135 for (GAMSSetRecord j : t7.outDB().getSet("j")) {
136 xup.clear();
137 xup.addRecord(i.key(0), j.key(0)).setValue(0);
138 mi.solve();
139 cout << "Scenario link blocked=" << i.key(0) << "-" << j.key(0) << endl;
140 cout << " Modelstatus: " << mi.modelStatusAsString() << endl;
141 cout << " Solvestatus: " << mi.solveStatusAsString() << endl;
142 cout << " Obj: " << mi.syncDb().getVariable("z").findRecord().level() << endl;
143 }
144 }
145
146 } catch (GAMSException &ex) {
147 cout << "GAMSException occured: " << ex.what() << endl;
148 } catch (exception &ex) {
149 cout << ex.what() << endl;
150 }
151
152 return 0;
153}
GAMSModelInstance addModelInstance(const std::string &modelInstanceName="")
GAMSVariable addVariable(const std::string &name, const int dimension, const GAMSEnum::VarType varType, const std::string &explanatoryText="")
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
GAMSSet getSet(const std::string &name)
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
std::string solveStatusAsString()
void solve(GAMSEnum::SymbolUpdateType updateType, std::ostream &output, GAMSModelInstanceOpt miOpt)
void instantiate(const std::string &modelDefinition, const gams::GAMSOptions &options, const std::vector< gams::GAMSModifier > &modifiers={ })
std::string modelStatusAsString()
GAMSDatabase syncDb()
void setAllModelTypes(const std::string &solver)
void setValue(const double val)
GAMSParameterRecord firstRecord(const std::vector< std::string > &slice)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSVariableRecord findRecord(const std::vector< std::string > &keys)
void setSystemDirectory(std::string systemDir)
string getModelText()
Get model as string.
Definition: transport7.cpp:34