Loading...
Searching...
No Matches
Transport6.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using GAMS;
6using System.Globalization;
7
8namespace TransportSeq
9{
19 {
20 static void RunScenario(GAMSWorkspace ws, GAMSCheckpoint cp, object ioMutex, double b)
21 {
22 string b_formatted = b.ToString(CultureInfo.InvariantCulture);
23 GAMSJob t6 = ws.AddJobFromString("bmult=" +
24 b_formatted +
25 "; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;", cp);
26 t6.Run();
27 // we need to make the output a critical section to avoid messed up report information
28 lock (ioMutex)
29 {
30 Console.WriteLine("Scenario bmult=" + b_formatted + ":");
31 Console.WriteLine(" Modelstatus: " + t6.OutDB.GetParameter("ms").FindRecord().Value);
32 Console.WriteLine(" Solvestatus: " + t6.OutDB.GetParameter("ss").FindRecord().Value);
33 Console.WriteLine(" Obj: " + t6.OutDB.GetVariable("z").FindRecord().Level);
34 }
35 }
36 static void Main(string[] args)
37 {
39 if (Environment.GetCommandLineArgs().Length > 1)
40 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
41 else
42 ws = new GAMSWorkspace();
44
45 // initialize a GAMSCheckpoint by running a GAMSJob
46 ws.AddJobFromString(GetModelText()).Run(cp);
47
48 double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
49
50 // run multiple parallel jobs using the created GAMSCheckpoint
51 Object ioMutex = new Object();
52 System.Threading.Tasks.Parallel.ForEach(bmultlist, delegate (double b) { RunScenario(ws, cp, ioMutex, b); });
53 }
54
55 static String GetModelText()
56 {
57 String model = @"
58 Sets
59 i canning plants / seattle, san-diego /
60 j markets / new-york, chicago, topeka / ;
61
62 Parameters
63
64 a(i) capacity of plant i in cases
65 / seattle 350
66 san-diego 600 /
67
68 b(j) demand at market j in cases
69 / new-york 325
70 chicago 300
71 topeka 275 / ;
72
73 Table d(i,j) distance in thousands of miles
74 new-york chicago topeka
75 seattle 2.5 1.7 1.8
76 san-diego 2.5 1.8 1.4 ;
77
78 Scalar f freight in dollars per case per thousand miles /90/ ;
79 Scalar bmult demand multiplier /1/;
80
81 Parameter c(i,j) transport cost in thousands of dollars per case ;
82
83 c(i,j) = f * d(i,j) / 1000 ;
84
85 Variables
86 x(i,j) shipment quantities in cases
87 z total transportation costs in thousands of dollars ;
88
89 Positive Variable x ;
90
91 Equations
92 cost define objective function
93 supply(i) observe supply limit at plant i
94 demand(j) satisfy demand at market j ;
95
96 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
97
98 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
99
100 demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
101
102 Model transport /all/ ;
103 Scalar ms 'model status', ss 'solve status';
104";
105
106 return model;
107 }
108
109 }
110}
GAMSVariable GetVariable(string variableIdentifier)
GAMSParameter GetParameter(string parameterIdentifier)
GAMSDatabase OutDB
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
new GAMSParameterRecord FindRecord(params string[] keys)
new GAMSVariableRecord FindRecord(params string[] keys)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
This is the 6th model in a series of tutorial examples. Here we show: How to run multiple GAMSJobs in...
Definition Transport6.cs:19