Loading...
Searching...
No Matches
Transport2.java
1package com.gams.examples.transport;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7
12import com.gams.api.GAMSJob;
13import com.gams.api.GAMSOptions;
15
21public class Transport2 {
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 // write "data" into file "tdata.gms" under GAMSWorkspace's working directory
39 try {
40 BufferedWriter file = new BufferedWriter(new FileWriter(ws.workingDirectory() + GAMSGlobals.FILE_SEPARATOR + "tdata.gms"));
41 file.write(data);
42 file.close();
43 } catch(IOException e) {
44 e.printStackTrace();
45 System.exit(-1);
46 }
47
48 // create GAMSJob "t2" from the "model" string variable
49 GAMSJob t2 = ws.addJobFromString(model);
50 // create GAMSOption "opt" and define "incname" as "tdata"
51 GAMSOptions opt = ws.addOptions();
52 opt.defines("incname", "tdata");
53 // run GAMSJob "t2" with GAMSOptions "opt"
54 t2.run(opt);
55
56 // retrieve GAMSVariable "x" from GAMSJob's output databases
57 GAMSVariable var = t2.OutDB().getVariable("x");
58 for (GAMSVariableRecord rec : var)
59 System.out.println("x(" + rec.getKey(0) + ", " + rec.getKey(1) + "): level=" + rec.getLevel() + " marginal=" + rec.getMarginal());
60
61 // dispose option and database
62 opt.dispose();
63 t2.OutDB().dispose();
64 // cleanup GAMSWorkspace's working directory
65 cleanup(ws.workingDirectory());
66 // terminate program
67 System.exit(0);
68 }
69
70 static void cleanup(String directory) {
71 File directoryToDelete = new File(directory);
72 String files[] = directoryToDelete.list();
73 for (String file : files) {
74 File fileToDelete = new File(directoryToDelete, file);
75 try {
76 fileToDelete.delete();
77 } catch(Exception e){
78 e.printStackTrace();
79 }
80 }
81 try {
82 directoryToDelete.delete();
83 } catch(Exception e) {
84 e.printStackTrace();
85 }
86 }
87
88
89 // data
90 static String data =
91 "Sets \n" +
92 " i canning plants / seattle, san-diego / \n" +
93 " j markets / new-york, chicago, topeka / ; \n" +
94 "Parameters \n" +
95 " \n" +
96 " a(i) capacity of plant i in cases \n" +
97 " / seattle 350 \n" +
98 " san-diego 600 / \n" +
99 " \n" +
100 " b(j) demand at market j in cases \n" +
101 " / new-york 325 \n" +
102 " chicago 300 \n" +
103 " topeka 275 / ; \n" +
104 " \n" +
105 "Table d(i,j) distance in thousands of miles \n" +
106 " new-york chicago topeka \n" +
107 " seattle 2.5 1.7 1.8 \n" +
108 " san-diego 2.5 1.8 1.4 ; \n" +
109 " \n" +
110 "Scalar f freight in dollars per case per thousand miles /90/ \n " +
111 " \n";
112
113 // model
114 static String model =
115 "Sets \n" +
116 " i canning plants \n" +
117 " j markets \n" +
118 " \n" +
119 "Parameters \n" +
120 " a(i) capacity of plant i in cases \n" +
121 " b(j) demand at market j in cases \n" +
122 " d(i,j) distance in thousands of miles \n" +
123 "Scalar f freight in dollars per case per thousand miles; \n" +
124 " \n" +
125 "$if not set incname $abort 'no include file name for data file provided'\n" +
126 "$include %incname% \n" +
127 " \n" +
128 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
129 " \n" +
130 " c(i,j) = f * d(i,j) / 1000 ; \n" +
131 " \n" +
132 " Variables \n" +
133 " x(i,j) shipment quantities in cases \n" +
134 " z total transportation costs in thousands of dollars ; \n" +
135 " \n" +
136 " Positive Variable x ; \n" +
137 " \n" +
138 " Equations \n" +
139 " \n" +
140 " cost define objective function \n" +
141 " supply(i) observe supply limit at plant i \n" +
142 " demand(j) satisfy demand at market j ; \n" +
143 " \n" +
144 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
145 " \n" +
146 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
147 " \n" +
148 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n" +
149 " \n" +
150 " Model transport /all/ ; \n" +
151 " \n" +
152 " Solve transport using lp minimizing z ; \n" +
153 " \n" +
154 "Display x.l, x.m ; \n" +
155 " \n";
156
157}
158
GAMSVariable getVariable(String identifier)
static final String FILE_SEPARATOR
GAMSDatabase OutDB()
void defines(String defStr, String asStr)
void setSystemDirectory(String directory)
GAMSJob addJobFromString(String source)
This example shows how to include data files to run a job with the simple GAMS [trnsport] model from ...
Definition: Transport2.java:21
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).