transport13.cpp
Go to the documentation of this file.
1/*
2  * GAMS - General Algebraic Modeling System C++ API
3  *
4  * Copyright (c) 2017-2022 GAMS Software GmbH <support@gams.com>
5  * Copyright (c) 2017-2022 GAMS Development Corp. <support@gams.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 #include "gams.h"
26 #include "transport.h"
27 
28 #include <iostream>
29 #include <map>
30 #include <string>
31 #include <tuple>
32 #include <vector>
33 
34 using namespace gams;
35 using namespace std;
36 
42 int main(int argc, char* argv[])
43 {
44  cout << "---------- Transport 13 --------------" << endl;
45 
46  try {
47  vector<string> plants {"Seattle", "San-Diego"};
48  vector<string> markets {"New-York", "Chicago", "Topeka"};
49  map<string, double> capacity {
50  { "Seattle", 350.0 }, { "San-Diego", 600.0 }
51  };
52  map<string, double> demand {
53  { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
54  };
55  map<tuple<string, string>, double> distance {
56  { make_tuple("Seattle", "New-York"), 2.5 },
57  { make_tuple("Seattle", "Chicago"), 1.7 },
58  { make_tuple("Seattle", "Topeka"), 1.8 },
59  { make_tuple("San-Diego", "New-York"), 2.5 },
60  { make_tuple("San-Diego", "Chicago"), 1.8 },
61  { make_tuple("San-Diego", "Topeka"), 1.4 }
62  };
63 
64  GAMSWorkspaceInfo wsInfo;
65  if (argc > 1)
66  wsInfo.setSystemDirectory(argv[1]);
67  GAMSWorkspace ws(wsInfo);
68 
69  Transport t(ws);
70 
71  for (string p : plants)
72  t.i().addRecord(p);
73 
74  for (string m : markets)
75  t.j().addRecord(m);
76 
77  for (string p : plants)
78  t.a().addRecord(p).setValue(capacity[p]);
79 
80  for (string m : markets)
81  t.b().addRecord(m).setValue(demand[m]);
82 
83  for (auto dis : distance)
84  t.d().addRecord(get<0>(dis.first), get<1>(dis.first)).setValue(distance[dis.first]);
85 
86  t.f().addRecord().setValue(90);
87 
88  t.opt().setAllModelTypes("cplex");
89 
90  t.run(GAMSCheckpoint(), cout);
91 
92  cout << "Objective: " << t.z().firstRecord().level() << endl;
93  for (GAMSVariableRecord rec : t.x())
94  cout << "x(" << rec.key(0) << "," << rec.key(1) << "): level=" << rec.level() <<
95  " marginal=" << rec.marginal() << endl;
96 
97  } catch (GAMSException &ex) {
98  cout << "GAMSException occured: " << ex.what() << endl;
99  } catch (exception &ex) {
100  cout << ex.what() << endl;
101  }
102 
103  return 0;
104 }
void setSystemDirectory(std::string systemDir)
Wrapper class definition for GAMS trnsport model.