33#if defined(__unix__) || defined(__linux__) || defined(__APPLE__)
41 cout <<
"---------- Transport 9 --------------" << endl;
42 cout <<
"Transport 9 is a Microsoft Windows only example." << endl;
48#include <QCoreApplication>
55 " i canning plants \n"
59 " a(i) capacity of plant i in cases \n"
60 " b(j) demand at market j in cases \n"
61 " d(i,j) distance in thousands of miles \n"
62 " Scalar f freight in dollars per case per thousand miles /90/; \n"
64 "$if not set gdxincname $abort 'no include file name for data file provided' \n"
65 "$gdxin %gdxincname% \n"
69 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
71 " c(i,j) = f * d(i,j) / 1000 ; \n"
74 " x(i,j) shipment quantities in cases \n"
75 " z total transportation costs in thousands of dollars ; \n"
77 " Positive Variable x ; \n"
80 " cost define objective function \n"
81 " supply(i) observe supply limit at plant i \n"
82 " demand(j) satisfy demand at market j ; \n"
84 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
86 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
88 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
90 " Model transport /all/ ; \n"
92 " Solve transport using lp minimizing z ; \n"
94 " Display x.l, x.m ; \n"
99void readSet(QSqlDatabase sqlDb,
GAMSDatabase db,
string strAccessSelect,
string setName,
int setDim,
string setExp =
"")
101 QSqlQuery query(sqlDb);
102 if (!query.exec(strAccessSelect.c_str()))
104 cout <<
"Error executing query on set '" << setName <<
"'" << endl;
105 cout << query.lastError().text().toStdString() << endl;
108 if (query.size() && (query.record().count() != setDim))
110 cout <<
"Number of fields in select statement does not match setDim" << endl;
115 vector<string> keys = vector<string>(setDim);
119 for (
int idx = 0; idx < setDim; idx++)
120 keys[idx] = query.value(idx).toString().toStdString();
128 QSqlQuery query(sqlDb);
129 if (!query.exec(strAccessSelect.c_str()))
131 cout <<
"Error executing query on parameter '" << parName <<
"'" << endl;
132 cout << query.lastError().text().toStdString() << endl;
135 if (query.size() && (query.record().count() != parDim+1))
137 cout <<
"Number of fields in select statement does not match parDim" << endl;
142 vector<string> keys = vector<string>(parDim);
146 for (
int idx = 0; idx < parDim; idx++)
147 keys[idx] = query.value(idx).toString().toStdString();
157 QSqlDatabase sqlDb = QSqlDatabase::addDatabase(
"QODBC",
"readConnection");
159 QString strAccessConn = (
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=" + ws.
systemDirectory() \
160 + cPathSep +
"apifiles" + cPathSep +
"Data" + cPathSep +
"transport.accdb").c_str();
161 sqlDb.setDatabaseName(strAccessConn);
166 readSet(sqlDb, db,
"SELECT Plant FROM Plant",
"i", 1,
"canning plants");
167 readSet(sqlDb, db,
"SELECT Market FROM Market",
"j", 1,
"markets");
170 readParameter(sqlDb, db,
"SELECT Plant,Capacity FROM Plant",
"a", 1,
"capacity of plant i in cases");
171 readParameter(sqlDb, db,
"SELECT Market,Demand FROM Market",
"b", 1,
"demand at market j in cases");
172 readParameter(sqlDb, db,
"SELECT Plant,Market,Distance FROM Distance",
"d", 2,
"distance in thousands of miles");
177 cout <<
"Error: Failed to create a database connection. " << sqlDb.lastError().text().toStdString() << endl;
187 if(domains.size() !=
static_cast<size_t>(var.
dim()))
189 cout <<
"Number of column names does not match the dimension of the variable." << endl;
194 QSqlQuery query(sqlDb);
195 query.exec((
"drop table " + varName).c_str());
197 string queryStr =
"create table " + varName +
"(";
198 for (
string dom : domains)
199 queryStr += dom +
" varchar(64), ";
200 queryStr +=
"lvl double)";
202 query.exec(queryStr.c_str());
206 queryStr =
"insert into " + varName +
"(";
207 for (
string dom : domains)
208 queryStr += dom +
", ";
209 queryStr +=
"lvl) values (";
210 for (
string key : rec.keys())
211 queryStr +=
"'" + key +
"', ";
212 queryStr += std::to_string(rec.level()) +
")";
213 if(!query.exec(queryStr.c_str()))
215 cout <<
"Error: Failed to write variable to the database" << endl;
216 cout << sqlDb.lastError().text().toStdString() << endl;
226 QSqlDatabase sqlDb = QSqlDatabase::addDatabase(
"QODBC",
"writeConnection");
228 QString strAccessConn = (
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=" + ws.
systemDirectory() \
229 + cPathSep +
"apifiles" + cPathSep +
"Data" + cPathSep +
"transport.accdb").c_str();
230 sqlDb.setDatabaseName(strAccessConn);
235 vector<string> domains{
"i",
"j"};
241 cout <<
"Error: Failed to create a database connection. " << sqlDb.lastError().text().toStdString() << endl;
251int main(
int argc,
char* argv[])
253 cout <<
"---------- Transport 9 --------------" << endl;
257 QCoreApplication app(argc, argv);
273 cout <<
"x(" << rec.key(0) <<
"," << rec.key(1) <<
"):" <<
" level=" << rec.level() <<
" marginal="
274 << rec.marginal() << endl;
279 cout <<
"GAMSException occured: " << ex.what() << endl;
280 }
catch (exception &ex) {
281 cout << ex.what() << endl;
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="")
GAMSVariable getVariable(const std::string &name)
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)
std::string systemDirectory() const
GAMSDatabase addDatabase(const std::string &databaseName="", const std::string &inModelName="")
void writeToAccess(GAMSWorkspace ws, GAMSDatabase db)
Write GAMSDatabase to MS Access.
void readSet(QSqlDatabase sqlDb, GAMSDatabase db, string strAccessSelect, string setName, int setDim, string setExp="")
Read GAMSSet from MS Access.
void readParameter(QSqlDatabase sqlDb, GAMSDatabase db, string strAccessSelect, string parName, int parDim, string parExp="")
Read GAMSParameter from MS Access.
void writeVariable(QSqlDatabase sqlDb, GAMSDatabase db, string varName, vector< string > domains)
Write GAMSVariable to MS Access.
string getModelText()
Get model as string.
GAMSDatabase readFromAccess(GAMSWorkspace ws)
Read data from MS Access into a GAMSDatabase.