Loading...
Searching...
No Matches
Transport7.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using GAMS;
6
7namespace TransportSeq
8{
24 {
25 static void Main(string[] args)
26 {
28 if (Environment.GetCommandLineArgs().Length > 1)
29 ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
30 else
31 ws = new GAMSWorkspace();
33
34 // initialize a GAMSCheckpoint by running a GAMSJob
35 GAMSJob t7 = ws.AddJobFromString(GetModelText());
36 t7.Run(cp);
37
38 // create a GAMSModelInstance and solve it multiple times with different scalar bmult
40
41 GAMSParameter bmult = mi.SyncDB.AddParameter("bmult", "demand multiplier");
42 GAMSOptions opt = ws.AddOptions();
43 opt.AllModelTypes = "cplex";
44
45 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
46 mi.Instantiate("transport use lp min z", opt, new GAMSModifier(bmult));
47
48 bmult.AddRecord().Value = 1.0;
49 double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
50
51 foreach (double b in bmultlist)
52 {
53 bmult.FirstRecord().Value = b;
54 mi.Solve();
55 Console.WriteLine("Scenario bmult=" + b + ":");
56 Console.WriteLine(" Modelstatus: " + mi.ModelStatus);
57 Console.WriteLine(" Solvestatus: " + mi.SolveStatus);
58 Console.WriteLine(" Obj: " + mi.SyncDB.GetVariable("z").FindRecord().Level);
59 }
60
61 // create a GAMSModelInstance and solve it with single links in the network blocked
62 mi = cp.AddModelInstance();
63
64 GAMSVariable x = mi.SyncDB.AddVariable("x", 2, VarType.Positive, "");
65 GAMSParameter xup = mi.SyncDB.AddParameter("xup", 2, "upper bound on x");
66
67 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare upper bound of X mutable
68 mi.Instantiate("transport use lp min z", new GAMSModifier(x,UpdateAction.Upper,xup));
69
70 foreach (GAMSSetRecord i in t7.OutDB.GetSet("i"))
71 foreach (GAMSSetRecord j in t7.OutDB.GetSet("j"))
72 {
73 xup.Clear();
74 xup.AddRecord(i.Key(0),j.Key(0)).Value = 0;
75 mi.Solve();
76 Console.WriteLine("Scenario link blocked: " + i.Key(0) + " - " + j.Key(0));
77 Console.WriteLine(" Modelstatus: " + mi.ModelStatus);
78 Console.WriteLine(" Solvestatus: " + mi.SolveStatus);
79 Console.WriteLine(" Obj: " + mi.SyncDB.GetVariable("z").FindRecord().Level);
80 }
81 }
82
83 static String GetModelText()
84 {
85 String model = @"
86 Sets
87 i canning plants / seattle, san-diego /
88 j markets / new-york, chicago, topeka / ;
89
90 Parameters
91
92 a(i) capacity of plant i in cases
93 / seattle 350
94 san-diego 600 /
95
96 b(j) demand at market j in cases
97 / new-york 325
98 chicago 300
99 topeka 275 / ;
100
101 Table d(i,j) distance in thousands of miles
102 new-york chicago topeka
103 seattle 2.5 1.7 1.8
104 san-diego 2.5 1.8 1.4 ;
105
106 Scalar f freight in dollars per case per thousand miles /90/ ;
107 Scalar bmult demand multiplier /1/;
108
109 Parameter c(i,j) transport cost in thousands of dollars per case ;
110
111 c(i,j) = f * d(i,j) / 1000 ;
112
113 Variables
114 x(i,j) shipment quantities in cases
115 z total transportation costs in thousands of dollars ;
116
117 Positive Variable x ;
118
119 Equations
120 cost define objective function
121 supply(i) observe supply limit at plant i
122 demand(j) satisfy demand at market j ;
123
124 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
125
126 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
127
128 demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
129
130 Model transport /all/ ;
131";
132
133 return model;
134 }
135
136 }
137}
GAMSModelInstance AddModelInstance(string modelInstanceName=null)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
void Instantiate(string modelDefinition, params GAMSModifier[] modifiers)
new GAMSParameterRecord AddRecord(params string[] keys)
string Key(int index)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
This is the 7th model in a series of tutorial examples. Here we show: How to create a GAMSModelInstan...
Definition: Transport7.cs:24
UpdateAction