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