Loading...
Searching...
No Matches
Transport12.java
1package com.gams.examples.transport;
2
3import java.io.File;
4import java.io.PrintStream;
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.List;
8
14import com.gams.api.GAMSGlobals;
15import com.gams.api.GAMSJob;
19import com.gams.api.GAMSOptions;
22import com.gams.api.GAMSSet;
24import com.gams.api.GAMSSymbol;
29
34public class Transport12
35{
36 public static void main(String[] args)
37 {
38 // check workspace info from command line arguments
40 if (args.length > 0)
41 wsInfo.setSystemDirectory( args[0] );
42 // create a directory
43 File workingDirectory = new File(System.getProperty("user.dir"), "Transport12");
44 workingDirectory.mkdir();
45 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
46 // create a workspace
47 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
48
49 // initialize a checkpont by running a job
51
52 GAMSJob t12 = ws.addJobFromString(model);
53 t12.run(cp);
54
55 // create a ModelInstance and solve it multiple times with different scalar bmult
57
58 double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
59
60 GAMSDatabase db1 = ws.addDatabase();
61
62 GAMSSet scen1 = db1.addSet("scen", 1, "");
63 GAMSParameter bmult = db1.addParameter("bmultlist", "", scen1);
64 GAMSParameter zscen1 = db1.addParameter("zscen", "", scen1);
65
66 int i = 0;
67 for (double b : bmultlist)
68 {
69 bmult.addRecord("s" + i).setValue(b);
70 scen1.addRecord("s" + i);
71 i++;
72 }
73
74 GAMSSet dict = db1.addSet("dict",3,"");
75 dict.addRecord( new String[]{ scen1.getName(), "scenario", "" } );
76 dict.addRecord( new String[]{"bmult", "param", bmult.getName() } );
77 dict.addRecord( new String[]{"z", "level", zscen1.getName()} );
78
79 GUSSCall(dict, mi, "transport use lp min z", null, null, null); //System.out);
80
81 for (GAMSParameterRecord rec : db1.getParameter(zscen1.getName()))
82 System.out.println(rec.getKey(0) + " obj: " + rec.getValue());
83
84 //*******************
85
87 GAMSDatabase db2 = ws.addDatabase();
88
89 GAMSSet scen2 = db2.addSet("scen", 1, "");
90 GAMSParameter zscen2 = db2.addParameter("zscen", "", scen2);
91 GAMSParameter xup = db2.addParameter("xup", 3, "");
92
93 for (int j = 0; j < 4; j++) {
94 for (GAMSSetRecord irec : t12.OutDB().getSet("i"))
95 for (GAMSSetRecord jrec : t12.OutDB().getSet("j"))
96 {
97 String[] keys = new String[] { "s" + j, irec.getKey(0), jrec.getKey(0) };
98 xup.addRecord(keys).setValue(j+1);
99 }
100 scen2.addRecord("s" + j);
101 }
102
103 GAMSSet dict2 = db2.addSet("dict", 3, "");
104 dict2.addRecord( new String[] {scen2.getName(), "scenario", ""} );
105 dict2.addRecord( new String[] {"x", "lower", xup.getName()} );
106 dict2.addRecord( new String[] {"z", "level", zscen2.getName()} );
107
108 GUSSCall(dict2, mi2, "transport use lp min z", null, null, System.out);
109
110 for (GAMSParameterRecord rec : db2.getParameter(zscen2.getName()))
111 System.out.println(rec.getKey(0) + " obj: " + rec.getValue());
112 }
113
114 // Needs to be called with an uninstantiated GAMSModelInstance
115 static void GUSSCall(GAMSSet dict, GAMSModelInstance mi, String solveStatement, GAMSOptions opt, GAMSModelInstanceOpt miOpt, PrintStream output)
116 {
117 List<Object[]> modifierList = Collections.synchronizedList( new ArrayList<Object[]>() );
118
119 if (dict.getDimension() != 3)
120 throw new GAMSException("Dict needs to be 3-dimensional");
121
122 String scenName = dict.getFirstRecord(new String[] { " ", "scenario", " " }).getKey(0);
123 GAMSSet scenSymbol = dict.getDatabase().getSet(scenName);
124
125 for (GAMSSetRecord rec : dict)
126 {
127
128 if (rec.getKey(1).toLowerCase().equals("scenario"))
129 continue;
130 if (rec.getKey(1).toLowerCase().equals("param"))
131 {
132 int modifierDim = dict.getDatabase().getParameter(rec.getKey(2)).getDimension() - scenSymbol.getDimension();
133 if (modifierDim < 0)
134 throw new GAMSException("Dimension of " + rec.getKey(2) + " too small");
135 GAMSModifier mod = new GAMSModifier(mi.SyncDB().addParameter(rec.getKey(0), modifierDim, ""));
136 GAMSParameter param = dict.getDatabase().getParameter( rec.getKey(2));
137 modifierList.add(
138 new Object[] { mod, param }
139 );
140 }
141 else if ((rec.getKey(1).toLowerCase().equals("lower")) ||
142 (rec.getKey(1).toLowerCase().equals("upper")) ||
143 (rec.getKey(1).toLowerCase().equals("fixed")))
144 {
145 int modifierDim = (int) (dict.getDatabase().getParameter(rec.getKey(2)).getDimension() - scenSymbol.getDimension());
146 if (modifierDim < 0)
147 throw new GAMSException("Dimension of " + rec.getKey(2) + " too small");
148 GAMSVariable modifierVar = null;
149 try {
150 modifierVar = dict.getDatabase().getVariable(rec.getKey(0));
151 }
152 catch (Exception e) {
153 modifierVar = mi.SyncDB().addVariable(rec.getKey(0),modifierDim, GAMSGlobals.VarType.FREE, "");
154 }
155 if (rec.getKey(1).toLowerCase().equals("lower"))
156 {
157 GAMSModifier mod = new GAMSModifier(modifierVar, GAMSGlobals.UpdateAction.LOWER, mi.SyncDB().addParameter(rec.getKey(2), modifierDim, ""));
158 GAMSParameter param = dict.getDatabase().getParameter(rec.getKey(2));
159 modifierList.add(
160 new Object[] { mod , param }
161 );
162 }
163 else if (rec.getKey(1).toLowerCase().equals("upper"))
164 {
165 GAMSModifier mod = new GAMSModifier(modifierVar, GAMSGlobals.UpdateAction.UPPER, mi.SyncDB().addParameter(rec.getKey(2), modifierDim, ""));
166 GAMSParameter param = dict.getDatabase().getParameter(rec.getKey(2));
167 modifierList.add(
168 new Object[] { mod , param }
169 );
170 }
171 else { // fixed
172 GAMSModifier mod = new GAMSModifier(modifierVar, GAMSGlobals.UpdateAction.FIXED, mi.SyncDB().addParameter(rec.getKey(2), modifierDim, ""));
173 GAMSParameter param = dict.getDatabase().getParameter(rec.getKey(2));
174 modifierList.add(
175 new Object[] { mod , param }
176 );
177 }
178 }
179 else if ((rec.getKey(1).toLowerCase().equals("level")) || (rec.getKey(1).toLowerCase().equals( "marginal")))
180 {
181 // Check that parameter exists in GAMSDatabase, will throw an exception if not
182 @SuppressWarnings("unused")
183 GAMSParameter x = dict.getDatabase().getParameter(rec.getKey(2));
184 }
185 else
186 throw new GAMSException("Cannot handle UpdateAction " + rec.getKey(1));
187 }
188
189 List<GAMSModifier> mL = Collections.synchronizedList( new ArrayList<GAMSModifier>() );
190 for (Object[] tuple : modifierList) {
191 if (tuple[0] instanceof GAMSModifier)
192 mL.add( (GAMSModifier) tuple[0] );
193 }
194 GAMSModifier[] mods = (GAMSModifier[]) mL.toArray(new GAMSModifier[mL.size()]);
195
196 mi.instantiate(solveStatement, opt, mods);
197
198 List<Object[]> outList = Collections.synchronizedList( new ArrayList<Object[]>() );
199
200 for (GAMSSetRecord s : scenSymbol)
201 {
202 for (Object[] tuple : modifierList)
203 {
205 GAMSParameter pscen = (GAMSParameter) tuple[1];
206
207 GAMSModifier m = (GAMSModifier) tuple[0];
208 if (m.getDataSymbol() == null)
210 else
211 p = m.getDataSymbol();
212
213 // Implemented SymbolUpdateType=BaseCase
214 p.clear();
215
217 String[] filter = new String[pscen.getDimension()];
218 for (int i = 0; i < scenSymbol.getDimension(); i++)
219 filter[i] = s.getKey(i);
220 for (int i = scenSymbol.getDimension(); i < pscen.getDimension(); i++)
221 filter[i] = " ";
222
223 try
224 {
225 rec = pscen.getFirstRecord(filter);
226 } catch (GAMSException e) {
227 continue;
228 }
229 do
230 { String[] myKeys = new String[p.getDimension()];
231 for (int i = 0; i < p.getDimension(); i++)
232 myKeys[i] = rec.getKey(scenSymbol.getDimension()+i);
233 p.addRecord(myKeys).setValue( rec.getValue() );
234 } while (rec.moveNext());
235 }
236
238 if (outList.size() == 0)
239 {
240 for (GAMSSetRecord rec : dict)
241 {
242 if ((rec.getKey(1).toLowerCase().equals("level")) || (rec.getKey(1).toLowerCase().equals("marginal")))
243 {
244 GAMSSymbol<?> sym = mi.SyncDB().getSymbol(rec.getKey(0));
245 GAMSParameter param = dict.getDatabase().getParameter(rec.getKey(2));
246 String str = rec.getKey(1).toLowerCase();
247 outList.add(
248 new Object[] { sym, param, str }
249 );
250 }
251 }
252 }
253 for (Object[] tuple : outList)
254 {
255 GAMSSymbol<?> symbol = (GAMSSymbol<?>) tuple[0];
256 GAMSParameter param = (GAMSParameter) tuple[1];
257 String str = (String)tuple[2];
258 String[] myKeys = new String[scenSymbol.getDimension() + symbol.getFirstRecord().getKeys().length];
259 for (int i = 0; i < scenSymbol.getDimension(); i++)
260 myKeys[i] = s.getKey(i);
261
262 if ((str.equals("level")) && (symbol instanceof GAMSVariable))
263 {
264 GAMSVariable var = (GAMSVariable) symbol;
265 for (GAMSVariableRecord rec : var)
266 {
267 for (int i = 0; i < rec.getKeys().length; i++)
268 myKeys[scenSymbol.getDimension() + i] = s.getKey(i);
269 param.addRecord(myKeys).setValue( rec.getLevel() );
270 }
271 }
272 else if ((str.equals("level")) && (symbol instanceof GAMSEquation))
273 {
274 GAMSEquation eq = (GAMSEquation) symbol;
275 for (GAMSEquationRecord rec : eq)
276 {
277 for (int i = 0; i < rec.getKeys().length; i++)
278 myKeys[scenSymbol.getDimension() + i] = s.getKey(i);
279 param.addRecord(myKeys).setValue( rec.getLevel() );
280 }
281 }
282 else if ((str.equals("marginal")) && (symbol instanceof GAMSVariable))
283 {
284 GAMSVariable var = (GAMSVariable) symbol;
285 for (GAMSVariableRecord rec : var)
286 {
287 for (int i = 0; i < rec.getKeys().length; i++)
288 myKeys[scenSymbol.getDimension() + i] = s.getKey(i);
289 param.addRecord(myKeys).setValue( rec.getMarginal() );
290 }
291 }
292 else if ((str.equals("marginal")) && (symbol instanceof GAMSEquation))
293 {
294 GAMSEquation eq = (GAMSEquation) symbol;
295 for (GAMSEquationRecord rec : eq)
296 {
297 for (int i = 0; i < rec.getKeys().length; i++)
298 myKeys[scenSymbol.getDimension() + i] = s.getKey(i);
299 param.addRecord(myKeys).setValue( rec.getMarginal() );
300 }
301 }
302 }
303 }
304 }
305
306 static String model =
307 "Sets \n" +
308 " i canning plants / seattle, san-diego / \n" +
309 " j markets / new-york, chicago, topeka / ; \n" +
310 " \n" +
311 "Parameters \n" +
312 " \n" +
313 " a(i) capacity of plant i in cases \n" +
314 " / seattle 350 \n" +
315 " san-diego 600 / \n" +
316 " \n" +
317 " b(j) demand at market j in cases \n" +
318 " / new-york 325 \n" +
319 " chicago 300 \n" +
320 " topeka 275 / ; \n" +
321 " \n" +
322 "Table d(i,j) distance in thousands of miles \n" +
323 " new-york chicago topeka \n" +
324 " seattle 2.5 1.7 1.8 \n" +
325 " san-diego 2.5 1.8 1.4 ; \n" +
326 " \n" +
327 "Scalar f freight in dollars per case per thousand miles /90/ ;\n" +
328 "Scalar bmult demand multiplier /1/; \n" +
329 " \n" +
330 "Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
331 " \n" +
332 " c(i,j) = f * d(i,j) / 1000 ; \n" +
333 " \n" +
334 "Variables \n" +
335 " x(i,j) shipment quantities in cases \n" +
336 " z total transportation costs in thousands of dollars ; \n" +
337 " \n" +
338 "Positive Variable x ; \n" +
339 " \n" +
340 "Equations \n" +
341 " cost define objective function \n" +
342 " supply(i) observe supply limit at plant i \n" +
343 " demand(j) satisfy demand at market j ; \n" +
344 " \n" +
345 "cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
346 " \n" +
347 "supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
348 " \n" +
349 "demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; \n" +
350 " \n" +
351 "Model transport /all/ ; \n" +
352 " \n";
353
354}
GAMSModelInstance addModelInstance()
GAMSVariable addVariable(String identifier, int dimension, GAMSGlobals.VarType varType)
GAMSParameter getParameter(String identifier)
GAMSSet addSet(String identifier, int dimension)
GAMSParameter addParameter(String identifier, int dimension)
GAMSSymbol<?> getSymbol(String identifier)
GAMSVariable getVariable(String identifier)
GAMSSet getSet(String identifier)
GAMSDatabase OutDB()
void instantiate(String modelDefinition, GAMSModifier ... modifiers)
GAMSParameter getDataSymbol()
GAMSSymbol<?> getGamsSymbol()
GAMSDatabase getDatabase()
T addRecord(Vector< String > keys)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
GAMSCheckpoint addCheckpoint()
This example demonstrates how to implement a GUSS approach using the GAMS Java API and the GAMS [trns...
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).
&#160;