transport11.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 
27 #include <iostream>
28 #include <map>
29 #include <tuple>
30 #include <vector>
31 
32 using namespace gams;
33 using namespace std;
34 
37 {
38  return "$onempty \n"
39  " Sets \n"
40  " i(*) canning plants / / \n"
41  " j(*) markets / / \n"
42  " \n"
43  " Parameters \n"
44  " a(i) capacity of plant i in cases / / \n"
45  " b(j) demand at market j in cases / / \n"
46  " d(i,j) distance in thousands of miles / / \n"
47  " Scalar f freight in dollars per case per thousand miles /0/; \n"
48  " \n"
49  " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
50  " \n"
51  " c(i,j) = f * d(i,j) / 1000 ; \n"
52  " \n"
53  " Variables \n"
54  " x(i,j) shipment quantities in cases \n"
55  " z total transportation costs in thousands of dollars ; \n"
56  " \n"
57  " Positive Variable x ; \n"
58  " \n"
59  " Equations \n"
60  " cost define objective function \n"
61  " supply(i) observe supply limit at plant i \n"
62  " demand(j) satisfy demand at market j ; \n"
63  " \n"
64  " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
65  " \n"
66  " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
67  " \n"
68  " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
69  " \n"
70  " Model transport /all/ ; \n"
71  " \n"
72  " Solve transport using lp minimizing z ; \n";
73 }
74 
76 string getModelText()
77 {
78  return "$if not set gdxincname $abort 'no include file name for data file provided'\n"
79  "$gdxin %gdxincname% \n"
80  "$onMulti \n"
81  "$load i j a b d f \n"
82  "$gdxin \n"
83  " \n"
84  " Display x.l, x.m ; \n";
85 }
86 
88 void createSaveRestart(int argc, char* argv[], const string &checkpointName)
89 {
90  GAMSWorkspaceInfo wsInfo;
91  if (argc > 1)
92  wsInfo.setSystemDirectory(argv[1]);
93  wsInfo.setWorkingDirectory("." +(cPathSep+ checkpointName));
94  GAMSWorkspace ws(wsInfo);
95 
97  GAMSOptions opt = ws.addOptions();
99 
100  auto checkpoint = ws.workingDirectory() + cPathSep + checkpointName;
101  GAMSCheckpoint cp = ws.addCheckpoint(checkpoint);
102  j1.run(opt, cp);
103 }
104 
110 int main(int argc, char* argv[])
111 {
112  cout << "---------- Transport 11 --------------" << endl;
113 
114  try {
115  // Create a save/restart file usually supplied by an application provider
116  // We create it for demonstration purpose
117  std::string cpName = "tbase";
118  createSaveRestart(argc, argv, cpName);
119 
120  // define some data by using C++ data structures
121  vector<string> plants { "Seattle", "San-Diego" };
122  vector<string> markets {"New-York", "Chicago", "Topeka" };
123  map<string, double> capacity {
124  { "Seattle", 350.0 }, { "San-Diego", 600.0 }
125  };
126  map<string, double> demand {
127  { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
128  };
129  map<tuple<string,string>, double> distance {
130  { make_tuple("Seattle", "New-York"), 2.5 },
131  { make_tuple("Seattle", "Chicago"), 1.7 },
132  { make_tuple("Seattle", "Topeka"), 1.8 },
133  { make_tuple("San-Diego", "New-York"), 2.5 },
134  { make_tuple("San-Diego", "Chicago"), 1.8 },
135  { make_tuple("San-Diego", "Topeka"), 1.4 }
136  };
137 
138  GAMSWorkspaceInfo wsInfo;
139  if (argc > 1)
140  wsInfo.setSystemDirectory(argv[1]);
141  wsInfo.setWorkingDirectory("." +(cPathSep+ cpName));
142  GAMSWorkspace ws(wsInfo);
143  ws.gamsLib("trnsport");
144 
145  // prepare a GAMSDatabase with data from the C++ data structures
146  GAMSDatabase db = ws.addDatabase();
147 
148  GAMSSet i = db.addSet("i", 1, "canning plants");
149  for (auto plant : plants)
150  i.addRecord(plant);
151 
152  GAMSSet j = db.addSet("j", 1, "markets");
153  for (auto market : markets)
154  j.addRecord(market);
155 
156  GAMSParameter a = db.addParameter("a", "capacity of plant i in cases", i);
157  for (auto plant : plants)
158  a.addRecord(plant).setValue(capacity[plant]);
159 
160  GAMSParameter b = db.addParameter("b", "demand at market j in cases", j);
161  for (auto market : markets)
162  b.addRecord(market).setValue(demand[market]);
163 
164  GAMSParameter d = db.addParameter("d", "distance in thousands of miles", i, j);
165  for (auto t : distance) {
166  auto tuple = t.first;
167  auto t1 = get<0>(tuple);
168  auto t2 = get<1>(tuple);
169  d.addRecord(t1, t2).setValue(distance[tuple]);
170  }
171 
172  GAMSParameter f = db.addParameter("f", "freight in dollars per case per thousand miles");
173  f.addRecord().setValue(90);
174 
175  // run a job using data from the created GAMSDatabase
176  GAMSCheckpoint cpBase = ws.addCheckpoint("tbase");
177  GAMSOptions opt = ws.addOptions();
178  GAMSJob t4 = ws.addJobFromString(getModelText(), cpBase);
179  opt.setDefine("gdxincname", db.name());
180  opt.setAllModelTypes("xpress");
181  t4.run(opt, db);
182  for (auto record : t4.outDB().getVariable("x"))
183  cout << "x(" << record.key(0) << "," << record.key(1) << "): level=" << record.level() <<
184  " marginal=" << record.marginal() << endl;
185 
186  } catch (GAMSException &ex) {
187  cout << "GAMSException occured: " << ex.what() << endl;
188  } catch (exception &ex) {
189  cout << ex.what() << endl;
190  }
191  return 0;
192 }
GAMSJob addJobFromString(const std::string &gamsSource, const std::string &jobName="")
void setSystemDirectory(std::string systemDir)
std::string workingDirectory() const
GAMSVariable getVariable(const std::string &name)
void setAllModelTypes(const std::string &solver)
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSDatabase outDB()
GAMSCheckpoint addCheckpoint(const std::string &checkpointName="")
void setWorkingDirectory(std::string workingDir)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
string getModelText()
Get model as string.
Definition: transport11.cpp:76
void setValue(const double val)
void createSaveRestart(int argc, char *argv[], const string &checkpointName)
Create Save and Restart checkpoint.
Definition: transport11.cpp:88
string getBaseModelText()
Get model as string.
Definition: transport11.cpp:36
void setDefine(const std::string &key, const std::string &value)
void setAction(const GAMSOptions::EAction::EActionEnum value)
std::string name()
GAMSOptions addOptions()