Loading...
Searching...
No Matches
Transport2.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using GAMS;
6
7namespace TransportSeq
8{
18 {
19 static void Main(string[] args)
20 {
22 if (Environment.GetCommandLineArgs().Length > 1)
23 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
24 else
25 ws = new GAMSWorkspace();
26 // write an include file containing data with GAMS syntax
27 using (StreamWriter writer = new StreamWriter(ws.WorkingDirectory + Path.DirectorySeparatorChar + "tdata.gms"))
28 {
29 writer.Write(GetDataText());
30 }
31 // run a job using an instance of GAMSOptions that defines the data include file
32 using (GAMSOptions opt = ws.AddOptions())
33 {
34 GAMSJob t2 = ws.AddJobFromString(GetModelText());
35 opt.Defines.Add("incname", "tdata");
36 t2.Run(opt);
37 foreach (GAMSVariableRecord rec in t2.OutDB.GetVariable("x"))
38 Console.WriteLine("x(" + rec.Key(0) + "," + rec.Key(1) + "): level=" + rec.Level + " marginal=" + rec.Marginal);
39 }
40 }
41
42 static String GetDataText()
43 {
44 String data = @"
45 Sets
46 i canning plants / seattle, san-diego /
47 j markets / new-york, chicago, topeka / ;
48
49 Parameters
50
51 a(i) capacity of plant i in cases
52 / seattle 350
53 san-diego 600 /
54
55 b(j) demand at market j in cases
56 / new-york 325
57 chicago 300
58 topeka 275 / ;
59
60 Table d(i,j) distance in thousands of miles
61 new-york chicago topeka
62 seattle 2.5 1.7 1.8
63 san-diego 2.5 1.8 1.4 ;
64
65 Scalar f freight in dollars per case per thousand miles /90/ ;
66";
67 return data;
68 }
69
70static String GetModelText()
71 {
72 String model = @"
73 Sets
74 i canning plants
75 j markets
76
77 Parameters
78 a(i) capacity of plant i in cases
79 b(j) demand at market j in cases
80 d(i,j) distance in thousands of miles
81 Scalar f freight in dollars per case per thousand miles;
82
83$if not set incname $abort 'no include file name for data file provided'
84$include %incname%
85
86 Parameter c(i,j) transport cost in thousands of dollars per case ;
87
88 c(i,j) = f * d(i,j) / 1000 ;
89
90 Variables
91 x(i,j) shipment quantities in cases
92 z total transportation costs in thousands of dollars ;
93
94 Positive Variable x ;
95
96 Equations
97 cost define objective function
98 supply(i) observe supply limit at plant i
99 demand(j) satisfy demand at market j ;
100
101 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
102
103 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
104
105 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
106
107 Model transport /all/ ;
108
109 Solve transport using lp minimizing z ;
110
111 Display x.l, x.m ;
112";
113
114 return model;
115 }
116
117 }
118}
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 2nd model in a series of tutorial examples. Here we show: How to use include files to sep...
Definition: Transport2.cs:18