Loading...
Searching...
No Matches
Transport8.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using System.Threading.Tasks;
6using GAMS;
7using System.Collections;
8
9namespace TransportSeq
10{
20 {
21 private static void ScenSolve(GAMSWorkspace ws, GAMSCheckpoint cp, Queue<double> bmultQueue, Object queueMutex, Object ioMutex)
22 {
24
25 lock (queueMutex)
26 {
27 mi = cp.AddModelInstance();
28 }
29 GAMSParameter bmult = mi.SyncDB.AddParameter("bmult", "demand multiplier");
30 GAMSOptions opt = ws.AddOptions();
31 opt.AllModelTypes = "cplex";
32 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
33 mi.Instantiate("transport use lp min z", opt, new GAMSModifier(bmult));
34
35 bmult.AddRecord().Value = 1.0;
36
37 while (true)
38 {
39 double b;
40 // dynamically get a bmult value from the queue instead of passing it to the different threads at creation time
41 lock (queueMutex)
42 {
43 if(0 == bmultQueue.Count)
44 return;
45 b = bmultQueue.Dequeue();
46 }
47 bmult.FirstRecord().Value = b;
48 mi.Solve();
49 // we need to make the ouput a critical section to avoid messed up report informations
50 lock (ioMutex)
51 {
52 Console.WriteLine("Scenario bmult=" + b + ":");
53 Console.WriteLine(" Modelstatus: " + mi.ModelStatus);
54 Console.WriteLine(" Solvestatus: " + mi.SolveStatus);
55 Console.WriteLine(" Obj: " + mi.SyncDB.GetVariable("z").FindRecord().Level);
56 }
57 }
58 }
59
60 static void Main(string[] args)
61 {
63 if (Environment.GetCommandLineArgs().Length > 1)
64 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
65 else
66 ws = new GAMSWorkspace();
68
69 // initialize a GAMSCheckpoint by running a GAMSJob
70 ws.AddJobFromString(GetModelText()).Run(cp);
71
72 Queue<double> bmultQueue = new Queue<double>(new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 });
73
74 // solve multiple model instances in parallel
75 Object queueMutex = new Object();
76 Object ioMutex = new Object();
77 Parallel.For(0, 2, delegate(int i) { ScenSolve(ws, cp, bmultQueue, queueMutex, ioMutex); });
78
79 }
80
81 static String GetModelText()
82 {
83 String model = @"
84 Sets
85 i canning plants / seattle, san-diego /
86 j markets / new-york, chicago, topeka / ;
87
88 Parameters
89
90 a(i) capacity of plant i in cases
91 / seattle 350
92 san-diego 600 /
93
94 b(j) demand at market j in cases
95 / new-york 325
96 chicago 300
97 topeka 275 / ;
98
99 Table d(i,j) distance in thousands of miles
100 new-york chicago topeka
101 seattle 2.5 1.7 1.8
102 san-diego 2.5 1.8 1.4 ;
103
104 Scalar f freight in dollars per case per thousand miles /90/ ;
105 Scalar bmult demand multiplier /1/;
106
107 Parameter c(i,j) transport cost in thousands of dollars per case ;
108
109 c(i,j) = f * d(i,j) / 1000 ;
110
111 Variables
112 x(i,j) shipment quantities in cases
113 z total transportation costs in thousands of dollars ;
114
115 Positive Variable x ;
116
117 Equations
118 cost define objective function
119 supply(i) observe supply limit at plant i
120 demand(j) satisfy demand at market j ;
121
122 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
123
124 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
125
126 demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
127
128 Model transport /all/ ;
129";
130
131 return model;
132 }
133
134 }
135}
GAMSModelInstance AddModelInstance(string modelInstanceName=null)
GAMSVariable GetVariable(string variableIdentifier)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
void Solve(SymbolUpdateType updateType=SymbolUpdateType.BaseCase, TextWriter output=null, GAMSModelInstanceOpt miOpt=null)
void Instantiate(string modelDefinition, params GAMSModifier[] modifiers)
new GAMSParameterRecord FirstRecord()
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSVariableRecord FindRecord(params string[] keys)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
This is the 8th model in a series of tutorial examples. Here we show: How to use a queue to solve mul...
Definition: Transport8.cs:20