Loading...
Searching...
No Matches
Transport13.cs
1using GAMS;
2using System;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7
8namespace TransportSeq
9{
19 {
20 static void Main(string[] args)
21 {
22 #region Static data section
23 List<string> plants = new List<string>()
24 {
25 "Seattle", "San-Diego"
26 };
27 List<string> markets = new List<string>()
28 {
29 "New-York", "Chicago", "Topeka"
30 };
31 Dictionary<string, double> capacity = new Dictionary<string, double>()
32 {
33 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
34 };
35 Dictionary<string, double> demand = new Dictionary<string, double>()
36 {
37 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
38 };
39 Dictionary<Tuple<string, string>, double> distance = new Dictionary<Tuple<string, string>, double>()
40 {
41 { new Tuple<string,string> ("Seattle", "New-York"), 2.5 },
42 { new Tuple<string,string> ("Seattle", "Chicago"), 1.7 },
43 { new Tuple<string,string> ("Seattle", "Topeka"), 1.8 },
44 { new Tuple<string,string> ("San-Diego", "New-York"), 2.5 },
45 { new Tuple<string,string> ("San-Diego", "Chicago"), 1.8 },
46 { new Tuple<string,string> ("San-Diego", "Topeka"), 1.4 }
47 };
48 #endregion Static data section
49
50
52 if (Environment.GetCommandLineArgs().Length > 1)
53 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
54 else
55 ws = new GAMSWorkspace();
56
57 Transport t = new Transport(ws);
58
59 #region Input data binding
60 foreach (string p in plants)
61 t.i.AddRecord(p);
62
63 foreach (string m in markets)
64 t.j.AddRecord(m);
65
66 foreach (string p in plants)
67 t.a.AddRecord(p).Value = capacity[p];
68
69 foreach (string m in markets)
70 t.b.AddRecord(m).Value = demand[m];
71
72 foreach (Tuple<string, string> dis in distance.Keys)
73 t.d.AddRecord(dis.Item1, dis.Item2).Value = distance[dis];
74
75 t.f.AddRecord().Value = 90;
76 #endregion Input data binding
77
78 t.opt.AllModelTypes = "cplex";
79
80 t.Run(output: Console.Out);
81
82 #region Output data binding
83 Console.WriteLine("Objective: " + t.z.FirstRecord().Level);
84
85 foreach (GAMSVariableRecord rec in t.x)
86 Console.WriteLine("x(" + rec.Key(0) + "," + rec.Key(1) + "): level=" + rec.Level + " marginal=" + rec.Marginal);
87 #endregion Output data binding
88 }
89 }
90
91 #region wrapper class
95 public class Transport
96 {
97 private GAMSSet fi, fj;
98 private GAMSParameter fa, fb, fd, ff;
99 private GAMSVariable fx, fz;
100 private GAMSOptions fopt;
101
102 private GAMSWorkspace fws;
103 private GAMSDatabase fDbIn1, fDbIn2, fDbOut1;
104
105 private GAMSJob job;
106
110 private string GetModelSource()
111 {
112 return @"
113 Sets
114 i canning plants
115 j markets
116
117 Parameters
118 a(i) capacity of plant i in cases
119 b(j) demand at market j in cases
120 d(i,j) distance in thousands of miles
121 Scalar f freight in dollars per case per thousand miles;
122
123$if not set dbIn1 $abort 'no file name for in-database 1 file provided'
124$gdxin %dbIn1%
125$load i j a b d
126
127$if not set dbIn2 $abort 'no file name for in-database 2 file provided'
128$gdxin %dbIn2%
129$load f
130$gdxin
131
132 Parameter c(i,j) transport cost in thousands of dollars per case ;
133
134 c(i,j) = f * d(i,j) / 1000 ;
135
136 Variables
137 x(i,j) shipment quantities in cases
138 z total transportation costs in thousands of dollars ;
139
140 Positive Variable x ;
141
142 Equations
143 cost define objective function
144 supply(i) observe supply limit at plant i
145 demand(j) satisfy demand at market j ;
146
147 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
148
149 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
150
151 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
152
153 Model transport /all/ ;
154
155 Solve transport using lp minimizing z ;
156
157$if not set dbOut1 $abort 'no file name for out-database 1 file provided'
158 execute_unload '%dbOut1%', x, z;
159";
160 }
161
166 {
167 fws = ws;
168 fopt = ws.AddOptions();
169
170 fDbIn1 = ws.AddDatabase(inModelName: "dbIn1");
171 fDbIn2 = ws.AddDatabase(inModelName: "dbIn2");
172
173 fopt.SolveLink = GAMSOptions.ESolveLink.LoadLibrary;
174 fopt.AllModelTypes = "Cplex";
175 fopt.Defines.Add("dbOut1", "dbOut1");
176
177 fi = fDbIn1.AddSet("i", "canning plants");
178 fj = fDbIn1.AddSet("j", "markets");
179 fa = fDbIn1.AddParameter("a", "capacity of plant i in cases", fi);
180 fb = fDbIn1.AddParameter("b", "demand at market j in cases", fj);
181 fd = fDbIn1.AddParameter("d", "distance in thousands of miles", fi, fj);
182 ff = fDbIn2.AddParameter("f", "freight in dollars per case per thousand miles");
183
184 job = ws.AddJobFromString(GetModelSource());
185 }
186
190 public void Run(GAMSCheckpoint checkpoint = null, TextWriter output = null)
191 {
192 if (!fDbIn1.CheckDomains())
193 throw new GAMSException("Domain Errors in Database 1");
194 if (!fDbIn2.CheckDomains())
195 throw new GAMSException("Domain Errors in Database 2");
196
197 job.Run(fopt, checkpoint, output, false, fDbIn1, fDbIn2);
198
199 fDbOut1 = fws.AddDatabaseFromGDX(fopt.Defines["dbOut1"] + ".gdx");
200 fx = fDbOut1.GetVariable("x");
201 fz = fDbOut1.GetVariable("z");
202 }
203
204 #region Data input symbols
208 public GAMSSet i
209 { get { return fi; } }
210
214 public GAMSSet j
215 { get { return fj; } }
216
221 { get { return fa; } }
222
227 { get { return fb; } }
228
233 { get { return fd; } }
234
239 { get { return ff; } }
240 #endregion Data input symbols
241
242 #region Data output symbols
247 { get { return fx; } }
248
253 { get { return fz; } }
254 #endregion Data output symbols
255
260 { get { return fopt; } }
261 }
262 #endregion
263}
GAMSVariable GetVariable(string variableIdentifier)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
Dictionary< string, string > Defines
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabaseFromGDX(string gdxFileName, string databaseName=null, string inModelName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
This is the 13th model in a series of tutorial examples. Here we show: How to run a GAMSJob using a w...
Wrapper class for GAMS trnsport model.
GAMSVariable z
z: total transportation costs in thousands of dollars
Transport(GAMSWorkspace ws)
A Transportation Problem.
GAMSParameter d
d(i,j): distance in thousands of miles
GAMSOptions opt
Options for the execution of the trnsport model.
GAMSVariable x
x(i,j): shipment quantities in cases
GAMSSet j
j(j): markets
GAMSParameter a
a(i): capacity of plant i in cases
GAMSParameter b
b(i): demand at market j in cases
GAMSParameter f
f: freight in dollars per case per thousand miles
void Run(GAMSCheckpoint checkpoint=null, TextWriter output=null)
Executes the trnsport model.
GAMSSet i
i(i): canning plants