1package com.gams.examples.transport;
6import java.nio.charset.*;
21 public static void main(String[] args) {
27 File workingDirectory =
new File(System.getProperty(
"user.dir"),
"TransportEngine");
28 workingDirectory.mkdir();
33 String engine_url = System.getenv(
"ENGINE_URL");
34 if (engine_url ==
null) {
35 System.out.println(
"Error: environment variable ENGINE_URL not set");
38 String engine_user = System.getenv(
"ENGINE_USER");
39 if (engine_user ==
null) {
40 System.out.println(
"Error: environment variable ENGINE_USER not set");
43 String engine_password = System.getenv(
"ENGINE_PASSWORD");
44 if (engine_password ==
null) {
45 System.out.println(
"Error: environment variable ENGINE_PASSWORD not set");
48 String engine_namespace = System.getenv(
"ENGINE_NAMESPACE");
49 if (engine_namespace ==
null) {
50 System.out.println(
"Error: environment variable ENGINE_NAMESPACE not set");
63 runEngineJob(ws, engine_configuration);
66 runAndInterruptEngineJob(ws, engine_configuration);
77 System.out.println(
"Running data from string");
79 .setRunEngineConfiguration(engine_configuration)
80 .setOutput( System.out )
84 System.out.println(
"Error: " + e.
getMessage());
88 tEDataDB.
export(
"tdata.gdx");
90 Map<String, Double> expectedLevels =
new HashMap<String, Double>();
92 expectedLevels.put(
"seattle.new-york", Double.valueOf(0.0));
93 expectedLevels.put(
"seattle.chicago", Double.valueOf(300.0));
94 expectedLevels.put(
"seattle.topeka", Double.valueOf(0.0));
95 expectedLevels.put(
"san-diego.new-york", Double.valueOf(325.0));
96 expectedLevels.put(
"san-diego.chicago", Double.valueOf(0.0));
97 expectedLevels.put(
"san-diego.topeka", Double.valueOf(275.0));
103 opt.
defines(
"gdxincname",
"tdata");
106 System.out.println(
"Running using an instance of GAMSOptions that defines the data include file.");
108 .setRunEngineConfiguration( engine_configuration )
109 .setGAMSOptions( opt )
112 { put(
"inex_string",
"{\"type\": \"include\", \"files\": [\"*.gdx\"]}" ); }
117 System.out.println(
"Ran model using an instance of GAMSOptions that defines the data include file:");
119 System.out.print(
"x(" + rec.getKey(0) +
", " + rec.getKey(1) +
"):");
120 System.out.print(
", level = " + rec.getLevel());
121 System.out.println(
", marginal = " + rec.getMarginal());
122 double diff = expectedLevels.get(rec.getKey(0)+
"."+rec.getKey(1)).doubleValue() - rec.getLevel();
123 if (Math.abs(diff) > 0.01) {
124 System.out.println(
"Unexpected result, expected level: "+expectedLevels.get(rec.getKey(0)+
"."+rec.getKey(1)));
129 System.out.println(
"Error: " + e.
getMessage());
136 System.out.println(
"Running data from a string.");
138 .setRunEngineConfiguration(engine_configuration)
139 .setOutput( System.out )
143 System.out.println(
"Error: " + e.
getMessage());
155 System.out.println(
"Running model from a string with checkpoint implicit database communication.");
157 .setRunEngineConfiguration( engine_configuration )
158 .setGAMSOptions( option )
159 .setGAMSCheckpoint( cp )
160 .setDatabases(
new ArrayList<GAMSDatabase>(Arrays.asList(tEa.
OutDB())) )
161 .setOutput( System.out )
164 System.out.println(
"Ran model from a string with checkpoint implicit database communication:");
166 System.out.print(
"x(" + rec.getKey(0) +
", " + rec.getKey(1) +
"):");
167 System.out.print(
", level = " + rec.getLevel());
168 System.out.println(
", marginal = " + rec.getMarginal());
169 double diff = expectedLevels.get(rec.getKey(0)+
"."+rec.getKey(1)).doubleValue() - rec.getLevel();
170 if (Math.abs(diff) > 0.01) {
171 System.out.println(
"Unexpected result, expected level: "+expectedLevels.get(rec.getKey(0)+
"."+rec.getKey(1)));
176 System.out.println(
"Error: " + e.
getMessage());
180 List<HashMap<String, Double>> bmultExpected =
new ArrayList<HashMap<String, Double>>();
181 bmultExpected.add(
new HashMap<String, Double>() {
182 { put(
"bmult", Double.valueOf(0.9) );
183 put(
"ms", Double.valueOf(1) );
184 put(
"ss", Double.valueOf(1) );
185 put(
"obj", Double.valueOf(138.31) );
188 bmultExpected.add(
new HashMap<String, Double>() {
189 { put(
"bmult", Double.valueOf(1.2) );
190 put(
"ms", Double.valueOf(4) );
191 put(
"ss", Double.valueOf(1) );
192 put(
"obj", Double.valueOf(184.41) );
196 for (HashMap<String, Double> m : bmultExpected) {
197 String bmultmodel =
"bmult="+m.get(
"bmult")+
"; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;";
200 System.out.println(
"Running model from a string with bmult="+m.get(
"bmult"));
202 .setRunEngineConfiguration(engine_configuration)
203 .setOutput( System.out )
207 System.out.println(
"Ran model from a string with bumult=" + m.get(
"bmult").doubleValue() +
":");
212 if (Math.abs(diff) > 0.01) {
213 System.out.println(
"Unexpected result, expected bmult: "+m.get(
"bmult").doubleValue());
217 if (Math.abs(diff) > 0.01) {
218 System.out.println(
"Unexpected result, expected ms: "+m.get(
"ms").doubleValue());
222 if (Math.abs(diff) > 0.01) {
223 System.out.println(
"Unexpected result, expected ss: "+m.get(
"ss").doubleValue());
227 if (Math.abs(diff) > 0.01) {
228 System.out.println(
"Unexpected result, expected obj: "+m.get(
"obj").doubleValue());
233 System.out.println(
"Error: " + e.
getMessage());
256 boolean fileCreated = logFile.createNewFile();
259 FileWriter fw =
new FileWriter(optionFile_1);
260 fw.write(
"epgap 0\n");
261 fw.write(
"interactive 1\n");
262 fw.write(
"iafile cplex.op2\n");
266 fw =
new FileWriter(optionFile_2);
267 fw.write(
"epgap 0.1\n");
270 HashSet<String> extraModelFiles =
new HashSet<String>(
271 Arrays.asList(optionFile_1.getAbsolutePath(), optionFile_2.getAbsolutePath(),
"claddat.gdx")
273 final Worker w =
new Worker(job, engine_configuration, opt, extraModelFiles,
new PrintStream(logFile));
277 if (logFile.exists() && logFile.length()==0) {
279 Thread.currentThread();
281 }
catch ( Exception e ) {
288 boolean interrupted =
false;
289 boolean terminated = (w.getState()==Thread.State.TERMINATED);
291 System.out.println(
"Sending interrupt GAMS job...");
294 System.out.println(
"Interrupted Cplex to continue with new option.");
301 }
catch (InterruptedException e) { e.printStackTrace(); }
304 boolean interrupted =
false;
306 Scanner scan =
new Scanner(logFile);
307 String searchstr =
"interrupted...";
308 while (scan.hasNext()) {
309 String line = scan.nextLine().toLowerCase().toString();
310 if (line.contains(searchstr)) {
311 System.out.println(line);
318 System.out.println(
"Expected the solver to be interrupted at least once.");
321 System.out.println(
"Interrupted successfully");
323 }
catch (FileNotFoundException e) {
325 }
catch (IOException e) {
332 static class Worker
extends Thread {
337 HashSet<String> extraModelFiles;
351 extraModelFiles = modelFiles;
358 System.out.println(
"Running GAMS Engine job...");
360 .setRunEngineConfiguration(configuration)
361 .setGAMSOptions(option)
366 }
catch(Exception e) {
376 " i canning plants / seattle, san-diego / \n" +
377 " j markets / new-york, chicago, topeka / ; \n" +
380 " a(i) capacity of plant i in cases \n" +
381 " / seattle 350 \n" +
382 " san-diego 600 / \n" +
384 " b(j) demand at market j in cases \n" +
385 " / new-york 325 \n" +
387 " topeka 275 / ; \n" +
389 "Table d(i,j) distance in thousands of miles \n" +
390 " new-york chicago topeka \n" +
391 " seattle 2.5 1.7 1.8 \n" +
392 " san-diego 2.5 1.8 1.4 ; \n" +
394 "Scalar f 'freight in dollars per case per thousand miles' / 90 / \n" +
395 " bmult 'demand multiplier' / 1 /;\n" +
399 static String model =
401 " i 'canning plants' \n"+
405 " a(i) 'capacity of plant i in cases' \n"+
406 " b(j) 'demand at market j in cases' \n"+
407 " d(i,j) 'distance in thousands of miles'; \n"+
409 "Scalar f 'freight in dollars per case per thousand miles'; \n"+
410 "Scalar bmult 'demand multiplier'; \n"+
412 "$if not set gdxincname $abort 'no include file name for data file provided'\n"+
413 "$gdxin %gdxincname% \n"+
414 "$load i j a b d f bmult \n"+
417 "$echo test > test.txt \n"+
419 "Parameter c(i,j) 'transport cost in thousands of dollars per case'; \n"+
420 " c(i,j) = f * d(i,j) / 1000 ; \n"+
423 " x(i,j) shipment quantities in cases \n"+
424 " z total transportation costs in thousands of dollars ; \n"+
426 "Positive Variable x; \n"+
429 " cost 'define objective function' \n"+
430 " supply(i) 'observe supply limit at plant i' \n"+
431 " demand(j) 'satisfy demand at market j'; \n"+
433 "cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"+
435 "supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"+
437 "demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; \n"+
439 "Model transport /all/ ; \n"+
441 "solve transport using lp minimizing z; \n"+
444 " ms 'model status' \n"+
445 " ss 'solve status'; \n"+
447 "Display x.l, x.m ; \n";
GAMSParameter getParameter(String identifier)
GAMSVariable getVariable(String identifier)
newBuilder setEngineOptions(HashMap< String, String > options)
newBuilder setExtraModelFiles(Set< String > modelFiles)
void runEngine(GAMSEngineRunParameters parameters)
void defines(String defStr, String asStr)
void setAllModelTypes(String value)
void setSolveLink(GAMSOptions.ESolveLink x)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
String workingDirectory()
GAMSCheckpoint addCheckpoint()
GAMSJob addJobFromGamsLib(String modelName)
This example shows how to run a GAMSJob on GAMS Engine using the runEngine method (since GAMS 44):
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).