Loading...
Searching...
No Matches
Transport4.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using GAMS;
6
7namespace TransportSeq
8{
21 {
22 static void Main(string[] args)
23 {
25 if (Environment.GetCommandLineArgs().Length > 1)
26 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
27 else
28 ws = new GAMSWorkspace();
29
30 // define some data by using C# data structures
31 List<string> plants = new List<string>()
32 {
33 "Seattle", "San-Diego"
34 };
35 List<string> markets = new List<string>()
36 {
37 "New-York", "Chicago", "Topeka"
38 };
39 Dictionary<string, double> capacity = new Dictionary<string, double>()
40 {
41 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
42 };
43 Dictionary<string, double> demand = new Dictionary<string, double>()
44 {
45 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
46 };
47 Dictionary<Tuple<string,string>, double> distance = new Dictionary<Tuple<string,string>, double>()
48 {
49 { new Tuple<string,string> ("Seattle", "New-York"), 2.5 },
50 { new Tuple<string,string> ("Seattle", "Chicago"), 1.7 },
51 { new Tuple<string,string> ("Seattle", "Topeka"), 1.8 },
52 { new Tuple<string,string> ("San-Diego", "New-York"), 2.5 },
53 { new Tuple<string,string> ("San-Diego", "Chicago"), 1.8 },
54 { new Tuple<string,string> ("San-Diego", "Topeka"), 1.4 }
55 };
56
57 // prepare a GAMSDatabase with data from the C# data structures
58 GAMSDatabase db = ws.AddDatabase();
59
60 GAMSSet i = db.AddSet("i", 1, "canning plants");
61 foreach (string p in plants)
62 i.AddRecord(p);
63
64 GAMSSet j = db.AddSet("j", 1, "markets");
65 foreach (string m in markets)
66 j.AddRecord(m);
67
68 GAMSParameter a = db.AddParameter("a", "capacity of plant i in cases", i);
69 foreach (string p in plants)
70 a.AddRecord(p).Value = capacity[p];
71
72 GAMSParameter b = db.AddParameter("b", "demand at market j in cases", j);
73 foreach (string m in markets)
74 b.AddRecord(m).Value = demand[m];
75
76 GAMSParameter d = db.AddParameter("d", "distance in thousands of miles", i, j);
77 foreach(Tuple<string,string> t in distance.Keys)
78 d.AddRecord(t.Item1,t.Item2).Value = distance[t];
79
80 GAMSParameter f = db.AddParameter("f", "freight in dollars per case per thousand miles");
81 f.AddRecord().Value = 90;
82
83 // run a job using data from the created GAMSDatabase
84 GAMSJob t4 = ws.AddJobFromString(GetModelText());
85 using (GAMSOptions opt = ws.AddOptions())
86 {
87 opt.Defines.Add("gdxincname", db.Name);
88 opt.AllModelTypes = "xpress";
89 t4.Run(opt, db);
90 foreach (GAMSVariableRecord rec in t4.OutDB.GetVariable("x"))
91 Console.WriteLine("x(" + rec.Key(0) + "," + rec.Key(1) + "): level=" + rec.Level + " marginal=" + rec.Marginal);
92 }
93 }
94
95 static String GetModelText()
96 {
97 String model = @"
98 Sets
99 i canning plants
100 j markets
101
102 Parameters
103 a(i) capacity of plant i in cases
104 b(j) demand at market j in cases
105 d(i,j) distance in thousands of miles
106 Scalar f freight in dollars per case per thousand miles;
107
108$if not set gdxincname $abort 'no include file name for data file provided'
109$gdxin %gdxincname%
110$load i j a b d f
111$gdxin
112
113 Parameter c(i,j) transport cost in thousands of dollars per case ;
114
115 c(i,j) = f * d(i,j) / 1000 ;
116
117 Variables
118 x(i,j) shipment quantities in cases
119 z total transportation costs in thousands of dollars ;
120
121 Positive Variable x ;
122
123 Equations
124 cost define objective function
125 supply(i) observe supply limit at plant i
126 demand(j) satisfy demand at market j ;
127
128 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
129
130 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
131
132 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
133
134 Model transport /all/ ;
135
136 Solve transport using lp minimizing z ;
137
138 Display x.l, x.m ;
139";
140
141 return model;
142 }
143
144 }
145}
GAMSVariable GetVariable(string variableIdentifier)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSDatabase OutDB
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)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
This is the 4th model in a series of tutorial examples. Here we show: How to define data using C# dat...
Definition: Transport4.cs:21