Loading...
Searching...
No Matches
Transport3.java
1package com.gams.examples.transport;
2
3import java.io.File;
4
6import com.gams.api.GAMSJob;
12
19public class Transport3 {
20
21 public static void main(String[] args) {
22 // check workspace info from command line arguments
24 if (args.length > 0)
25 wsInfo.setSystemDirectory( args[0] );
26 // create a directory
27 File workingDirectory = new File(System.getProperty("user.dir"), "Transport3");
28 workingDirectory.mkdir();
29 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
30 // create a workspace
31 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
32
33 // Create and run a job from a data file, then explicitly export to a GDX file
34 GAMSJob t3 = ws.addJobFromString(data);
35 t3.run();
36 t3.OutDB().export( ws.workingDirectory() + GAMSGlobals.FILE_SEPARATOR + "tdata.gdx" );
37
38 // run a job using an instance of GAMSOptions that defines the data include file
39 t3 = ws.addJobFromString(model);
40
41 GAMSOptions opt = ws.addOptions();
42 opt.defines("gdxincname", "tdata");
43 t3.run(opt);
44
45 GAMSVariable x = t3.OutDB().getVariable("x");
46 for (GAMSVariableRecord rec : x)
47 System.out.println("x(" + rec.getKey(0) + ", " + rec.getKey(1) + "): level=" + rec.getLevel() + " marginal=" + rec.getMarginal());
48 System.out.println();
49
50 // similar to the previous run but without exporting database into a file
51 GAMSJob t3a = ws.addJobFromString(data);
52 GAMSJob t3b = ws.addJobFromString(model);
53 opt = ws.addOptions();
54
55 t3a.run();
56
57 opt.defines("gdxincname", t3a.OutDB().getName());
58 t3b.run(opt, t3a.OutDB());
59
60 for(GAMSVariableRecord rec : t3b.OutDB().getVariable("x"))
61 System.out.println("x(" + rec.getKey(0) + ", " + rec.getKey(1) + "): level=" + rec.getLevel() + " marginal=" + rec.getMarginal());
62
63 }
64
65 static String data =
66 "Sets \n" +
67 " i canning plants / seattle, san-diego / \n" +
68 " j markets / new-york, chicago, topeka / ; \n" +
69 "Parameters \n" +
70 " \n" +
71 " a(i) capacity of plant i in cases \n" +
72 " / seattle 350 \n" +
73 " san-diego 600 / \n" +
74 " \n" +
75 " b(j) demand at market j in cases \n" +
76 " / new-york 325 \n" +
77 " chicago 300 \n" +
78 " topeka 275 / ; \n" +
79 " \n" +
80 "Table d(i,j) distance in thousands of miles \n" +
81 " new-york chicago topeka \n" +
82 " seattle 2.5 1.7 1.8 \n" +
83 " san-diego 2.5 1.8 1.4 ; \n" +
84 " \n" +
85 "Scalar f freight in dollars per case per thousand miles /90/ ; \n ";
86
87 static String model = "Sets \n" +
88 " i canning plants \n" +
89 " j markets \n" +
90 " \n" +
91 " Parameters \n" +
92 " a(i) capacity of plant i in cases \n" +
93 " b(j) demand at market j in cases \n" +
94 " d(i,j) distance in thousands of miles \n" +
95 " Scalar f freight in dollars per case per thousand miles; \n" +
96 " \n" +
97 "$if not set gdxincname $abort 'no include file name for data file provided'\n" +
98 "$gdxin %gdxincname% \n" +
99 "$load i j a b d f \n" +
100 "$gdxin \n" +
101 " \n" +
102 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
103 " \n" +
104 " c(i,j) = f * d(i,j) / 1000 ; \n" +
105 " \n" +
106 " Variables \n" +
107 " x(i,j) shipment quantities in cases \n" +
108 " z total transportation costs in thousands of dollars ; \n" +
109 " \n" +
110 " Positive Variable x ; \n" +
111 " \n" +
112 " Equations \n" +
113 " \n" +
114 " cost define objective function \n" +
115 " supply(i) observe supply limit at plant i \n" +
116 " demand(j) satisfy demand at market j ; \n" +
117 " \n" +
118 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
119 " \n" +
120 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
121 " \n" +
122 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n" +
123 " \n" +
124 " Model transport /all/ ; \n" +
125 " \n" +
126 " Solve transport using lp minimizing z ; \n" +
127 " \n" +
128 " Display x.l, x.m ; \n" +
129 " \n";
130
131}
GAMSVariable getVariable(String identifier)
static final String FILE_SEPARATOR
GAMSDatabase OutDB()
void defines(String defStr, String asStr)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
This example shows how to set a non-default working directory, to read data from string,...
Definition: Transport3.java:19
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).