Loading...
Searching...
No Matches
transport3.m
1function transport3(varargin)
2
3 % create a directory
4 workingDirectory = fullfile(pwd, 'transport3');
5 mkdir(workingDirectory);
6
7 % create a workspace
8 wsInfo = gams.control.WorkspaceInfo();
9 wsInfo.workingDirectory = workingDirectory;
10 if nargin > 0
11 wsInfo.systemDirectory = varargin{1};
12 end
13 ws = gams.control.Workspace(wsInfo);
14
15 data = {
16 'Sets '
17 ' i canning plants / seattle, san-diego / '
18 ' j markets / new-york, chicago, topeka / ; '
19 'Parameters '
20 ' '
21 ' a(i) capacity of plant i in cases '
22 ' / seattle 350 '
23 ' san-diego 600 / '
24 ' '
25 ' b(j) demand at market j in cases '
26 ' / new-york 325 '
27 ' chicago 300 '
28 ' topeka 275 / ; '
29 ' '
30 'Table d(i,j) distance in thousands of miles '
31 ' new-york chicago topeka '
32 ' seattle 2.5 1.7 1.8 '
33 ' san-diego 2.5 1.8 1.4 ; '
34 ' '
35 'Scalar f freight in dollars per case per thousand miles /90/ ;'};
36 data = sprintf('%s\n', data{:});
37
38 model = {
39 'Sets '
40 ' i canning plants '
41 ' j markets '
42 ' '
43 ' Parameters '
44 ' a(i) capacity of plant i in cases '
45 ' b(j) demand at market j in cases '
46 ' d(i,j) distance in thousands of miles '
47 ' Scalar f freight in dollars per case per thousand miles; '
48 ' '
49 '$if not set gdxincname $abort ''no include file name for data file provided'''
50 '$gdxin %gdxincname% '
51 '$load i j a b d f '
52 '$gdxin '
53 ' '
54 ' Parameter c(i,j) transport cost in thousands of dollars per case ; '
55 ' '
56 ' c(i,j) = f * d(i,j) / 1000 ; '
57 ' '
58 ' Variables '
59 ' x(i,j) shipment quantities in cases '
60 ' z total transportation costs in thousands of dollars ; '
61 ' '
62 ' Positive Variable x ; '
63 ' '
64 ' Equations '
65 ' '
66 ' cost define objective function '
67 ' supply(i) observe supply limit at plant i '
68 ' demand(j) satisfy demand at market j ; '
69 ' '
70 ' cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; '
71 ' '
72 ' supply(i) .. sum(j, x(i,j)) =l= a(i) ; '
73 ' '
74 ' demand(j) .. sum(i, x(i,j)) =g= b(j) ; '
75 ' '
76 ' Model transport /all/ ; '
77 ' '
78 ' Solve transport using lp minimizing z ; '
79 ' '
80 ' Display x.l, x.m ; '};
81 model = sprintf('%s\n', model{:});
82
83 % create and run a job from a data file, then explicitly export to a GDX file
84 t3 = ws.addJobFromString(data);
85 t3.run();
86 t3.outDB.export(fullfile(ws.workingDirectory, 'tdata.gdx'));
87
88 % clear database
89 t3.outDB.dispose();
90
91 % run a job using an instance of Options that defines the data include file
92 t3 = ws.addJobFromString(model);
93 opt = ws.addOptions();
94 opt.defines('gdxincname', 'tdata');
95 t3.run(opt);
96
97 % retrieve Variable 'x' from Job's output databases
98 for x = t3.outDB.getVariable('x').records
99 fprintf('x(%s,%s): level=%g marginal=%g\n', x{1}.keys{:}, x{1}.level, x{1}.marginal);
100 end
101
102 % clear database
103 t3.outDB.dispose();
104
105 % similar to the previous run but without exporting database into a file
106 t3a = ws.addJobFromString(data);
107 t3b = ws.addJobFromString(model);
108 opt = ws.addOptions();
109 t3a.run();
110 opt.defines('gdxincname', t3a.outDB.name);
111 t3b.run(opt, t3a.outDB);
112
113 % retrieve Variable 'x' from Job's output databases
114 for x = t3b.outDB.getVariable('x').records
115 fprintf('x(%s,%s): level=%g marginal=%g\n', x{1}.keys{:}, x{1}.level, x{1}.marginal);
116 end
117
118 % clear option and database
119 opt.dispose();
120 t3a.outDB.dispose();
121 t3b.outDB.dispose();
122
123 % remove working directory
124 rmdir(ws.workingDirectory, 's');
125
126end