1function transport4(varargin)
3 % check workspace info from arguments
5 wsInfo = gams.control.WorkspaceInfo();
6 wsInfo.systemDirectory = varargin{1};
7 ws = gams.control.Workspace(wsInfo);
9 ws = gams.control.Workspace();
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; '
23 '$if not set gdxincname $abort ''no include file name for data file provided'''
24 '$gdxin %gdxincname% '
28 ' Parameter c(i,j) transport cost in thousands of dollars per case ; '
30 ' c(i,j) = f * d(i,j) / 1000 ; '
33 ' x(i,j) shipment quantities in cases '
34 ' z total transportation costs in thousands of dollars ; '
36 ' Positive Variable x ; '
40 ' cost define objective function '
41 ' supply(i) observe supply limit at plant i '
42 ' demand(j) satisfy demand at market j ; '
44 ' cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; '
46 ' supply(i) .. sum(j, x(i,j)) =l= a(i) ; '
48 ' demand(j) .. sum(i, x(i,j)) =g= b(j) ; '
50 ' Model transport /all/ ; '
52 ' Solve transport using lp minimizing z ; '
54 ' Display x.l, x.m ; '};
55 model = sprintf(
'%s\n', model{:});
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;
75 % add a database and add input data into the database
76 db = ws.addDatabase();
78 i = db.addSet(
'i', 1,
'canning plants');
83 j = db.addSet(
'j', 1,
'markets');
88 a = db.addParameter(
'a',
'capacity of plant i in cases', i);
90 rec = a.addRecord(p{1});
91 rec.value = capacity(p{1});
94 b = db.addParameter(
'b',
'demand at market j in cases', j);
96 rec = b.addRecord(m{1});
97 rec.value = demand(m{1});
100 d = db.addParameter(
'd',
'distance in thousands of miles', i, j);
103 rec = d.addRecord(p{1}, m{1});
104 rec.value = distance([p{1},
'.', m{1}]);
108 f = db.addParameter(
'f',
'freight in dollars per case per thousand miles');
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);
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);
127 % set option of all model types for xpress and run the job again
128 opt.setAllModelTypes('xpress
');
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);
137 % clear option and database
142 %
remove working directory
143 rmdir(ws.workingDirectory,
's');