Loading...
Searching...
No Matches
Transport4.java
1package com.gams.examples.transport;
2
3import java.io.File;
4import java.util.Arrays;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8import java.util.Vector;
9
11import com.gams.api.GAMSJob;
12import com.gams.api.GAMSOptions;
14import com.gams.api.GAMSSet;
19
25public class Transport4 {
26
27 public static void main(String[] args) {
28 // check workspace info from command line arguments
30 if (args.length > 0)
31 wsInfo.setSystemDirectory( args[0] );
32 // create a directory
33 File workingDirectory = new File(System.getProperty("user.dir"), "Transport4");
34 workingDirectory.mkdir();
35 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
36 // create a workspace
37 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
38
39 // prepare input data
40 List<String> plants = Arrays.asList("Seattle", "San-Diego");
41 List<String> markets = Arrays.asList("New-York", "Chicago", "Topeka");
42 Map<String, Double> capacity = new HashMap<String, Double>();
43 {
44 capacity.put("Seattle", Double.valueOf(350.0));
45 capacity.put("San-Diego", Double.valueOf(600.0));
46 }
47 Map<String, Double> demand = new HashMap<String, Double>();
48 {
49 demand.put("New-York", Double.valueOf(325.0));
50 demand.put("Chicago", Double.valueOf(300.0));
51 demand.put("Topeka", Double.valueOf(275.0));
52 }
53
54 Map<Vector<String>, Double> distance = new HashMap<Vector<String>, Double>();
55 {
56 distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "New-York"}) ), Double.valueOf(2.5));
57 distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "Chicago"}) ), Double.valueOf(1.7));
58 distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "Topeka"}) ), Double.valueOf(1.8));
59 distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "New-York"}) ), Double.valueOf(2.5));
60 distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "Chicago"}) ), Double.valueOf(1.8));
61 distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "Topeka"}) ), Double.valueOf(1.4));
62 }
63
64 // add a database and add input data into the database
65 GAMSDatabase db = ws.addDatabase();
66
67 GAMSSet i = db.addSet("i", 1, "canning plants");
68 for(String p : plants)
69 i.addRecord(p);
70
71 GAMSSet j = db.addSet("j", 1, "markets");
72 for(String m : markets)
73 j.addRecord(m);
74
75 GAMSParameter a = db.addParameter("a", "capacity of plant i in cases", i);
76 for (String p : plants) {
77 a.addRecord(p).setValue( capacity.get(p) );
78 }
79
80 GAMSParameter b = db.addParameter("b", "demand at market j in cases", j);
81 for(String m : markets)
82 b.addRecord(m).setValue( demand.get(m) );
83
84 GAMSParameter d = db.addParameter("d", "distance in thousands of miles", i, j);
85 for(Vector<String> vd : distance.keySet())
86 d.addRecord(vd).setValue( distance.get(vd).doubleValue() );
87
88 GAMSParameter f = db.addParameter("f", "freight in dollars per case per thousand miles");
89 f.addRecord().setValue( 90 );
90
91 // create and run a job from the model and read gdx include file from the database
92 GAMSJob t4 = ws.addJobFromString(model);
93 GAMSOptions opt = ws.addOptions();
94 opt.defines("gdxincname", db.getName());
95
96 t4.run(opt, db);
97
98 GAMSVariable var = t4.OutDB().getVariable("x");
99 for(GAMSVariableRecord rec : var)
100 System.out.println("x(" + rec.getKey(0) + ", " + rec.getKey(1) + "): level=" + rec.getLevel() + " marginal=" + rec.getMarginal());
101 System.out.println();
102
103 // set option of all model types for xpress and run the job again
104 opt.setAllModelTypes("xpress");
105 t4.run(opt, db);
106
107 for(GAMSVariableRecord rec : t4.OutDB().getVariable("x"))
108 System.out.println("x(" + rec.getKey(0) + "," + rec.getKey(1) + "): level=" + rec.getLevel() + " marginal=" + rec.getMarginal());
109
110 }
111
112 static String model =
113 "Sets \n" +
114 " i canning plants \n" +
115 " j markets \n" +
116 " \n" +
117 " Parameters \n" +
118 " a(i) capacity of plant i in cases \n" +
119 " b(j) demand at market j in cases \n" +
120 " d(i,j) distance in thousands of miles \n" +
121 " Scalar f freight in dollars per case per thousand miles; \n" +
122 " \n" +
123 "$if not set gdxincname $abort 'no include file name for data file provided'\n" +
124 "$gdxin %gdxincname% \n" +
125 "$load i j a b d f \n" +
126 "$gdxin \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
GAMSSet addSet(String identifier, int dimension)
GAMSParameter addParameter(String identifier, int dimension)
GAMSVariable getVariable(String identifier)
GAMSDatabase OutDB()
void defines(String defStr, String asStr)
void setAllModelTypes(String value)
T addRecord(Vector< String > keys)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
This example shows how to prepare a GAMSDatabase from Java data structures.
Definition: Transport4.java:25
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).
&#160;