Loading...
Searching...
No Matches
TransportClass.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using GAMS;
7
8namespace TransportSeq
9{
13 public class Transport
14 {
15 private GAMSSet fi, fj;
16 private GAMSParameter fa, fb, fd, ff;
17 private GAMSVariable fx, fz;
18 private GAMSOptions fopt;
19
20 private GAMSWorkspace fws;
21 private GAMSDatabase fDbIn1, fDbIn2, fDbOut1;
22
23 private GAMSJob job;
24
28 private string GetModelSource()
29 {
30 return @"
31 Sets
32 i canning plants
33 j markets
34
35 Parameters
36 a(i) capacity of plant i in cases
37 b(j) demand at market j in cases
38 d(i,j) distance in thousands of miles
39 Scalar f freight in dollars per case per thousand miles;
40
41$if not set dbIn1 $abort 'no file name for in-database 1 file provided'
42$gdxin %dbIn1%
43$load i j a b d
44
45$if not set dbIn2 $abort 'no file name for in-database 2 file provided'
46$gdxin %dbIn2%
47$load f
48$gdxin
49
50 Parameter c(i,j) transport cost in thousands of dollars per case ;
51
52 c(i,j) = f * d(i,j) / 1000 ;
53
54 Variables
55 x(i,j) shipment quantities in cases
56 z total transportation costs in thousands of dollars ;
57
58 Positive Variable x ;
59
60 Equations
61 cost define objective function
62 supply(i) observe supply limit at plant i
63 demand(j) satisfy demand at market j ;
64
65 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
66
67 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
68
69 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
70
71 Model transport /all/ ;
72
73 Solve transport using lp minimizing z ;
74
75$if not set dbOut1 $abort 'no file name for out-database 1 file provided'
76 execute_unload '%dbOut1%', x, z;
77";
78 }
79
84 {
85 fws = ws;
86 fopt = ws.AddOptions();
87
88 fDbIn1 = ws.AddDatabase(inModelName: "dbIn1");
89 fDbIn2 = ws.AddDatabase(inModelName: "dbIn2");
90
91 fopt.SolveLink = GAMSOptions.ESolveLink.LoadLibrary;
92 fopt.AllModelTypes = "Cplex";
93 fopt.Defines.Add("dbOut1", "dbOut1");
94
95 fi = fDbIn1.AddSet("i", "canning plants");
96 fj = fDbIn1.AddSet("j", "markets");
97 fa = fDbIn1.AddParameter("a", "capacity of plant i in cases", fi);
98 fb = fDbIn1.AddParameter("b", "demand at market j in cases", fj);
99 fd = fDbIn1.AddParameter("d", "distance in thousands of miles", fi, fj);
100 ff = fDbIn2.AddParameter("f", "freight in dollars per case per thousand miles");
101
102 job = ws.AddJobFromString(GetModelSource());
103 }
104
108 public void Run(GAMSCheckpoint checkpoint = null, TextWriter output = null)
109 {
110 if (!fDbIn1.CheckDomains())
111 throw new GAMSException("Domain Errors in Database 1");
112 if (!fDbIn2.CheckDomains())
113 throw new GAMSException("Domain Errors in Database 2");
114
115 job.Run(fopt, checkpoint, output, false, fDbIn1, fDbIn2);
116
117 fDbOut1 = fws.AddDatabaseFromGDX(fopt.Defines["dbOut1"] + ".gdx");
118 fx = fDbOut1.GetVariable("x");
119 fz = fDbOut1.GetVariable("z");
120 }
121
122 #region Data input symbols
126 public GAMSSet i
127 { get { return fi; } }
128
132 public GAMSSet j
133 { get { return fj; } }
134
139 { get { return fa; } }
140
145 { get { return fb; } }
146
151 { get { return fd; } }
152
157 { get { return ff; } }
158 #endregion Data input symbols
159
160 #region Data output symbols
165 { get { return fx; } }
166
171 { get { return fz; } }
172 #endregion Data output symbols
173
178 { get { return fopt; } }
179 }
180}
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)
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