Loading...
Searching...
No Matches
GAMSClientClass.cs
1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.Runtime.Remoting.Channels;
7using System.Runtime.Remoting.Channels.Tcp;
9using GAMS;
10
11
12namespace GAMSClient
13{
28 {
29 static int Main(string[] args)
30 {
31
32 bool RunLocal = false;
33
34 // define some data by using C# data structures
35 List<string> plants = new List<string>()
36 {
37 "Seattle", "San-Diego"
38 };
39 List<string> markets = new List<string>()
40 {
41 "New-York", "Chicago", "Topeka"
42 };
43 Dictionary<string, double> capacity = new Dictionary<string, double>()
44 {
45 { "Seattle", 350.0 }, { "San-Diego", 600.0 }
46 };
47 Dictionary<string, double> demand = new Dictionary<string, double>()
48 {
49 { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
50 };
51 Dictionary<Tuple<string, string>, double> distance = new Dictionary<Tuple<string, string>, double>()
52 {
53 { new Tuple<string,string> ("Seattle", "New-York"), 2.5 },
54 { new Tuple<string,string> ("Seattle", "Chicago"), 1.7 },
55 { new Tuple<string,string> ("Seattle", "Topeka"), 1.8 },
56 { new Tuple<string,string> ("San-Diego", "New-York"), 2.5 },
57 { new Tuple<string,string> ("San-Diego", "Chicago"), 1.8 },
58 { new Tuple<string,string> ("San-Diego", "Topeka"), 1.4 }
59 };
60
62
63 // create a GAMSDatabase for the results
64 GAMSDatabase db = ws.AddDatabase();
65
66 GAMSSet i = db.AddSet("i", 1, "canning plants");
67 foreach (string p in plants)
68 i.AddRecord(p);
69
70 GAMSSet j = db.AddSet("j", 1, "markets");
71 foreach (string m in markets)
72 j.AddRecord(m);
73
74 GAMSParameter a = db.AddParameter("a", 1, "capacity of plant i in cases");
75 foreach (string p in plants)
76 a.AddRecord(p).Value = capacity[p];
77
78 GAMSParameter b = db.AddParameter("b", 1, "demand at market j in cases");
79 foreach (string m in markets)
80 b.AddRecord(m).Value = demand[m];
81
82 GAMSParameter d = db.AddParameter("d", 2, "distance in thousands of miles");
83 foreach (Tuple<string, string> t in distance.Keys)
84 d.AddRecord(t.Item1, t.Item2).Value = distance[t];
85
86 GAMSParameter f = db.AddParameter("f", 0, "freight in dollars per case per thousand miles");
87 f.AddRecord().Value = 90;
88
89 GAMSOptions opt = ws.AddOptions();
90 opt.AllModelTypes = "soplex";
91 opt.ResLim = 10;
92
93 GAMSDatabase dbResult;
94 if (RunLocal)
95 {
96 opt.Defines.Add("GDXInFile", db.Name);
97 opt.Defines.Add("GDXOutFile", Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName()));
98 GAMSJob job = ws.AddJobFromString(GetModelText());
99 job.Run(opt, Console.Out, false, db);
100 dbResult = ws.AddDatabaseFromGDX(opt.Defines["GDXOutFile"]);
101 }
102 else
103 {
104 string optFile = Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName());
105 opt.Export(optFile);
106 byte[] ParameterFile = File.ReadAllBytes(optFile);
107
108
109 string GDXInFileName = Path.ChangeExtension(Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName()), ".gdx");
110 db.Export(GDXInFileName);
111 byte[] GDXInFile = File.ReadAllBytes(GDXInFileName);
112
113 ChannelServices.RegisterChannel(new TcpClientChannel(), false);
114 GAMSRemoteClass rGAMS = (GAMSRemoteClass)Activator.GetObject(typeof(GAMSRemoteClass), "tcp://localhost:8686/GAMSSERVER");
115 if (rGAMS == null)
116 {
117 Console.WriteLine("Could not locate GAMS server");
118 return 1;
119 }
120
121 byte[] GDXOutFile = new byte[0];
122 string LogOutput = string.Empty;
123 string Message = string.Empty;
124
125 int rCode = rGAMS.RunServer(GetModelText(), GDXInFile, ParameterFile, ref GDXOutFile, ref LogOutput, ref Message);
126
127 if (rCode != 0)
128 {
129 Console.WriteLine(LogOutput.ToString());
130 Console.WriteLine("Problems with Remote GAMS Object:\n{0}" + Message);
131 return rCode;
132 }
133 else
134 {
135 Console.WriteLine(LogOutput);
136 Console.WriteLine(Message);
137 string GDXOutFileName = Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName());
138 File.WriteAllBytes(GDXOutFileName, GDXOutFile);
139 dbResult = ws.AddDatabaseFromGDX(GDXOutFileName);
140 }
141 }
142
143 foreach (GAMSVariableRecord rec in dbResult.GetVariable("x"))
144 Console.WriteLine("x(" + rec.Key(0) + "," + rec.Key(1) + "): level=" + rec.Level + " marginal=" + rec.Marginal);
145 return 0;
146 }
147
148 static string GetModelText()
149 {
150 String model = @"
151 Sets
152 i canning plants
153 j markets
154
155 Parameters
156 a(i) capacity of plant i in cases
157 b(j) demand at market j in cases
158 d(i,j) distance in thousands of miles
159 Scalar f freight in dollars per case per thousand miles;
160
161$if not set GDXInFile $abort 'no input file name for data file provided'
162$if not set GDXOutFile $abort 'no output file name for result file provided'
163
164$gdxin '%GDXInFile%'
165$load i j a b d f
166
167 Parameter c(i,j) transport cost in thousands of dollars per case ;
168
169 c(i,j) = f * d(i,j) / 1000 ;
170
171 Variables
172 x(i,j) shipment quantities in cases
173 z total transportation costs in thousands of dollars ;
174
175 Positive Variable x ;
176
177 Equations
178 cost define objective function
179 supply(i) observe supply limit at plant i
180 demand(j) satisfy demand at market j ;
181
182 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
183
184 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
185
186 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
187
188 Model transport /all/ ;
189
190 Solve transport using lp minimizing z ;
191
192 execute_unload '%GDXOutFile%', x, z, demand, supply;
193";
194
195 return model;
196 }
197 }
198}
This example demonstrates how to implement a simple GAMS Server. The example has two parts: GAMSServe...
This example demonstrates how to implement a simple GAMS Server. The example has two parts: GAMSServe...
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
void Export(string filePath)
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSSetRecord AddRecord(params string[] keys)
string Key(int index)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)