Loading...
Searching...
No Matches
Transport10.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Excel = Microsoft.Office.Interop.Excel;
6using GAMS;
7using System.Diagnostics;
8using System.IO;
9
10
11namespace TransportSeq
12{
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 // Reading input data from workbook
32 var excelApp = new Excel.Application();
33 Excel.Workbook wb = excelApp.Workbooks.Open(Path.Combine(ws.SystemDirectory, @"apifiles/Data/transport.xlsx"));
34
35 Excel.Range range;
36
37 Excel.Worksheet capacity = (Excel.Worksheet)wb.Worksheets.get_Item("capacity");
38 range = capacity.UsedRange;
39 Array capacityData = (Array)range.Cells.Value;
40 int iCount = capacity.UsedRange.Columns.Count;
41
42 Excel.Worksheet demand = (Excel.Worksheet)wb.Worksheets.get_Item("demand");
43 range = demand.UsedRange;
44 Array demandData = (Array)range.Cells.Value;
45 int jCount = range.Columns.Count;
46
47 Excel.Worksheet distance = (Excel.Worksheet)wb.Worksheets.get_Item("distance");
48 range = distance.UsedRange;
49 Array distanceData = (Array)range.Cells.Value;
50
51 // number of markets/plants have to be the same in all spreadsheets
52 Debug.Assert((range.Columns.Count - 1) == jCount && (range.Rows.Count - 1) == iCount,
53 "Size of the spreadsheets doesn't match");
54 wb.Close();
55
56 // Creating the GAMSDatabase and fill with the workbook data
57 GAMSDatabase db = ws.AddDatabase();
58
59 GAMSSet i = db.AddSet("i", 1, "Plants");
60 GAMSSet j = db.AddSet("j", 1, "Markets");
61 GAMSParameter capacityParam = db.AddParameter("a", "Capacity", i);
62 GAMSParameter demandParam = db.AddParameter("b", "Demand", j);
63 GAMSParameter distanceParam = db.AddParameter("d", "Distance", i, j);
64
65 for (int ic = 1; ic <= iCount; ic++)
66 {
67 i.AddRecord((string)capacityData.GetValue(1, ic));
68 capacityParam.AddRecord((string)capacityData.GetValue(1, ic)).Value = (double)capacityData.GetValue(2, ic);
69 }
70 for (int jc = 1; jc <= jCount; jc++)
71 {
72 j.AddRecord((string)demandData.GetValue(1, jc));
73 demandParam.AddRecord((string)demandData.GetValue(1, jc)).Value = (double)demandData.GetValue(2, jc);
74 for (int ic = 1; ic <= iCount; ic++)
75 {
76 distanceParam.AddRecord((string)distanceData.GetValue(ic + 1, 1), (string)distanceData.GetValue(1, jc + 1)).Value = (double)distanceData.GetValue(ic + 1, jc + 1);
77 }
78 }
79
80 // Create and run the GAMSJob
81 using (GAMSOptions opt = ws.AddOptions())
82 {
83 GAMSJob t10 = ws.AddJobFromString(GetModelText());
84 opt.Defines.Add("gdxincname", db.Name);
85 opt.AllModelTypes = "xpress";
86 t10.Run(opt, db);
87 foreach (GAMSVariableRecord rec in t10.OutDB.GetVariable("x"))
88 Console.WriteLine("x(" + rec.Key(0) + "," + rec.Key(1) + "): level=" + rec.Level + " marginal=" + rec.Marginal);
89 }
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 /90/;
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
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}
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
Dictionary< string, string > Defines
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSSetRecord AddRecord(params string[] keys)
string Key(int index)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
This is the 10th model in a series of tutorial examples. Here we show: How to fill a GAMSDatabase by ...
Definition: Transport10.cs:22