2using System.Collections.Generic;
5using Excel = Microsoft.Office.Interop.Excel;
7using System.Diagnostics;
23 static void Main(
string[] args)
26 if (Environment.GetCommandLineArgs().Length > 1)
27 ws =
new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
32 var excelApp =
new Excel.Application();
33 Excel.Workbook wb = excelApp.Workbooks.Open(Path.Combine(ws.
SystemDirectory,
@"apifiles/Data/transport.xlsx"));
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;
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;
47 Excel.Worksheet distance = (
Excel.Worksheet)wb.Worksheets.get_Item(
"distance");
48 range = distance.UsedRange;
49 Array distanceData = (Array)range.Cells.Value;
52 Debug.Assert((range.Columns.Count - 1) == jCount && (range.Rows.Count - 1) == iCount,
53 "Size of the spreadsheets doesn't match");
65 for (
int ic = 1; ic <= iCount; ic++)
67 i.
AddRecord((
string)capacityData.GetValue(1, ic));
68 capacityParam.
AddRecord((
string)capacityData.GetValue(1, ic)).Value = (
double)capacityData.GetValue(2, ic);
70 for (
int jc = 1; jc <= jCount; jc++)
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++)
76 distanceParam.
AddRecord((
string)distanceData.GetValue(ic + 1, 1), (string)distanceData.GetValue(1, jc + 1)).Value = (
double)distanceData.GetValue(ic + 1, jc + 1);
85 opt.AllModelTypes =
"xpress";
88 Console.WriteLine(
"x(" + rec.
Key(0) +
"," + rec.
Key(1) +
"): level=" + rec.Level +
" marginal=" + rec.
Marginal);
92 static String GetModelText()
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/;
105$if not set gdxincname $abort 'no include file name for data file provided'
110 Parameter c(i,j) transport cost in thousands of dollars per case ;
112 c(i,j) = f * d(i,j) / 1000 ;
115 x(i,j) shipment quantities in cases
116 z total transportation costs in thousands of dollars ;
118 Positive Variable x ;
121 cost define objective function
122 supply(i) observe supply limit at plant i
123 demand(j) satisfy demand at market j ;
125 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
127 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
129 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
131 Model transport /all/ ;
133 Solve transport using lp minimizing z ;
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)
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 ...