Loading...
Searching...
No Matches
transport7.m
1function transport7(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 / seattle, san-diego / '
15 ' j markets / new-york, chicago, topeka / ; '
16 ' '
17 'Parameters '
18 ' a(i) capacity of plant i in cases '
19 ' / seattle 350 '
20 ' san-diego 600 / '
21 ' '
22 ' b(j) demand at market j in cases '
23 ' / new-york 325 '
24 ' chicago 300 '
25 ' topeka 275 / ; '
26 ' '
27 'Table d(i,j) distance in thousands of miles '
28 ' new-york chicago topeka '
29 'seattle 2.5 1.7 1.8 '
30 'san-diego 2.5 1.8 1.4 ; '
31 ' '
32 'Scalar f freight in dollars per case per thousand miles /90/ ; '
33 'Scalar bmult demand multiplier /1/; '
34 ' '
35 'Parameter c(i,j) transport cost in thousands of dollars per case ; '
36 ' c(i,j) = f * d(i,j) / 1000 ; '
37 ' '
38 'Variables '
39 ' x(i,j) shipment quantities in cases '
40 ' z total transportation costs in thousands of dollars ; '
41 ' '
42 'Positive Variable x ; '
43 ' '
44 'Equations '
45 ' cost define objective function '
46 ' supply(i) observe supply limit at plant i '
47 ' demand(j) satisfy demand at market j ; '
48 ' '
49 ' cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; '
50 ' '
51 ' supply(i) .. sum(j, x(i,j)) =l= a(i) ; '
52 ' '
53 ' demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; '
54 ' '
55 'Model transport /all/ ; '
56 ' '};
57 model = sprintf('%s\n', model{:});
58
59 % create a checkpoint
60 cp = ws.addCheckpoint();
61
62 % initialize a checkpoint by running a job
63 t7 = ws.addJobFromString(model);
64 t7.run(cp);
65
66 % create a ModelInstance and solve it multiple times with different scalar bmult
67 mi = cp.addModelInstance();
68 bmult = mi.syncDB.addParameter('bmult', 'demand multiplier');
69 opt = ws.addOptions();
70 opt.setAllModelTypes('cplex');
71
72 % instantiate the ModelInstance and pass a model definition and Modifier to declare bmult mutable
73 mi.instantiate('transport use lp min z', opt, gams.control.Modifier(bmult));
74
75 rec = bmult.addRecord();
76 rec.value = 1.0;
77 bmultlist = [0.6, 0.7 , 0.8, 0.9, 1.0, 1.1, 1.2, 1.3];
78
79 for i = 1:numel(bmultlist)
80 rec.value = bmultlist(i);
81 mi.solve();
82
83 fprintf('Scenario bmult=%f:\n', bmultlist(i));
84 fprintf(' Modelstatus: %s\n', mi.modelStatus.select);
85 fprintf(' Solvestatus: %s\n', mi.solveStatus.select);
86 fprintf(' Obj: %f\n', mi.syncDB.getVariable('z').record.level);
87 end
88
89 % clear ModelInstance
90 mi.dispose();
91
92 % create a ModelInstance and solve it with single links in the network blocked
93 mi = cp.addModelInstance();
94
95 x = mi.syncDB.addVariable('x', 2, gams.control.globals.VarType.POSITIVE, '');
96 xup = mi.syncDB.addParameter('xup', 2, 'upper bound on x');
97
98 % instantiate the ModelInstance and pass a model definition and Modifier to declare upper bound of X mutable
99 mi.instantiate('transport use lp min z', gams.control.Modifier(x, gams.control.globals.UpdateAction.UPPER, xup));
100
101 for i = t7.outDB.getSet('i').records
102 for j = t7.outDB.getSet('j').records
103 xup.clear();
104 keys = {i{1}.key(1), j{1}.key(1)};
105 rec = xup.addRecord(keys);
106 rec.value = 0;
107 mi.solve();
108
109 fprintf('Scenario link blocked: %s - %s\n', keys{:});
110 fprintf(' Modelstatus: %s\n', mi.modelStatus.select);
111 fprintf(' Solvestatus: %s\n', mi.solveStatus.select);
112 fprintf(' Obj: %f\n', mi.syncDB.getVariable('z').record.level);
113 end
114 end
115
116 % clear ModelInstance
117 mi.dispose();
118
119 % clear database
120 t7.outDB.dispose();
121
122 % remove working directory
123 rmdir(ws.workingDirectory, 's');
124end