Loading...
Searching...
No Matches
transport8.cpp
Go to the documentation of this file.
1
25#include "gams.h"
26#include <vector>
27#include <mutex>
28#include <thread>
29#include <iostream>
30
31using namespace gams;
32using namespace std;
33
35void scenSolve(GAMSWorkspace* ws, GAMSCheckpoint* cp, vector<double>* bmultVector, std::mutex* vectorMutex, std::mutex* ioMutex)
36{
37 unique_lock<mutex> vectorLock(*vectorMutex);
39 vectorLock.unlock();
40
41 GAMSParameter bmult = mi.syncDb().addParameter("bmult", "demand multiplier");
42 GAMSOptions opt = ws->addOptions();
43 opt.setAllModelTypes("cplex");
44 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
45 mi.instantiate("transport use lp min z", opt, GAMSModifier(bmult));
46
47 bmult.addRecord().setValue(1.0);
48
49 while (true)
50 {
51 double b;
52
53 // dynamically get a bmult value from the vector instead of passing it to the different threads at creation time
54 vectorLock.lock();
55 if (bmultVector->empty())
56 return;
57 b = bmultVector->back();
58 bmultVector->pop_back();
59 vectorLock.unlock();
60 bmult.firstRecord().setValue(b);
61 mi.solve();
62
63 // we need to make the output a critical section to avoid messed up report informations
64 unique_lock<mutex> ioLock(*ioMutex);
65 cout << "Scenario bmult=" << b << ":" << endl;
66 cout << " Modelstatus: " << mi.modelStatus() << endl;
67 cout << " Solvestatus: " << mi.solveStatus() << endl;
68 cout << " Obj: " << mi.syncDb().getVariable("z").findRecord().level() << endl;
69 ioLock.unlock();
70 }
71}
72
75{
76 return "Sets \n"
77 " i canning plants / seattle, san-diego / \n"
78 " j markets / new-york, chicago, topeka / ; \n"
79 " \n"
80 "Parameters \n"
81 " \n"
82 " a(i) capacity of plant i in cases \n"
83 " / seattle 350 \n"
84 " san-diego 600 / \n"
85 " \n"
86 " b(j) demand at market j in cases \n"
87 " / new-york 325 \n"
88 " chicago 300 \n"
89 " topeka 275 / ; \n"
90 " \n"
91 "Table d(i, j) distance in thousands of miles \n"
92 " new-york chicago topeka \n"
93 " seattle 2.5 1.7 1.8 \n"
94 " san-diego 2.5 1.8 1.4; \n"
95 " \n"
96 "Scalar f freight in dollars per case per thousand miles / 90 / ; \n"
97 "Scalar bmult demand multiplier / 1 / ; \n"
98 " \n"
99 "Parameter c(i, j) transport cost in thousands of dollars per case; \n"
100 " \n"
101 "c(i, j) = f * d(i, j) / 1000; \n"
102 " \n"
103 "Variables \n"
104 " x(i, j) shipment quantities in cases \n"
105 " z total transportation costs in thousands of dollars; \n"
106 " \n"
107 "Positive Variable x; \n"
108 " \n"
109 "Equations \n"
110 " cost define objective function \n"
111 " supply(i) observe supply limit at plant i \n"
112 " demand(j) satisfy demand at market j; \n"
113 " \n"
114 "cost.. z =e= sum((i, j), c(i, j)*x(i, j)); \n"
115 " \n"
116 "supply(i).. sum(j, x(i, j)) =l= a(i); \n"
117 " \n"
118 "demand(j).. sum(i, x(i, j)) =g= bmult*b(j); \n"
119 " \n"
120 "Model transport / all / ; \n";
121}
122
128int main(int argc, char* argv[])
129{
130 cout << "---------- Transport 8 --------------" << endl;
131
132 try {
133 GAMSWorkspaceInfo wsInfo;
134 if (argc > 1)
135 wsInfo.setSystemDirectory(argv[1]);
136 GAMSWorkspace ws(wsInfo);
137 GAMSCheckpoint cp = ws.addCheckpoint();
138
139 // initialize a GAMSCheckpoint by running a GAMSJob
140 ws.addJobFromString(getModelText()).run(cp);
141
142 vector<double> bmultVector = { 1.3, 1.2, 1.1, 1.0, 0.9, 0.8, 0.7, 0.6 };
143 int nrThreads = 2;
144 // solve multiple model instances in parallel
145 std::mutex vectorMutex;
146 std::mutex ioMutex;
147 vector<thread> v;
148 for (int i = 0; i < nrThreads; i++)
149 v.emplace_back([&ws, &cp, &bmultVector, &vectorMutex, &ioMutex] {scenSolve(&ws, &cp, &bmultVector, &vectorMutex, &ioMutex);});
150 for (auto& t : v)
151 t.join();
152
153 } catch (GAMSException &ex) {
154 cout << "GAMSException occured: " << ex.what() << endl;
155 } catch (exception &ex) {
156 cout << ex.what() << endl;
157 }
158
159 return 0;
160}
161
GAMSModelInstance addModelInstance(const std::string &modelInstanceName="")
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
GAMSVariable getVariable(const std::string &name)
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={ })
GAMSEnum::SolveStat solveStatus()
GAMSEnum::ModelStat modelStatus()
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)
GAMSOptions addOptions()
void scenSolve(GAMSWorkspace *ws, GAMSCheckpoint *cp, vector< double > *bmultVector, std::mutex *vectorMutex, std::mutex *ioMutex)
Solve the model in different scenarios.
Definition: transport8.cpp:35
string getModelText()
Get model as string.
Definition: transport8.cpp:74