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{
18
20 {
21 static void Main(string[] args)
22 {
23 // Set useLock to true if --UseLock flag is present, false otherwise
24 bool useLock = Array.Exists(args, arg => string.Equals(arg, "--UseLock", StringComparison.OrdinalIgnoreCase));
25
26 double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
27 Optimizer optim = new Optimizer(useLock);
28 Parallel.ForEach(bmultlist, delegate (double bmult)
29 {
30 Console.WriteLine("Scenario bmult=" + bmult + ", Obj:" + optim.solve(bmult));
31 });
32 }
33 }
34
36 {
37 private GAMSWorkspace ws;
38 private readonly bool _useLock;
39
40 public Optimizer(bool useLock = false)
41 {
42 _useLock = useLock;
43
44 if (Environment.GetCommandLineArgs().Length > 1 && !Environment.GetCommandLineArgs()[1].StartsWith("-"))
45 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
46 else
47 ws = new GAMSWorkspace();
48 }
49
50
51
52 private static readonly object ProcessStartLock = new object();
53
54 public double solve(double mult)
55 {
56 using (GAMSDatabase gDb = ws.AddDatabase())
57 using (GAMSOptions gOption = ws.AddOptions())
58 {
59 GAMSParameter f = gDb.AddParameter("f", "freight in dollars per case per thousand miles");
60 f.AddRecord().Value = 90 * mult;
61
62 GAMSJob gModJob = ws.AddJobFromString(GetModelText());
63
64 string gdxPath = Path.Combine(ws.WorkingDirectory, gDb.Name + ".gdx");
65 gOption.Defines.Add("gdxincname", gdxPath);
66
67 // Parallel process instantiation on Linux can cause a process to get stuck completely.
68 // To prevent this, the --UseLock flag forces the application to instantiate processes
69 // safely one after another, at the cost of performance.
70 // Example usage:
71 //
72 // dotnet run --UseLock
73 //
74 // If _useLock is true, execution slows down, but successful execution is guaranteed.
75 // If _useLock is false, jobs run fully parallel for best performance.
76 if (_useLock)
77 {
78 lock (ProcessStartLock)
79 {
80 gModJob.Run(gOption, gDb);
81 }
82 }
83 else
84 {
85 gModJob.Run(gOption, gDb);
86 }
87
88 return gModJob.OutDB.GetVariable("z").FirstRecord().Level;
89 }
90 }
91
92 private String GetModelText()
93 {
94 String model = @"
95 Sets
96 i canning plants / seattle, san-diego /
97 j markets / new-york, chicago, topeka / ;
98
99 Parameters
100
101 a(i) capacity of plant i in cases
102 / seattle 350
103 san-diego 600 /
104
105 b(j) demand at market j in cases
106 / new-york 325
107 chicago 300
108 topeka 275 / ;
109
110 Table d(i,j) distance in thousands of miles
111 new-york chicago topeka
112 seattle 2.5 1.7 1.8
113 san-diego 2.5 1.8 1.4 ;
114
115 Scalar f freight in dollars per case per thousand miles;
116
117$if not set gdxincname $abort 'no include file name for data file provided'
118$gdxin %gdxincname%
119$load f
120$gdxin
121
122 Parameter c(i,j) transport cost in thousands of dollars per case ;
123
124 c(i,j) = f * d(i,j) / 1000 ;
125
126 Variables
127 x(i,j) shipment quantities in cases
128 z total transportation costs in thousands of dollars ;
129
130 Positive Variable x ;
131
132 Equations
133 cost define objective function
134 supply(i) observe supply limit at plant i
135 demand(j) satisfy demand at market j ;
136
137 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
138
139 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
140
141 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
142
143 Model transport /all/ ;
144
145 Solve transport using lp minimizing z ;
146
147 Display x.l, x.m ;
148";
149
150 return model;
151 }
152 }
153}
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...