Loading...
Searching...
No Matches
TransportGDX.cs
1using System;
2using System.Collections.Generic;
3using GAMS;
4
5namespace TransportSeq
6{
20 {
21 static void Main(string[] args)
22 {
24 if (Environment.GetCommandLineArgs().Length > 1)
25 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
26 else
27 ws = new GAMSWorkspace();
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
56 // prepare a GAMSDatabase with data from the C# data structures
57 GAMSDatabase db = ws.AddDatabase();
58
59 GAMSSet i = db.AddSet("i", 1, "canning plants");
60 foreach (string p in plants)
61 i.AddRecord(p);
62
63 GAMSSet j = db.AddSet("j", 1, "markets");
64 foreach (string m in markets)
65 j.AddRecord(m);
66
67 GAMSParameter a = db.AddParameter("a", "capacity of plant i in cases", i);
68 foreach (string p in plants)
69 a.AddRecord(p).Value = capacity[p];
70
71 GAMSParameter b = db.AddParameter("b", "demand at market j in cases", j);
72 foreach (string m in markets)
73 b.AddRecord(m).Value = demand[m];
74
75 GAMSParameter d = db.AddParameter("d", "distance in thousands of miles", i, j);
76 foreach (Tuple<string, string> t in distance.Keys)
77 d.AddRecord(t.Item1, t.Item2).Value = distance[t];
78
79 GAMSParameter f = db.AddParameter("f", "freight in dollars per case per thousand miles");
80 f.AddRecord().Value = 90;
81
82 // export the GAMSDatabase to a GDX file with name 'data.gdx' located in the 'workingDirectory' of the GAMSWorkspace
83 db.Export("data.gdx");
84
85 // add a new GAMSDatabase and initialize it from the GDX file just created
86 GAMSDatabase db2 = ws.AddDatabaseFromGDX("data.gdx");
87
88 // read data from symbols into C# data structures
89 List<string> plants2 = new List<string>();
90 foreach (GAMSSetRecord item in db2.GetSet("i"))
91 plants2.Add(item.Key(0));
92
93 List<string> markets2 = new List<string>();
94 foreach (GAMSSetRecord item in db2.GetSet("j"))
95 markets2.Add(item.Key(0));
96
97 Dictionary<string, double> capacity2 = new Dictionary<string, double>();
98 foreach (GAMSParameterRecord item in db2.GetParameter("a"))
99 capacity2.Add(item.Key(0), item.Value);
100
101 Dictionary<string, double> demand2 = new Dictionary<string, double>();
102 foreach (GAMSParameterRecord item in db2.GetParameter("b"))
103 demand2.Add(item.Key(0), item.Value);
104
105 Dictionary<Tuple<string, string>, double> distance2 = new Dictionary<Tuple<string, string>, double>();
106 foreach (GAMSParameterRecord item in db2.GetParameter("d"))
107 distance2.Add(new Tuple<string, string>(item.Key(0), item.Key(1)), item.Value);
108
109 // print out data read from GDX file
110 Console.WriteLine("Data read from data.gdx");
111 Console.WriteLine("\nSet i:");
112 foreach (string p in plants2)
113 Console.WriteLine(" " + p);
114 Console.WriteLine("\nSet j:");
115 foreach (string m in markets2)
116 Console.WriteLine(" " + m);
117 Console.WriteLine("\nParameter a:");
118 foreach (var item in capacity2)
119 Console.WriteLine(" " + item.Key + ": " + item.Value);
120 Console.WriteLine("\nParameter b:");
121 foreach (var item in demand2)
122 Console.WriteLine(" " + item.Key + ": " + item.Value);
123 Console.WriteLine("\nParameter d:");
124 foreach (var item in distance2)
125 Console.WriteLine(" " + item.Key + ": " + item.Value);
126
127 return;
128 }
129 }
130}
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSSetRecord AddRecord(params string[] keys)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
This example shows the use of the GAMSDatabase class for reading and writing GDX files....