Loading...
Searching...
No Matches
transport11.m
1function transport11(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 % Create a save/restart file usually supplied by an application provider
13 % We create it for demonstration purpose
14 create_save_restart(ws.workingDirectory, ws.systemDirectory, ws.debugLevel, 'tbase');
15
16 model = {
17 '$if not set gdxincname $abort ''no include file name for data file provided'' '
18 '$gdxin %gdxincname% '
19 '$onMulti '
20 '$load i j a b d f '
21 '$gdxin '
22 ' '
23 'Display x.l, x.m ; '
24 ' '};
25 model = sprintf('%s\n', model{:});
26
27 % prepare input data
28 plants = {'Seattle', 'San-Diego'};
29 markets = {'New-York', 'Chicago', 'Topeka'};
30 capacity = containers.Map();
31 capacity('Seattle') = 350;
32 capacity('San-Diego') = 600;
33 demand = containers.Map();
34 demand('New-York') = 325;
35 demand('Chicago') = 300;
36 demand('Topeka') = 275;
37 distance = containers.Map();
38 distance('Seattle.New-York') = 2.5;
39 distance('Seattle.Chicago') = 1.7;
40 distance('Seattle.Topeka') = 1.8;
41 distance('San-Diego.New-York') = 2.5;
42 distance('San-Diego.Chicago') = 1.8;
43 distance('San-Diego.Topeka') = 1.4;
44
45 % add a database and add input data into the database
46 db = ws.addDatabase();
47
48 i = db.addSet('i', 1, 'canning plants');
49 for p = plants
50 i.addRecord(p{1});
51 end
52
53 j = db.addSet('j', 1, 'markets');
54 for m = markets
55 j.addRecord(m{1});
56 end
57
58 a = db.addParameter('a', 'capacity of plant i in cases', i);
59 for p = plants
60 rec = a.addRecord(p{1});
61 rec.value = capacity(p{1});
62 end
63
64 b = db.addParameter('b', 'demand at market j in cases', j);
65 for m = markets
66 rec = b.addRecord(m{1});
67 rec.value = demand(m{1});
68 end
69
70 d = db.addParameter('d', 'distance in thousands of miles', i, j);
71 for p = plants
72 for m = markets
73 rec = d.addRecord(p{1}, m{1});
74 rec.value = distance([p{1}, '.', m{1}]);
75 end
76 end
77
78 f = db.addParameter('f', 'freight in dollars per case per thousand miles');
79 rec = f.addRecord();
80 rec.value = 90;
81
82 % run a job using data from the created Database
83 cpBase = ws.addCheckpoint('tbase');
84 opt = ws.addOptions();
85 t11 = ws.addJobFromString(model, cpBase);
86 opt.defines('gdxincname', db.name);
87 opt.setAllModelTypes('xpress');
88 t11.run(opt, db);
89
90 % retrieve Variable 'x' from Job's output databases
91 for x = t11.outDB.getVariable('x').records;
92 fprintf('x(%s,%s): level=%g marginal=%g\n', x{1}.keys{:}, x{1}.level, x{1}.marginal);
93 end
94
95 % clear option and database
96 opt.dispose();
97 t11.outDB.dispose();
98
99 % remove working directory
100 rmdir(ws.workingDirectory, 's');
101end
102
103function create_save_restart(workdir, systemdir, debuglevel, cpFileName)
104
105 ws = gams.control.Workspace(workdir, systemdir, debuglevel);
106
107 basemodel = {
108 '$onempty '
109 ' Sets '
110 ' i(*) canning plants / / '
111 ' j(*) markets / / '
112 ' '
113 ' Parameters '
114 ' a(i) capacity of plant i in cases / / '
115 ' b(j) demand at market j in cases / / '
116 ' d(i,j) distance in thousands of miles / / '
117 ' Scalar f freight in dollars per case per thousand miles /0/; '
118 ' '
119 ' Parameter c(i,j) transport cost in thousands of dollars per case ;'
120 ' '
121 ' c(i,j) = f * d(i,j) / 1000 ; '
122 ' '
123 ' Variables '
124 ' x(i,j) shipment quantities in cases '
125 ' z total transportation costs in thousands of dollars ; '
126 ' '
127 ' Positive Variable x ; '
128 ' '
129 ' Equations '
130 ' cost define objective function '
131 ' supply(i) observe supply limit at plant i '
132 ' demand(j) satisfy demand at market j ; '
133 ' '
134 ' cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; '
135 ' '
136 ' supply(i) .. sum(j, x(i,j)) =l= a(i) ; '
137 ' '
138 ' demand(j) .. sum(i, x(i,j)) =g= b(j) ; '
139 ' '
140 ' Model transport /all/ ; '
141 ' '
142 ' Solve transport using lp minimizing z ; '
143 ' '};
144 basemodel = sprintf('%s\n', basemodel{:});
145
146 j1 = ws.addJobFromString(basemodel);
147 opt = ws.addOptions();
148 opt.action = gams.control.options.Action.CompileOnly;
149 cp = ws.addCheckpoint('tbase');
150 j1.run(opt, cp);
151 opt.dispose();
152 j1.outDB.dispose();
153end