Transport11.java
1package com.gams.examples.transport;
2 
3 import java.io.File;
4 import java.util.Arrays;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Vector;
9 
10 import com.gams.api.GAMSCheckpoint;
11 import com.gams.api.GAMSDatabase;
12 import com.gams.api.GAMSJob;
13 import com.gams.api.GAMSOptions;
14 import com.gams.api.GAMSParameter;
15 import com.gams.api.GAMSSet;
17 import com.gams.api.GAMSWorkspace;
19 
24 public class Transport11 {
25 
26  public static void main(String[] args) {
27  // check workspace info from command line arguments
28  GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
29  if (args.length > 0) {
30  wsInfo.setSystemDirectory( args[0] );
31  }
32  // create a directory
33  File workingDirectory = new File(System.getProperty("user.dir"), "Transport11");
34  workingDirectory.mkdir();
35  wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
36  // create a workspace
37  GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
38 
39  // Create a save/restart file usually supplied by an application provider
40  // We create it for demonstration purpose
41  CreateSaveRestart( ws, "tbase" );
42 
43  // define some data by using Java data structures
44  List<String> plants = Arrays.asList("Seattle", "San-Diego");
45  List<String> markets = Arrays.asList("New-York", "Chicago", "Topeka");
46 
47  Map<String, Double> capacity = new HashMap<String, Double>();
48  {
49  capacity.put("Seattle", Double.valueOf(350.0));
50  capacity.put("San-Diego", Double.valueOf(600.0));
51  }
52 
53  Map<String, Double> demand = new HashMap<String, Double>();
54  {
55  demand.put("New-York", Double.valueOf(325.0));
56  demand.put("Chicago", Double.valueOf(300.0));
57  demand.put("Topeka", Double.valueOf(275.0));
58  }
59 
60  Map<Vector<String>, Double> distance = new HashMap<Vector<String>, Double>();
61  {
62  distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "New-York"}) ), Double.valueOf(2.5));
63  distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "Chicago"}) ), Double.valueOf(1.7));
64  distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "Topeka"}) ), Double.valueOf(1.8));
65  distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "New-York"}) ), Double.valueOf(2.5));
66  distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "Chicago"}) ), Double.valueOf(1.8));
67  distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "Topeka"}) ), Double.valueOf(1.4));
68  }
69 
70  ws = new GAMSWorkspace(wsInfo);
71 
72  // prepare a GAMSDatabase with data from the Java data structures
73  GAMSDatabase db = ws.addDatabase();
74 
75  GAMSSet i = db.addSet("i", 1, "canning plants");
76  for (String p : plants)
77  i.addRecord(p);
78 
79  GAMSSet j = db.addSet("j", 1, "markets");
80  for (String m : markets)
81  j.addRecord(m);
82 
83  GAMSParameter a = db.addParameter("a", "capacity of plant i in cases", i);
84  for (String p : plants)
85  a.addRecord(p).setValue( capacity.get(p) );
86 
87  GAMSParameter b = db.addParameter("b", "demand at market j in cases", j);
88  for (String m : markets)
89  b.addRecord(m).setValue( demand.get(m) );
90 
91  GAMSParameter d = db.addParameter("d", "distance in thousands of miles", i, j);
92  for(Vector<String> vd : distance.keySet())
93  d.addRecord(vd).setValue( distance.get(vd).doubleValue() );
94 
95  GAMSParameter f = db.addParameter("f", "freight in dollars per case per thousand miles");
96  f.addRecord().setValue( 90 );
97 
98  // run a job using data from the created GAMSDatabase
99  GAMSCheckpoint cpBase = ws.addCheckpoint("tbase");
100 
101  GAMSOptions opt = ws.addOptions();
102  GAMSJob t11 = ws.addJobFromString(model, cpBase);
103  opt.defines("gdxincname", db.getName());
104  opt.setAllModelTypes("xpress");
105  t11.run(opt, db);
106 
107  for (GAMSVariableRecord rec : t11.OutDB().getVariable("x"))
108  System.out.println("x(" + rec.getKey(0) + "," + rec.getKey(1) + "): level=" + rec.getLevel() + " marginal=" + rec.getMarginal());
109  }
110 
111  static void CreateSaveRestart(GAMSWorkspace ws, String cpFileName) {
112  //GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
113  //wsInfo.setWorkingDirectory(workingDirectory);
114  //GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
115 
116  GAMSJob j1 = ws.addJobFromString(baseModel);
117  GAMSOptions opt = ws.addOptions();
118 
119  opt.setAction( GAMSOptions.EAction.CompileOnly );
120 
121  GAMSCheckpoint cp = ws.addCheckpoint(cpFileName);
122  j1.run(opt, cp);
123 
124  opt.dispose();
125  }
126 
127  static String baseModel =
128  "$onempty \n"+
129  " Sets \n"+
130  " i(*) canning plants / / \n"+
131  " j(*) markets / / \n"+
132  " \n"+
133  " Parameters \n"+
134  " a(i) capacity of plant i in cases / / \n"+
135  " b(j) demand at market j in cases / / \n"+
136  " d(i,j) distance in thousands of miles / / \n"+
137  " Scalar f freight in dollars per case per thousand miles /0/; \n"+
138  " \n"+
139  " Parameter c(i,j) transport cost in thousands of dollars per case ;\n"+
140  " \n"+
141  " c(i,j) = f * d(i,j) / 1000 ; \n"+
142  " \n"+
143  " Variables \n"+
144  " x(i,j) shipment quantities in cases \n"+
145  " z total transportation costs in thousands of dollars ; \n"+
146  " \n"+
147  " Positive Variable x ; \n"+
148  " \n"+
149  " Equations \n"+
150  " cost define objective function \n"+
151  " supply(i) observe supply limit at plant i \n"+
152  " demand(j) satisfy demand at market j ; \n"+
153  " \n"+
154  " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"+
155  " \n"+
156  " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"+
157  " \n"+
158  " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"+
159  " \n"+
160  " Model transport /all/ ; \n"+
161  " \n"+
162  " Solve transport using lp minimizing z ; \n"+
163  " \n";
164 
165  static String model =
166  "$if not set gdxincname $abort 'no include file name for data file provided' \n"+
167  "$gdxin %gdxincname% \n"+
168  "$onMulti \n"+
169  "$load i j a b d f \n"+
170  "$gdxin \n"+
171  " \n"+
172  "Display x.l, x.m ; \n"+
173  " \n";
174 }
void setAction(GAMSOptions.EAction x)
void defines(String defStr, String asStr)
void setSystemDirectory(String directory)
This example shows how to create and use save/restart file.
GAMSParameter addParameter(String identifier, int dimension)
GAMSSet addSet(String identifier, int dimension)
GAMSJob addJobFromString(String source)
GAMSCheckpoint addCheckpoint()
void setWorkingDirectory(String directory)
GAMSDatabase OutDB()
GAMSVariable getVariable(String identifier)
T addRecord(Vector< String > keys)
void setAllModelTypes(String value)