Loading...
Searching...
No Matches
TransportGDX.java
1package com.gams.examples.transport;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9import java.util.Map.Entry;
10import java.util.Vector;
11
15import com.gams.api.GAMSSet;
19
27public class TransportGDX {
28
29 public static void main(String[] args) {
30 // check workspace info from command line arguments
32 if (args.length > 0)
33 wsInfo.setSystemDirectory( args[0] );
34 // create a directory
35 File workingDirectory = new File(System.getProperty("user.dir"), "TransportGDX");
36 workingDirectory.mkdir();
37 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
38 // create a workspace
39 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
40
41 // prepare input data
42 List<String> plants = Arrays.asList("Seattle", "San-Diego");
43 List<String> markets = Arrays.asList("New-York", "Chicago", "Topeka");
44 Map<String, Double> capacity = new HashMap<String, Double>();
45 {
46 capacity.put("Seattle", Double.valueOf(350.0));
47 capacity.put("San-Diego", Double.valueOf(600.0));
48 }
49 Map<String, Double> demand = new HashMap<String, Double>();
50 {
51 demand.put("New-York", Double.valueOf(325.0));
52 demand.put("Chicago", Double.valueOf(300.0));
53 demand.put("Topeka", Double.valueOf(275.0));
54 }
55
56 Map<Vector<String>, Double> distance = new HashMap<Vector<String>, Double>();
57 {
58 distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "New-York"}) ), Double.valueOf(2.5));
59 distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "Chicago"}) ), Double.valueOf(1.7));
60 distance.put( new Vector<String>( Arrays.asList(new String[]{"Seattle", "Topeka"}) ), Double.valueOf(1.8));
61 distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "New-York"}) ), Double.valueOf(2.5));
62 distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "Chicago"}) ), Double.valueOf(1.8));
63 distance.put( new Vector<String>( Arrays.asList(new String[]{"San-Diego", "Topeka"}) ), Double.valueOf(1.4));
64 }
65
66 // add database and input data into the database
67 GAMSDatabase db = ws.addDatabase();
68
69 GAMSSet i = db.addSet("i", 1, "canning plants");
70 for(String p : plants)
71 i.addRecord(p);
72
73 GAMSSet j = db.addSet("j", 1, "markets");
74 for(String m : markets)
75 j.addRecord(m);
76
77 GAMSParameter a = db.addParameter("a", "capacity of plant i in cases", i);
78 for (String p : plants) {
79 a.addRecord(p).setValue( capacity.get(p) );
80 }
81
82 GAMSParameter b = db.addParameter("b", "demand at market j in cases", j);
83 for(String m : markets)
84 b.addRecord(m).setValue( demand.get(m) );
85
86 GAMSParameter d = db.addParameter("d", "distance in thousands of miles", i, j);
87 for(Vector<String> vd : distance.keySet())
88 d.addRecord(vd).setValue( distance.get(vd).doubleValue() );
89
90 GAMSParameter f = db.addParameter("f", "freight in dollars per case per thousand miles");
91 f.addRecord().setValue( 90 );
92
93 // export the GAMSDatabase to a GDX file with name 'data.gdx' located in the 'workingDirectory' of the GAMSWorkspace
94 db.export("data.gdx");
95
96 // add a new GAMSDatabase and initialize it from the GDX file just created
97 GAMSDatabase gdxdb = ws.addDatabaseFromGDX("data.gdx");
98
99 // read symbol data from the database and fill them into Java data structures
100 List<String> gdxPlants = new ArrayList<String>();
101 for(GAMSSetRecord rec : gdxdb.getSet("i"))
102 gdxPlants.add(rec.getKey(0));
103
104 List<String> gdxMarkets = new ArrayList<String>();
105 for(GAMSSetRecord rec : gdxdb.getSet("j"))
106 gdxMarkets.add(rec.getKey(0));
107
108 Map<String, Double> gdxCapacity = new HashMap<String, Double>();
109 for(GAMSParameterRecord rec : gdxdb.getParameter("a"))
110 gdxCapacity.put(rec.getKey(0), rec.getValue());
111
112 Map<String, Double> gdxDemand = new HashMap<String, Double>();;
113 for(GAMSParameterRecord rec : gdxdb.getParameter("b"))
114 gdxDemand.put(rec.getKey(0), rec.getValue());
115
116 Map<Vector<String>, Double> gdxDistance = new HashMap<Vector<String>, Double>();
117 for(GAMSParameterRecord rec : gdxdb.getParameter("d"))
118 gdxDistance.put( new Vector<String>( Arrays.asList(new String[]{rec.getKey(0), rec.getKey(1)}) ), rec.getValue());
119
120 double gdxFreight = gdxdb.getParameter("f").getFirstRecord().getValue();
121
122 // print out data read from GDX file
123 System.out.println("Data read from data.gdx");
124 System.out.println ("Set i: " + gdxdb.getSet("i").getText() );
125 for(String p : gdxPlants)
126 System.out.println (" " + p);
127
128 System.out.println ("Set j: "+ gdxdb.getSet("j").getText());
129 for(String m : gdxMarkets)
130 System.out.println (" " + m);
131
132 System.out.println ("Parameter a: "+ gdxdb.getParameter("a").getText());
133 for (Entry<String, Double> aEntry : gdxCapacity.entrySet())
134 System.out.println (" " + aEntry.getKey() + ": " + aEntry.getValue());
135
136 System.out.println ("Parameter b: "+ gdxdb.getParameter("b").getText());
137 for (Entry<String, Double> bEntry : gdxDemand.entrySet())
138 System.out.println (" " + bEntry.getKey() + ": " + bEntry.getValue());
139
140 System.out.println ("Parameter d: " + gdxdb.getParameter("d").getText());
141 for (Entry<Vector<String>, Double> dEntry : gdxDistance.entrySet())
142 System.out.println (" " + dEntry.getKey() + ": " + dEntry.getValue());
143
144 System.out.println ("Scalar f: " + gdxdb.getParameter("f").getText());
145 System.out.println (" " + gdxFreight);
146 }
147}
GAMSParameter getParameter(String identifier)
GAMSSet addSet(String identifier, int dimension)
GAMSParameter addParameter(String identifier, int dimension)
GAMSSet getSet(String identifier)
T addRecord(Vector< String > keys)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSDatabase addDatabaseFromGDX(String gdxFileName)
This example shows the use of the GAMSDatabase class for reading and writing GDX files.
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).
&#160;