Loading...
Searching...
No Matches
Transport14.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using GAMS;
6using System.Threading.Tasks;
7
8namespace TransportSeq
9{
19 {
20 static void Main(string[] args)
21 {
22 double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
23 Optimizer optim = new Optimizer();
24 Parallel.ForEach(bmultlist, delegate(double bmult)
25 {
26 Console.WriteLine("Scenario bmult=" + bmult + ", Obj:" + optim.solve(bmult));
27 });
28 }
29 }
30
32 {
33 private GAMSWorkspace ws;
34
35 public Optimizer()
36 {
37 if (Environment.GetCommandLineArgs().Length > 1)
38 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
39 else
40 ws = new GAMSWorkspace();
41 }
42
43 public double solve(double mult)
44 {
45 GAMSDatabase gDb = ws.AddDatabase();
46
47 GAMSParameter f = gDb.AddParameter("f", "freight in dollars per case per thousand miles");
48 f.AddRecord().Value = 90 * mult;
49
50 GAMSJob gModJob = ws.AddJobFromString(GetModelText());
51
52 GAMSOptions gOption = ws.AddOptions();
53 gOption.Defines.Add("gdxincname", gDb.Name);
54 gModJob.Run(gOption, gDb);
55
56 return gModJob.OutDB.GetVariable("z").FirstRecord().Level;
57 }
58
59 private String GetModelText()
60 {
61 String model = @"
62 Sets
63 i canning plants / seattle, san-diego /
64 j markets / new-york, chicago, topeka / ;
65
66 Parameters
67
68 a(i) capacity of plant i in cases
69 / seattle 350
70 san-diego 600 /
71
72 b(j) demand at market j in cases
73 / new-york 325
74 chicago 300
75 topeka 275 / ;
76
77 Table d(i,j) distance in thousands of miles
78 new-york chicago topeka
79 seattle 2.5 1.7 1.8
80 san-diego 2.5 1.8 1.4 ;
81
82 Scalar f freight in dollars per case per thousand miles;
83
84$if not set gdxincname $abort 'no include file name for data file provided'
85$gdxin %gdxincname%
86$load f
87$gdxin
88
89 Parameter c(i,j) transport cost in thousands of dollars per case ;
90
91 c(i,j) = f * d(i,j) / 1000 ;
92
93 Variables
94 x(i,j) shipment quantities in cases
95 z total transportation costs in thousands of dollars ;
96
97 Positive Variable x ;
98
99 Equations
100 cost define objective function
101 supply(i) observe supply limit at plant i
102 demand(j) satisfy demand at market j ;
103
104 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
105
106 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
107
108 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
109
110 Model transport /all/ ;
111
112 Solve transport using lp minimizing z ;
113
114 Display x.l, x.m ;
115";
116
117 return model;
118 }
119 }
120}
GAMSVariable GetVariable(string variableIdentifier)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
GAMSDatabase OutDB
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 GAMSVariableRecord FirstRecord()
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 14th model in a series of tutorial examples. Here we show: How to run multiple GAMSJobs i...
Definition: Transport14.cs:19