1function transport11(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();
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');
17 '$if not set gdxincname $abort ''no include file name for data file provided'' '
18 '$gdxin %gdxincname% '
25 model = sprintf(
'%s\n', model{:});
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;
45 % add a database and add input data into the database
46 db = ws.addDatabase();
48 i = db.addSet(
'i', 1,
'canning plants');
53 j = db.addSet(
'j', 1,
'markets');
58 a = db.addParameter(
'a',
'capacity of plant i in cases', i);
60 rec = a.addRecord(p{1});
61 rec.value = capacity(p{1});
64 b = db.addParameter(
'b',
'demand at market j in cases', j);
66 rec = b.addRecord(m{1});
67 rec.value = demand(m{1});
70 d = db.addParameter(
'd',
'distance in thousands of miles', i, j);
73 rec = d.addRecord(p{1}, m{1});
74 rec.value = distance([p{1},
'.', m{1}]);
78 f = db.addParameter(
'f',
'freight in dollars per case per thousand miles');
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');
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);
95 % clear option and database
99 % remove working directory
100 rmdir(ws.workingDirectory, 's
');
103function create_save_restart(workdir, systemdir, debuglevel, cpFileName)
105 ws = gams.control.Workspace(workdir, systemdir, debuglevel);
110 ' i(*) canning plants / /
'
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/;
'
119 ' Parameter c(i,j) transport cost in thousands of dollars per case ;
'
121 ' c(i,j) = f * d(i,j) / 1000 ;
'
124 ' x(i,j) shipment quantities in cases
'
125 ' z total transportation costs in thousands of dollars ;
'
127 ' Positive Variable x ;
'
130 ' cost define objective
function '
131 ' supply(i) observe supply limit at plant i
'
132 ' demand(j) satisfy demand at market j ;
'
134 ' cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
'
136 ' supply(i) .. sum(j, x(i,j)) =l= a(i) ;
'
138 ' demand(j) .. sum(i, x(i,j)) =g= b(j) ;
'
140 ' Model transport /all/ ;
'
142 ' Solve transport
using lp minimizing z ;
'
144 basemodel = sprintf('%s\n
', basemodel{:});
146 j1 = ws.addJobFromString(basemodel);
147 opt = ws.addOptions();
148 opt.action = gams.control.options.Action.CompileOnly;
149 cp = ws.addCheckpoint('tbase
');