transport10.cpp File Reference
This is the 10th model in a series of tutorial examples. More...
#include "gams.h"
#include <iostream>
#include <vector>
#include <QAxObject>
#include <Windows.h>
Go to the source code of this file.
Functions | |
string | getModelText () |
GAMSParameter | sheetToParameter (QAxObject *sheets, string sheetName, GAMSDatabase db, string paramName, string paramText, GAMSSet set) |
GAMSParameter | sheetToParameter (QAxObject *sheets, string sheetName, GAMSDatabase db, string paramName, string paramText, GAMSSet set1, GAMSSet set2) |
int | main (int argc, char *argv[]) |
Detailed Description
This is the 10th model in a series of tutorial examples.
Here we show:
- How to fill a GAMSDatabase by reading from MS Excel
Definition in file transport10.cpp.
Function Documentation
◆ getModelText()
string getModelText | ( | ) |
Definition at line 49 of file transport10.cpp.
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}
Referenced by main().
◆ main()
int main | ( | int | argc, |
char * | argv[] ) |
Definition at line 165 of file transport10.cpp.
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
187
188 // read parameters
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();
203 t10.run(opt, db);
205 cout << "x(" << record.key(0) << "," << record.key(1) << "): level=" << record.level() <<
206 " marginal=" << record.marginal() << endl;
207
208 ::CoUninitialize();
209
211 cout << "GAMSException occured: " << ex.what() << endl;
212 } catch (exception &ex) {
213 cout << ex.what() << endl;
214 }
215
216 return 0;
217}
GAMSSet addSet(const std::string &name, const int dimension, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
std::string name()
GAMSVariable getVariable(const std::string &name)
GAMSDatabase outDB()
void run()
void setAllModelTypes(const std::string &solver)
void setDefine(const std::string &key, const std::string &value)
void setSystemDirectory(const std::string &systemDir)
GAMSParameter sheetToParameter(QAxObject *sheets, string sheetName, GAMSDatabase db, string paramName, string paramText, GAMSSet set)
Definition transport10.cpp:104
◆ sheetToParameter() [1/2]
GAMSParameter sheetToParameter | ( | QAxObject * | sheets, |
string | sheetName, | ||
GAMSDatabase | db, | ||
string | paramName, | ||
string | paramText, | ||
GAMSSet | set ) |
Creates an 1 dimensional GAMSParameter and transfers data from an Excel sheet (horizontal ordered)
- Parameters
-
sheets The sheets object of an open Excel workbook sheetName The name of the sheet to be read db The GAMSDatabase where the GAMSParameter is created in paramName The name of the new GAMSParameter paramText The explplanatory text of the new GAMSParameter set The GAMSSet for GAMSParameter dimension
- Returns
- The new GAMSParameter in the GAMSDatabase
Definition at line 104 of file transport10.cpp.
106{
107 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
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);
119 rec.setValue(value);
120 }
121 return param;
122}
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
void setValue(const double val)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)
Referenced by main().
◆ sheetToParameter() [2/2]
GAMSParameter sheetToParameter | ( | QAxObject * | sheets, |
string | sheetName, | ||
GAMSDatabase | db, | ||
string | paramName, | ||
string | paramText, | ||
GAMSSet | set1, | ||
GAMSSet | set2 ) |
Creates a 2 dimensional GAMSParameter and transfers data from an Excel sheet
- Parameters
-
sheets The sheets object of an open Excel workbook sheetName The name of the sheet to be read db The GAMSDatabase where the GAMSParameter is created in paramName The name of the new GAMSParameter paramText The explplanatory text of the new GAMSParameter set1 The GAMSSet for first GAMSParameter dimension set2 The GAMSSet for second GAMSParameter dimension
- Returns
- The new GAMSParameter in the GAMSDatabase
Definition at line 135 of file transport10.cpp.
137{
138 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
139 vector<GAMSDomain> sets {set1, set2};
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();
153 double value = sheet->querySubObject("Cells( int, int )", i, j)->dynamicCall("Value()").toDouble();
154 rec.setValue(value);
155 }
156 }
157 return param;
158}