Loading...
Searching...
No Matches
Transport1.java
1package com.gams.examples.transport;
2
3import java.io.*;
4
5import com.gams.api.*;
6
12public class Transport1 {
13
14 public static void main(String[] args) {
15 GAMSWorkspace ws = null;
16 // check workspace info from command line arguments
17 if (args.length > 0) {
19 wsInfo.setSystemDirectory(args[0]);
20 // create GAMSWorkspace "ws" with user-specified system directory and the default working directory
21 // (the directory named with current date and time under System.getProperty("java.io.tmpdir"))
22 ws = new GAMSWorkspace(wsInfo);
23 } else {
24 // create GAMSWorkspace "ws" with default system directory and default working directory
25 // (the directory named with current date and time under System.getProperty("java.io.tmpdir"))
26 ws = new GAMSWorkspace();
27 }
28
29 // create GAMSJob "t1" from "trnsport" model in GAMS Model Libraries
30 GAMSJob t1 = ws.addJobFromGamsLib("trnsport");
31 // run GAMSJob "t1"
32 t1.run();
33
34 // retrieve GAMSVariable "x" from GAMSJob's output databases
35 System.out.println("Ran with Default:");
36 GAMSVariable x = t1.OutDB().getVariable("x");
37 for (GAMSVariableRecord rec : x) {
38 System.out.print("x(" + rec.getKey(0) + ", " + rec.getKey(1) + "):");
39 System.out.print(", level = " + rec.getLevel());
40 System.out.println(", marginal = " + rec.getMarginal());
41 }
42
43 // create GAMSOptions "opt1"
44 GAMSOptions opt1 = ws.addOptions();
45 // set all model types of "opt1" for "xpress"
46 opt1.setAllModelTypes("xpress");
47 // run GAMSJob "t1" with GAMSOptions "opt1"
48 t1.run(opt1);
49
50 // retrieve GAMSVariable "x" from GAMSJob's output databases
51 GAMSDatabase db1 = t1.OutDB();
52 System.out.println("Ran with XPRESS:");
53 for (GAMSVariableRecord rec : db1.getVariable("x")) {
54 System.out.print("x(" + rec.getKey(0) + ", " + rec.getKey(1) + "):");
55 System.out.print(", level = " + rec.getLevel());
56 System.out.println(", marginal = " + rec.getMarginal());
57 }
58
59 // write file "xpress.opt" under GAMSWorkspace's working directory
60 try {
61 BufferedWriter optFile = new BufferedWriter(new FileWriter(ws.workingDirectory() + GAMSGlobals.FILE_SEPARATOR + "xpress.opt"));
62 optFile.write("algorithm=barrier");
63 optFile.close();
64 } catch(IOException e) {
65 e.printStackTrace();
66 System.exit(-1);
67 }
68
69 // create GAMSOptions "opt2"
70 GAMSOptions opt2 = ws.addOptions();
71 // set all model types of "opt2" for "xpress"
72 opt2.setAllModelTypes( "xpress" );
73 // for "opt2", use "xpress.opt" as solver's option file
74 opt2.setOptFile(1);
75
76 try {
77 // run GAMSJob "t2" with GAMSOptions "opt2" and capture log into "transport1_xpress.log".
78 PrintStream output = new PrintStream(new File(ws.workingDirectory() + GAMSGlobals.FILE_SEPARATOR +"transport1_xpress.log"));
79 t1.run(opt2, output);
80 } catch (FileNotFoundException e) {
81 // run GAMSJob "t2" with GAMSOptions "opt2" and log is written to standard output
82 t1.run(opt2);
83 }
84
85 // retrieve GAMSVariable "x" from GAMSJob's output databases
86 GAMSDatabase db2 = t1.OutDB();
87 System.out.println("Ran with XPRESS with non-default option:");
88 for (GAMSVariableRecord rec : db2.getVariable("x")) {
89 System.out.print("x(" + rec.getKey(0) + ", " + rec.getKey(1) + "):");
90 System.out.print(", level = " + rec.getLevel());
91 System.out.println(", marginal = " + rec.getMarginal());
92 }
93
94 // dispose option and database
95 opt1.dispose();
96 opt2.dispose();
97 db1.dispose();
98 db2.dispose();
99 // cleanup GAMSWorkspace's working directory
100 cleanup(ws.workingDirectory());
101 // terminate program
102 System.exit(0);
103 }
104
105 static void cleanup(String directory) {
106 File directoryToDelete = new File(directory);
107 String files[] = directoryToDelete.list();
108 for (String file : files) {
109 File fileToDelete = new File(directoryToDelete, file);
110 try {
111 fileToDelete.delete();
112 } catch(Exception e){
113 e.printStackTrace();
114 }
115 }
116 try {
117 directoryToDelete.delete();
118 } catch(Exception e) {
119 e.printStackTrace();
120 }
121 }
122}
GAMSVariable getVariable(String identifier)
static final String FILE_SEPARATOR
GAMSDatabase OutDB()
void setAllModelTypes(String value)
void setSystemDirectory(String directory)
GAMSJob addJobFromGamsLib(String modelName)
This example shows how to create and run a GAMSJob from the simple GAMS [trnsport] model from the GAM...
Definition: Transport1.java:12
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).