Loading...
Searching...
No Matches
Transport3.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using GAMS;
6
7namespace TransportSeq
8{
24 {
25 static void Main(string[] args)
26 {
28 if (Environment.GetCommandLineArgs().Length > 1)
29 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
30 else
31 ws = new GAMSWorkspace();
32
33 // data from a string with GAMS syntax with explicit export to GDX file
34 GAMSJob t3 = ws.AddJobFromString(GetDataText());
35 t3.Run();
36 t3.OutDB.Export(ws.WorkingDirectory + Path.DirectorySeparatorChar + "tdata.gdx");
37
38 // run a job using an instance of GAMSOptions that defines the data include file
39 t3 = ws.AddJobFromString(GetModelText());
40 using (GAMSOptions opt = ws.AddOptions())
41 {
42 opt.Defines.Add("gdxincname", "tdata");
43 opt.AllModelTypes = "xpress";
44 t3.Run(opt);
45 foreach (GAMSVariableRecord rec in t3.OutDB.GetVariable("x"))
46 Console.WriteLine("x(" + rec.Key(0) + "," + rec.Key(1) + "): level=" + rec.Level + " marginal=" + rec.Marginal);
47 }
48
49 // same but with implicit database communication
50 using (GAMSOptions opt = ws.AddOptions())
51 {
52 GAMSJob t3a = ws.AddJobFromString(GetDataText());
53 GAMSJob t3b = ws.AddJobFromString(GetModelText());
54 t3a.Run();
55 opt.Defines.Add("gdxincname", t3a.OutDB.Name);
56 opt.AllModelTypes = "xpress";
57 t3b.Run(opt, t3a.OutDB);
58 foreach (GAMSVariableRecord rec in t3b.OutDB.GetVariable("x"))
59 Console.WriteLine("x(" + rec.Key(0) + "," + rec.Key(1) + "): level=" + rec.Level + " marginal=" + rec.Marginal);
60 }
61
62 }
63
64 static String GetDataText()
65 {
66 String data = @"
67 Sets
68 i canning plants / seattle, san-diego /
69 j markets / new-york, chicago, topeka / ;
70
71 Parameters
72
73 a(i) capacity of plant i in cases
74 / seattle 350
75 san-diego 600 /
76
77 b(j) demand at market j in cases
78 / new-york 325
79 chicago 300
80 topeka 275 / ;
81
82 Table d(i,j) distance in thousands of miles
83 new-york chicago topeka
84 seattle 2.5 1.7 1.8
85 san-diego 2.5 1.8 1.4 ;
86
87 Scalar f freight in dollars per case per thousand miles /90/ ;
88";
89 return data;
90 }
91
92 static String GetModelText()
93 {
94 String model = @"
95 Sets
96 i canning plants
97 j markets
98
99 Parameters
100 a(i) capacity of plant i in cases
101 b(j) demand at market j in cases
102 d(i,j) distance in thousands of miles
103 Scalar f freight in dollars per case per thousand miles;
104
105$if not set gdxincname $abort 'no include file name for data file provided'
106$gdxin %gdxincname%
107$load i j a b d f
108$gdxin
109
110 Parameter c(i,j) transport cost in thousands of dollars per case ;
111
112 c(i,j) = f * d(i,j) / 1000 ;
113
114 Variables
115 x(i,j) shipment quantities in cases
116 z total transportation costs in thousands of dollars ;
117
118 Positive Variable x ;
119
120 Equations
121 cost define objective function
122 supply(i) observe supply limit at plant i
123 demand(j) satisfy demand at market j ;
124
125 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
126
127 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
128
129 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
130
131 Model transport /all/ ;
132
133 Solve transport using lp minimizing z ;
134
135 Display x.l, x.m ;
136";
137
138 return model;
139 }
140
141 }
142}
GAMSVariable GetVariable(string variableIdentifier)
void Export(string filePath=null)
GAMSDatabase OutDB
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
Dictionary< string, string > Defines
string Key(int index)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
This is the 3rd model in a series of tutorial examples. Here we show: How to read data from string an...
Definition: Transport3.cs:24