Loading...
Searching...
No Matches
transport10.cpp
Go to the documentation of this file.
1
26#include "gams.h"
27#include <iostream>
28#include <vector>
29
30using namespace gams;
31using namespace std;
32
33#if defined(__unix__) || defined(__linux__) || defined(__APPLE__)
34
37int main()
38{
39 cout << "---------- Transport 10 --------------" << endl;
40 cout << "Transport 10 is a Microsoft Windows only example." << endl;
41 return 0;
42}
43
44#else
45
46#include <QAxObject>
47#include <Windows.h> // includes "Ole2.h" that includes "objbase.h" to access CoInitialize() and CoUninitialize()
48
49string getModelText()
50{
51 return " Sets \n"
52 " i canning plants \n"
53 " j markets \n"
54 " \n"
55 " Parameters \n"
56 " a(i) capacity of plant i in cases \n"
57 " b(j) demand at market j in cases \n"
58 " d(i,j) distance in thousands of miles \n"
59 " Scalar f freight in dollars per case per thousand miles /90/; \n"
60 " \n"
61 "$if not set gdxincname $abort 'no include file name for data file provided'\n"
62 "$gdxin %gdxincname% \n"
63 "$load i j a b d \n"
64 "$gdxin \n"
65 " \n"
66 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
67 " \n"
68 " c(i,j) = f * d(i,j) / 1000 ; \n"
69 " \n"
70 " Variables \n"
71 " x(i,j) shipment quantities in cases \n"
72 " z total transportation costs in thousands of dollars ; \n"
73 " \n"
74 " Positive Variable x ; \n"
75 " \n"
76 " Equations \n"
77 " cost define objective function \n"
78 " supply(i) observe supply limit at plant i \n"
79 " demand(j) satisfy demand at market j ; \n"
80 " \n"
81 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
82 " \n"
83 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
84 " \n"
85 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
86 " \n"
87 " Model transport /all/ ; \n"
88 " \n"
89 " Solve transport using lp minimizing z ; \n"
90 " \n"
91 " Display x.l, x.m ; \n";
92}
93
104GAMSParameter sheetToParameter(QAxObject* sheets, string sheetName
105 , GAMSDatabase db, string paramName, string paramText, GAMSSet set)
106{
107 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
108 GAMSParameter param = db.addParameter(paramName, paramText, set);
109
110 QAxObject* usedrange = sheet->querySubObject( "UsedRange");
111 QAxObject * columns = usedrange->querySubObject("Columns");
112 int intCols = columns->property("Count").toInt();
113
114 for (int i = 1; i <= intCols; i++) {
115 std::string name = sheet->querySubObject("Cells( int, int )", 1, i)->dynamicCall("Value()").toString().toStdString();
116 double value = sheet->querySubObject("Cells( int, int )", 2, i)->dynamicCall("Value()").toDouble();
117 set.addRecord(name);
118 GAMSParameterRecord rec = param.addRecord(name);
119 rec.setValue(value);
120 }
121 return param;
122}
123
135GAMSParameter sheetToParameter(QAxObject* sheets, string sheetName
136 , GAMSDatabase db, string paramName, string paramText, GAMSSet set1, GAMSSet set2)
137{
138 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
139 vector<GAMSDomain> sets {set1, set2};
140 GAMSParameter param = db.addParameter(paramName, paramText, sets);
141
142 QAxObject* usedrange = sheet->querySubObject( "UsedRange");
143 QAxObject * columns = usedrange->querySubObject("Columns");
144 int intCols = columns->property("Count").toInt();
145 QAxObject * rows = usedrange->querySubObject("Rows");
146 int intRows = rows->property("Count").toInt();
147
148 for (int j = 2; j <= intCols; j++) {
149 string namej = sheet->querySubObject("Cells( int, int )", 1, j)->dynamicCall("Value()").toString().toStdString();
150 for (int i = 2; i <= intRows; ++i) {
151 string namei = sheet->querySubObject("Cells( int, int )", i, 1)->dynamicCall("Value()").toString().toStdString();
152 GAMSParameterRecord rec = param.addRecord(namei, namej);
153 double value = sheet->querySubObject("Cells( int, int )", i, j)->dynamicCall("Value()").toDouble();
154 rec.setValue(value);
155 }
156 }
157 return param;
158}
159
165int main(int argc, char* argv[])
166{
167 cout << "---------- Transport 10 --------------" << endl;
168 try {
169 ::CoInitialize(0); // initialize thread to use ActiveX (some systems may need CoInititializeEx)
170
171 GAMSWorkspaceInfo wsInfo;
172 if (argc > 1)
173 wsInfo.setSystemDirectory(argv[1]);
174 GAMSWorkspace ws(wsInfo);
175
176 // Creating the GAMSDatabase and fill with the workbook data
177 GAMSDatabase db = ws.addDatabase();
178 QString fileName = QString::fromStdString(ws.systemDirectory())+ cPathSep + "apifiles" + cPathSep + "Data" + cPathSep + "transport.xlsx";
179
180 QAxObject* excel = new QAxObject( "Excel.Application", 0 );
181 QAxObject* workbooks = excel->querySubObject( "Workbooks" );
182 QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", fileName );
183 QAxObject* sheets = workbook->querySubObject( "Worksheets" );
184
185 GAMSSet i = db.addSet("i", 1, "Plants");
186 GAMSSet j = db.addSet("j", 1, "Markets");
187
188 // read parameters
189 sheetToParameter(sheets, "capacity", db, "a", "Capacity", i);
190 sheetToParameter(sheets, "demand", db, "b", "Demand", j);
191 sheetToParameter(sheets, "distance", db, "d", "Distance", i, j);
192
193 // clean up and close up
194 workbook->dynamicCall("Close()");
195 excel->dynamicCall("Quit()");
196
197
198 // Create and run the GAMSJob
199 GAMSOptions opt = ws.addOptions();
200 GAMSJob t10 = ws.addJobFromString(getModelText());
201 opt.setDefine("gdxincname", db.name());
202 opt.setAllModelTypes("xpress");
203 t10.run(opt, db);
204 for (GAMSVariableRecord record : t10.outDB().getVariable("x"))
205 cout << "x(" << record.key(0) << "," << record.key(1) << "): level=" << record.level() <<
206 " marginal=" << record.marginal() << endl;
207
208 ::CoUninitialize();
209
210 } catch (GAMSException &ex) {
211 cout << "GAMSException occured: " << ex.what() << endl;
212 } catch (exception &ex) {
213 cout << ex.what() << endl;
214 }
215
216 return 0;
217}
218#endif
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 setSystemDirectory(std::string systemDir)
GAMSParameter sheetToParameter(QAxObject *sheets, string sheetName, GAMSDatabase db, string paramName, string paramText, GAMSSet set)