Loading...
Searching...
No Matches
transport10.cpp
Go to the documentation of this file.
1/*
2 *
3 * GAMS - General Algebraic Modeling System C++ API
4 *
5 * Copyright (c) 2017-2023 GAMS Software GmbH <support@gams.com>
6 * Copyright (c) 2017-2023 GAMS Development Corp. <support@gams.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27#include "gams.h"
28#include <iostream>
29#include <vector>
30
31using namespace gams;
32using namespace std;
33
34#if defined(__unix__) || defined(__linux__) || defined(__APPLE__)
35
38int main()
39{
40 cout << "---------- Transport 10 --------------" << endl;
41 cout << "Transport 10 is a Microsoft Windows only example." << endl;
42 return 0;
43}
44
45#else
46
47#include <QAxObject>
48#include <Windows.h> // includes "Ole2.h" that includes "objbase.h" to access CoInitialize() and CoUninitialize()
49
50string getModelText()
51{
52 return " Sets \n"
53 " i canning plants \n"
54 " j markets \n"
55 " \n"
56 " Parameters \n"
57 " a(i) capacity of plant i in cases \n"
58 " b(j) demand at market j in cases \n"
59 " d(i,j) distance in thousands of miles \n"
60 " Scalar f freight in dollars per case per thousand miles /90/; \n"
61 " \n"
62 "$if not set gdxincname $abort 'no include file name for data file provided'\n"
63 "$gdxin %gdxincname% \n"
64 "$load i j a b d \n"
65 "$gdxin \n"
66 " \n"
67 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
68 " \n"
69 " c(i,j) = f * d(i,j) / 1000 ; \n"
70 " \n"
71 " Variables \n"
72 " x(i,j) shipment quantities in cases \n"
73 " z total transportation costs in thousands of dollars ; \n"
74 " \n"
75 " Positive Variable x ; \n"
76 " \n"
77 " Equations \n"
78 " cost define objective function \n"
79 " supply(i) observe supply limit at plant i \n"
80 " demand(j) satisfy demand at market j ; \n"
81 " \n"
82 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
83 " \n"
84 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
85 " \n"
86 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
87 " \n"
88 " Model transport /all/ ; \n"
89 " \n"
90 " Solve transport using lp minimizing z ; \n"
91 " \n"
92 " Display x.l, x.m ; \n";
93}
94
105GAMSParameter sheetToParameter(QAxObject* sheets, string sheetName
106 , GAMSDatabase db, string paramName, string paramText, GAMSSet set)
107{
108 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
109 GAMSParameter param = db.addParameter(paramName, paramText, set);
110
111 QAxObject* usedrange = sheet->querySubObject( "UsedRange");
112 QAxObject * columns = usedrange->querySubObject("Columns");
113 int intCols = columns->property("Count").toInt();
114
115 for (int i = 1; i <= intCols; i++) {
116 std::string name = sheet->querySubObject("Cells( int, int )", 1, i)->dynamicCall("Value()").toString().toStdString();
117 double value = sheet->querySubObject("Cells( int, int )", 2, i)->dynamicCall("Value()").toDouble();
118 set.addRecord(name);
119 GAMSParameterRecord rec = param.addRecord(name);
120 rec.setValue(value);
121 }
122 return param;
123}
124
136GAMSParameter sheetToParameter(QAxObject* sheets, string sheetName
137 , GAMSDatabase db, string paramName, string paramText, GAMSSet set1, GAMSSet set2)
138{
139 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
140 vector<GAMSDomain> sets {set1, set2};
141 GAMSParameter param = db.addParameter(paramName, paramText, sets);
142
143 QAxObject* usedrange = sheet->querySubObject( "UsedRange");
144 QAxObject * columns = usedrange->querySubObject("Columns");
145 int intCols = columns->property("Count").toInt();
146 QAxObject * rows = usedrange->querySubObject("Rows");
147 int intRows = rows->property("Count").toInt();
148
149 for (int j = 2; j <= intCols; j++) {
150 string namej = sheet->querySubObject("Cells( int, int )", 1, j)->dynamicCall("Value()").toString().toStdString();
151 for (int i = 2; i <= intRows; ++i) {
152 string namei = sheet->querySubObject("Cells( int, int )", i, 1)->dynamicCall("Value()").toString().toStdString();
153 GAMSParameterRecord rec = param.addRecord(namei, namej);
154 double value = sheet->querySubObject("Cells( int, int )", i, j)->dynamicCall("Value()").toDouble();
155 rec.setValue(value);
156 }
157 }
158 return param;
159}
160
166int main(int argc, char* argv[])
167{
168 cout << "---------- Transport 10 --------------" << endl;
169 try {
170 ::CoInitialize(0); // initialize thread to use ActiveX (some systems may need CoInititializeEx)
171
172 GAMSWorkspaceInfo wsInfo;
173 if (argc > 1)
174 wsInfo.setSystemDirectory(argv[1]);
175 GAMSWorkspace ws(wsInfo);
176
177 // Creating the GAMSDatabase and fill with the workbook data
178 GAMSDatabase db = ws.addDatabase();
179 QString fileName = QString::fromStdString(ws.systemDirectory())+ cPathSep + "apifiles" + cPathSep + "Data" + cPathSep + "transport.xlsx";
180
181 QAxObject* excel = new QAxObject( "Excel.Application", 0 );
182 QAxObject* workbooks = excel->querySubObject( "Workbooks" );
183 QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", fileName );
184 QAxObject* sheets = workbook->querySubObject( "Worksheets" );
185
186 GAMSSet i = db.addSet("i", 1, "Plants");
187 GAMSSet j = db.addSet("j", 1, "Markets");
188
189 // read parameters
190 sheetToParameter(sheets, "capacity", db, "a", "Capacity", i);
191 sheetToParameter(sheets, "demand", db, "b", "Demand", j);
192 sheetToParameter(sheets, "distance", db, "d", "Distance", i, j);
193
194 // clean up and close up
195 workbook->dynamicCall("Close()");
196 excel->dynamicCall("Quit()");
197
198
199 // Create and run the GAMSJob
200 GAMSOptions opt = ws.addOptions();
201 GAMSJob t10 = ws.addJobFromString(getModelText());
202 opt.setDefine("gdxincname", db.name());
203 opt.setAllModelTypes("xpress");
204 t10.run(opt, db);
205 for (GAMSVariableRecord record : t10.outDB().getVariable("x"))
206 cout << "x(" << record.key(0) << "," << record.key(1) << "): level=" << record.level() <<
207 " marginal=" << record.marginal() << endl;
208
209 ::CoUninitialize();
210
211 } catch (GAMSException &ex) {
212 cout << "GAMSException occured: " << ex.what() << endl;
213 } catch (exception &ex) {
214 cout << ex.what() << endl;
215 }
216
217 return 0;
218}
219#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)