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