Loading...
Searching...
No Matches
transport_gdx.m
1function transport_gdx(varargin)
2
3 % check workspace info from arguments
4 if nargin > 0
5 wsInfo = gams.control.WorkspaceInfo();
6 wsInfo.systemDirectory = varargin{1};
7 ws = gams.control.Workspace(wsInfo);
8 else
9 ws = gams.control.Workspace();
10 end
11
12 % prepare input data
13 plants = {'Seattle', 'San-Diego'};
14 markets = {'New-York', 'Chicago', 'Topeka'};
15 capacity = containers.Map();
16 capacity('Seattle') = 350;
17 capacity('San-Diego') = 600;
18 demand = containers.Map();
19 demand('New-York') = 325;
20 demand('Chicago') = 300;
21 demand('Topeka') = 275;
22 distance = containers.Map();
23 distance('Seattle.New-York') = 2.5;
24 distance('Seattle.Chicago') = 1.7;
25 distance('Seattle.Topeka') = 1.8;
26 distance('San-Diego.New-York') = 2.5;
27 distance('San-Diego.Chicago') = 1.8;
28 distance('San-Diego.Topeka') = 1.4;
29
30 % add a database and add input data into the database
31 db = ws.addDatabase();
32
33 i = db.addSet('i', 1, 'canning plants');
34 for p = plants
35 i.addRecord(p{1});
36 end
37
38 j = db.addSet('j', 1, 'markets');
39 for m = markets
40 j.addRecord(m{1});
41 end
42
43 a = db.addParameter('a', 'capacity of plant i in cases', i);
44 for p = plants
45 rec = a.addRecord(p{1});
46 rec.value = capacity(p{1});
47 end
48
49 b = db.addParameter('b', 'demand at market j in cases', j);
50 for m = markets
51 rec = b.addRecord(m{1});
52 rec.value = demand(m{1});
53 end
54
55 d = db.addParameter('d', 'distance in thousands of miles', i, j);
56 for p = plants
57 for m = markets
58 rec = d.addRecord(p{1}, m{1});
59 rec.value = distance([p{1}, '.', m{1}]);
60 end
61 end
62
63 f = db.addParameter('f', 'freight in dollars per case per thousand miles');
64 rec = f.addRecord();
65 rec.value = 90;
66
67 % export the Database to a GDX file with name 'data.gdx' located in the 'workingDirectory' of the Workspace
68 db.export('data.gdx');
69
70 % add a new Database and initialize it from the GDX file just created
71 gdxdb = ws.addDatabaseFromGDX('data.gdx');
72
73 % read symbol data from the database and fill them into Matlab data structures
74 gdxPlants = {};
75 for rec = gdxdb.getSet('i').records
76 gdxPlants{end+1} = rec{1}.key(1);
77 end
78
79 gdxMarkets = {};
80 for rec = gdxdb.getSet('j').records
81 gdxMarkets{end+1} = rec{1}.key(1);
82 end
83
84 gdxCapacity = containers.Map();
85 for rec = gdxdb.getParameter('a').records
86 gdxCapacity(rec{1}.key(1)) = rec{1}.value;
87 end
88
89 gdxDemand = containers.Map();
90 for rec = gdxdb.getParameter('b').records
91 gdxDemand(rec{1}.key(1)) = rec{1}.value;
92 end
93
94 gdxDistance = containers.Map();
95 for rec = gdxdb.getParameter('d').records
96 gdxDistance([rec{1}.key(1), '.', rec{1}.key(2)]) = rec{1}.value;
97 end
98
99 gdxFreight = gdxdb.getParameter('f').record.value;
100
101 % print out data read from GDX file
102 fprintf('Data read from data.gdx\n');
103 fprintf('Set i: %s\n', gdxdb.getSet('i').text);
104 for p = gdxPlants
105 fprintf(' %s\n', p{1});
106 end
107
108 fprintf('Set j: %s\n', gdxdb.getSet('j').text);
109 for m = gdxMarkets
110 fprintf(' %s\n', m{1});
111 end
112
113 fprintf('Parameter a: %s\n', gdxdb.getParameter('a').text);
114 for a = keys(gdxCapacity)
115 fprintf(' %s: %g\n', a{1}, gdxCapacity(a{1}));
116 end
117
118 fprintf('Parameter b: %s\n', gdxdb.getParameter('b').text);
119 for b = keys(gdxDemand)
120 fprintf(' %s: %g\n', b{1}, gdxDemand(b{1}));
121 end
122
123 fprintf('Parameter d: %s\n', gdxdb.getParameter('d').text);
124 for d = keys(gdxDistance)
125 fprintf(' %s: %g\n', d{1}, gdxDistance(d{1}));
126 end
127
128 fprintf('Scalar f: %s\n %g\n', gdxdb.getParameter('f').text, gdxFreight);
129end