Loading...
Searching...
No Matches
transport6.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#include <thread>
30#include <mutex>
31
32using namespace gams;
33using namespace std;
34
36void runScenario(GAMSWorkspace* ws, const GAMSCheckpoint& cp, mutex* ioMutex, double b)
37{
38 auto t6 = ws->addJobFromString("bmult=" + to_string(b) + "; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;", cp);
39 t6.run();
40
41 // we need to make the ouput a critical section to avoid messed up report information
42 lock_guard<mutex> lck(*ioMutex);
43 cout << "Scenario bmult=" << b << ":" << endl;
44 cout << " Modelstatus: " << t6.outDB().getParameter("ms").findRecord().value() << endl;
45 cout << " Solvestatus: " << t6.outDB().getParameter("ss").findRecord().value() << endl;
46 cout << " Obj: " << t6.outDB().getVariable("z").findRecord().level() << endl;
47}
48
51{
52 return "Sets \n"
53 " i canning plants / seattle, san-diego / \n"
54 " j markets / new-york, chicago, topeka / ; \n"
55 " \n"
56 "Parameters \n"
57 " \n"
58 " a(i) capacity of plant i in cases \n"
59 " / seattle 350 \n"
60 " san-diego 600 / \n"
61 " \n"
62 " b(j) demand at market j in cases \n"
63 " / new-york 325 \n"
64 " chicago 300 \n"
65 " topeka 275 / ; \n"
66 " \n"
67 "Table d(i, j) distance in thousands of miles \n"
68 " new-york chicago topeka \n"
69 " seattle 2.5 1.7 1.8 \n"
70 " san-diego 2.5 1.8 1.4; \n"
71 " \n"
72 "Scalar f freight in dollars per case per thousand miles / 90 / ; \n"
73 "Scalar bmult demand multiplier / 1 / ; \n"
74 " \n"
75 "Parameter c(i, j) transport cost in thousands of dollars per case; \n"
76 " \n"
77 "c(i, j) = f * d(i, j) / 1000; \n"
78 " \n"
79 "Variables \n"
80 " x(i, j) shipment quantities in cases \n"
81 " z total transportation costs in thousands of dollars; \n"
82 " \n"
83 "Positive Variable x; \n"
84 " \n"
85 "Equations \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= bmult*b(j); \n"
95 " \n"
96 "Model transport / all / ; \n"
97 "Scalar ms 'model status', ss 'solve status' \n";
98}
99
100
106int main(int argc, char* argv[])
107{
108 cout << "---------- Transport 6 --------------" << endl;
109
110 try {
111 GAMSWorkspaceInfo wsInfo;
112 if (argc > 1)
113 wsInfo.setSystemDirectory(argv[1]);
114 GAMSWorkspace ws(wsInfo);
115 GAMSCheckpoint cp = ws.addCheckpoint();
116
117 // initialize a GAMSCheckpoint by running a GAMSJob
118 ws.addJobFromString(getModelText()).run(cp);
119
120 vector<double> bmultlist = { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
121
122 // run multiple parallel jobs using the created GAMSCheckpoint
123 mutex ioMutex;
124 vector<thread> v;
125 for(double b : bmultlist)
126 v.emplace_back([&ws, cp, &ioMutex, b]{runScenario(&ws, cp, &ioMutex, b);});
127 for (auto& t : v)
128 t.join();
129
130 } catch (GAMSException &ex) {
131 cout << "GAMSException occured: " << ex.what() << endl;
132 } catch (exception &ex) {
133 cout << ex.what() << endl;
134 }
135
136 return 0;
137}
void setSystemDirectory(std::string systemDir)
GAMSJob addJobFromString(const std::string &gamsSource, const std::string &jobName="")
string getModelText()
Get model as string.
Definition: transport6.cpp:50
void runScenario(GAMSWorkspace *ws, const GAMSCheckpoint &cp, mutex *ioMutex, double b)
Run the job with a different scenario.
Definition: transport6.cpp:36