Loading...
Searching...
No Matches
TransportModel.m
1classdef TransportModel < handle
2
3 properties (SetAccess = private)
4 i
5 j
6 a
7 b
8 c
9 d
10 f
11 x
12 z
13 opt
14 ws
15 dbin1
16 dbin2
17 dbout1
18 job
19 end
20
21 methods
22
23 function obj = TransportModel(ws)
24
25 obj.ws = ws;
26 obj.opt = obj.ws.addOptions();
27
28 obj.dbin1 = obj.ws.addDatabase('dbIn1');
29 obj.dbin2 = obj.ws.addDatabase('dbIn2');
30
31 obj.opt.defines('dbIn1', 'dbIn1');
32 obj.opt.defines('dbIn2', 'dbIn2');
33 obj.opt.setAllModelTypes('cplex');
34 obj.opt.defines('dbOut1', 'dbOut1');
35 obj.opt.solveLink = gams.control.options.SolveLink.LoadLibrary;
36
37 obj.i = obj.dbin1.addSet('i', 'canning plants');
38 obj.j = obj.dbin1.addSet('j', 'markets');
39 obj.a = obj.dbin1.addParameter('a', 'capacity of plant i in cases', obj.i);
40 obj.b = obj.dbin1.addParameter('b', 'demand at market j in cases', obj.j);
41 obj.d = obj.dbin1.addParameter('d', 'distance in thousands of miles', obj.i, obj.j);
42 obj.f = obj.dbin2.addParameter('f', 'freight in dollars per case per thousand miles');
43
44 obj.job = obj.ws.addJobFromString(obj.modelSource());
45 end
46
47 function run(obj, cp, output)
48 if ~obj.dbin1.checkDomains()
49 error('Domain Errors in Database 1');
50 end
51 if ~obj.dbin2.checkDomains()
52 error('Domain Errors in Database 2');
53 end
54
55 if isempty(cp) && isempty(output)
56 obj.job.run(obj.opt, false, obj.dbin1, obj.dbin2);
57 elseif isempty(cp)
58 obj.job.run(obj.opt, output, false, obj.dbin1, obj.dbin2);
59 elseif isempty(output)
60 obj.job.run(obj.opt, checkpoint, false, obj.dbin1, obj.dbin2);
61 else
62 obj.job.run(obj.opt, checkpoint, output, false, obj.dbin1, obj.dbin2);
63 end
64
65 obj.dbout1 = obj.ws.addDatabaseFromGDX(sprintf('%s.gdx', obj.opt.getDefinitionOf('dbOut1')));
66 obj.x = obj.dbout1.getVariable('x');
67 obj.z = obj.dbout1.getVariable('z');
68 end
69
70 function model = modelSource(obj)
71 model = {
72 'Sets '
73 ' i canning plants '
74 ' j markets '
75 ' '
76 ' Parameters '
77 ' a(i) capacity of plant i in cases '
78 ' b(j) demand at market j in cases '
79 ' d(i,j) distance in thousands of miles '
80 ' Scalar f freight in dollars per case per thousand miles; '
81 ' '
82 '$if not set dbIn1 $abort ''no file name for in-database 1 file provided'' '
83 '$gdxin %dbIn1% '
84 '$load i j a b d '
85 '$gdxin '
86 ' '
87 '$if not set dbIn2 $abort ''no file name for in-database 2 file provided'' '
88 '$gdxin %dbIn2% '
89 '$load f '
90 '$gdxin '
91 ' '
92 ' Parameter c(i,j) transport cost in thousands of dollars per case ; '
93 ' '
94 ' c(i,j) = f * d(i,j) / 1000 ; '
95 ' '
96 ' Variables '
97 ' x(i,j) shipment quantities in cases '
98 ' z total transportation costs in thousands of dollars ; '
99 ' '
100 ' Positive Variable x ; '
101 ' '
102 ' Equations '
103 ' '
104 ' cost define objective function '
105 ' supply(i) observe supply limit at plant i '
106 ' demand(j) satisfy demand at market j ; '
107 ' '
108 ' cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; '
109 ' '
110 ' supply(i) .. sum(j, x(i,j)) =l= a(i) ; '
111 ' '
112 ' demand(j) .. sum(i, x(i,j)) =g= b(j) ; '
113 ' '
114 ' Model transport /all/ ; '
115 ' '
116 ' Solve transport using lp minimizing z ; '
117 ' '
118 ' Display x.l, x.m ; '
119 '$if not set dbOut1 $abort ''no file name for out-database 1 file provided'' '
120 'execute_unload ''%dbOut1%'', x, z; '
121 ' '};
122 model = sprintf('%s\n', model{:});
123 end
124
125 end
126
127end