optimizer.cpp
Go to the documentation of this file.
1/*
2  * GAMS - General Algebraic Modeling System C++ API
3  *
4  * Copyright (c) 2017-2022 GAMS Software GmbH <support@gams.com>
5  * Copyright (c) 2017-2022 GAMS Development Corp. <support@gams.com>s
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 #include "optimizer.h"
26 
27 #include <iostream>
28 #include <thread>
29 
30 using namespace gams;
31 using namespace std;
32 
35 Optimizer::Optimizer(int argc, char *argv[])
36 {
37  GAMSWorkspaceInfo wsInfo;
38  if (argc > 1)
39  wsInfo.setSystemDirectory(argv[1]);
40  ws = GAMSWorkspace(wsInfo);
41 }
42 
43 double Optimizer::solve(double mult)
44 {
45  GAMSDatabase gDb = ws.addDatabase();
46 
47  GAMSParameter f = gDb.addParameter("f", "freight in dollars per case per thousand miles");
48  f.addRecord().setValue(90 * mult);
49 
50  GAMSJob gModJob = ws.addJobFromString(getModelText());
51 
52  GAMSOptions gOption = ws.addOptions();
53  gOption.setDefine("gdxincname", gDb.name());
54  gModJob.run(gOption, gDb);
55 
56  return gModJob.outDB().getVariable("z").firstRecord().level();
57 }
58 
59 string Optimizer::getModelText()
60 {
61  return " Sets \n"
62  " i canning plants / seattle, san-diego / \n"
63  " j markets / new-york, chicago, topeka / ; \n"
64  " \n"
65  " Parameters \n"
66  " \n"
67  " a(i) capacity of plant i in cases \n"
68  " / seattle 350 \n"
69  " san-diego 600 / \n"
70  " \n"
71  " b(j) demand at market j in cases \n"
72  " / new-york 325 \n"
73  " chicago 300 \n"
74  " topeka 275 / ; \n"
75  " \n"
76  " Table d(i,j) distance in thousands of miles \n"
77  " new-york chicago topeka \n"
78  " seattle 2.5 1.7 1.8 \n"
79  " san-diego 2.5 1.8 1.4 ; \n"
80  " \n"
81  " Scalar f freight in dollars per case per thousand miles; \n"
82  " \n"
83  "$if not set gdxincname $abort 'no include file name for data file provided'\n"
84  "$gdxin %gdxincname% \n"
85  "$load f \n"
86  "$gdxin \n"
87  " \n"
88  " Parameter c(i,j) transport cost in thousands of dollars per case ; \n"
89  " \n"
90  " c(i,j) = f * d(i,j) / 1000 ; \n"
91  " \n"
92  " Variables \n"
93  " x(i,j) shipment quantities in cases \n"
94  " z total transportation costs in thousands of dollars ; \n"
95  " \n"
96  " Positive Variable x ; \n"
97  " \n"
98  " Equations \n"
99  " cost define objective function \n"
100  " supply(i) observe supply limit at plant i \n"
101  " demand(j) satisfy demand at market j ; \n"
102  " \n"
103  " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"
104  " \n"
105  " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"
106  " \n"
107  " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"
108  " \n"
109  " Model transport /all/ ; \n"
110  " \n"
111  " Solve transport using lp minimizing z ; \n"
112  " \n"
113  "Display x.l, x.m ; \n";
114 }
void setSystemDirectory(std::string systemDir)
GAMSVariable getVariable(const std::string &name)
GAMSVariableRecord firstRecord(const std::vector< std::string > &slice)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSDatabase outDB()
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
void setValue(const double val)
void setDefine(const std::string &key, const std::string &value)
Wrapper class definition for executing transport14.
std::string name()