transport11.cpp File Reference
This is the 11th model in a series of tutorial examples. More...
#include "gams.h"
#include <iostream>
#include <map>
#include <tuple>
#include <vector>
Go to the source code of this file.
Functions | |
string | getBaseModelText () |
Get model as string. | |
string | getModelText () |
Get model as string. | |
void | createSaveRestart (int argc, char *argv[], const string &checkpointName) |
Create Save and Restart checkpoint. | |
int | main (int argc, char *argv[]) |
Detailed Description
This is the 11th model in a series of tutorial examples.
Here we show:
- How to create and use a save/restart file
Definition in file transport11.cpp.
Function Documentation
◆ createSaveRestart()
void createSaveRestart | ( | int | argc, |
char * | argv[], | ||
const string & | checkpointName ) |
Create Save and Restart checkpoint.
Definition at line 88 of file transport11.cpp.
89{
90 GAMSWorkspaceInfo wsInfo;
91 if (argc > 1) {
92 wsInfo.setSystemDirectory(argv[1]);
93 }
95 GAMSWorkspace ws(wsInfo);
96
98 GAMSOptions opt = ws.addOptions();
100
101 auto checkpoint = ws.workingDirectory() + cPathSep + checkpointName;
102 GAMSCheckpoint cp = ws.addCheckpoint(checkpoint);
103 j1.run(opt, cp);
104}
void run()
void setAction(const GAMSOptions::EAction::EActionEnum value)
void setWorkingDirectory(const std::string &workingDir)
void setSystemDirectory(const std::string &systemDir)
CompileOnly
Referenced by main().
◆ getBaseModelText()
string getBaseModelText | ( | ) |
Get model as string.
Definition at line 36 of file transport11.cpp.
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}
Referenced by createSaveRestart().
◆ getModelText()
string getModelText | ( | ) |
Get model as string.
Definition at line 76 of file transport11.cpp.
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}
Referenced by main().
◆ main()
int main | ( | int | argc, |
char * | argv[] ) |
Definition at line 111 of file transport11.cpp.
112{
113 cout << "---------- Transport 11 --------------" << endl;
114
115 try {
116 // Create a save/restart file usually supplied by an application provider
117 // We create it for demonstration purpose
118 std::string cpName = "tbase";
119 createSaveRestart(argc, argv, cpName);
120
121 // define some data by using C++ data structures
122 vector<string> plants { "Seattle", "San-Diego" };
123 vector<string> markets {"New-York", "Chicago", "Topeka" };
124 map<string, double> capacity {
125 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
126 };
127 map<string, double> demand {
128 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
129 };
130 map<tuple<string,string>, double> distance {
131 { make_tuple("Seattle", "New-York"), 2.5 },
132 { make_tuple("Seattle", "Chicago"), 1.7 },
133 { make_tuple("Seattle", "Topeka"), 1.8 },
134 { make_tuple("San-Diego", "New-York"), 2.5 },
135 { make_tuple("San-Diego", "Chicago"), 1.8 },
136 { make_tuple("San-Diego", "Topeka"), 1.4 }
137 };
138
139 GAMSWorkspaceInfo wsInfo;
140 if (argc > 1)
141 wsInfo.setSystemDirectory(argv[1]);
143 GAMSWorkspace ws(wsInfo);
144 ws.gamsLib("trnsport");
145
146 // prepare a GAMSDatabase with data from the C++ data structures
147 GAMSDatabase db = ws.addDatabase();
148
150 for (auto plant : plants)
151 i.addRecord(plant);
152
154 for (auto market : markets)
155 j.addRecord(market);
156
158 for (auto plant : plants)
160
162 for (auto market : markets)
164
166 for (auto t : distance) {
167 auto tuple = t.first;
168 auto t1 = get<0>(tuple);
169 auto t2 = get<1>(tuple);
171 }
172
175
176 // run a job using data from the created GAMSDatabase
178 GAMSOptions opt = ws.addOptions();
182 t4.run(opt, db);
184 cout << "x(" << record.key(0) << "," << record.key(1) << "): level=" << record.level() <<
185 " marginal=" << record.marginal() << endl;
186
188 cout << "GAMSException occured: " << ex.what() << endl;
189 } catch (exception &ex) {
190 cout << ex.what() << endl;
191 }
192 return 0;
193}
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
std::string name()
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
void setAllModelTypes(const std::string &solver)
void setDefine(const std::string &key, const std::string &value)
void setValue(const double val)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
void createSaveRestart(int argc, char *argv[], const string &checkpointName)
Create Save and Restart checkpoint.
Definition transport11.cpp:88