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