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