3using System.Collections.Generic;
6using System.Runtime.Remoting.Channels;
7using System.Runtime.Remoting.Channels.Tcp;
29 static int Main(
string[] args)
32 bool RunLocal =
false;
35 List<string> plants =
new List<string>()
37 "Seattle",
"San-Diego"
39 List<string> markets =
new List<string>()
41 "New-York",
"Chicago",
"Topeka"
43 Dictionary<string, double> capacity =
new Dictionary<string, double>()
45 {
"Seattle", 350.0 }, {
"San-Diego", 600.0 }
47 Dictionary<string, double> demand =
new Dictionary<string, double>()
49 {
"New-York", 325.0 }, {
"Chicago", 300.0 }, {
"Topeka", 275.0 }
51 Dictionary<Tuple<string, string>,
double> distance =
new Dictionary<Tuple<string, string>,
double>()
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 }
67 foreach (
string p
in plants)
70 GAMSSet j = db.AddSet(
"j", 1,
"markets");
71 foreach (
string m
in markets)
74 GAMSParameter a = db.AddParameter(
"a", 1,
"capacity of plant i in cases");
75 foreach (
string p
in plants)
78 GAMSParameter b = db.AddParameter(
"b", 1,
"demand at market j in cases");
79 foreach (
string m
in markets)
82 GAMSParameter d = db.AddParameter(
"d", 2,
"distance in thousands of miles");
83 foreach (Tuple<string, string> t
in distance.Keys)
86 GAMSParameter f = db.AddParameter(
"f", 0,
"freight in dollars per case per thousand miles");
90 opt.AllModelTypes =
"soplex";
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"]);
104 string optFile = Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName());
106 byte[] ParameterFile = File.ReadAllBytes(optFile);
109 string GDXInFileName = Path.ChangeExtension(Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName()),
".gdx");
110 db.Export(GDXInFileName);
111 byte[] GDXInFile = File.ReadAllBytes(GDXInFileName);
113 ChannelServices.RegisterChannel(
new TcpClientChannel(),
false);
117 Console.WriteLine(
"Could not locate GAMS server");
121 byte[] GDXOutFile =
new byte[0];
122 string LogOutput =
string.Empty;
123 string Message =
string.Empty;
125 int rCode = rGAMS.RunServer(GetModelText(), GDXInFile, ParameterFile, ref GDXOutFile, ref LogOutput, ref Message);
129 Console.WriteLine(LogOutput.ToString());
130 Console.WriteLine(
"Problems with Remote GAMS Object:\n{0}" + Message);
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);
144 Console.WriteLine(
"x(" + rec.
Key(0) +
"," + rec.
Key(1) +
"): level=" + rec.Level +
" marginal=" + rec.
Marginal);
148 static string GetModelText()
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;
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'
167 Parameter c(i,j) transport cost in thousands of dollars per case ;
169 c(i,j) = f * d(i,j) / 1000 ;
172 x(i,j) shipment quantities in cases
173 z total transportation costs in thousands of dollars ;
175 Positive Variable x ;
178 cost define objective function
179 supply(i) observe supply limit at plant i
180 demand(j) satisfy demand at market j ;
182 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
184 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
186 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
188 Model transport /all/ ;
190 Solve transport using lp minimizing z ;
192 execute_unload '%GDXOutFile%', x, z, demand, supply;
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)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)