Loading...
Searching...
No Matches
TransportGDX.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using GAMS;
6
7namespace TransportSeq
8{
22 {
23 static void Main(string[] args)
24 {
26 if (Environment.GetCommandLineArgs().Length > 1)
27 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
28 else
29 ws = new GAMSWorkspace();
30
31 // define some data by using C# data structures
32 List<string> plants = new List<string>()
33 {
34 "Seattle", "San-Diego"
35 };
36 List<string> markets = new List<string>()
37 {
38 "New-York", "Chicago", "Topeka"
39 };
40 Dictionary<string, double> capacity = new Dictionary<string, double>()
41 {
42 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
43 };
44 Dictionary<string, double> demand = new Dictionary<string, double>()
45 {
46 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
47 };
48 Dictionary<Tuple<string, string>, double> distance = new Dictionary<Tuple<string, string>, double>()
49 {
50 { new Tuple<string,string> ("Seattle", "New-York"), 2.5 },
51 { new Tuple<string,string> ("Seattle", "Chicago"), 1.7 },
52 { new Tuple<string,string> ("Seattle", "Topeka"), 1.8 },
53 { new Tuple<string,string> ("San-Diego", "New-York"), 2.5 },
54 { new Tuple<string,string> ("San-Diego", "Chicago"), 1.8 },
55 { new Tuple<string,string> ("San-Diego", "Topeka"), 1.4 }
56 };
57
58 // prepare a GAMSDatabase with data from the C# data structures
59 GAMSDatabase db = ws.AddDatabase();
60
61 GAMSSet i = db.AddSet("i", 1, "canning plants");
62 foreach (string p in plants)
63 i.AddRecord(p);
64
65 GAMSSet j = db.AddSet("j", 1, "markets");
66 foreach (string m in markets)
67 j.AddRecord(m);
68
69 GAMSParameter a = db.AddParameter("a", "capacity of plant i in cases", i);
70 foreach (string p in plants)
71 a.AddRecord(p).Value = capacity[p];
72
73 GAMSParameter b = db.AddParameter("b", "demand at market j in cases", j);
74 foreach (string m in markets)
75 b.AddRecord(m).Value = demand[m];
76
77 GAMSParameter d = db.AddParameter("d", "distance in thousands of miles", i, j);
78 foreach (Tuple<string, string> t in distance.Keys)
79 d.AddRecord(t.Item1, t.Item2).Value = distance[t];
80
81 GAMSParameter f = db.AddParameter("f", "freight in dollars per case per thousand miles");
82 f.AddRecord().Value = 90;
83
84 // export the GAMSDatabase to a GDX file with name 'data.gdx' located in the 'workingDirectory' of the GAMSWorkspace
85 db.Export("data.gdx");
86
87 // add a new GAMSDatabase and initialize it from the GDX file just created
88 GAMSDatabase db2 = ws.AddDatabaseFromGDX("data.gdx");
89
90 // read data from symbols into C# data structures
91 List<string> plants2 = new List<string>();
92 foreach (GAMSSetRecord item in db2.GetSet("i"))
93 plants2.Add(item.Key(0));
94
95 List<string> markets2 = new List<string>();
96 foreach (GAMSSetRecord item in db2.GetSet("j"))
97 markets2.Add(item.Key(0));
98
99 Dictionary<string, double> capacity2 = new Dictionary<string, double>();
100 foreach (GAMSParameterRecord item in db2.GetParameter("a"))
101 capacity2.Add(item.Key(0), item.Value);
102
103 Dictionary<string, double> demand2 = new Dictionary<string, double>();
104 foreach (GAMSParameterRecord item in db2.GetParameter("b"))
105 demand2.Add(item.Key(0), item.Value);
106
107 Dictionary<Tuple<string, string>, double> distance2 = new Dictionary<Tuple<string, string>, double>();
108 foreach (GAMSParameterRecord item in db2.GetParameter("d"))
109 distance2.Add(new Tuple<string, string>(item.Key(0), item.Key(1)), item.Value);
110
111 // print out data read from GDX file
112 Console.WriteLine("Data read from data.gdx");
113 Console.WriteLine("\nSet i:");
114 foreach (string p in plants2)
115 Console.WriteLine(" " + p);
116 Console.WriteLine("\nSet j:");
117 foreach (string m in markets2)
118 Console.WriteLine(" " + m);
119 Console.WriteLine("\nParameter a:");
120 foreach (var item in capacity2)
121 Console.WriteLine(" " + item.Key + ": " + item.Value);
122 Console.WriteLine("\nParameter b:");
123 foreach (var item in demand2)
124 Console.WriteLine(" " + item.Key + ": " + item.Value);
125 Console.WriteLine("\nParameter d:");
126 foreach (var item in distance2)
127 Console.WriteLine(" " + item.Key + ": " + item.Value);
128
129 return;
130 }
131 }
132}
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
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 example shows the use of the GAMSDatabase class for reading and writing GDX files....
Definition: TransportGDX.cs:22