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