Loading...
Searching...
No Matches
Transport11.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using GAMS;
7
8namespace TransportSeq
9{
19 {
20
21 static void Main(string[] args)
22 {
23
24 // Create a save/restart file usually supplied by an application provider
25 // We create it for demonstration purpose
26 string wDir = Path.Combine(".", "tmp");
27 CreateSaveRestart(Path.Combine(wDir, "tbase"));
28
29 // define some data by using C# data structures
30 List<string> plants = new List<string>()
31 {
32 "Seattle", "San-Diego"
33 };
34 List<string> markets = new List<string>()
35 {
36 "New-York", "Chicago", "Topeka"
37 };
38 Dictionary<string, double> capacity = new Dictionary<string, double>()
39 {
40 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
41 };
42 Dictionary<string, double> demand = new Dictionary<string, double>()
43 {
44 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
45 };
46 Dictionary<Tuple<string, string>, double> distance = new Dictionary<Tuple<string, string>, double>()
47 {
48 { new Tuple<string,string> ("Seattle", "New-York"), 2.5 },
49 { new Tuple<string,string> ("Seattle", "Chicago"), 1.7 },
50 { new Tuple<string,string> ("Seattle", "Topeka"), 1.8 },
51 { new Tuple<string,string> ("San-Diego", "New-York"), 2.5 },
52 { new Tuple<string,string> ("San-Diego", "Chicago"), 1.8 },
53 { new Tuple<string,string> ("San-Diego", "Topeka"), 1.4 }
54 };
55
57 if (Environment.GetCommandLineArgs().Length > 1)
58 ws = new GAMSWorkspace(workingDirectory: wDir, systemDirectory: Environment.GetCommandLineArgs()[1]);
59 else
60 ws = new GAMSWorkspace(workingDirectory: wDir);
61 // prepare a GAMSDatabase with data from the C# data structures
62 GAMSDatabase db = ws.AddDatabase();
63
64 GAMSSet i = db.AddSet("i", 1, "canning plants");
65 foreach (string p in plants)
66 i.AddRecord(p);
67
68 GAMSSet j = db.AddSet("j", 1, "markets");
69 foreach (string m in markets)
70 j.AddRecord(m);
71
72 GAMSParameter a = db.AddParameter("a", "capacity of plant i in cases", i);
73 foreach (string p in plants)
74 a.AddRecord(p).Value = capacity[p];
75
76 GAMSParameter b = db.AddParameter("b", "demand at market j in cases", j);
77 foreach (string m in markets)
78 b.AddRecord(m).Value = demand[m];
79
80 GAMSParameter d = db.AddParameter("d", "distance in thousands of miles", i, j);
81 foreach (Tuple<string, string> t in distance.Keys)
82 d.AddRecord(t.Item1, t.Item2).Value = distance[t];
83
84 GAMSParameter f = db.AddParameter("f", "freight in dollars per case per thousand miles");
85 f.AddRecord().Value = 90;
86
87 // run a job using data from the created GAMSDatabase
88 GAMSCheckpoint cpBase = ws.AddCheckpoint("tbase");
89 using (GAMSOptions opt = ws.AddOptions())
90 {
91 GAMSJob t4 = ws.AddJobFromString(GetModelText(), cpBase);
92 opt.Defines.Add("gdxincname", db.Name);
93 opt.AllModelTypes = "xpress";
94 t4.Run(opt, db);
95 foreach (GAMSVariableRecord rec in t4.OutDB.GetVariable("x"))
96 Console.WriteLine("x(" + rec.Key(0) + "," + rec.Key(1) + "): level=" + rec.Level + " marginal=" + rec.Marginal);
97 }
98 }
99
100 static void CreateSaveRestart(string cpFileName)
101 {
102 GAMSWorkspace ws;
103 if (Environment.GetCommandLineArgs().Length > 1)
104 ws = new GAMSWorkspace(workingDirectory: Path.GetDirectoryName(cpFileName), systemDirectory: Environment.GetCommandLineArgs()[1]);
105 else
106 ws = new GAMSWorkspace(workingDirectory: Path.GetDirectoryName(cpFileName));
107 GAMSJob j1 = ws.AddJobFromString(GetBaseModelText());
108 GAMSOptions opt = ws.AddOptions();
109
110 opt.Action = GAMSOptions.EAction.CompileOnly;
111
112
113 GAMSCheckpoint cp = ws.AddCheckpoint(Path.GetFileName(cpFileName));
114 j1.Run(opt, cp);
115
116 opt.Dispose();
117 }
118
119 static String GetBaseModelText()
120 {
121 String model = @"
122$onempty
123 Sets
124 i(*) canning plants / /
125 j(*) markets / /
126
127 Parameters
128 a(i) capacity of plant i in cases / /
129 b(j) demand at market j in cases / /
130 d(i,j) distance in thousands of miles / /
131 Scalar f freight in dollars per case per thousand miles /0/;
132
133 Parameter c(i,j) transport cost in thousands of dollars per case ;
134
135 c(i,j) = f * d(i,j) / 1000 ;
136
137 Variables
138 x(i,j) shipment quantities in cases
139 z total transportation costs in thousands of dollars ;
140
141 Positive Variable x ;
142
143 Equations
144 cost define objective function
145 supply(i) observe supply limit at plant i
146 demand(j) satisfy demand at market j ;
147
148 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
149
150 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
151
152 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
153
154 Model transport /all/ ;
155
156 Solve transport using lp minimizing z ;
157
158";
159
160 return model;
161 }
162
163 static String GetModelText()
164 {
165 String model = @"
166$if not set gdxincname $abort 'no include file name for data file provided'
167$gdxin %gdxincname%
168$onMulti
169$load i j a b d f
170$gdxin
171
172 Display x.l, x.m ;";
173
174 return model;
175 }
176
177 }
178}
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
Dictionary< string, string > Defines
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSSetRecord AddRecord(params string[] keys)
string Key(int index)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
This is the 11th model in a series of tutorial examples. Here we show: How to create and use a save/r...
Definition: Transport11.cs:19