TransportModel.java
1package com.gams.examples.transport;
2
3import java.io.PrintStream;
4
8import com.gams.api.GAMSJob;
11import com.gams.api.GAMSSet;
14
19public class TransportModel
20{
21 private GAMSSet fi, fj;
22 private GAMSParameter fa, fb, fd, ff;
23 private GAMSVariable fx, fz;
24 private GAMSOptions fopt;
25
26 private GAMSWorkspace fws;
27 private GAMSDatabase fDbIn1, fDbIn2, fDbOut1;
28
29 private GAMSJob job;
30
35 {
36 fws = ws;
37 fopt = ws.addOptions();
38
39 fDbIn1 = ws.addDatabase("dbIn1");
40 fDbIn2 = ws.addDatabase("dbIn2");
41
42 fopt.defines("dbIn1", "dbIn1");
43 fopt.defines("dbIn2", "dbIn2");
44
45 fopt.setSolveLink( GAMSOptions.ESolveLink.LoadLibrary );
46 fopt.setAllModelTypes( "Cplex" );
47 fopt.defines("dbOut1", "dbOut1");
48
49 fi = fDbIn1.addSet("i", "canning plants");
50 fj = fDbIn1.addSet("j", "markets");
51 fa = fDbIn1.addParameter("a", "capacity of plant i in cases", fi);
52 fb = fDbIn1.addParameter("b", "demand at market j in cases", fj);
53 fd = fDbIn1.addParameter("d", "distance in thousands of miles", new Object[] { fi, fj} );
54 ff = fDbIn2.addParameter("f", "freight in dollars per case per thousand miles");
55
56 job = ws.addJobFromString( this.getModelSource() );
57 }
58
62 public void run(GAMSCheckpoint checkpoint)
63 {
64 this.run(checkpoint, null);
65 }
66
70 public void run(PrintStream output)
71 {
72 this.run(null, output);
73 }
74
79 public void run(GAMSCheckpoint checkpoint, PrintStream output) {
80 if (!fDbIn1.checkDomains())
81 throw new GAMSException("Domain Errors in Database 1");
82 if (!fDbIn2.checkDomains())
83 throw new GAMSException("Domain Errors in Database 2");
84
85 GAMSDatabase[] databases = new GAMSDatabase[] {fDbIn1, fDbIn2 };
86 job.run( fopt, checkpoint, output, false, databases );
87
88 fDbOut1 = fws.addDatabaseFromGDX(fopt.getDefinitionOf("dbOut1") + ".gdx");
89 fx = fDbOut1.getVariable("x");
90 fz = fDbOut1.getVariable("z");
91 }
92
94 public GAMSSet geti() { return fi; }
95
97 public GAMSSet getj() { return fj; }
98
100 public GAMSParameter geta() { return fa; }
101
103 public GAMSParameter getb() { return fb; }
104
106 public GAMSParameter getd() { return fd; }
107
109 public GAMSParameter getf() { return ff; }
110
112 public GAMSVariable getx() { return fx; }
113
115 public GAMSVariable getz() { return fz; }
116
118 public GAMSOptions getopt() { return fopt; }
119
121 public String getModelSource() { return model; }
122
123 private static String model =
124 "Sets \n" +
125 " i canning plants \n" +
126 " j markets \n" +
127 " \n" +
128 " Parameters \n" +
129 " a(i) capacity of plant i in cases \n" +
130 " b(j) demand at market j in cases \n" +
131 " d(i,j) distance in thousands of miles \n" +
132 " Scalar f freight in dollars per case per thousand miles; \n" +
133 " \n" +
134 "$if not set dbIn1 $abort 'no file name for in-database 1 file provided' \n" +
135 "$gdxin %dbIn1% \n" +
136 "$load i j a b d \n" +
137 "$gdxin \n" +
138 " \n" +
139 "$if not set dbIn2 $abort 'no file name for in-database 2 file provided' \n" +
140 "$gdxin %dbIn2% \n" +
141 "$load f \n" +
142 "$gdxin \n" +
143 " \n" +
144 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
145 " \n" +
146 " c(i,j) = f * d(i,j) / 1000 ; \n" +
147 " \n" +
148 " Variables \n" +
149 " x(i,j) shipment quantities in cases \n" +
150 " z total transportation costs in thousands of dollars ; \n" +
151 " \n" +
152 " Positive Variable x ; \n" +
153 " \n" +
154 " Equations \n" +
155 " \n" +
156 " cost define objective function \n" +
157 " supply(i) observe supply limit at plant i \n" +
158 " demand(j) satisfy demand at market j ; \n" +
159 " \n" +
160 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
161 " \n" +
162 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
163 " \n" +
164 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n" +
165 " \n" +
166 " Model transport /all/ ; \n" +
167 " \n" +
168 " Solve transport using lp minimizing z ; \n" +
169 " \n" +
170 " Display x.l, x.m ; \n" +
171 "$if not set dbOut1 $abort 'no file name for out-database 1 file provided' \n" +
172 "execute_unload '%dbOut1%', x, z; \n" +
173 " \n";
174}
GAMSVariable getz()
z: total transportation costs in thousands of dollars
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS)...
GAMSVariable getVariable(String identifier)
GAMSParameter addParameter(String identifier, int dimension)
This example shows the wrapper model of a transportation problem based on the simple GAMS [trnsport] ...
 
GAMSSet addSet(String identifier, int dimension)
void run(GAMSCheckpoint checkpoint, PrintStream output)
Executes the trnsport model.
void setSolveLink(GAMSOptions.ESolveLink x)
GAMSDatabase addDatabaseFromGDX(String gdxFileName)
GAMSParameter geta()
a(i): capacity of plant i in cases
void run(GAMSCheckpoint checkpoint)
Executes the trnsport model.
GAMSOptions getopt()
Options for the execution of the trnsport model.
void defines(String defStr, String asStr)
GAMSParameter getb()
b(i): demand at market j in cases
GAMSParameter getd()
d(i,j): distance in thousands of miles
void run(PrintStream output)
Executes the trnsport model.
GAMSVariable getx()
x(i,j): shipment quantities in cases
GAMSJob addJobFromString(String source)
void setAllModelTypes(String value)
TransportModel(GAMSWorkspace ws)
TransportModel constructor.
String getDefinitionOf(String str)
GAMSParameter getf()
f: freight in dollars per case per thousand miles
String getModelSource()
Provide the source of trnsport model.