Loading...
Searching...
No Matches
Transport12.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using GAMS;
6
7namespace TransportSeq
8{
18 {
19 // Needs to be called with an uninstantiated GAMSModelInstance
20 static void GUSSCall(GAMSSet dict, GAMSModelInstance mi, string solveStatement, GAMSOptions opt = null, GAMSModelInstanceOpt miOpt = null, TextWriter output=null)
21 {
22 List<Tuple<GAMSModifier, GAMSParameter>> modifierList = new List<Tuple<GAMSModifier, GAMSParameter>>();
23
24 if (dict.Dim != 3)
25 throw new GAMSException("Dict needs to be 3-dimensional");
26
27 string scenName = dict.FirstRecord(new string[] { " ", "scenario", " " }).Key(0);
28 GAMSSet scenSymbol = dict.GAMSDatabase.GetSet(scenName);
29
30
31 foreach (GAMSSetRecord rec in dict)
32 {
33 if (rec.Key(1).ToLower() == "scenario")
34 continue;
35 if (rec.Key(1).ToLower() == "param")
36 {
37 int modifierDim = dict.GAMSDatabase.GetParameter(rec.Key(2)).Dim - scenSymbol.Dim;
38 if (modifierDim < 0)
39 throw new GAMSException("Dimension of " + rec.Key(2) + " too small");
40 modifierList.Add(new Tuple<GAMSModifier, GAMSParameter>
41 (new GAMSModifier(mi.SyncDB.AddParameter(rec.Key(0), modifierDim, "")),
42 dict.GAMSDatabase.GetParameter(rec.Key(2))));
43 }
44 else if ((rec.Key(1).ToLower() == "lower") || (rec.Key(1).ToLower() == "upper") || (rec.Key(1).ToLower() == "fixed"))
45 {
46 int modifierDim = dict.GAMSDatabase.GetParameter(rec.Key(2)).Dim - scenSymbol.Dim;
47 if (modifierDim < 0)
48 throw new GAMSException("Dimension of " + rec.Key(2) + " too small");
49 GAMSVariable modifierVar;
50 try
51 {
52 modifierVar = dict.GAMSDatabase.GetVariable(rec.Key(0));
53 }
54 catch (Exception)
55 {
56 modifierVar = mi.SyncDB.AddVariable(rec.Key(0),modifierDim, VarType.Free, "");
57 }
58 if (rec.Key(1).ToLower() == "lower")
59 modifierList.Add(new Tuple<GAMSModifier, GAMSParameter>
60 (new GAMSModifier(modifierVar, UpdateAction.Lower, mi.SyncDB.AddParameter(rec.Key(2), modifierDim, "")),
61 dict.GAMSDatabase.GetParameter(rec.Key(2))));
62 else if (rec.Key(1).ToLower() == "upper")
63 modifierList.Add(new Tuple<GAMSModifier, GAMSParameter>
64 (new GAMSModifier(modifierVar, UpdateAction.Upper, mi.SyncDB.AddParameter(rec.Key(2), modifierDim, "")),
65 dict.GAMSDatabase.GetParameter(rec.Key(2))));
66 else // fixed
67 modifierList.Add(new Tuple<GAMSModifier, GAMSParameter>
68 (new GAMSModifier(modifierVar, UpdateAction.Fixed, mi.SyncDB.AddParameter(rec.Key(2), modifierDim, "")),
69 dict.GAMSDatabase.GetParameter(rec.Key(2))));
70 }
71 else if ((rec.Key(1).ToLower() == "level") || (rec.Key(1).ToLower() == "marginal"))
72 {
73 // Check that parameter exists in GAMSDatabase, will throw an exception if not
75 }
76 else
77 throw new GAMSException("Cannot handle UpdateAction " + rec.Key(1));
78 }
79 List<GAMSModifier> mL = new List<GAMSModifier>();
80 foreach (Tuple<GAMSModifier,GAMSParameter> tup in modifierList)
81 mL.Add(tup.Item1);
82 mi.Instantiate(solveStatement, opt, mL.ToArray());
83
84 List<Tuple<GAMSSymbol, GAMSParameter, string>> outList = new List<Tuple<GAMSSymbol, GAMSParameter, string>>();
85
86 foreach (GAMSSetRecord s in scenSymbol)
87 {
88 foreach (Tuple<GAMSModifier, GAMSParameter> tup in modifierList)
89 {
91 GAMSParameter pscen = tup.Item2;
92
93 if (tup.Item1.DataSym == null)
94 p = (GAMSParameter)tup.Item1.GamsSym;
95 else
96 p = tup.Item1.DataSym;
97
98 // Implemented SymbolUpdateType=BaseCase
99 p.Clear();
100
102 string[] filter = new string[pscen.Dim];
103 for (int i = 0; i < scenSymbol.Dim; i++)
104 filter[i] = s.Key(i);
105 for (int i = scenSymbol.Dim; i < pscen.Dim; i++)
106 filter[i] = " ";
107 try
108 {
109 rec = pscen.FirstRecord(filter);
110 }
111 catch (GAMSException)
112 {
113 continue;
114 }
115 do
116 {
117 string[] myKeys = new string[p.Dim];
118 for (int i = 0; i < p.Dim; i++)
119 myKeys[i] = rec.Key(scenSymbol.Dim+i);
120 p.AddRecord(myKeys).Value = rec.Value;
121 } while (rec.MoveNext());
122 }
123
124 mi.Solve(GAMSModelInstance.SymbolUpdateType.BaseCase, output, miOpt);
125 if (outList.Count == 0)
126 foreach (GAMSSetRecord rec in dict)
127 if ((rec.Key(1).ToLower() == "level") || (rec.Key(1).ToLower() == "marginal"))
128 outList.Add(new Tuple<GAMSSymbol, GAMSParameter, string>(mi.SyncDB.GetSymbol(rec.Key(0)), dict.GAMSDatabase.GetParameter(rec.Key(2)), rec.Key(1).ToLower()));
129
130 foreach (Tuple<GAMSSymbol, GAMSParameter, string> tup in outList)
131 {
132 string[] myKeys = new string[scenSymbol.Dim + tup.Item1.FirstRecord().Keys.Length];
133 for (int i = 0; i < scenSymbol.Dim; i++)
134 myKeys[i] = s.Key(i);
135
136 if ((tup.Item3 == "level") && (tup.Item1 is GAMSVariable))
137 foreach (GAMSVariableRecord rec in tup.Item1)
138 {
139 for (int i = 0; i < rec.Keys.Length; i++)
140 myKeys[scenSymbol.Dim + i] = s.Key(i);
141 tup.Item2.AddRecord(myKeys).Value = rec.Level;
142 }
143 else if ((tup.Item3 == "level") && (tup.Item1 is GAMSEquation))
144 foreach (GAMSEquationRecord rec in tup.Item1)
145 {
146 for (int i = 0; i < rec.Keys.Length; i++)
147 myKeys[scenSymbol.Dim + i] = s.Key(i);
148 tup.Item2.AddRecord(myKeys).Value = rec.Level;
149 }
150 else if ((tup.Item3 == "marginal") && (tup.Item1 is GAMSVariable))
151 foreach (GAMSVariableRecord rec in tup.Item1)
152 {
153 for (int i = 0; i < rec.Keys.Length; i++)
154 myKeys[scenSymbol.Dim + i] = s.Key(i);
155 tup.Item2.AddRecord(myKeys).Value = rec.Marginal;
156 }
157 else if ((tup.Item3 == "marginal") && (tup.Item1 is GAMSEquation))
158 foreach (GAMSEquationRecord rec in tup.Item1)
159 {
160 for (int i = 0; i < rec.Keys.Length; i++)
161 myKeys[scenSymbol.Dim + i] = s.Key(i);
162 tup.Item2.AddRecord(myKeys).Value = rec.Marginal;
163 }
164 }
165 }
166 }
167
168 static void Main(string[] args)
169 {
170 GAMSWorkspace ws;
171 if (Environment.GetCommandLineArgs().Length > 1)
172 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
173 else
174 ws = new GAMSWorkspace();
176
177 // initialize a GAMSCheckpoint by running a GAMSJob
178 GAMSJob t12 = ws.AddJobFromString(GetModelText());
179 t12.Run(cp);
180
181 // create a GAMSModelInstance and solve it multiple times with different scalar bmult
183
184 double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
185
186 GAMSDatabase db = ws.AddDatabase();
187
188 GAMSSet scen = db.AddSet("scen", 1, "");
189 GAMSParameter bmult = db.AddParameter("bmultlist", "", scen);
190 GAMSParameter zscen = db.AddParameter("zscen", "", scen);
191
192 int i = 0;
193 foreach (double b in bmultlist)
194 {
195 bmult.AddRecord("s" + i).Value = b;
196 scen.AddRecord("s" + i++);
197 }
198
199 GAMSSet dict = db.AddSet("dict",3,"");
200 dict.AddRecord(scen.Name, "scenario", "");
201 dict.AddRecord("bmult", "param", bmult.Name);
202 dict.AddRecord("z", "level", zscen.Name);
203
204
205 GUSSCall(dict, mi, "transport use lp min z");
206
207 foreach (GAMSParameterRecord rec in db.GetParameter(zscen.Name))
208 Console.WriteLine(rec.Key(0) + " obj: " + rec.Value);
209
210 //*******************
211
212 GAMSModelInstance mi2 = cp.AddModelInstance();
213 GAMSDatabase db2 = ws.AddDatabase();
214
215 GAMSSet scen2 = db2.AddSet("scen", 1, "");
216 GAMSParameter zscen2 = db2.AddParameter("zscen", "", scen2);
217 GAMSParameter xup = db2.AddParameter("xup", 3, "");
218
219 for (int j = 0; j < 4; j++)
220 {
221 foreach (GAMSSetRecord irec in t12.OutDB.GetSet("i"))
222 foreach (GAMSSetRecord jrec in t12.OutDB.GetSet("j"))
223 xup.AddRecord("s" + j, irec.Key(0), jrec.Key(0)).Value = j+1;
224 scen2.AddRecord("s" + j);
225 }
226
227
228 GAMSSet dict2 = db2.AddSet("dict", 3, "");
229 dict2.AddRecord(scen2.Name, "scenario", "");
230 dict2.AddRecord("x", "lower", xup.Name);
231 dict2.AddRecord("z", "level", zscen2.Name);
232
233 GUSSCall(dict2, mi2, "transport use lp min z", output: Console.Out);
234
235 foreach (GAMSParameterRecord rec in db2.GetParameter(zscen2.Name))
236 Console.WriteLine(rec.Key(0) + " obj: " + rec.Value);
237 }
238
239 static String GetModelText()
240 {
241 String model = @"
242 Sets
243 i canning plants / seattle, san-diego /
244 j markets / new-york, chicago, topeka / ;
245
246 Parameters
247
248 a(i) capacity of plant i in cases
249 / seattle 350
250 san-diego 600 /
251
252 b(j) demand at market j in cases
253 / new-york 325
254 chicago 300
255 topeka 275 / ;
256
257 Table d(i,j) distance in thousands of miles
258 new-york chicago topeka
259 seattle 2.5 1.7 1.8
260 san-diego 2.5 1.8 1.4 ;
261
262 Scalar f freight in dollars per case per thousand miles /90/ ;
263 Scalar bmult demand multiplier /1/;
264
265 Parameter c(i,j) transport cost in thousands of dollars per case ;
266
267 c(i,j) = f * d(i,j) / 1000 ;
268
269 Variables
270 x(i,j) shipment quantities in cases
271 z total transportation costs in thousands of dollars ;
272
273 Positive Variable x ;
274
275 Equations
276 cost define objective function
277 supply(i) observe supply limit at plant i
278 demand(j) satisfy demand at market j ;
279
280 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
281
282 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
283
284 demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
285
286 Model transport /all/ ;
287";
288
289 return model;
290 }
291
292 }
293}
GAMSModelInstance AddModelInstance(string modelInstanceName=null)
GAMSVariable GetVariable(string variableIdentifier)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSParameter GetParameter(string parameterIdentifier)
GAMSSet GetSet(string setIdentifier)
GAMSVariable AddVariable(string identifier, int dimension, VarType varType, string explanatoryText="")
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
GAMSSymbol GetSymbol(string symbolIdentifier)
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
void Solve(SymbolUpdateType updateType=SymbolUpdateType.BaseCase, TextWriter output=null, GAMSModelInstanceOpt miOpt=null)
void Instantiate(string modelDefinition, params GAMSModifier[] modifiers)
new GAMSParameterRecord FirstRecord()
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSSetRecord AddRecord(params string[] keys)
new GAMSSetRecord FirstRecord()
string Key(int index)
GAMSDatabase GAMSDatabase
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
This is the 12th model in a series of tutorial examples. Here we show: How to implement a GUSS approa...
Definition: Transport12.cs:18
UpdateAction